JSON Schema Validation

Intro:

I have already used JSON in so many ways and in so many projects but never know about JSON Schema. So after doing some digging into  draft-handrews-json-schema-validation-01, I have came up with following info,

Terminology:
  • The terms "schema", "instance", and "meta-schema" are to be interpreted as defined in the JSON Schema core specification [json-schema]. The JSON Schema Core specification is where the terminology for a schema is defined.
  • The terms "applicable" and "attached" are to be interpreted as defined in Section 3 of the JSON Schema validation specification [json-schema-validation]The JSON Schema Validation specification is the document that defines the valid ways to define validation constraints. This document also defines a set of keywords that can be used to specify validations for a JSON API. In the examples that follow, we’ll be using some of these keywords.
  • The terms "link", "link context" (or "context"), "link target" (or "target"), and "target attributes" are to be interpreted as defined in  Section 2 of the JSON Hyper-Schema specification  [json-schema-hypermedia].This is another extension of the JSON Schema spec, wherein, the hyperlink and hypermedia-related keywords are defined.

JSON Schema is used to validate the format and structure of a JSON ObjectJSON Schema can be used to require that a given JSON document (an instance) satisfies a certain number of criteria. These criteria are asserted by using keywords described in this specification.

JSON Schema definition:

The following is a simple JSON Object representing a employee info:

  "id": 1,
  "name": "Hemakumar",
  "email": "hemakumar@mudimi.com"
}

JSON Schema will be as follow:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "title": "Employee",
  "description": "An employee information",
  "type": "object",
  "properties": {
    "id": {
      "description": "The unique identifier for a employee",
      "type": "integer"
    },
    "name": {
      "description": "Name of the employee",
      "type": "string"
    },
    "email": {
      "type": "string",
      "description": "Email of employee."
      "minLength": 8,
      "exclusiveMinimum": true
    }
  },
  "required": ["id", "name", "price"]
}
JSON Schema is also a JSON document, and that document MUST be an object. Object members (or properties) defined by JSON Schema are called keywords.

Let’s explain the keywords that we have used in our sample:
  1. The $schema keyword states that this schema is written according to the draft v6 specification.
  2. The title and description keywords are descriptive only, in that they do not add constraints to the data being validated. The intent of the schema is stated with these two keywords: describes a product.
  3. The type keyword defines the first constraint on our JSON data: it has to be a JSON Object.
Also, a JSON Schema may contain properties which are not schema keywords. In our case idnameemail will be members (or properties) of the JSON Object.
For each property, we can define the type. We defined email and name as string and id as number. In JSON Schema a number can have a minimum and maximum and string can have minLength and maxLength. By default this minimum is inclusive, so we need to specify exclusiveMinimum.
Finally, the Schema tells that idname, and email are required.
In next page, we can discuss more about json schema validation with java and node.

Comments

Popular posts from this blog

conditional subschemas (writing conditions inside a JSON Schema)

JSON Schema validation keywords with more explanation

nodejs/javascript client for Rabbitmq