Skip to content

Instantly share code, notes, and snippets.

@tautologistics
Last active January 2, 2016 02:29
Show Gist options
  • Save tautologistics/8237623 to your computer and use it in GitHub Desktop.
Save tautologistics/8237623 to your computer and use it in GitHub Desktop.
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));
@tautologistics
Copy link
Author

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:

  • array must not not have an event of type "inventory sent"

...which can be simplified to...

  • array must not not have an event of type "inventory sent"

v5 has a proposed "contains" that does exactly what we want but it is not released yet...

@vector-man
Copy link

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