Wednesday, August 29th, 2007
Cerny.js: Schema for JSON
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:
-
-
// Define a Person schema
-
Person = {
-
firstName: nonEmptyString,
-
middleName: optional(nonEmptyString),
-
lastName: nonEmptyString,
-
dateOfBirth: optional(pastIsoDate)
-
};
-
-
// Create a Person
-
var csp = {
-
firstName: "Charles",
-
middleName: "Sanders",
-
lastName: "Peirce",
-
dateOfBirth: "1839-09-10"
-
};
-
-
// Validate the Person
-
var report = CERNY.schema.validate(csp, Person);
-












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" } } } } } ] }}
http://r.versluis.millipede.nl/
While I like the idea, what is it actually good for in practice? JSON usually coming from the server, and typically has no errors.
What about XML Schema validation ? I’m not interested in json.
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.
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.
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.
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.
@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!
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… :)
Wow, very nice solution. I found it to be very useful.
I find this very useful to validate data sended by the server to a SCORM.
Thanks !
Thanks.Good article