Socket
Socket
Sign inDemoInstall

tv4

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tv4 - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3

.npmignore

2

package.json
{
"name": "tv4",
"version": "1.0.2",
"version": "1.0.3",
"author": "Geraint Luff",

@@ -5,0 +5,0 @@ "description": "A public domain JSON Schema validator for JavaScript",

@@ -14,3 +14,6 @@ ValidatorContext.prototype.validateArray = function validateArray(data, schema) {

if (data.length < schema.minItems) {
return (new ValidationError(ErrorCodes.ARRAY_LENGTH_SHORT, "Array is too short (" + data.length + "), minimum " + schema.minItems)).prefixWith(null, "minItems");
var error = (new ValidationError(ErrorCodes.ARRAY_LENGTH_SHORT, "Array is too short (" + data.length + "), minimum " + schema.minItems)).prefixWith(null, "minItems");
if (this.handleError(error)) {
return error;
}
}

@@ -20,3 +23,6 @@ }

if (data.length > schema.maxItems) {
return (new ValidationError(ErrorCodes.ARRAY_LENGTH_LONG, "Array is too long (" + data.length + " chars), maximum " + schema.maxItems)).prefixWith(null, "maxItems");
var error = (new ValidationError(ErrorCodes.ARRAY_LENGTH_LONG, "Array is too long (" + data.length + " chars), maximum " + schema.maxItems)).prefixWith(null, "maxItems");
if (this.handleError(error)) {
return error;
}
}

@@ -32,3 +38,6 @@ }

if (recursiveCompare(data[i], data[j])) {
return (new ValidationError(ErrorCodes.ARRAY_UNIQUE, "Array items are not unique (indices " + i + " and " + j + ")")).prefixWith(null, "uniqueItems");
var error = (new ValidationError(ErrorCodes.ARRAY_UNIQUE, "Array items are not unique (indices " + i + " and " + j + ")")).prefixWith(null, "uniqueItems");
if (this.handleError(error)) {
return error;
}
}

@@ -55,3 +64,6 @@ }

if (!schema.additionalItems) {
return (new ValidationError(ErrorCodes.ARRAY_ADDITIONAL_ITEMS, "Additional items not allowed")).prefixWith("" + i, "additionalItems");
error = (new ValidationError(ErrorCodes.ARRAY_ADDITIONAL_ITEMS, "Additional items not allowed")).prefixWith("" + i, "additionalItems");
if (this.handleError(error)) {
return error;
}
}

@@ -58,0 +70,0 @@ } else if (error = this.validateAll(data[i], schema.additionalItems, [i], ["additionalItems"])) {

@@ -16,3 +16,6 @@ ValidatorContext.prototype.validateObject = function validateObject(data, schema) {

if (keys.length < schema.minProperties) {
return new ValidationError(ErrorCodes.OBJECT_PROPERTIES_MINIMUM, "Too few properties defined (" + keys.length + "), minimum " + schema.minProperties).prefixWith(null, "minProperties");
var error = new ValidationError(ErrorCodes.OBJECT_PROPERTIES_MINIMUM, "Too few properties defined (" + keys.length + "), minimum " + schema.minProperties).prefixWith(null, "minProperties");
if (this.handleError(error)) {
return error;
}
}

@@ -22,3 +25,6 @@ }

if (keys.length > schema.maxProperties) {
return new ValidationError(ErrorCodes.OBJECT_PROPERTIES_MAXIMUM, "Too many properties defined (" + keys.length + "), maximum " + schema.maxProperties).prefixWith(null, "maxProperties");
var error = new ValidationError(ErrorCodes.OBJECT_PROPERTIES_MAXIMUM, "Too many properties defined (" + keys.length + "), maximum " + schema.maxProperties).prefixWith(null, "maxProperties");
if (this.handleError(error)) {
return error;
}
}

@@ -34,3 +40,6 @@ }

if (data[key] === undefined) {
return new ValidationError(ErrorCodes.OBJECT_REQUIRED, "Missing required property: " + key).prefixWith(null, "" + i).prefixWith(null, "required")
var error = new ValidationError(ErrorCodes.OBJECT_REQUIRED, "Missing required property: " + key).prefixWith(null, "" + i).prefixWith(null, "required");
if (this.handleError(error)) {
return error;
}
}

@@ -66,3 +75,6 @@ }

if (!schema.additionalProperties) {
return new ValidationError(ErrorCodes.OBJECT_ADDITIONAL_PROPERTIES, "Additional properties not allowed").prefixWith(key, "additionalProperties");
error = new ValidationError(ErrorCodes.OBJECT_ADDITIONAL_PROPERTIES, "Additional properties not allowed").prefixWith(key, "additionalProperties");
if (this.handleError(error)) {
return error;
}
}

@@ -87,3 +99,6 @@ } else {

if (data[dep] === undefined) {
return new ValidationError(ErrorCodes.OBJECT_DEPENDENCY_KEY, "Dependency failed - key must exist: " + dep).prefixWith(null, depKey).prefixWith(null, "dependencies");
error = new ValidationError(ErrorCodes.OBJECT_DEPENDENCY_KEY, "Dependency failed - key must exist: " + dep).prefixWith(null, depKey).prefixWith(null, "dependencies");
if (this.handleError(error)) {
return error;
}
}

@@ -94,3 +109,6 @@ } else if (Array.isArray(dep)) {

if (data[requiredKey] === undefined) {
return new ValidationError(ErrorCodes.OBJECT_DEPENDENCY_KEY, "Dependency failed - key must exist: " + requiredKey).prefixWith(null, "" + i).prefixWith(null, depKey).prefixWith(null, "dependencies");
error = new ValidationError(ErrorCodes.OBJECT_DEPENDENCY_KEY, "Dependency failed - key must exist: " + requiredKey).prefixWith(null, "" + i).prefixWith(null, depKey).prefixWith(null, "dependencies");
if (this.handleError(error)) {
return error;
}
}

@@ -97,0 +115,0 @@ }

@@ -13,3 +13,3 @@ ValidatorContext.prototype.validateString = function validateString(data, schema) {

if (data.length < schema.minLength) {
return (ErrorCodes.STRING_LENGTH_SHORT, new ValidationError("String is too short (" + data.length + " chars), minimum " + schema.minLength)).prefixWith(null, "minLength");
return new ValidationError(ErrorCodes.STRING_LENGTH_SHORT, "String is too short (" + data.length + " chars), minimum " + schema.minLength).prefixWith(null, "minLength");
}

@@ -19,3 +19,3 @@ }

if (data.length > schema.maxLength) {
return (ErrorCodes.STRING_LENGTH_LONG, new ValidationError("String is too long (" + data.length + " chars), maximum " + schema.maxLength)).prefixWith(null, "maxLength");
return new ValidationError(ErrorCodes.STRING_LENGTH_LONG, "String is too long (" + data.length + " chars), maximum " + schema.maxLength).prefixWith(null, "maxLength");
}

@@ -22,0 +22,0 @@ }

@@ -35,1 +35,8 @@ tests.add("no length constraints", function () {

});
tests.add("check error message", function () {
var data = "test1234";
var schema = {maxLength: 5};
var valid = tv4.validate(data, schema);
return typeof tv4.error.message !== "undefined";
});

@@ -72,2 +72,14 @@ tests.add("validateMultiple returns array of errors", function () {

return true;
});
tests.add("validateMultiple handles multiple missing properties", function () {
var data = {};
var schema = {
required: ["one", "two"]
};
var result = tv4.validateMultiple(data, schema);
this.assert(result.valid == false, "should not validate");
this.assert(result.errors.length == 2, "exactly two errors, not " + result.errors.length);
return true;
});

@@ -250,3 +250,3 @@ /**

if (data.length < schema.minLength) {
return (ErrorCodes.STRING_LENGTH_SHORT, new ValidationError("String is too short (" + data.length + " chars), minimum " + schema.minLength)).prefixWith(null, "minLength");
return new ValidationError(ErrorCodes.STRING_LENGTH_SHORT, "String is too short (" + data.length + " chars), minimum " + schema.minLength).prefixWith(null, "minLength");
}

@@ -256,3 +256,3 @@ }

if (data.length > schema.maxLength) {
return (ErrorCodes.STRING_LENGTH_LONG, new ValidationError("String is too long (" + data.length + " chars), maximum " + schema.maxLength)).prefixWith(null, "maxLength");
return new ValidationError(ErrorCodes.STRING_LENGTH_LONG, "String is too long (" + data.length + " chars), maximum " + schema.maxLength).prefixWith(null, "maxLength");
}

@@ -286,3 +286,6 @@ }

if (data.length < schema.minItems) {
return (new ValidationError(ErrorCodes.ARRAY_LENGTH_SHORT, "Array is too short (" + data.length + "), minimum " + schema.minItems)).prefixWith(null, "minItems");
var error = (new ValidationError(ErrorCodes.ARRAY_LENGTH_SHORT, "Array is too short (" + data.length + "), minimum " + schema.minItems)).prefixWith(null, "minItems");
if (this.handleError(error)) {
return error;
}
}

@@ -292,3 +295,6 @@ }

if (data.length > schema.maxItems) {
return (new ValidationError(ErrorCodes.ARRAY_LENGTH_LONG, "Array is too long (" + data.length + " chars), maximum " + schema.maxItems)).prefixWith(null, "maxItems");
var error = (new ValidationError(ErrorCodes.ARRAY_LENGTH_LONG, "Array is too long (" + data.length + " chars), maximum " + schema.maxItems)).prefixWith(null, "maxItems");
if (this.handleError(error)) {
return error;
}
}

@@ -304,3 +310,6 @@ }

if (recursiveCompare(data[i], data[j])) {
return (new ValidationError(ErrorCodes.ARRAY_UNIQUE, "Array items are not unique (indices " + i + " and " + j + ")")).prefixWith(null, "uniqueItems");
var error = (new ValidationError(ErrorCodes.ARRAY_UNIQUE, "Array items are not unique (indices " + i + " and " + j + ")")).prefixWith(null, "uniqueItems");
if (this.handleError(error)) {
return error;
}
}

@@ -327,3 +336,6 @@ }

if (!schema.additionalItems) {
return (new ValidationError(ErrorCodes.ARRAY_ADDITIONAL_ITEMS, "Additional items not allowed")).prefixWith("" + i, "additionalItems");
error = (new ValidationError(ErrorCodes.ARRAY_ADDITIONAL_ITEMS, "Additional items not allowed")).prefixWith("" + i, "additionalItems");
if (this.handleError(error)) {
return error;
}
}

@@ -359,3 +371,6 @@ } else if (error = this.validateAll(data[i], schema.additionalItems, [i], ["additionalItems"])) {

if (keys.length < schema.minProperties) {
return new ValidationError(ErrorCodes.OBJECT_PROPERTIES_MINIMUM, "Too few properties defined (" + keys.length + "), minimum " + schema.minProperties).prefixWith(null, "minProperties");
var error = new ValidationError(ErrorCodes.OBJECT_PROPERTIES_MINIMUM, "Too few properties defined (" + keys.length + "), minimum " + schema.minProperties).prefixWith(null, "minProperties");
if (this.handleError(error)) {
return error;
}
}

@@ -365,3 +380,6 @@ }

if (keys.length > schema.maxProperties) {
return new ValidationError(ErrorCodes.OBJECT_PROPERTIES_MAXIMUM, "Too many properties defined (" + keys.length + "), maximum " + schema.maxProperties).prefixWith(null, "maxProperties");
var error = new ValidationError(ErrorCodes.OBJECT_PROPERTIES_MAXIMUM, "Too many properties defined (" + keys.length + "), maximum " + schema.maxProperties).prefixWith(null, "maxProperties");
if (this.handleError(error)) {
return error;
}
}

@@ -377,3 +395,6 @@ }

if (data[key] === undefined) {
return new ValidationError(ErrorCodes.OBJECT_REQUIRED, "Missing required property: " + key).prefixWith(null, "" + i).prefixWith(null, "required")
var error = new ValidationError(ErrorCodes.OBJECT_REQUIRED, "Missing required property: " + key).prefixWith(null, "" + i).prefixWith(null, "required");
if (this.handleError(error)) {
return error;
}
}

@@ -409,3 +430,6 @@ }

if (!schema.additionalProperties) {
return new ValidationError(ErrorCodes.OBJECT_ADDITIONAL_PROPERTIES, "Additional properties not allowed").prefixWith(key, "additionalProperties");
error = new ValidationError(ErrorCodes.OBJECT_ADDITIONAL_PROPERTIES, "Additional properties not allowed").prefixWith(key, "additionalProperties");
if (this.handleError(error)) {
return error;
}
}

@@ -430,3 +454,6 @@ } else {

if (data[dep] === undefined) {
return new ValidationError(ErrorCodes.OBJECT_DEPENDENCY_KEY, "Dependency failed - key must exist: " + dep).prefixWith(null, depKey).prefixWith(null, "dependencies");
error = new ValidationError(ErrorCodes.OBJECT_DEPENDENCY_KEY, "Dependency failed - key must exist: " + dep).prefixWith(null, depKey).prefixWith(null, "dependencies");
if (this.handleError(error)) {
return error;
}
}

@@ -437,3 +464,6 @@ } else if (Array.isArray(dep)) {

if (data[requiredKey] === undefined) {
return new ValidationError(ErrorCodes.OBJECT_DEPENDENCY_KEY, "Dependency failed - key must exist: " + requiredKey).prefixWith(null, "" + i).prefixWith(null, depKey).prefixWith(null, "dependencies");
error = new ValidationError(ErrorCodes.OBJECT_DEPENDENCY_KEY, "Dependency failed - key must exist: " + requiredKey).prefixWith(null, "" + i).prefixWith(null, depKey).prefixWith(null, "dependencies");
if (this.handleError(error)) {
return error;
}
}

@@ -440,0 +470,0 @@ }

(function(s){function k(b,a){if(b===a)return!0;if("object"==typeof b&&"object"==typeof a){if(Array.isArray(b)!=Array.isArray(a))return!1;if(Array.isArray(b)){if(b.length!=a.length)return!1;for(var c=0;c<b.length;c++)if(!k(b[c],a[c]))return!1}else{for(c in b)if(void 0===a[c]&&void 0!==b[c])return!1;for(c in a)if(void 0===b[c]&&void 0!==a[c])return!1;for(c in b)if(!k(b[c],a[c]))return!1}return!0}return!1}function q(b){return(b=String(b).replace(/^\s+|\s+$/g,"").match(/^([^:\/?#]+:)?(\/\/(?:[^:@]*(?::[^:@]*)?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/))?
{href:b[0]||"",protocol:b[1]||"",authority:b[2]||"",host:b[3]||"",hostname:b[4]||"",port:b[5]||"",pathname:b[6]||"",search:b[7]||"",hash:b[8]||""}:null}function p(b,a){a=q(a||"");b=q(b||"");var c;if(!a||!b)c=null;else{c=(a.protocol||b.protocol)+(a.protocol||a.authority?a.authority:b.authority);var d;d=a.protocol||a.authority||"/"===a.pathname.charAt(0)?a.pathname:a.pathname?(b.authority&&!b.pathname?"/":"")+b.pathname.slice(0,b.pathname.lastIndexOf("/")+1)+a.pathname:b.pathname;var h=[];d.replace(/^(\.\.?(\/|$))+/,
"").replace(/\/(\.(\/|$))+/g,"/").replace(/\/\.\.$/,"/../").replace(/\/?[^\/]*/g,function(a){"/.."===a?h.pop():h.push(a)});d=h.join("").replace(/^\//,"/"===d.charAt(0)?"/":"");c=c+d+(a.protocol||a.authority||a.pathname?a.search:a.search||b.search)+a.hash}return c}function m(b,a){void 0==a?a=b.id:"string"==typeof b.id&&(a=p(a,b.id),b.id=a);if("object"==typeof b)if(Array.isArray(b))for(var c=0;c<b.length;c++)m(b[c],a);else if("string"==typeof b.$ref)b.$ref=p(a,b.$ref);else for(c in b)"enum"!=c&&m(b[c],
a)}function f(b,a,c,d,h){if(void 0==b)throw Error("No code supplied for error: "+a);this.code=b;this.message=a;this.dataPath=c?c:"";this.schemaPath=d?d:"";this.subErrors=h?h:null}function r(b,a,c){if("string"==typeof a.id&&a.id.substring(0,c.length)==c){var d=a.id.substring(c.length);if(0<c.length&&"/"==c.charAt(c.length-1)||"#"==d.charAt(0)||"?"==d.charAt(0))void 0==b[a.id]&&(b[a.id]=a)}if("object"==typeof a)for(var h in a)"enum"!=h&&"object"==typeof a[h]&&r(b,a[h],c);return b}var e=function(b,a){this.missing=
{href:b[0]||"",protocol:b[1]||"",authority:b[2]||"",host:b[3]||"",hostname:b[4]||"",port:b[5]||"",pathname:b[6]||"",search:b[7]||"",hash:b[8]||""}:null}function p(b,a){a=q(a||"");b=q(b||"");var c;if(!a||!b)c=null;else{c=(a.protocol||b.protocol)+(a.protocol||a.authority?a.authority:b.authority);var d;d=a.protocol||a.authority||"/"===a.pathname.charAt(0)?a.pathname:a.pathname?(b.authority&&!b.pathname?"/":"")+b.pathname.slice(0,b.pathname.lastIndexOf("/")+1)+a.pathname:b.pathname;var f=[];d.replace(/^(\.\.?(\/|$))+/,
"").replace(/\/(\.(\/|$))+/g,"/").replace(/\/\.\.$/,"/../").replace(/\/?[^\/]*/g,function(a){"/.."===a?f.pop():f.push(a)});d=f.join("").replace(/^\//,"/"===d.charAt(0)?"/":"");c=c+d+(a.protocol||a.authority||a.pathname?a.search:a.search||b.search)+a.hash}return c}function m(b,a){void 0==a?a=b.id:"string"==typeof b.id&&(a=p(a,b.id),b.id=a);if("object"==typeof b)if(Array.isArray(b))for(var c=0;c<b.length;c++)m(b[c],a);else if("string"==typeof b.$ref)b.$ref=p(a,b.$ref);else for(c in b)"enum"!=c&&m(b[c],
a)}function g(b,a,c,d,f){if(void 0==b)throw Error("No code supplied for error: "+a);this.code=b;this.message=a;this.dataPath=c?c:"";this.schemaPath=d?d:"";this.subErrors=f?f:null}function r(b,a,c){if("string"==typeof a.id&&a.id.substring(0,c.length)==c){var d=a.id.substring(c.length);if(0<c.length&&"/"==c.charAt(c.length-1)||"#"==d.charAt(0)||"?"==d.charAt(0))void 0==b[a.id]&&(b[a.id]=a)}if("object"==typeof a)for(var f in a)"enum"!=f&&"object"==typeof a[f]&&r(b,a[f],c);return b}var e=function(b,a){this.missing=
[];this.schemas=b?Object.create(b.schemas):{};this.collectMultiple=a;this.errors=[];this.handleError=a?this.collectError:this.returnError};e.prototype.returnError=function(b){return b};e.prototype.collectError=function(b){b&&this.errors.push(b);return null};e.prototype.prefixErrors=function(b,a,c){for(;b<this.errors.length;b++)this.errors[b]=this.errors[b].prefixWith(a,c);return this};e.prototype.getSchema=function(b){if(void 0!=this.schemas[b])return b=this.schemas[b];var a=b,c="";-1!=b.indexOf("#")&&
(c=b.substring(b.indexOf("#")+1),a=b.substring(0,b.indexOf("#")));if(void 0!=this.schemas[a]){b=this.schemas[a];c=decodeURIComponent(c);if(""==c)return b;if("/"!=c.charAt(0))return;for(var c=c.split("/").slice(1),d=0;d<c.length;d++){var h=c[d].replace("~1","/").replace("~0","~");if(void 0==b[h]){b=void 0;break}b=b[h]}if(void 0!=b)return b}void 0==this.missing[a]&&(this.missing.push(a),this.missing[a]=a)};e.prototype.addSchema=function(b,a){var c={};c[b]=a;m(a,b);r(c,a,b);for(var d in c)this.schemas[d]=
c[d];return c};e.prototype.validateAll=function(b,a,c,d){if(void 0!=a.$ref&&(a=this.getSchema(a.$ref),!a))return null;var h=this.errors.length;if((b=this.validateBasic(b,a)||this.validateNumeric(b,a)||this.validateString(b,a)||this.validateArray(b,a)||this.validateObject(b,a)||this.validateCombinations(b,a)||null)||h!=this.errors.length)for(;c&&c.length||d&&d.length;){a=c&&c.length?""+c.pop():null;var e=d&&d.length?""+d.pop():null;b&&(b=b.prefixWith(a,e));this.prefixErrors(h,a,e)}return this.handleError(b)};
e.prototype.validateBasic=function(b,a){var c;return(c=this.validateType(b,a))||(c=this.validateEnum(b,a))?c.prefixWith(null,"type"):null};e.prototype.validateType=function(b,a){if(void 0==a.type)return null;var c=typeof b;null==b?c="null":Array.isArray(b)&&(c="array");var d=a.type;"object"!=typeof d&&(d=[d]);for(var h=0;h<d.length;h++){var e=d[h];if(e==c||"integer"==e&&"number"==c&&0==b%1)return null}return new f(g.INVALID_TYPE,"invalid data type: "+c)};e.prototype.validateEnum=function(b,a){if(void 0==
a["enum"])return null;for(var c=0;c<a["enum"].length;c++)if(k(b,a["enum"][c]))return null;return new f(g.ENUM_MISMATCH,"No enum match for: "+JSON.stringify(b))};e.prototype.validateNumeric=function(b,a){return this.validateMultipleOf(b,a)||this.validateMinMax(b,a)||null};e.prototype.validateMultipleOf=function(b,a){var c=a.multipleOf||a.divisibleBy;return void 0==c?null:"number"==typeof b&&0!=b%c?new f(g.NUMBER_MULTIPLE_OF,"Value "+b+" is not a multiple of "+c):null};e.prototype.validateMinMax=function(b,
a){if("number"!=typeof b)return null;if(void 0!=a.minimum){if(b<a.minimum)return(new f(g.NUMBER_MINIMUM,"Value "+b+" is less than minimum "+a.minimum)).prefixWith(null,"minimum");if(a.exclusiveMinimum&&b==a.minimum)return(new f(g.NUMBER_MINIMUM_EXCLUSIVE,"Value "+b+" is equal to exclusive minimum "+a.minimum)).prefixWith(null,"exclusiveMinimum")}if(void 0!=a.maximum){if(b>a.maximum)return(new f(g.NUMBER_MAXIMUM,"Value "+b+" is greater than maximum "+a.maximum)).prefixWith(null,"maximum");if(a.exclusiveMaximum&&
b==a.maximum)return(new f(g.NUMBER_MAXIMUM_EXCLUSIVE,"Value "+b+" is equal to exclusive maximum "+a.maximum)).prefixWith(null,"exclusiveMaximum")}return null};e.prototype.validateString=function(b,a){return this.validateStringLength(b,a)||this.validateStringPattern(b,a)||null};e.prototype.validateStringLength=function(b,a){return"string"!=typeof b?null:void 0!=a.minLength&&b.length<a.minLength?(g.STRING_LENGTH_SHORT,new f("String is too short ("+b.length+" chars), minimum "+a.minLength)).prefixWith(null,
"minLength"):void 0!=a.maxLength&&b.length>a.maxLength?(g.STRING_LENGTH_LONG,new f("String is too long ("+b.length+" chars), maximum "+a.maxLength)).prefixWith(null,"maxLength"):null};e.prototype.validateStringPattern=function(b,a){return"string"!=typeof b||void 0==a.pattern?null:!RegExp(a.pattern).test(b)?(new f(g.STRING_PATTERN,"String does not match pattern")).prefixWith(null,"pattern"):null};e.prototype.validateArray=function(b,a){return!Array.isArray(b)?null:this.validateArrayLength(b,a)||this.validateArrayUniqueItems(b,
a)||this.validateArrayItems(b,a)||null};e.prototype.validateArrayLength=function(b,a){return void 0!=a.minItems&&b.length<a.minItems?(new f(g.ARRAY_LENGTH_SHORT,"Array is too short ("+b.length+"), minimum "+a.minItems)).prefixWith(null,"minItems"):void 0!=a.maxItems&&b.length>a.maxItems?(new f(g.ARRAY_LENGTH_LONG,"Array is too long ("+b.length+" chars), maximum "+a.maxItems)).prefixWith(null,"maxItems"):null};e.prototype.validateArrayUniqueItems=function(b,a){if(a.uniqueItems)for(var c=0;c<b.length;c++)for(var d=
c+1;d<b.length;d++)if(k(b[c],b[d]))return(new f(g.ARRAY_UNIQUE,"Array items are not unique (indices "+c+" and "+d+")")).prefixWith(null,"uniqueItems");return null};e.prototype.validateArrayItems=function(b,a){if(void 0==a.items)return null;var c;if(Array.isArray(a.items))for(var d=0;d<b.length;d++)if(d<a.items.length){if(c=this.validateAll(b[d],a.items[d],[d],["items",d]))return c}else{if(void 0!=a.additionalItems)if("boolean"==typeof a.additionalItems){if(!a.additionalItems)return(new f(g.ARRAY_ADDITIONAL_ITEMS,
"Additional items not allowed")).prefixWith(""+d,"additionalItems")}else if(c=this.validateAll(b[d],a.additionalItems,[d],["additionalItems"]))return c}else for(d=0;d<b.length;d++)if(c=this.validateAll(b[d],a.items,[d],["items"]))return c;return null};e.prototype.validateObject=function(b,a){return"object"!=typeof b||null==b||Array.isArray(b)?null:this.validateObjectMinMaxProperties(b,a)||this.validateObjectRequiredProperties(b,a)||this.validateObjectProperties(b,a)||this.validateObjectDependencies(b,
a)||null};e.prototype.validateObjectMinMaxProperties=function(b,a){var c=Object.keys(b);return void 0!=a.minProperties&&c.length<a.minProperties?(new f(g.OBJECT_PROPERTIES_MINIMUM,"Too few properties defined ("+c.length+"), minimum "+a.minProperties)).prefixWith(null,"minProperties"):void 0!=a.maxProperties&&c.length>a.maxProperties?(new f(g.OBJECT_PROPERTIES_MAXIMUM,"Too many properties defined ("+c.length+"), maximum "+a.maxProperties)).prefixWith(null,"maxProperties"):null};e.prototype.validateObjectRequiredProperties=
function(b,a){if(void 0!=a.required)for(var c=0;c<a.required.length;c++){var d=a.required[c];if(void 0===b[d])return(new f(g.OBJECT_REQUIRED,"Missing required property: "+d)).prefixWith(null,""+c).prefixWith(null,"required")}return null};e.prototype.validateObjectProperties=function(b,a){var c,d;for(d in b){var h=!1;if(void 0!=a.properties&&void 0!=a.properties[d]&&(h=!0,c=this.validateAll(b[d],a.properties[d],[d],["properties",d])))return c;if(void 0!=a.patternProperties)for(var e in a.patternProperties)if(RegExp(e).test(d)&&
(h=!0,c=this.validateAll(b[d],a.patternProperties[e],[d],["patternProperties",e])))return c;if(!h&&void 0!=a.additionalProperties)if("boolean"==typeof a.additionalProperties){if(!a.additionalProperties)return(new f(g.OBJECT_ADDITIONAL_PROPERTIES,"Additional properties not allowed")).prefixWith(d,"additionalProperties")}else if(c=this.validateAll(b[d],a.additionalProperties,[d],["additionalProperties"]))return c}return null};e.prototype.validateObjectDependencies=function(b,a){var c;if(void 0!=a.dependencies)for(var d in a.dependencies)if(void 0!==
b[d])if(c=a.dependencies[d],"string"==typeof c){if(void 0===b[c])return(new f(g.OBJECT_DEPENDENCY_KEY,"Dependency failed - key must exist: "+c)).prefixWith(null,d).prefixWith(null,"dependencies")}else if(Array.isArray(c))for(var e=0;e<c.length;e++){var j=c[e];if(void 0===b[j])return(new f(g.OBJECT_DEPENDENCY_KEY,"Dependency failed - key must exist: "+j)).prefixWith(null,""+e).prefixWith(null,d).prefixWith(null,"dependencies")}else if(c=this.validateAll(b,c,[],["dependencies",d]))return c;return null};
e.prototype.validateCombinations=function(b,a){return this.validateAllOf(b,a)||this.validateAnyOf(b,a)||this.validateOneOf(b,a)||this.validateNot(b,a)||null};e.prototype.validateAllOf=function(b,a){if(void 0==a.allOf)return null;for(var c,d=0;d<a.allOf.length;d++)if(c=this.validateAll(b,a.allOf[d],[],["allOf",d]))return c;return null};e.prototype.validateAnyOf=function(b,a){if(void 0==a.anyOf)return null;for(var c=[],d=this.errors.length,e=0;e<a.anyOf.length;e++){var j=this.errors.length,l=this.validateAll(b,
a.anyOf[e],[],["anyOf",e]);if(null==l&&j==this.errors.length)return this.errors=this.errors.slice(0,d),null;l&&c.push(l.prefixWith(null,""+e).prefixWith(null,"anyOf"))}c=c.concat(this.errors.slice(d));this.errors=this.errors.slice(0,d);return new f(g.ANY_OF_MISSING,'Data does not match any schemas from "anyOf"',"","/anyOf",c)};e.prototype.validateOneOf=function(b,a){if(void 0==a.oneOf)return null;for(var c=null,d=[],e=this.errors.length,j=0;j<a.oneOf.length;j++){var l=this.errors.length,k=this.validateAll(b,
a.oneOf[j],[],["oneOf",j]);if(null==k&&l==this.errors.length)if(null==c)c=j;else return this.errors=this.errors.slice(0,e),new f(g.ONE_OF_MULTIPLE,'Data is valid against more than one schema from "oneOf": indices '+c+" and "+j,"","/oneOf");else k&&d.push(k.prefixWith(null,""+j).prefixWith(null,"oneOf"))}return null==c?(d=d.concat(this.errors.slice(e)),this.errors=this.errors.slice(0,e),new f(g.ONE_OF_MISSING,'Data does not match any schemas from "oneOf"',"","/oneOf",d)):null};e.prototype.validateNot=
function(b,a){if(void 0==a.not)return null;var c=this.errors.length,d=this.validateAll(b,a.not),e=this.errors.slice(c);this.errors=this.errors.slice(0,c);return null==d&&0==e.length?new f(g.NOT_PASSED,'Data matches schema from "not"',"","/not"):null};var g={INVALID_TYPE:0,ENUM_MISMATCH:1,ANY_OF_MISSING:10,ONE_OF_MISSING:11,ONE_OF_MULTIPLE:12,NOT_PASSED:13,NUMBER_MULTIPLE_OF:100,NUMBER_MINIMUM:101,NUMBER_MINIMUM_EXCLUSIVE:102,NUMBER_MAXIMUM:103,NUMBER_MAXIMUM_EXCLUSIVE:104,STRING_LENGTH_SHORT:200,
STRING_LENGTH_LONG:201,STRING_PATTERN:202,OBJECT_PROPERTIES_MINIMUM:300,OBJECT_PROPERTIES_MAXIMUM:301,OBJECT_REQUIRED:302,OBJECT_ADDITIONAL_PROPERTIES:303,OBJECT_DEPENDENCY_KEY:304,ARRAY_LENGTH_SHORT:400,ARRAY_LENGTH_LONG:401,ARRAY_UNIQUE:402,ARRAY_ADDITIONAL_ITEMS:403};f.prototype={prefixWith:function(b,a){null!=b&&(b=b.replace("~","~0").replace("/","~1"),this.dataPath="/"+b+this.dataPath);null!=a&&(a=a.replace("~","~0").replace("/","~1"),this.schemaPath="/"+a+this.schemaPath);if(null!=this.subErrors)for(var c=
0;c<this.subErrors.length;c++)this.subErrors[c].prefixWith(b,a);return this}};var n=new e;s.tv4={validate:function(b,a){var c=new e(n);"string"==typeof a&&(a={$ref:a});c.addSchema("",a);var d=c.validateAll(b,a);this.error=d;this.missing=c.missing;return this.valid=null==d},validateResult:function(){var b={};this.validate.apply(b,arguments);return b},validateMultiple:function(b,a){var c=new e(n,!0);"string"==typeof a&&(a={$ref:a});c.addSchema("",a);c.validateAll(b,a);var d={};d.errors=c.errors;d.missing=
c.missing;d.valid=0==d.errors.length;return d},addSchema:function(b,a){return n.addSchema(b,a)},getSchema:function(b){return n.getSchema(b)},missing:[],error:null,normSchema:m,resolveUrl:p,errorCodes:g}})("undefined"!==typeof module&&module.exports?exports:this);
(c=b.substring(b.indexOf("#")+1),a=b.substring(0,b.indexOf("#")));if(void 0!=this.schemas[a]){b=this.schemas[a];c=decodeURIComponent(c);if(""==c)return b;if("/"!=c.charAt(0))return;for(var c=c.split("/").slice(1),d=0;d<c.length;d++){var f=c[d].replace("~1","/").replace("~0","~");if(void 0==b[f]){b=void 0;break}b=b[f]}if(void 0!=b)return b}void 0==this.missing[a]&&(this.missing.push(a),this.missing[a]=a)};e.prototype.addSchema=function(b,a){var c={};c[b]=a;m(a,b);r(c,a,b);for(var d in c)this.schemas[d]=
c[d];return c};e.prototype.validateAll=function(b,a,c,d){if(void 0!=a.$ref&&(a=this.getSchema(a.$ref),!a))return null;var f=this.errors.length;if((b=this.validateBasic(b,a)||this.validateNumeric(b,a)||this.validateString(b,a)||this.validateArray(b,a)||this.validateObject(b,a)||this.validateCombinations(b,a)||null)||f!=this.errors.length)for(;c&&c.length||d&&d.length;){a=c&&c.length?""+c.pop():null;var e=d&&d.length?""+d.pop():null;b&&(b=b.prefixWith(a,e));this.prefixErrors(f,a,e)}return this.handleError(b)};
e.prototype.validateBasic=function(b,a){var c;return(c=this.validateType(b,a))||(c=this.validateEnum(b,a))?c.prefixWith(null,"type"):null};e.prototype.validateType=function(b,a){if(void 0==a.type)return null;var c=typeof b;null==b?c="null":Array.isArray(b)&&(c="array");var d=a.type;"object"!=typeof d&&(d=[d]);for(var f=0;f<d.length;f++){var e=d[f];if(e==c||"integer"==e&&"number"==c&&0==b%1)return null}return new g(h.INVALID_TYPE,"invalid data type: "+c)};e.prototype.validateEnum=function(b,a){if(void 0==
a["enum"])return null;for(var c=0;c<a["enum"].length;c++)if(k(b,a["enum"][c]))return null;return new g(h.ENUM_MISMATCH,"No enum match for: "+JSON.stringify(b))};e.prototype.validateNumeric=function(b,a){return this.validateMultipleOf(b,a)||this.validateMinMax(b,a)||null};e.prototype.validateMultipleOf=function(b,a){var c=a.multipleOf||a.divisibleBy;return void 0==c?null:"number"==typeof b&&0!=b%c?new g(h.NUMBER_MULTIPLE_OF,"Value "+b+" is not a multiple of "+c):null};e.prototype.validateMinMax=function(b,
a){if("number"!=typeof b)return null;if(void 0!=a.minimum){if(b<a.minimum)return(new g(h.NUMBER_MINIMUM,"Value "+b+" is less than minimum "+a.minimum)).prefixWith(null,"minimum");if(a.exclusiveMinimum&&b==a.minimum)return(new g(h.NUMBER_MINIMUM_EXCLUSIVE,"Value "+b+" is equal to exclusive minimum "+a.minimum)).prefixWith(null,"exclusiveMinimum")}if(void 0!=a.maximum){if(b>a.maximum)return(new g(h.NUMBER_MAXIMUM,"Value "+b+" is greater than maximum "+a.maximum)).prefixWith(null,"maximum");if(a.exclusiveMaximum&&
b==a.maximum)return(new g(h.NUMBER_MAXIMUM_EXCLUSIVE,"Value "+b+" is equal to exclusive maximum "+a.maximum)).prefixWith(null,"exclusiveMaximum")}return null};e.prototype.validateString=function(b,a){return this.validateStringLength(b,a)||this.validateStringPattern(b,a)||null};e.prototype.validateStringLength=function(b,a){return"string"!=typeof b?null:void 0!=a.minLength&&b.length<a.minLength?(new g(h.STRING_LENGTH_SHORT,"String is too short ("+b.length+" chars), minimum "+a.minLength)).prefixWith(null,
"minLength"):void 0!=a.maxLength&&b.length>a.maxLength?(new g(h.STRING_LENGTH_LONG,"String is too long ("+b.length+" chars), maximum "+a.maxLength)).prefixWith(null,"maxLength"):null};e.prototype.validateStringPattern=function(b,a){return"string"!=typeof b||void 0==a.pattern?null:!RegExp(a.pattern).test(b)?(new g(h.STRING_PATTERN,"String does not match pattern")).prefixWith(null,"pattern"):null};e.prototype.validateArray=function(b,a){return!Array.isArray(b)?null:this.validateArrayLength(b,a)||this.validateArrayUniqueItems(b,
a)||this.validateArrayItems(b,a)||null};e.prototype.validateArrayLength=function(b,a){if(void 0!=a.minItems&&b.length<a.minItems){var c=(new g(h.ARRAY_LENGTH_SHORT,"Array is too short ("+b.length+"), minimum "+a.minItems)).prefixWith(null,"minItems");if(this.handleError(c))return c}return void 0!=a.maxItems&&b.length>a.maxItems&&(c=(new g(h.ARRAY_LENGTH_LONG,"Array is too long ("+b.length+" chars), maximum "+a.maxItems)).prefixWith(null,"maxItems"),this.handleError(c))?c:null};e.prototype.validateArrayUniqueItems=
function(b,a){if(a.uniqueItems)for(var c=0;c<b.length;c++)for(var d=c+1;d<b.length;d++)if(k(b[c],b[d])){var f=(new g(h.ARRAY_UNIQUE,"Array items are not unique (indices "+c+" and "+d+")")).prefixWith(null,"uniqueItems");if(this.handleError(f))return f}return null};e.prototype.validateArrayItems=function(b,a){if(void 0==a.items)return null;var c;if(Array.isArray(a.items))for(var d=0;d<b.length;d++)if(d<a.items.length){if(c=this.validateAll(b[d],a.items[d],[d],["items",d]))return c}else{if(void 0!=
a.additionalItems)if("boolean"==typeof a.additionalItems){if(!a.additionalItems&&(c=(new g(h.ARRAY_ADDITIONAL_ITEMS,"Additional items not allowed")).prefixWith(""+d,"additionalItems"),this.handleError(c)))return c}else if(c=this.validateAll(b[d],a.additionalItems,[d],["additionalItems"]))return c}else for(d=0;d<b.length;d++)if(c=this.validateAll(b[d],a.items,[d],["items"]))return c;return null};e.prototype.validateObject=function(b,a){return"object"!=typeof b||null==b||Array.isArray(b)?null:this.validateObjectMinMaxProperties(b,
a)||this.validateObjectRequiredProperties(b,a)||this.validateObjectProperties(b,a)||this.validateObjectDependencies(b,a)||null};e.prototype.validateObjectMinMaxProperties=function(b,a){var c=Object.keys(b);if(void 0!=a.minProperties&&c.length<a.minProperties){var d=(new g(h.OBJECT_PROPERTIES_MINIMUM,"Too few properties defined ("+c.length+"), minimum "+a.minProperties)).prefixWith(null,"minProperties");if(this.handleError(d))return d}return void 0!=a.maxProperties&&c.length>a.maxProperties&&(d=(new g(h.OBJECT_PROPERTIES_MAXIMUM,
"Too many properties defined ("+c.length+"), maximum "+a.maxProperties)).prefixWith(null,"maxProperties"),this.handleError(d))?d:null};e.prototype.validateObjectRequiredProperties=function(b,a){if(void 0!=a.required)for(var c=0;c<a.required.length;c++){var d=a.required[c];if(void 0===b[d]&&(d=(new g(h.OBJECT_REQUIRED,"Missing required property: "+d)).prefixWith(null,""+c).prefixWith(null,"required"),this.handleError(d)))return d}return null};e.prototype.validateObjectProperties=function(b,a){var c,
d;for(d in b){var f=!1;if(void 0!=a.properties&&void 0!=a.properties[d]&&(f=!0,c=this.validateAll(b[d],a.properties[d],[d],["properties",d])))return c;if(void 0!=a.patternProperties)for(var e in a.patternProperties)if(RegExp(e).test(d)&&(f=!0,c=this.validateAll(b[d],a.patternProperties[e],[d],["patternProperties",e])))return c;if(!f&&void 0!=a.additionalProperties)if("boolean"==typeof a.additionalProperties){if(!a.additionalProperties&&(c=(new g(h.OBJECT_ADDITIONAL_PROPERTIES,"Additional properties not allowed")).prefixWith(d,
"additionalProperties"),this.handleError(c)))return c}else if(c=this.validateAll(b[d],a.additionalProperties,[d],["additionalProperties"]))return c}return null};e.prototype.validateObjectDependencies=function(b,a){var c;if(void 0!=a.dependencies)for(var d in a.dependencies)if(void 0!==b[d]){var f=a.dependencies[d];if("string"==typeof f){if(void 0===b[f]&&(c=(new g(h.OBJECT_DEPENDENCY_KEY,"Dependency failed - key must exist: "+f)).prefixWith(null,d).prefixWith(null,"dependencies"),this.handleError(c)))return c}else if(Array.isArray(f))for(var e=
0;e<f.length;e++){if(c=f[e],void 0===b[c]&&(c=(new g(h.OBJECT_DEPENDENCY_KEY,"Dependency failed - key must exist: "+c)).prefixWith(null,""+e).prefixWith(null,d).prefixWith(null,"dependencies"),this.handleError(c)))return c}else if(c=this.validateAll(b,f,[],["dependencies",d]))return c}return null};e.prototype.validateCombinations=function(b,a){return this.validateAllOf(b,a)||this.validateAnyOf(b,a)||this.validateOneOf(b,a)||this.validateNot(b,a)||null};e.prototype.validateAllOf=function(b,a){if(void 0==
a.allOf)return null;for(var c,d=0;d<a.allOf.length;d++)if(c=this.validateAll(b,a.allOf[d],[],["allOf",d]))return c;return null};e.prototype.validateAnyOf=function(b,a){if(void 0==a.anyOf)return null;for(var c=[],d=this.errors.length,e=0;e<a.anyOf.length;e++){var j=this.errors.length,l=this.validateAll(b,a.anyOf[e],[],["anyOf",e]);if(null==l&&j==this.errors.length)return this.errors=this.errors.slice(0,d),null;l&&c.push(l.prefixWith(null,""+e).prefixWith(null,"anyOf"))}c=c.concat(this.errors.slice(d));
this.errors=this.errors.slice(0,d);return new g(h.ANY_OF_MISSING,'Data does not match any schemas from "anyOf"',"","/anyOf",c)};e.prototype.validateOneOf=function(b,a){if(void 0==a.oneOf)return null;for(var c=null,d=[],e=this.errors.length,j=0;j<a.oneOf.length;j++){var l=this.errors.length,k=this.validateAll(b,a.oneOf[j],[],["oneOf",j]);if(null==k&&l==this.errors.length)if(null==c)c=j;else return this.errors=this.errors.slice(0,e),new g(h.ONE_OF_MULTIPLE,'Data is valid against more than one schema from "oneOf": indices '+
c+" and "+j,"","/oneOf");else k&&d.push(k.prefixWith(null,""+j).prefixWith(null,"oneOf"))}return null==c?(d=d.concat(this.errors.slice(e)),this.errors=this.errors.slice(0,e),new g(h.ONE_OF_MISSING,'Data does not match any schemas from "oneOf"',"","/oneOf",d)):null};e.prototype.validateNot=function(b,a){if(void 0==a.not)return null;var c=this.errors.length,d=this.validateAll(b,a.not),e=this.errors.slice(c);this.errors=this.errors.slice(0,c);return null==d&&0==e.length?new g(h.NOT_PASSED,'Data matches schema from "not"',
"","/not"):null};var h={INVALID_TYPE:0,ENUM_MISMATCH:1,ANY_OF_MISSING:10,ONE_OF_MISSING:11,ONE_OF_MULTIPLE:12,NOT_PASSED:13,NUMBER_MULTIPLE_OF:100,NUMBER_MINIMUM:101,NUMBER_MINIMUM_EXCLUSIVE:102,NUMBER_MAXIMUM:103,NUMBER_MAXIMUM_EXCLUSIVE:104,STRING_LENGTH_SHORT:200,STRING_LENGTH_LONG:201,STRING_PATTERN:202,OBJECT_PROPERTIES_MINIMUM:300,OBJECT_PROPERTIES_MAXIMUM:301,OBJECT_REQUIRED:302,OBJECT_ADDITIONAL_PROPERTIES:303,OBJECT_DEPENDENCY_KEY:304,ARRAY_LENGTH_SHORT:400,ARRAY_LENGTH_LONG:401,ARRAY_UNIQUE:402,
ARRAY_ADDITIONAL_ITEMS:403};g.prototype={prefixWith:function(b,a){null!=b&&(b=b.replace("~","~0").replace("/","~1"),this.dataPath="/"+b+this.dataPath);null!=a&&(a=a.replace("~","~0").replace("/","~1"),this.schemaPath="/"+a+this.schemaPath);if(null!=this.subErrors)for(var c=0;c<this.subErrors.length;c++)this.subErrors[c].prefixWith(b,a);return this}};var n=new e;s.tv4={validate:function(b,a){var c=new e(n);"string"==typeof a&&(a={$ref:a});c.addSchema("",a);var d=c.validateAll(b,a);this.error=d;this.missing=
c.missing;return this.valid=null==d},validateResult:function(){var b={};this.validate.apply(b,arguments);return b},validateMultiple:function(b,a){var c=new e(n,!0);"string"==typeof a&&(a={$ref:a});c.addSchema("",a);c.validateAll(b,a);var d={};d.errors=c.errors;d.missing=c.missing;d.valid=0==d.errors.length;return d},addSchema:function(b,a){return n.addSchema(b,a)},getSchema:function(b){return n.getSchema(b)},missing:[],error:null,normSchema:m,resolveUrl:p,errorCodes:h}})("undefined"!==typeof module&&
module.exports?exports:this);
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc