Created
August 15, 2012 10:25
-
-
Save d-adamkiewicz/3358513 to your computer and use it in GitHub Desktop.
JSV: checking JSON against array schema example
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 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