Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Writing Domain Specific Languages with JSON Schema Yos Riady yos.io bit.ly/2JbMTrn

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

JSON is weakly typed { “userName”: “Yos”, “favouriteNumber”: 42, “interests”: [“programming”, “swimming”] }

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

message Person { required string user_name = 1 optional int64 favourite_number = 2 repeated string interests = 3 } Protocol Buffers are strongly typed

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

How can we validate JSON data with types?

Slide 10

Slide 10 text

Enter JSON Schema { “userName”: { “type”: “string” }, “favouriteNumber”: { “type”: “integer” }, “interests”: { “type”: “array”, “items”: { “type”: “string”} } }

Slide 11

Slide 11 text

JSON Schema helps you validate JSON data.

Slide 12

Slide 12 text

jsonschemalint.com

Slide 13

Slide 13 text

JSON Schema 101 { "type": "string" } Valid Invalid “foo” 123 false

Slide 14

Slide 14 text

JSON Schema Basic Types ● string ● Numeric types ● object ● array ● boolean ● null

Slide 15

Slide 15 text

{ "type": "object", "properties": { "userName": { "type": "string" } }, "required": ["userName"], "additionalProperties": false, } JSON Schema 101 Valid Invalid { “userName”: “foo” } { “userName”: 1 } { “name”: “foo” } {}

Slide 16

Slide 16 text

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’

Slide 17

Slide 17 text

Additional JSON Schema Validations { "type": "number", "minimum": 0, "maximum": 100, "exclusiveMaximum": true } { "type": "string", "pattern": "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$" }

Slide 18

Slide 18 text

Combining JSON Schemas ● AnyOf ● AllOf ● OneOf ● not { "anyOf": [ { "type": "string" }, { "type": "number" } ] }

Slide 19

Slide 19 text

Structuring Complex Schemas { "definitions": { "person": { ... } }, "type": "object", "properties": { "user": { "$ref": "#/definitions/person" } } }

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

jsonschemalint.com

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Hapi.js object validation

Slide 24

Slide 24 text

Dynamic forms

Slide 25

Slide 25 text

Content Modeling in Headless CMSes

Slide 26

Slide 26 text

Content Modeling in Headless CMSes

Slide 27

Slide 27 text

Writing Specification Languages

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

Writing a DSL with JSON Schema

Slide 30

Slide 30 text

Domain Specific Languages ● Computer language specialized to a particular application domain ○ In contrast to General Purpose Languages (GPLs)

Slide 31

Slide 31 text

OpenAPI / Swagger

Slide 32

Slide 32 text

Web Automation Markup Language (waml.io)

Slide 33

Slide 33 text

waml.io/editor

Slide 34

Slide 34 text

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"] }

Slide 35

Slide 35 text

Writing a DSL with JSON Schema "steps": { "type": "array", "minItems": 1, "items": { "$ref": "#/definitions/Step" } }

Slide 36

Slide 36 text

Writing a DSL with JSON Schema "Step": { "type": "object", "description": "A step represents a single user interaction.", "oneOf": [ { "$ref": "#/definitions/VisitStep" }, { "$ref": "#/definitions/ClickStep" } ] }

Slide 37

Slide 37 text

"ClickStep": { "type": "object", "properties": { "click": { "oneOf": [ { "type": "string" }, { "type": "object", "properties": { "selector": { "type": "string" }, "index": { "type": "integer", "minimum": 0 } } } ] }, }, }

Slide 38

Slide 38 text

github.com/waml-lang/waml

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

● 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

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Writing Domain Specific Languages with JSON Schema Yos Riady yos.io bit.ly/2JbMTrn

Slide 43

Slide 43 text

Q&A