Forest header image

Symfony Finland
Random things on PHP, Symfony and web development

Consider using a JSON Schema powered JavaScript CRUD in your applications

Many web applications are essentially a CRUD interface. This stands for simply Create, Read, Update and Delete. This is often the backbone of applications of all sorts ranging from content management systems to custom web applications like travel sites.

Traditionationally these interfaces have been built on the server side with individual pages linking from one page to another and all editing done via a form that is then handled by the server to modify the location of the data.

For Symfony there are options for CRUD interfaces like EasyAdmin and Sonata. In the meanwhile there has been tremendous progress not only in the JavaScript language itself but the browser compatibility and the related ecosystem.

One of the results is the JSON Schema specification which is essentially a JSON format that allows expressing constraints about JSON files. This is very much like the Document Type Definitions (DTDs) that developers have worked with in the XML realm.

You don't need to go all in on browser CRUDs

With the specification in place there are already software components for validating and consuming JSON Schemas. For certain kind of data and use cases a CRUD dynamically created from a JSON Schema on the client side might be just the thing you need. NoSQL didn't eat SQL, but lead to hybrids. Maybe CRUDs can be hybrids too?

JSON and browser powered CRUDs might initially sound like something that you would only use in a new, shiny project, but embedding a JSON editor to an established application to store highly dynamic data like user profile settings might be a valid option.

While JSON is natively consumed by NoSQL databases like MongoDB and CouchDB, many traditional relational databases now have JSON datatypes, like MySQL for example since 5.7. Add to this the fact that PHP can validate JSON Schemas just as well as the browser can, then you can validate the input on submit time to make sure it's what you expect it to be.

A JSON schema powered CRUD isn't probably the perfect solution for everything, but they do offer quite a bit of flexibility over more traditional server generated options. And being able to validate with the input data with the exact same ruleset (schema) on browser and server side can reduce duplication.

A few good JSON Schema tools:


Written by Jani Tarvainen on Sunday May 22, 2016
Permalink - Tags: json, javascript, symfony, doctrine

« The Symfony Advantage: Sharing skills across Bolt CMS, Drupal, eZ Platform... - GraphQL with PHP and the Symfony Framework »