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