Created
August 6, 2016 11:58
-
-
Save sebilasse/6691e0ee170ac2c45b5e5319c645634d to your computer and use it in GitHub Desktop.
node.js test validation of schema.org JSON Schema, see https://github.com/geraintluff/schema-org-gen/pull/4
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 fs = require('fs'); | |
var path = require('path'); | |
var tv4 = require('tv4'); | |
var dir = '/ENTER_THE_PATH_TO__schema.org_DIR__HERE'; | |
var schema = path.join(dir, 'Event.json'); | |
var data = { | |
"@context": "http://schema.org", | |
"@type": "Event", | |
"name": "Example Band goes to San Francisco", | |
"startDate" : "2013-09-14T21:30", | |
"url" : "http://example.com/tourdates.html", | |
"location" : { | |
"@type" : "Place", | |
"sameAs" : "http://www.hi-dive.com", | |
"name" : "The Hi-Dive", | |
"address" : "7 S. Broadway, Denver, CO 80209" | |
} | |
} | |
function cleanStack(e) { | |
delete e.stack; | |
if (e.subErrors) { | |
e.subErrors = e.subErrors.map(cleanStack); | |
} | |
return e; | |
} | |
fs.readdir(dir, function(err, files){ | |
files.map(function(filename) { | |
if (filename.charAt(0) === '.') { return filename; } | |
console.log(filename); | |
var url = dir+filename; | |
var schema = JSON.parse(fs.readFileSync(url, 'utf8')); | |
tv4.addSchema(url, schema); | |
}); | |
console.log('added',files.length,'schemas'); | |
var valid = tv4.validateMultiple(data, schema); | |
if (valid.errors) { | |
valid.errors = valid.errors.map(cleanStack); | |
} | |
console.log(JSON.stringify(valid)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment