Last active
August 29, 2015 14:09
-
-
Save BanksySan/5fb39b00d9de22c02d8e to your computer and use it in GitHub Desktop.
How to validate against a simple JSON schema.
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 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