I have just started using JSON Schema to describe the JSON-LD formats exchanged by our web services and one of the first things that struck me was the number of double quotes required when manually typing up schemata. JSON is a great way to exchange data, but it would not have been my first choice for implementing a schema language.
So I whipped up a little Groovy DSL (I’m calling it jsonc so far) that simplifies the task of manually creating JSON schemata while still looking a lot like the pure JSON version. For example, here is the example schema from the JSON Schema web page expressed in the Groovy DSL (note: the outer curly braces are not required):
title "Example schema"
type object
properties {
firstName { type string }
lastName { type string }
age {
description "Age in years."
type integer
minimum 0
}
}
required "firstName", "lastName"
The jsonc program reads a DSL script like the one above and generates the equivalent JSON schema. The DSL is not a new schema language, it is simply an alternate way to describe the JSON object that will be handed to a JSON schema validator.
The Groovy DSL has a few advantages over the pure JSON version (some of these advantages might just be that I am not familiar enough with JSON schema yet).
1. Requires typing far fewer quotes, colons, and braces.
2. C/C++/Java style comments are supported.
3. Ability to use variables and flow of control structures in the schema
4. Ability to pass command line parameters to jsonc that will be available for use inside the schema
In fact, since the DSL is just a Groovy script it can include almost any arbitrary Groovy/Java code, e.g. read external config files, throw exceptions, etc. For example, the following script will generate the same schema as above:
// The list of string properties to be defined for this object.
props = ["firstName", "lastName"]
title "Example schema"
type object
properties {
// Iterate over the ‘props’ list and declare each element as a string property.
props.each {
"$it" { type string }
}
age {
description "Age in years."
type integer
minimum 0
}
}
// Require all the string properties we defined above.
required props
At the moment the program does no checking to determine if the input is actually a valid schema; it simply reads some input and generates some JSON as output. If the output is a valid JSON schema great, if not… que sera sera… garbage in garbage out.
The jsonc program is also not yet complete in that I am not sure that everything supported by JSON schema can be expressed in the DSL, but anything missing should be easy enough to add. However, it does everything I want so it is good enough for me. Before expending anymore effort on the project I was wondering if there was any other interest? Is anyone else working on anything similar? The project is really only at the proof of concept stage and would need more work before it was ready for prime time, but given any other interest I will put it up on Github for others to play with.
Cheers,
Keith
I am not a YAML fan, however, I should have named my thread a “Simplified Syntax for JSON Schema”. My “alternate syntax” is really just a Groovy script, it just so happens that the Groovy looks (almost) exactly like the Javascript; just without all the double quotes, colons, and commas. Executing the schema (Groovy script) produces a JSON object; either to stdout using the command line tool, or to a Java Object (Jackson JsonNode) using the API. From that point on existing tools can be used. So I am not spending any time dreaming up a new syntax.
The Groovy scripting bits were trivial and it took an afternoon to mix in Francis Galiegue’s validator (https://github.com/fge/json-schema-validator) to come up with something that can validate JSON using the simplified syntax.
We are already using the syntax internally and I am finding it a handy way to generate JSON data for unit tests as well. In the future I may put a “playground” type web app up to play with. In the meantime brave souls can look at the (still rough around the edges) code at https://github.com/oanc/org.anc.json.schema-compiler
Cheers,
Keith