Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Writing Domain Specific Languages with JSON Schema

Writing Domain Specific Languages with JSON Schema

In this talk, learn how you can use JSON Schema to write a specification language. You will learn about what Domain Specific Languages are, how the WAML web automation language was developed using JSON Schema, and the far-reaching applications of JSON Schema in other domains such as Headless CMSes.

Yos Riady

April 02, 2018
Tweet

More Decks by Yos Riady

Other Decks in Programming

Transcript

  1. 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
  2. message Person { required string user_name = 1 optional int64

    favourite_number = 2 repeated string interests = 3 } Protocol Buffers are strongly typed
  3. Enter JSON Schema { “userName”: { “type”: “string” }, “favouriteNumber”:

    { “type”: “integer” }, “interests”: { “type”: “array”, “items”: { “type”: “string”} } }
  4. JSON Schema Basic Types • string • Numeric types •

    object • array • boolean • null
  5. { "type": "object", "properties": { "userName": { "type": "string" }

    }, "required": ["userName"], "additionalProperties": false, } JSON Schema 101 Valid Invalid { “userName”: “foo” } { “userName”: 1 } { “name”: “foo” } {}
  6. 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’
  7. Additional JSON Schema Validations { "type": "number", "minimum": 0, "maximum":

    100, "exclusiveMaximum": true } { "type": "string", "pattern": "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$" }
  8. Combining JSON Schemas • AnyOf • AllOf • OneOf •

    not { "anyOf": [ { "type": "string" }, { "type": "number" } ] }
  9. Structuring Complex Schemas { "definitions": { "person": { ... }

    }, "type": "object", "properties": { "user": { "$ref": "#/definitions/person" } } }
  10. 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
  11. 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
  12. Domain Specific Languages • Computer language specialized to a particular

    application domain ◦ In contrast to General Purpose Languages (GPLs)
  13. 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"] }
  14. Writing a DSL with JSON Schema "steps": { "type": "array",

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

    "description": "A step represents a single user interaction.", "oneOf": [ { "$ref": "#/definitions/VisitStep" }, { "$ref": "#/definitions/ClickStep" } ] }
  16. "ClickStep": { "type": "object", "properties": { "click": { "oneOf": [

    { "type": "string" }, { "type": "object", "properties": { "selector": { "type": "string" }, "index": { "type": "integer", "minimum": 0 } } } ] }, }, }
  17. 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
  18. • 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
  19. Q&A