Writing Domain Specific Languages
with JSON Schema
Yos Riady
yos.io
bit.ly/2JbMTrn
Weak types in JSON
What is JSON Schema?
Introduction Schema Uses DSL Conclusion
JSON Schema use
cases
Summary and further
learning
Writing a DSL with
JSON Schema
JSON is weakly typed
{
“userName”: “Yos”,
“favouriteNumber”: 42,
“interests”: [“programming”, “swimming”]
}
message Person {
required string user_name = 1
optional int64 favourite_number = 2
repeated string interests = 3
}
Protocol Buffers are strongly typed
How can we validate
JSON data with types?
Enter JSON Schema
{
“userName”: { “type”: “string” },
“favouriteNumber”: { “type”: “integer” },
“interests”: { “type”: “array”, “items”: { “type”: “string”} }
}
JSON Schema helps you validate
JSON data.
jsonschemalint.com
JSON Schema 101
{
"type": "string"
}
Valid Invalid
“foo” 123
false
JSON Schema Basic Types
● string
● Numeric types
● object
● array
● boolean
● null
{
"type": "object",
"properties": {
"userName": {
"type": "string"
}
},
"required": ["userName"],
"additionalProperties": false,
}
JSON Schema 101 Valid Invalid
{
“userName”:
“foo”
}
{
“userName”: 1
}
{
“name”: “foo”
}
{}
JSON Schema 101
Invalid Validation Message
{
“userName”: 1
}
Field .userName should be a string
{
“name”: “foo”
}
Should NOT have additional properties
{} Should have required property ‘userName’
Additional JSON Schema Validations
{
"type": "number",
"minimum": 0,
"maximum": 100,
"exclusiveMaximum": true
}
{
"type": "string",
"pattern": "^(([0-9]{3}))?[0-9]{3}-[0-9]{4}$"
}
Combining JSON Schemas
● AnyOf
● AllOf
● OneOf
● not
{
"anyOf": [
{ "type": "string" },
{ "type": "number" }
]
}
Structuring Complex Schemas
{
"definitions": {
"person": { ... }
},
"type": "object",
"properties": {
"user": { "$ref": "#/definitions/person" }
}
}
jsonschemalint.com
Use Cases for JSON Schema
● Message / data validation
○ Object Relational Mappers (ORMs)
● Schemaless objects in Web Applications
● Content Management in Headless CMSes
● Writing Specification Languages
○ OpenAPI / Swagger
○ AWS States Language
○ WAML
Hapi.js object validation
Dynamic forms
Content Modeling in Headless CMSes
Content Modeling in Headless CMSes
Writing Specification Languages
Weak types in JSON
What is JSON Schema?
Introduction Schema Uses DSL Conclusion
JSON Schema use
cases
Summary and further
learning
Writing a DSL with
JSON Schema
Writing a DSL with JSON Schema
Domain Specific Languages
● Computer language specialized to a particular application domain
○ In contrast to General Purpose Languages (GPLs)
OpenAPI / Swagger
Web Automation Markup Language (waml.io)
waml.io/editor
Writing a DSL with JSON Schema
"waml": {
"description": "WAML Specification semantic version number.",
"type": "string",
"pattern": "^([0-9]{1,}.[0-9]{1,}.[0-9]{1,})$",
"enum": ["0.1.0"]
}
Writing a DSL with JSON Schema
"steps": {
"type": "array",
"minItems": 1,
"items": {
"$ref": "#/definitions/Step"
}
}
Writing a DSL with JSON Schema
"Step": {
"type": "object",
"description": "A step represents a single user interaction.",
"oneOf": [
{
"$ref": "#/definitions/VisitStep"
},
{
"$ref": "#/definitions/ClickStep"
}
]
}
"ClickStep": {
"type": "object",
"properties": {
"click": {
"oneOf": [
{
"type": "string"
},
{
"type": "object",
"properties": {
"selector": { "type": "string" },
"index": { "type": "integer", "minimum": 0 }
}
}
]
},
},
}
github.com/waml-lang/waml
Weak types in JSON
What is JSON Schema?
Introduction Schema Uses DSL Conclusion
JSON Schema use
cases
Summary and further
learning
Writing a DSL with
JSON Schema
● JSON is weakly typed
● JSON Schema
○ What is JSON Schema?
○ JSON validation with JSON Schema
○ Use cases
● Domain Specific Languages
○ What are DSLs?
○ Writing a DSL with JSON Schema
In Closing
Writing Domain Specific Languages
with JSON Schema
Yos Riady
yos.io
bit.ly/2JbMTrn
Q&A

Writing Domain Specific Languages with JSON Schema

  • 2.
    Writing Domain SpecificLanguages with JSON Schema Yos Riady yos.io bit.ly/2JbMTrn
  • 3.
    Weak types inJSON What is JSON Schema? Introduction Schema Uses DSL Conclusion JSON Schema use cases Summary and further learning Writing a DSL with JSON Schema
  • 5.
    JSON is weaklytyped { “userName”: “Yos”, “favouriteNumber”: 42, “interests”: [“programming”, “swimming”] }
  • 7.
    message Person { requiredstring user_name = 1 optional int64 favourite_number = 2 repeated string interests = 3 } Protocol Buffers are strongly typed
  • 9.
    How can wevalidate JSON data with types?
  • 10.
    Enter JSON Schema { “userName”:{ “type”: “string” }, “favouriteNumber”: { “type”: “integer” }, “interests”: { “type”: “array”, “items”: { “type”: “string”} } }
  • 11.
    JSON Schema helpsyou validate JSON data.
  • 12.
  • 13.
    JSON Schema 101 { "type":"string" } Valid Invalid “foo” 123 false
  • 14.
    JSON Schema BasicTypes ● string ● Numeric types ● object ● array ● boolean ● null
  • 15.
    { "type": "object", "properties": { "userName":{ "type": "string" } }, "required": ["userName"], "additionalProperties": false, } JSON Schema 101 Valid Invalid { “userName”: “foo” } { “userName”: 1 } { “name”: “foo” } {}
  • 16.
    JSON Schema 101 InvalidValidation Message { “userName”: 1 } Field .userName should be a string { “name”: “foo” } Should NOT have additional properties {} Should have required property ‘userName’
  • 17.
    Additional JSON SchemaValidations { "type": "number", "minimum": 0, "maximum": 100, "exclusiveMaximum": true } { "type": "string", "pattern": "^(([0-9]{3}))?[0-9]{3}-[0-9]{4}$" }
  • 18.
    Combining JSON Schemas ●AnyOf ● AllOf ● OneOf ● not { "anyOf": [ { "type": "string" }, { "type": "number" } ] }
  • 19.
    Structuring Complex Schemas { "definitions":{ "person": { ... } }, "type": "object", "properties": { "user": { "$ref": "#/definitions/person" } } }
  • 21.
  • 22.
    Use Cases forJSON Schema ● Message / data validation ○ Object Relational Mappers (ORMs) ● Schemaless objects in Web Applications ● Content Management in Headless CMSes ● Writing Specification Languages ○ OpenAPI / Swagger ○ AWS States Language ○ WAML
  • 23.
  • 24.
  • 25.
    Content Modeling inHeadless CMSes
  • 26.
    Content Modeling inHeadless CMSes
  • 27.
  • 28.
    Weak types inJSON What is JSON Schema? Introduction Schema Uses DSL Conclusion JSON Schema use cases Summary and further learning Writing a DSL with JSON Schema
  • 29.
    Writing a DSLwith JSON Schema
  • 30.
    Domain Specific Languages ●Computer language specialized to a particular application domain ○ In contrast to General Purpose Languages (GPLs)
  • 31.
  • 32.
    Web Automation MarkupLanguage (waml.io)
  • 33.
  • 34.
    Writing a DSLwith JSON Schema "waml": { "description": "WAML Specification semantic version number.", "type": "string", "pattern": "^([0-9]{1,}.[0-9]{1,}.[0-9]{1,})$", "enum": ["0.1.0"] }
  • 35.
    Writing a DSLwith JSON Schema "steps": { "type": "array", "minItems": 1, "items": { "$ref": "#/definitions/Step" } }
  • 36.
    Writing a DSLwith JSON Schema "Step": { "type": "object", "description": "A step represents a single user interaction.", "oneOf": [ { "$ref": "#/definitions/VisitStep" }, { "$ref": "#/definitions/ClickStep" } ] }
  • 37.
    "ClickStep": { "type": "object", "properties":{ "click": { "oneOf": [ { "type": "string" }, { "type": "object", "properties": { "selector": { "type": "string" }, "index": { "type": "integer", "minimum": 0 } } } ] }, }, }
  • 38.
  • 39.
    Weak types inJSON What is JSON Schema? Introduction Schema Uses DSL Conclusion JSON Schema use cases Summary and further learning Writing a DSL with JSON Schema
  • 40.
    ● JSON isweakly typed ● JSON Schema ○ What is JSON Schema? ○ JSON validation with JSON Schema ○ Use cases ● Domain Specific Languages ○ What are DSLs? ○ Writing a DSL with JSON Schema In Closing
  • 42.
    Writing Domain SpecificLanguages with JSON Schema Yos Riady yos.io bit.ly/2JbMTrn
  • 43.