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.

1b0ab2500efc1b91424fb49045312929?s=128

Yos Riady

April 02, 2018
Tweet

Transcript

  1. None
  2. Writing Domain Specific Languages with JSON Schema Yos Riady yos.io

    bit.ly/2JbMTrn
  3. 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
  4. None
  5. JSON is weakly typed { “userName”: “Yos”, “favouriteNumber”: 42, “interests”:

    [“programming”, “swimming”] }
  6. None
  7. message Person { required string user_name = 1 optional int64

    favourite_number = 2 repeated string interests = 3 } Protocol Buffers are strongly typed
  8. None
  9. How can we validate JSON data with types?

  10. Enter JSON Schema { “userName”: { “type”: “string” }, “favouriteNumber”:

    { “type”: “integer” }, “interests”: { “type”: “array”, “items”: { “type”: “string”} } }
  11. JSON Schema helps you validate JSON data.

  12. jsonschemalint.com

  13. JSON Schema 101 { "type": "string" } Valid Invalid “foo”

    123 false
  14. JSON Schema Basic Types • 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 Invalid Validation Message { “userName”: 1 }

    Field .userName should be a string { “name”: “foo” } Should NOT have additional properties {} Should have required property ‘userName’
  17. Additional JSON Schema Validations { "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" } } }
  20. None
  21. jsonschemalint.com

  22. 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
  23. Hapi.js object validation

  24. Dynamic forms

  25. Content Modeling in Headless CMSes

  26. Content Modeling in Headless CMSes

  27. Writing Specification Languages

  28. 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
  29. Writing a DSL with JSON Schema

  30. Domain Specific Languages • Computer language specialized to a particular

    application domain ◦ In contrast to General Purpose Languages (GPLs)
  31. OpenAPI / Swagger

  32. Web Automation Markup Language (waml.io)

  33. waml.io/editor

  34. 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"] }
  35. Writing a DSL with JSON Schema "steps": { "type": "array",

    "minItems": 1, "items": { "$ref": "#/definitions/Step" } }
  36. Writing a DSL with 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. github.com/waml-lang/waml

  39. 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
  40. • 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
  41. None
  42. Writing Domain Specific Languages with JSON Schema Yos Riady yos.io

    bit.ly/2JbMTrn
  43. Q&A