Last active
January 2, 2016 02:29
-
-
Save tautologistics/8237623 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tv4 = require('tv4'); | |
var schema = { | |
"type": "object", | |
"$schema": "http://json-schema.org/draft-04/schema", | |
"id": "#", | |
"required": ["events"], | |
"properties": { | |
"events": { | |
"type": "array", | |
"minItems": 1, | |
"not": { | |
"items": { | |
"type": "object", | |
"required": ["event_name"], | |
"properties": { | |
"event_name": { | |
"type": "string", | |
"not": { | |
"enum": ["inventory sent"] | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}; | |
var data = { | |
"events":[ | |
{ "event_name": "foo" }, | |
{ "event_name": "inventory sent" }, | |
{ "event_name": "foo" } | |
] | |
}; | |
console.log(tv4.validateMultiple(data, schema)); |
I've been looking for a good example on this. Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This validates the "events" object, and requires that at least one item in that array has a the property "event_name" with the value "inventory sent".
JSON schema v4 does not have a native validation keyword for this so a double negative has to be used:
...which can be simplified to...
not nothave an event of type "inventory sent"v5 has a proposed "contains" that does exactly what we want but it is not released yet...