Skip to content

Instantly share code, notes, and snippets.

@BanksySan
Last active August 29, 2015 14:09
Show Gist options
  • Save BanksySan/5fb39b00d9de22c02d8e to your computer and use it in GitHub Desktop.
Save BanksySan/5fb39b00d9de22c02d8e to your computer and use it in GitHub Desktop.
How to validate against a simple JSON schema.
var x = {
a: String,
b: [String],
c: Number,
d: Boolean,
e: Object
};
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
var log = console.log;
var subValidationSelector = function(item, callback) {
if (Array.isArray(item)) {
log('item ' + item + ' is an array.');
} else {
switch (item) {
case String:
log('item is string');
break;
case Number:
log('item is number');
break;
case Boolean:
log('item is boolean');
break;
case Object:
log('item is object');
break;
default:
log('DUNNO!');
break;
}
}
};
var props = Object.getOwnPropertyNames(x);
log('props: ' + JSON.stringify(props));
for (var p in x) {
subValidationSelector(x[p]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment