Skip to content

Instantly share code, notes, and snippets.

@d-adamkiewicz
Created August 15, 2012 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d-adamkiewicz/3358513 to your computer and use it in GitHub Desktop.
Save d-adamkiewicz/3358513 to your computer and use it in GitHub Desktop.
JSV: checking JSON against array schema example
var JSV = require('jsv').JSV;
var json, schema;
var env = JSV.createEnvironment();
var report = env.validate(
json = [{a:1}, {b:1}],
schema = {type:'array',items:{type:'object', properties:{a:{type:'number'}}, additionalProperties: false}}
);
console.log(JSON.stringify(json, null, 4) + JSON.stringify(schema, null, 4));
if (report.errors.length === 0) {
console.log("success");
} else {
console.log("failure");
}
report = env.validate(
json = [{a:1}, {b:1}],
// unless 'additionalProperties' is set to false 'validate'
// doesn't care about names and values types of inner properties
schema = {type:'array',items:{type:'object', properties:{a:{type:'number'}}}}
);
console.log(JSON.stringify(json, null, 4) + JSON.stringify(schema, null, 4));
if (report.errors.length === 0) {
console.log("success");
} else {
console.log("failure");
}
report = env.validate(
json = [{a:1, b:1}],
schema = {type:'array',items:{type:'object', properties:{a:{type:'number'}, b:{type:'number'}}, additionalProperties: false}}
);
console.log(JSON.stringify(json, null, 4) + JSON.stringify(schema, null, 4));
if (report.errors.length === 0) {
console.log("success");
} else {
console.log("failure");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment