Coder Social home page Coder Social logo

go-jsonschema's Introduction

go-jsonschema is a tool to generate Go data types from JSON Schema definitions.

This tool generates Go data types and structs that corresponds to definitions in the schema, along with unmarshaling code that validates the input JSON according to the schema's validation rules.

Installing

  • Binary install: Get a release here.

  • From source: Go 1.11 or later, with Go modules enabled, is advisable in order to get the right dependencies. To install:

$ go get github.com/atombender/go-jsonschema/...
$ go install github.com/atombender/go-jsonschema/cmd/gojsonschema@latest

Usage

At its most basic:

$ gojsonschema -p main schema.json

This will write a Go source file to standard output, declared under the package main.

You can generate code for multiple schemas in the same invocation, optionally writing to different files inside different packages:

$ gojsonschema \
  --schema-package=https://example.com/schema1=github.com/myuser/myproject \
   --schema-output=https://example.com/schema1=schema1.go \
  --schema-package=https://example.com/schema2=github.com/myuser/myproject/stuff \
   --schema-output=https://example.com/schema2=stuff/schema2.go \
  schema1.json schema2.json

This will create schema1.go (declared as package myproject) and stuff/schema2.go (declared as package stuff). If schema1.json refers to schema2.json or vice versa, the two Go files will import the other package that it depends on. Note the flag format:

--schema-package=https://example.com/schema1=github.com/myuser/myproject \
                 ^                           ^
                 |                           |
                 schema $id                  full import URL

Status

While not finished, go-jsonschema can be used today. Aside from some minor features, only specific validations remain to be fully implemented.

Validation

  • Core (RFC draft)
    • Data model (§4.2.1)
      • null
      • boolean
      • object
      • array
      • number
        • Option to use json.Number
      • string
    • Location identifiers (§8.2.3)
      • References against top-level names: #/Definitions/someName
      • References against nested names: #/Definitions/someName/Definitions/someOtherName
      • References against top-level names in external files: myschema.json#/Definitions/someName
      • References against nested names: myschema.json#/Definitions/someName/Definitions/someOtherName
    • Comments (§9)
  • Validation (RFC draft)
    • Schema annotations (§10)
      • description
      • default (only for struct fields)
      • readOnly
      • writeOnly
      • title (N/A)
      • examples (N/A)
    • General validation (§6.1)
      • enum
      • type (single)
      • type (multiple; note: partial support, limited validation)
      • const
    • Numeric validation (§6.2)
      • multipleOf
      • maximum
      • exclusiveMaximum
      • minimum
      • exclusiveMinimum
    • String validation (§6.3)
      • maxLength
      • minLength
      • pattern
    • Array validation (§6.4)
      • items
      • maxItems
      • minItems
      • uniqueItems
      • additionalItems
      • contains
    • Object validation (§6.5)
      • required
      • properties
      • patternProperties
      • dependencies
      • propertyNames
      • maxProperties
      • minProperties
    • Conditional subschemas (§6.6)
      • if
      • then
      • else
    • Boolean subschemas (§6.7)
      • allOf
      • anyOf
      • oneOf
      • not
    • Semantic formats (§7.3)
      • Dates and times
      • Email addresses
      • Hostnames
      • IP addresses
      • Resource identifiers
      • URI-template
      • JSON pointers
      • Regex

License

MIT license. See LICENSE file.

go-jsonschema's People

Contributors

atombender avatar gonzaloserrano avatar haya14busa avatar kwuerl avatar louis77 avatar pantafive avatar tjungblu avatar vaihtovirta avatar xwjdsh avatar zapling 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.