json_schema

An attempt to implement json schema validation in Crystal.

Work in progress but could be a useful start

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      json_schema:
        github: kefahi/json_schema.cr
  2. Run shards install

Usage

Sample data.json

{
    "id": 1,
    "name": "A green door",
    "price": 12.50,
    "tags": ["home", "green"]
}

Sample schema.json

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Product",
    "description": "A product from Acme's catalog",
    "type": "object",
    "properties": {
      "id": {
          "description": "The unique identifier for a product",
          "type": "integer"
      },
      "name": {
          "description": "The unique identifier for a product",
          "type": "string"
      },
      "price": {
          "type": "number",
          "minimum": 0,
          "exclusiveMinimum": true
      },
      "dimensions": {
          "type": "object",
          "properties": {
              "length": {"type": "number"},
              "width": {"type": "number"},
              "height": {"type": "number"}
          },
          "required": ["length", "width", "height"]
      }
			
    },
    "required": ["id"]
}

Sample code

require "json_schema"
json_data = JSON.parse File.read "data.json" 
schema = JSON::Schema.from_json File.read "schema.json"
schema.validate json_data

Contributing

  1. Fork it (https://github.com/kefahi/json_schema.cr/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors