Enterprise Interface Architecture:
JSON Schema

Tomasz Ducin

17th June 2015, Warsaw

#10

Tomek Ducin

JavaScript, Java, Python

senior software consultant @ Cybercom Poland

blah, blah, blah...

Enterprise Interface Architecture

?

Agenda

  1. JSON
  2. schemas
  3. JSON Schema
  4. JSON Schema - Tool Stack

Douglas Crockford

creator of JSON

JSON:

brought to you by

Java Script Object Notation

  • defining object literals: {} and []
  • their values might be:
    • string (enclosed with ")
    • number (double type)
    • constant: false, true, null
  • allows unlimited nesting
  • flexible: will accept everything

Data Serialization Format

JS

N

YAML

used by

  RFC 7159 vs ECMA-404

QUIZ - is this a valid JSON?

  • {}
  • []
  • [1,2,3]
  • {a:1, b:2}
  • {"a":1, "b":2}
  • {"a":1, 'b':2}
  • [{1:2}, []]
  • [true, false, undefined]
  • ["warsawJS hell yeah!"]
  • "warsawJS hallelujah!"
  • true
  • Number(true)

$

<?xml version="1.0" encoding="UTF-8"?>

<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
  <orderperson>John Smith</orderperson>
  <shipto>
    <name>Ola Nordmann</name>
    <address>Langgt 23</address>
    <city>4000 Stavanger</city>
    <country>Norway</country>
  </shipto>
  <item>
    <title>Empire Burlesque</title>
    <note>Special Edition</note>
    <quantity>1</quantity>
    <price>10.90</price>
  </item>
  <item>
    <title>Hide your heart</title>
    <quantity>1</quantity>
    <price>9.90</price>
  </item>
</shiporder>
<xs:element name="shipto">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="address" type="xs:string"/>
      <xs:element name="city" type="xs:string"/>
      <xs:element name="country" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="shiporder">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="orderperson" type="xs:string"/>
      <xs:element name="shipto">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="address" type="xs:string"/>
            <xs:element name="city" type="xs:string"/>
            <xs:element name="country" type="xs:string"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="item" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="note" type="xs:string" minOccurs="0"/>
            <xs:element name="quantity" type="xs:positiveInteger"/>
            <xs:element name="price" type="xs:decimal"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="orderid" type="xs:string" use="required"/>
  </xs:complexType>
</xs:element>

</xs:schema>

JavaScript is dynamically-typed

so is JSON

no compile-time check

communication between components relies on rigid structures

what if the structure changes from one side?

that's where schema comes in

I made 1500 100 900 apps and had no problems with JSON

what to validate?

  • api communication

  • configuration

  • internal app definitions

JSON Schema

  • XSD for XML

  • JSON Schema for JSON

JSON Schema

http://json-schema.org/

  • a specification

  • JSON used to define JSON

  • a meta-definition

{
  "type": "boolean"
}
{
  "type": "integer",
  "minimum": 600,
  "maximum": 700,
  "multipleOf": 5,
  "exclusiveMinimum": true
}
{
  "type": "string"
}
true
"WarsawJS"
605
{
  "type": "array",
  "minItems": 5,
  "items": {
    "enum": [
      "green",
      "blue",
      "yellow"
    ]
  }
}
[
  "green",
  "blue",
  "green",
  "green",
  "yellow",
  "green"
]
{
  "title": "Example Schema",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string"
    },
    "lastName": {
      "type": "string"
    },
    "age": {
      "description": "Age in years",
      "type": "integer",
      "minimum": 0
    }
  },
  "additionalProperties": false,
  "required": ["firstName", "lastName"]
}
{
  "firstName": "John",
  "lastName": "Lennon",
  "age": 40
}
{
  "firstName": "Paul",
  "lastName": "McCartney"
}
{
  ...
  "properties": {
    "storage": {
      "type": "object",
      "oneOf": [
        { "$ref": "#/definitions/diskDevice" },
        { "$ref": "#/definitions/diskUUID" },
        { "$ref": "#/definitions/nfs" },
        { "$ref": "#/definitions/tmpfs" }
      ]
    }
  }
  ...
}
$ref, oneOf, allOf, anyOf

JSON Schema Tool Stack

JSON Schema online generator

node.js-based
JSON Schema Validator

Mocks &

Fake Data

fake data
online generator

node.js-based

fake data generator

fake data generator grunt plugin

  • it's not that trendy YET!!!
  • meta-definition
  • validation base
  • fake data generator
  • Java had it, and so does JS
  • enterprise will ❤ it!

THX!

4  Q&A