angular-ui-form-validation
Version:
A plugin for performing validation in angularjs without writing lots of boilerplate code or duplicating logic.
71 lines (57 loc) • 2.6 kB
JavaScript
//jshint ignore:start
(function(self) {
/**
JSOL stands for JavaScript Object Literal which is a string representing
an object in JavaScript syntax.
For example:
{foo:"bar"} is equivalent to {"foo":"bar"} in JavaScript. Both are valid JSOL.
Note that {"foo":"bar"} is proper JSON[1] therefore you can use one of the many
JSON parsers out there like json2.js[2] or even the native browser's JSON parser,
if available.
However, {foo:"bar"} is NOT proper JSON but valid Javascript syntax for
representing an object with one key, "foo" and its value, "bar".
Using a JSON parser is not an option since this is NOT proper JSON.
You can use JSOL.parse to safely parse any string that reprsents a JavaScript Object Literal.
JSOL.parse will throw an Invalid JSOL exception on function calls, function declarations and variable references.
Examples:
JSOL.parse('{foo:"bar"}'); // valid
JSOL.parse('{evil:(function(){alert("I\'m evil");})()}'); // invalid function calls
JSOL.parse('{fn:function() { }}'); // invalid function declarations
var bar = "bar";
JSOL.parse('{foo:bar}'); // invalid variable references
[1] http://www.json.org
[2] http://www.json.org/json2.js
*/
if (!self.JSOL) {
self.JSOL = {};
}
// Used for trimming whitespace
var trim = /^(\s|\u00A0)+|(\s|\u00A0)+$/g;
if (typeof self.JSOL.parse !== "function") {
self.JSOL.parse = function(inText) {
var text = inText.replace(/\n/, '');
// make sure text is a "string"
if (typeof text !== "string" || !text) {
return null;
}
// Make sure leading/trailing whitespace is removed
text = text.replace(trim, "");
//****Invalid JSOL modification to allow for single quoted strings: TODO rename JSOL variable and file to something else, no longer valid JSOL
text = text.replace(/'/g, '"');
// Make sure the incoming text is actual JSOL (or Javascript Object Literal)
// Logic borrowed from http://json.org/json2.js
if ( /^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
.replace(/(?:^|:|,)(?:\s*\[)+/g, ":")
/** everything up to this point is json2.js **/
/** this is the 5th stage where it accepts unquoted keys **/
.replace(/\w*\s*\:/g, ":")) ) {
return (new Function("return " + text))();
}
else {
throw("Invalid JSOL: " + text);
}
};
}
})(window);
//jshint ignore:end