AJAX applications depend upon JSON services conforming to expectations. Unexpected behavior can occur if services provide responses in an unexpected format or with invalid content. For complex applications, you can mitigate the risk of unexpected behavior by implementing custom routines to validate service responses. Alternatively, you can exploit JSON Schema to validate input.
JSON Schema is a draft standard that specifies a JSON-based format for defining the structure of JSON data. As of this writing, the latest is draft-03 (see Resources).
This article compares a few of the JSON Schema validation routines. Learn to use the foremost libraries, and explore considerations and best practices for creating libraries to validate communications. The article also includes a new utility to help you write JSON Schemas.
Download the samples used in this article.
Choosing a library for your application
|Library (Author)||Draft versions support||Approximate library size|
|JSV: JSON Schema Validator (Gary Court)||draft-01, draft-02, draft-03||120KB|
|json-schema (Kris Zyp)||draft-03||10KB (requires CommonJS)|
|dojox.json.schema (Kris Zyp)||draft-02||10KB (requires Dojo)|
|schema.js (Andreas Kalsch)||draft-02 (partial)||10KB (requires CommonJS)|
An application based on Dojo might use the dojox.json.schema library because it is included in the toolkit. An application that needs to support multiple versions of the (draft) standard may use JSV.
dojox.json.schema appears to be a fork of json-schema so it will be similar in usage. schema.js implements only a subset of draft-02. This article concentrates on examples for using dojox.json.schema and JSV.
Listing 1 shows an HTML
snippet that validates a simple object. It is designed to be injected into
head HTML element.
Listing 1. Single use of dojox.json.schema
Listing 2 is an HTML snippet
that validates a simple object. It is designed to be injected into the
head HTML element.
Listing 2. Single use of JSV
JSV provides advanced failure information in the
errors array. Each error may contain the
- message: Human-readable error message.
- uri: URI of the failing object location.
- schemaUri: URI of the schema location causing failure.
- Attribute: Schema constraint causing failure.
- Details: Free-form array that includes further information, such as expected values.
Combining JSON Schema validation with XMLHttpRequest
When writing a library to obtain schemas and validate communications, consider:
- Overhead on page load. Preloading schemas may appear attractive but can slow down page load time.
- Overhead on AJAX calls. Lazy-loading schemas will have an impact on the first call that uses each schema. Validating every communication could introduce performance issues. Consider applying validation to more complex services.
Writing JSON schemas
The JSON Schema definition has many nuances, so writing and testing schemas can be challenging. This article includes the JSON Schema Lint utility, which you can download, to help you create and test JSON Schema.
Hints and tips
- The JSON Schema Internet Draft (see Resources) has a full definition of the JSON Schema specification and is an invaluable resource. Be sure to look at the latest draft.
- Consider using JSON Schema to assist with documenting services. Use
descriptionattribute to describe properties.
- When writing schemas, balance the needs of the application with validation strictness. Fully defining every attribute may make for rigorous validation, but it may also introduce fragility if the service is evolving with the application. JSON Schema allows for partial validation that can help in this area.
- Use the advanced capabilities of JSON Schema to lock down properties.
You can use
additionalProperties, enum, minItems, maxItems, and so on to increase constraints.
- When you need to allow for a property that might be multiple types,
you can use an array to define these. Alternatively, use the
|Sample, showing use of dojox.json.schema||dojox_json_schema-example.html||2KB|
|Sample, showing use of JSV||jsv-example.html||2KB|
|Utility for creating and testing JSON Schema||jsonschema.zip||140KB|
- JSON Schema: Learn more about the latest schemas, tools, and discussion.
- A JSON Media Type for Describing the Structure and Meaning of JSON Documents: Read the latest JSON Schema Internet Draft (a working document of the Internet Engineering Task Force (IETF).
- Documentation for dojox.json.schema: Learn how dojox.json.schema implements JSON Schema to provide data validation against JSON Schemas.
- Andreas Kalsch's schema.js library: Get more information about this sophisticated JSON Schema-based data validation and adaptation.
- Kris Zyp's json-schema library: Read about JSON Schema specifications, reference schemas, and a CommonJS implementation.
- developerWorks Web development zone: Find articles covering various web-based solutions. See the Web development technical library for a wide range of technical articles and tips, tutorials, standards, and IBM Redbooks.
- developerWorks technical events and webcasts: Stay current with technology in these sessions.
- developerWorks Live! briefings: Get up to speed quickly on IBM products and tools as well as IT industry trends.
- developerWorks on-demand demos: Watch demos ranging from product installation and setup for beginners, to advanced functionality for experienced developers.
- developerWorks on Twitter: Join today to follow developerWorks tweets.
Get products and technologies
- IBM product evaluation versions: Download or explore the online trials in the IBM SOA Sandbox and get your hands on application development tools and middleware products from DB2, Lotus, Rational, Tivoli, and WebSphere.
- developerWorks community: Connect with other developerWorks users while exploring the developer-driven blogs, forums, groups, and wikis.
- Find other developerWorks members interested in web development.