Giter Site home page Giter Site logo

wryun / yajsonschema Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 0.0 1.14 MB

Make JSON schemas easier with a basic YAML DSL

Home Page: https://wryun.github.io/yajsonschema

License: Apache License 2.0

Go 100.00%
yaml json json-schema json-schema-generator yaml-validator json-validation

yajsonschema's Introduction

Build Status GoDoc API

yajsonschema (Yet Another JSON Schema [something]) is a tool which converts a schema written in the yaml format defined here into a proper JSON Schema (draft-04). i.e. it's primarily an easier way to write json schemas, but you can also use it directly as a validator.

It tries to be as easy as possible to use by starting from the idea that a normal document should act as its own schema (i.e. should match itself).

WARNING: this is largely 'proof-of-concept' and hasn't been used beyond the test-cases.

Example schema

---
name: !type {
  type: string,
  minLength: 1
}
---
fileType: !enum [json, yaml]
organisation: !ref name
evil?: !type boolean
people:
- name: !ref name
  age: !type {
    type: integer,
    min: 0
  }
  -: false # definition of JSON Schema additionalProperties
- funkiness: true

Things you may notice here:

  • there are two YAML documents, the first being a list of definitions accessible via ref. If you have only one document, this is the schema.
  • we use yaml custom types (e.g. '!ref') to abbreviate things
  • normal json schemas default to object properties being non-required, but yajsonschema defaults to required (using '?' at the end of the property name to represent optional)
  • ways to break out of 'this should match itself':
    • an array indicates that items should match any of the items in the array (i.e. anyOf), not that it should be an exact match to all the items as in json schema
    • !ref myname is shorthand for {"$ref": "#/definitions/myname"}
    • !type (object) = standard type definition
      • this is a useful way to 'break out' into standard json schema. You can use definition references to jump back to yajsonschema syntax.
    • !type int = type definition where the type is set to int and no other validation is applied
    • !enum [arr] gives you a list of possibilities for this value
    • for objects, '?' at the end of properties indicates that it's an optional property, and '-' is the 'additionalProperties' property in the schema (with special handling for true/false so that they're passed through with their semantic meaning rather than intepreted as an enum/const)

Using as a Go library

See usage in cmd/yajsonschema.go and API documentation on godoc

Using as a CLI tool

To generate the json schema (on stdout):

yajsonschema -s schema.yaml

To validate json documents immediately:

yajsonschema -s schema.yaml myinput.json myinput2.json

(error code of 2 indicates failure to validate; output on stderr)

Similar work

There're a bunch other validators and 'json schema generators'. I don't know of any others that rely on yaml custom types like this, or are inspired explicitly by the idea of being as close as possible to the input (i.e. such that one can use input as a validation schema, and can develop a schema by 'relaxing' an example input appropriately).

TODO

  • clean up API docs (include examples etc.)
  • upstream a better version of custom tag support to go-yaml
  • package javascript as npm package
  • use a js schema validator rather than the Go one on website (bloats JS hugely)
  • add some random shields
  • 'release' the CLI via github (i.e. binary builds)

yajsonschema's People

Contributors

wryun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.