All Projects → soylent → Jschema

soylent / Jschema

Licence: mit
JSON Schema implementation

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Jschema

Spec Tools
Clojure(Script) tools for clojure.spec
Stars: ✭ 524 (+1646.67%)
Mutual labels:  json-schema
Baloo
Expressive end-to-end HTTP API testing made easy in Go
Stars: ✭ 702 (+2240%)
Mutual labels:  json-schema
Movement
Movement is an easier, simpler way to explore and use NIEM. Want to join the Movement and contribute to it? Start here.
Stars: ✭ 19 (-36.67%)
Mutual labels:  json-schema
Jsonforms
Customizable JSON Schema-based forms with React, Angular and Vue support out of the box.
Stars: ✭ 542 (+1706.67%)
Mutual labels:  json-schema
Malli
Data-Driven Schemas for Clojure/Script.
Stars: ✭ 667 (+2123.33%)
Mutual labels:  json-schema
Conf
Simple config handling for your app or module
Stars: ✭ 707 (+2256.67%)
Mutual labels:  json-schema
Invenio
Invenio digital library framework
Stars: ✭ 469 (+1463.33%)
Mutual labels:  json-schema
Fastapi
FastAPI framework, high performance, easy to learn, fast to code, ready for production
Stars: ✭ 39,588 (+131860%)
Mutual labels:  json-schema
Pydantic
Data parsing and validation using Python type hints
Stars: ✭ 8,362 (+27773.33%)
Mutual labels:  json-schema
Electron Crash Report Service
Aggregate crash reports for Electron apps
Stars: ✭ 17 (-43.33%)
Mutual labels:  json-schema
Json Forms
JSON Schema to HTML form generator, supporting dynamic subschemas (on the fly resolution). Extensible and customizable library with zero dependencies. Bootstrap add-ons provided
Stars: ✭ 549 (+1730%)
Mutual labels:  json-schema
Jsonschema2pojo
Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
Stars: ✭ 5,633 (+18676.67%)
Mutual labels:  json-schema
Quicktype
Generate types and converters from JSON, Schema, and GraphQL
Stars: ✭ 7,459 (+24763.33%)
Mutual labels:  json-schema
Json Schema Ref Parser
Parse, Resolve, and Dereference JSON Schema $ref pointers in Node and browsers
Stars: ✭ 532 (+1673.33%)
Mutual labels:  json-schema
Jsonschema2graphql
Convert JSON schema to GraphQL types
Stars: ✭ 24 (-20%)
Mutual labels:  json-schema
Schm
Composable schemas for JavaScript and Node.js
Stars: ✭ 498 (+1560%)
Mutual labels:  json-schema
Swagger Parser
Swagger 2.0 and OpenAPI 3.0 parser/validator
Stars: ✭ 710 (+2266.67%)
Mutual labels:  json-schema
Ansible Schema Generator
Generate JSON schema for language servers from Ansible module documentation
Stars: ✭ 30 (+0%)
Mutual labels:  json-schema
Spectral
A flexible JSON/YAML linter for creating automated style guides, with baked in support for OpenAPI v2 & v3.
Stars: ✭ 876 (+2820%)
Mutual labels:  json-schema
Apispec
A pluggable API specification generator. Currently supports the OpenAPI Specification (f.k.a. the Swagger specification)..
Stars: ✭ 831 (+2670%)
Mutual labels:  json-schema

JSchema

Describe and validate your JSON data using JSchema – an implementation of JSON schema v4. For more information on JSON schema please refer to the official documentation – JSON Schema Documentation.

Build Status Coverage Status Code Climate

Features

  • Implements JSON Schema draft 4 strictly according to the specification
  • Small, efficient and thread-safe
  • No dependencies
  • Clean and extensible code
  • Tested on Rubinius, MRI, and JRuby

Installation

Add jschema to your Gemfile and execute bundle install.

Basic usage example

require 'jschema'

# Create a new schema describing user profile data
schema = JSchema.build(
  'type' => 'object',
  'properties' => {
    'email'    => { 'type' => 'string', 'format' => 'email' },
    'password' => { 'type' => 'string', 'minLength' => 6 },
    'sex'      => { 'type' => 'string', 'enum' => ['m', 'f'] }
  },
  'required' => ['email', 'password']
)

# Validate input and return an array of validation errors
schema.validate 'invalid_data'
# => ["`invalid_data` must be an object"]

schema.validate('email' => '[email protected]', 'password' => 'kielbasa')
# => []

# Validate input and return boolean value indicating validation result
schema.valid? 'invalid_data'
# => false

schema.valid?('email' => '[email protected]', 'password' => 'kielbasa')
# => true

More advanced example

The following example shows how you can validate user's input. In this example we implement a simple service that allows us to search for comics.

First let's define schema for the search query. It can also be used as documentation for our service. We can add title and description fields for each query param.

// comic_search_query.json
{
    "title": "Comic search query",
    "description": "Fetches lists of comics with optional filters",
    "type": "object",
    "properties": {
        "characters": {
            "description": "Return only comics which feature the specified characters",
            "type": "array",
            "items": { "pattern": "^\\d+$" },
            "minItems": 1
        },
        "format": {
            "description": "Filter by the issue format type (comic or collection)",
            "type": "string",
            "enum": [ "comic", "collection" ]
        },
        "hasDigitalIssue": {
            "description": "Include only results which are available digitally",
            "type": "string",
            "enum": [ "1", "0" ],
            "default": "1"
        },
        "limit": {
            "description": "Limit the result set to the specified number of resources",
            "type": "integer",
            "minimum": 1,
            "maximum": 100
        }
    },
    "required": [ "characters" ]
}

Now we can use our query schema in order to validate user's input. Here is implementation of the comic search service:

# config.ru
require 'jschema'
require 'rack'
require 'json'

class ComicSearch
  class << self
    def call(env)
      request = Rack::Request.new(env)

      # Validate request params using JSON schema
      validation_errors = query_schema.validate(request.params)

      if validation_errors.empty?
        # Query is valid, request can be processed further
        Rack::Response.new('Valid query', 200)
      else
        # Query is not valid, show validation errors
        Rack::Response.new(validation_errors, 400)
      end
    end

    private

    def query_schema
      # Create a new instance of JSchema unless it has
      # already been created.
      Thread.current[:schema] ||= begin
        schema_data = JSON.parse File.read('comic_search_query.json')
        JSchema.build(schema_data)
      end
    end
  end
end

run ComicSearch

Run the service:

$ rackup -D

Make a valid request:

$ curl -v --globoff ":9292/?characters[]=1"
...
< HTTP/1.1 200 OK

Make a bad request:

$ curl -v --globoff ":9292/?characters[]=first"
...
< HTTP/1.1 400 Bad Request
< first must match pattern "^\\d+$"

Fragments

You can validate against part of a schema by extracting it into a new schema object with Schema#fragment:

schema = JSchema.build(
  'type' => 'object',
  'properties' => {
    'email': { '$ref' => '#/definitions/email' }
  },
  'definitions' => {
    'email' => { 'type' => 'string', 'format' => 'email' }
  })

email_schema = schema.fragment('#/definitions/email')
email_schema.valid?('[email protected]')
# => true

email_schema.valid?('invalidexample.com')
# => false

Contributing

Pull requests are very welcome!

  • Please make sure that your changes don't break the tests by running:

    $ bundle exec rake
    
  • Run a single test suite:

    $ ruby -Ilib test/test_suite.rb
    
  • Run a single test:

    $ ruby -Ilib test/test_suite.rb -n /test_method_name/
    

License

MIT License (MIT)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].