Wednesday, August 29th, 2007

Cerny.js: Schema for JSON

Category: JavaScript, JSON, Library

Cerny.js brings you the fun and frolics you have grown to love from XML schema:

A Cerny Schema allows us to validate a JavaScript object. If an object passes the validation, we can be sure that certain statements are true for the object, if it fails validation, the validation report will inform us, which statements do not hold. Schemas allow us to write simpler code, because certain facts about an object are given and do not have to be checked. This will also result in more readable and thus better maintainable JavaScript code. On the other hand if we do not use schemas, the assumptions that we make on the data structures we are working on are scattered in the code and the same assumption might checked more than once.

A simple example is that of a Person, which a schema, and validation:


  1. // Define a Person schema
  2. Person = {
  3.   firstName: nonEmptyString,
  4.   middleName: optional(nonEmptyString),
  5.   lastName: nonEmptyString,
  6.   dateOfBirth: optional(pastIsoDate)
  7. };
  9. // Create a Person
  10. var csp = {
  11.   firstName: "Charles",
  12.   middleName: "Sanders",
  13.   lastName: "Peirce",
  14.   dateOfBirth: "1839-09-10"
  15. };
  17. // Validate the Person
  18. var report = CERNY.schema.validate(csp, Person);

Posted by Dion Almaer at 7:42 am

2.9 rating from 21 votes


Comments feed TrackBack URI

I have also build something like this.

{ "object":{ "properties":[ { "name":"prop1", "type":{ "array":{ "minLength":0, "maxLength":5, "type":{ "string":{ "minLength":0, "maxLength":5, "default":"test" } } } } } ] }}

Comment by Rob — August 29, 2007

While I like the idea, what is it actually good for in practice? JSON usually coming from the server, and typically has no errors.

Comment by András Bártházi — August 29, 2007

What about XML Schema validation ? I’m not interested in json.

Comment by jr — August 29, 2007

It would be a better syntax with regexes:

Person = {
firstName: /^.+$/,
middleName: /^.*/,
lastName: /^.+$/,
dateOfBirth: /^\d\d\d\d-\d\d-\d\d$/

But you don’t need JSON schema for this – you just code it.

Comment by Junior DeSuza — August 29, 2007

Those who do not learn from XML are doomed to repeat it :)
I guess the next logical step are namespaces, because once you have schemas and want to re-use them, you need a clean way to combine them.

Comment by Tim — August 29, 2007

Structural definition for validation is included in the JSPON specification for JSON. Have you considered using this standard to ingest structural definitions for your validation? This allows JSON objects to include their own structure/validation information.
@Andras: This is useful when the JSON data is going to be modified. By including validation information with the objects, invalid data can be detected by the client before sending it to the server.

Comment by Kris Zyp — August 29, 2007

Cerny has been around for a while, it has some pretty cool features beyond those in the code snippet above. Certainly worth looking at if you expect a client to be sending back JSON objects, or just to do complex form validation, etc.

Comment by Adam Sanderson — August 29, 2007

@Junior: Your Person schema is actually a Cerny schema, which can be passed to the CERNY.schema.validate method. It has some drawbacks though: 1. you can pass in 2001-55-77 for a birth date, but that is certainly not a valid date, 2. if you want to change the meaning of non empty string (unlikely in this example), you have to edit three lines of code, where as in my solution, you have to only have to redefine the function nonEmptyString, 3. middleName is not optional in your case, it must be present.
@Andras: I agree with Kris. Validation during development time *is* useful and helps to detect errors early. During development the document types that are sent back from the server will change because it is a work in progress. If the document type is described in whichever formal way, it helps communication between developers. This might not be helpful for a team that fits in one room, but in larger teams communication is more difficult, and thus it needs support from all ends.
@Adam: Thanks!

Comment by Robert Cerny — August 30, 2007

A great idea and an elegant execution from someone who obviously has a highly organised mind. Just wish I was organised enough to get around to using it… :)

Comment by Jerome — August 30, 2007

Wow, very nice solution. I found it to be very useful.

Comment by Rajnikant Parekh — October 10, 2007

I find this very useful to validate data sended by the server to a SCORM.

Thanks !

Comment by nautilebleu — December 13, 2007

Leave a comment

You must be logged in to post a comment.