Skip to content

Instantly share code, notes, and snippets.

@ksafranski
Created February 11, 2014 02:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksafranski/8928093 to your computer and use it in GitHub Desktop.
Save ksafranski/8928093 to your computer and use it in GitHub Desktop.
var validate = function (data, model, cb) {
var failures = [];
var regEx;
var validJSON;
var processNode;
var result;
var traverseNodes;
// Define common regular expressions
regEx = {
alphanum: /^[a-zA-Z0-9]+$/,
email: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
url: /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/,
ipv4: /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
phone: /^(([0-9]{1})*[- .(]*([0-9]{3})[- .)]*[0-9]{3}[- .]*[0-9]{4})+$/,
ssn: /^([0-9]{3}[-]*[0-9]{2}[-]*[0-9]{4})+$/
};
// Tests for valid JSON
validJSON = function (value) {
try {
JSON.parse(value);
} catch (e) {
return false;
}
return true;
};
// Tests, or calls test, on node
processNode = function (value, valid) {
switch (valid) {
case 'string':
result = (typeof value === "string") ? true : false;
break;
case 'number':
result = (typeof value === "number") ? true : false;
break;
case 'boolean':
result = (typeof value === "boolean" || value === "true" || value === "false") ? true : false;
break;
case 'array':
result = (Object.prototype.toString.call( value ) === '[object Array]') ? true : false;
break;
case 'json':
result = validJSON(value);
break;
default:
result = (regEx.hasOwnProperty(valid)) ? regEx[valid].test(value) : false;
}
return result;
};
// Recursively traverses nodes in the data and model objects
traverseNodes = function (obj, model, fn) {
for (var i in obj) {
if (model.hasOwnProperty(i)) {
// If node is an object resulre
if (obj[i] !== null && typeof(obj[i])==='object' && Object.prototype.toString.call( obj[i] ) !== '[object Array]') {
traverseNodes(obj[i],model[i],fn);
} else {
// Process node
if (!fn(obj[i],model[i])) {
// On failure, result gets added to array of failures
failures.push(i);
}
}
} else {
// Node does not exist in model
failures.push(i);
}
}
};
// Call function to travers
traverseNodes(data, model, processNode);
// Check for failures, fire callback
if (failures.length) {
cb(true, failures);
} else {
cb(null);
}
};
// ##################################################
// Define data to be tested
// ##################################################
var data = {
"name": "test",
"active": true,
"email": "test@email.com",
"url": "https://www.google.com",
"count": 4,
"arr": [
"a", "b", "c", "d"
],
"testobj": {
"one": "a",
"two": "b",
"three": {
"three1": "a",
"three2": "b"
}
},
"test1": "some_string"
};
// ##################################################
// Define model to test against
// ##################################################
var model = {
"name": "string",
"active": "boolean",
"email": "email",
"url": "url",
"count": "number",
"arr": "array",
"testobj": {
"one": "string",
"two": "string",
"three": {
"three1": "string",
"three2": "string"
}
},
"test1": "string"
};
// ##################################################
// Run it...
// ##################################################
validate(data, model, function (err, failures) {
if (err) {
console.log(failures);
} else {
console.log("PASSED!");
}
});
@ksafranski
Copy link
Author

Have it in a Fiddle: http://jsfiddle.net/xbX3c/14/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment