Skip to content

Instantly share code, notes, and snippets.

@apsoto
Created July 26, 2013 00:27
Show Gist options
  • Save apsoto/6085088 to your computer and use it in GitHub Desktop.
Save apsoto/6085088 to your computer and use it in GitHub Desktop.
JSON sample validation
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"type" : "object",
"properties" : {
"class" : {
"type" : "array",
"items" : {
"type" : "integer"
}
}
}
}
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"type" : "object",
"properties" : {
"class" : {
"allOf" : [{"$ref":"definitions.json#/definitions/sirenClass"}]
}
}
}
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"type" : "object",
"definitions" : {
"sirenClass" : {
"type" : "array",
"items" : {
"type" : "integer"
}
}
}
}
> schema
=> "bundle_with_ref.json"
> data
=> "{\"class\":[\"collection\"]}"
> JSON::Validator.clear_cache ; JSON::Validator.fully_validate(schema, data, validate_schema:true, record_errors:true)
=> []
> schema='bundle_no_ref.json'
=> "bundle_no_ref.json"
> JSON::Validator.clear_cache ; JSON::Validator.fully_validate(schema, data, validate_schema:true, record_errors:true)
=> ["The property '#/class/0' of type String did not match the following type: integer in schema file:///Users/asoto/projects/wurl/servers/wurl-api/bundle_no_ref.json#"]
{
"class" : [ "collection" ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment