An attempt to implement json schema validation in Crystal.
Work in progress but could be a useful start
-
Add the dependency to your
shard.yml
:dependencies: json_schema: github: kefahi/json_schema.cr
-
Run
shards install
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
- Fork it (https://github.com/kefahi/json_schema.cr/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
- Kefah Issa - This is a continuation on work from DougEverly