Giter Site home page Giter Site logo

json_schema's Introduction

json_schema

A JSON Schema V4 and Hyperschema V4 parser and validator.

Validate some data based on a JSON Schema:

gem install json_schema
validate-schema schema.json data.json

Programmatic

require "json"
require "json_schema"

# parse the schema - raise SchemaError if it's invalid
schema_data = JSON.parse(File.read("schema.json"))
schema = JsonSchema.parse!(schema_data)

# expand $ref nodes - raise SchemaError if unable to resolve
schema.expand_references!

# validate some data - raise ValidationError if it doesn't conform
data = JSON.parse(File.read("data.json"))
schema.validate!(data)

# iterate through hyperschema links
schema.links.each do |link|
  puts "#{link.method} #{link.href}"
end

# abort on first error, instead of listing them all:
schema.validate!(data, fail_fast: true)

Errors have a message (for humans), and type (for machines). ValidationErrors also include a path, a JSON pointer to the location in the supplied document which violated the schema. See errors for more info.

Non-bang methods return a two-element array, with true/false at index 0 to indicate pass/fail, and an array of errors at index 1 (if any).

Passing fail_fast: true (default: false) will cause the validator to abort on the first error it encounters and report just that. Even on fully valid data this can offer some speed improvement, since it doesn't have to collect error messages that might be later discarded (think of e.g. the anyOf directive).

Development

Run the test suite with:

rake

Or run specific suites or tests with:

ruby -Ilib -Itest test/json_schema/validator_test.rb
ruby -Ilib -Itest test/json_schema/validator_test.rb -n /anyOf/

Release

  1. Update the version in json_schema.gemspec as appropriate for semantic versioning and add details to CHANGELOG.

  2. git commit those changes with a message like "Bump version to x.y.z".

  3. Run the release task:

    bundle exec rake release
    

json_schema's People

Contributors

brandur avatar gjtorikian avatar tenderlove avatar okitan avatar breunigs avatar greysteil avatar jhawthorn avatar olleolleolle avatar nornagon avatar jkamerer avatar bkeepers avatar geemus avatar ys avatar slhck avatar braintreeps avatar jamiecobbett avatar isaacseymour avatar gudmundur avatar gabrielrios avatar esiegel avatar edwardbetts avatar floatplane avatar bf4 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.