lib/har/viewer/scripts/preview/jsonSchema.js

Summary

Maintainability
A
0 mins
Test Coverage
/**
 * JSONSchema Validator - Validates JavaScript objects using JSON Schemas
 *    (http://www.json.com/json-schema-proposal/)
 *
 * Copyright (c) 2007 Kris Zyp SitePen (www.sitepen.com)
 * Licensed under the MIT (MIT-LICENSE.txt) license.
To use the validator call JSONSchema.validate with an instance object and an optional schema object.
If a schema is provided, it will be used to validate. If the instance object refers to a schema (self-validating),
that schema will be used to validate and the schema parameter is not necessary (if both exist,
both validations will occur).
The validate method will return an array of validation errors. If there are no errors, then an
empty list will be returned. A validation error will have two properties:
"property" which indicates which property had the error
"message" which indicates what the error was
 */

define("preview/jsonSchema",[],function(){var e={validate:function(e,t){return this._validate(e,t,!1)},checkPropertyChange:function(e,t,n){return this._validate(e,t,n||"property")},_validate:function(e,t,n){function i(e,t,o,u){function f(e){r.push({property:o,message:e})}function l(e,t){if(e){if(!(typeof e!="string"||e=="any"||(e=="null"?t===null:typeof t==e)||t instanceof Array&&e=="array"||e=="integer"&&t%1===0))return[{property:o,message:typeof t+" value found, but a "+e+" is required"}];if(e instanceof Array){var n=[];for(var s=0;s<e.length;s++)if(!(n=l(e[s],t)).length)break;if(n.length)return n}else if(typeof e=="object"){var u=r;r=[],i(t,e,o);var a=r;return r=u,a}}return[]}var a;o+=o?typeof u=="number"?"["+u+"]":typeof u=="undefined"?"":"."+u:u;if((typeof t!="object"||t instanceof Array)&&(o||typeof t!="function"))return typeof t=="function"?e instanceof t||f("is not an instance of the class/constructor "+t.name):t&&f("Invalid schema/property definition "+t),null;n&&t.readonly&&f("is a readonly field, it can not be changed"),t["extends"]&&i(e,t["extends"],o,u);if(e===undefined)t.optional||f("is missing and it is not optional");else{r=r.concat(l(t.type,e)),t.disallow&&!l(t.disallow,e).length&&f(" disallowed value was matched");if(e!==null){if(e instanceof Array){if(t.items)if(t.items instanceof Array)for(u=0,a=e.length;u<a;u++)r.concat(i(e[u],t.items[u],o,u));else for(u=0,a=e.length;u<a;u++)r.concat(i(e[u],t.items,o,u));t.minItems&&e.length<t.minItems&&f("There must be a minimum of "+t.minItems+" in the array"),t.maxItems&&e.length>t.maxItems&&f("There must be a maximum of "+t.maxItems+" in the array")}else t.properties&&r.concat(s(e,t.properties,o,t.additionalProperties));t.pattern&&typeof e=="string"&&!e.match(t.pattern)&&f("does not match the regex pattern "+t.pattern),t.maxLength&&typeof e=="string"&&e.length>t.maxLength&&f("may only be "+t.maxLength+" characters long"),t.minLength&&typeof e=="string"&&e.length<t.minLength&&f("must be at least "+t.minLength+" characters long"),typeof t.minimum!==undefined&&typeof e==typeof t.minimum&&t.minimum>e&&f("must have a minimum value of "+t.minimum),typeof t.maximum!==undefined&&typeof e==typeof t.maximum&&t.maximum<e&&f("must have a maximum value of "+t.maximum);if(t["enum"]){var c=t["enum"];a=c.length;var h;for(var p=0;p<a;p++)if(c[p]===e){h=1;break}h||f("does not have a value in the enumeration "+c.join(", "))}typeof t.maxDecimal=="number"&&e.toString().match(new RegExp("\\.[0-9]{"+(t.maxDecimal+1)+",}"))&&f("may only have "+t.maxDecimal+" digits of decimal places")}}return null}function s(e,t,s,o){if(typeof t=="object"){(typeof e!="object"||e instanceof Array)&&r.push({property:s,message:"an object is required"});for(var u in t)if(t.hasOwnProperty(u)&&(u.charAt(0)!="_"||u.charAt(1)!="_")){var a=e[u],f=t[u];i(a,f,s,u)}}for(u in e){e.hasOwnProperty(u)&&(u.charAt(0)!="_"||u.charAt(1)!="_")&&t&&!t[u]&&o===!1&&r.push({property:s,message:typeof a+"The property "+u+" is not defined in the schema and the schema does not allow additional properties"});var l=t&&t[u]&&t[u].requires;l&&!(l in e)&&r.push({property:s,message:"the presence of the property "+u+" requires that "+l+" also be present"}),a=e[u],t&&typeof t=="object"&&!(u in t)&&i(a,o,s,u),!n&&a&&a.$schema&&(r=r.concat(i(a,a.$schema,s,u)))}return r}var r=[];return t&&i(e,t,"",n||""),!n&&e&&e.$schema&&i(e,e.$schema,"",""),{valid:!r.length,errors:r}}};return e});