$30 off During Our Annual Pro Sale. View Details »

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. View Slide

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

    View Slide

  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

    View Slide

  4. View Slide

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

    View Slide

  6. View Slide

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

    View Slide

  8. View Slide

  9. How can we validate
    JSON data with types?

    View Slide

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

    View Slide

  11. JSON Schema helps you validate
    JSON data.

    View Slide

  12. jsonschemalint.com

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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’

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  20. View Slide

  21. jsonschemalint.com

    View Slide

  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

    View Slide

  23. Hapi.js object validation

    View Slide

  24. Dynamic forms

    View Slide

  25. Content Modeling in Headless CMSes

    View Slide

  26. Content Modeling in Headless CMSes

    View Slide

  27. Writing Specification Languages

    View Slide

  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

    View Slide

  29. Writing a DSL with JSON Schema

    View Slide

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

    View Slide

  31. OpenAPI / Swagger

    View Slide

  32. Web Automation Markup Language (waml.io)

    View Slide

  33. waml.io/editor

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  38. github.com/waml-lang/waml

    View Slide

  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

    View Slide

  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

    View Slide

  41. View Slide

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

    View Slide

  43. Q&A

    View Slide