-
Notifications
You must be signed in to change notification settings - Fork 1
/
jscheme.min.js
1 lines (1 loc) · 7.89 KB
/
jscheme.min.js
1
!function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b){var c,d;c=a("./scheme"),d=new c,d["new"]=function(){return new c},b.exports=d,"undefined"!=typeof window&&null!==window&&(window.jScheme=d)},{"./scheme":3}],2:[function(a,b){var c;b.exports=c=function(){function a(a){var b;this.inputString=a.inputString,this.scheme=a.scheme,this.property=a.property,this.parent=a.parent,this.validators=[],this.location=this.getLocation(),this.scheme.propertiesRequired&&null!=(b=this.parent)&&b.addRequiredProperty(this.property),this.addValidations(this.inputString)}var b;return b=/\w[\w ]*\w/g,a.prototype.getLocation=function(){return null==this.property?"":null!=this.parent?this.parent.location+this.scheme.writeProperty(this.property):this.scheme.writeProperty(this.property)},a.prototype.getPropLocation=function(a){return""+this.location+this.scheme.writeProperty(a)},a.prototype.addValidations=function(a){for(var c,d,e;c=b.exec(a);)d=c[0],"optional"===d?this.parent.removeRequiredProperty(this.property):"required"===d?this.parent.addRequiredProperty(this.property):0===d.indexOf("array of ")?(this.validators.push("array"),this.arrayValidator=d.slice(9)):-1!==d.indexOf(" or ")?(e=d.split(" or "),console.log("todo")):this.validators.push(d);return void 0},a.prototype.validate=function(a,b){var c,d,e,f,g,h,i,j;if(c=!0,null==a&&this.isOptional())return c;for(g=this.scheme.validators,j=this.validators||[],h=0,i=j.length;i>h;h++){if(d=j[h],f=g[d],null==f)return b.add("missing validator "+d,{location:this.location});e=f(a),e!==!0&&(b.add(e,{location:this.location,defaultMessage:""+d+" validator failed"}),c=!1)}return(c=this.validateArray(a,b))&&(c=this.validateRequiredProperties(a,b))?c:!1},a.prototype.validateArray=function(a,b){var c,d,e,f,g,h,i,j,k;if(null==this.arrayValidator)return!0;if(e=!0,h=this.scheme.validators[this.arrayValidator],null==h)return b.add("missing validator "+this.arrayValidator,{location:this.location});for(k=a||[],d=i=0,j=k.length;j>i;d=++i)c=k[d],g=h(c),g!==!0&&(f=""+this.location+"["+d+"]",b.add(g,{location:f,defaultMessage:""+this.arrayValidator+" validator failed"}),e=!1);return e},a.prototype.validateOtherProperty=function(a,b,c){var d;return null!=this.otherPropertyValidator?(this.scheme.errors=void 0,(d=this.otherPropertyValidator.call(this,a,b))?!0:(null!=this.scheme.errors?c.join(this.scheme.errors,{location:this.getPropLocation(a)}):c.add("additional property check failed",{location:this.getPropLocation(a)}),!1)):this.scheme.allowAdditionalProperties?!0:(c.add("unspecified additional property",{location:this.getPropLocation(a)}),!1)},a.prototype.validateRequiredProperties=function(a,b){var c,d,e,f;d=!0,f=this.requiredProperties;for(e in f)c=f[e],null==a[e]&&c&&(b.add("required property missing",{location:this.getPropLocation(e)}),d=!1);return d},a.prototype.addRequiredProperty=function(a){return null==this.requiredProperties&&(this.requiredProperties={}),this.requiredProperties[a]=!0},a.prototype.removeRequiredProperty=function(a){var b;return null!=(b=this.requiredProperties)?b[a]=void 0:void 0},a.prototype.isOptional=function(){return null!=this.parent?!this.parent.requiredProperties[this.property]==!0:void 0},a}()},{}],3:[function(a,b){var c,d,e,f,g;e=a("./validation_errors"),c=a("./property_validator"),g=a("./validators"),f=a("./type"),b.exports=d=function(){function a(){this.validators=Object.create(g),this.schemas={},this.propertiesRequired=!0,this.allowAdditionalProperties=!0}var b;return b=/^[a-zA-Z]\w*$/,a.prototype.configure=function(a){this.propertiesRequired=a.propertiesRequired,this.allowAdditionalProperties=a.allowAdditionalProperties},a.prototype.add=function(a,b){return f.isFunction(b)?this.addValidator(a,b):this.addSchema(a,this.parseConfigObj(b,void 0,a)),this},a.prototype.addSchema=function(a,b){if(null!=this.validators[a])throw new Error("A validator is alredy registered under this name: "+a);return this.schemas[a]=b,this.validators[a]=function(a){return function(c){var d;return d=a.recursiveValidate(b,c),d.hasErrors()?d:!0}}(this),this},a.prototype.addValidator=function(a,b){return this.validators[a]=b,this},a.prototype.validate=function(a,b){var c;return this.errors=void 0,c=this.schemas[a],null==c?(this.errors=new e,this.errors.add("missing schema",{location:a}),!1):(this.errors=this.recursiveValidate(c,b).setRoot(a),!this.errors.hasErrors())},a.prototype.hasErrors=function(){var a;return null!=(a=this.errors)?a.hasErrors():void 0},a.prototype.getErrorMessages=function(){var a;return null!=(a=this.errors)?a.getMessages():void 0},a.prototype.recursiveValidate=function(a,b){var c,d,g,h,i,j;h=a.__validator,c=new e,h.validate(b,c);for(g in b)j=b[g],null!=a[g]?(i=a[g].__validator,d=i.validate(j,c),d&&null==i.childSchemaName&&f.isObject(j)&&c.join(this.recursiveValidate(a[g],j))):h.validateOtherProperty(g,j,c);return c},a.prototype.parseConfigObj=function(a,b){var d,e,g;null==b&&(b=new c({inputString:"object",scheme:this}));for(d in a)g=a[d],this.addParentValidator(b,d,g)||(f.isString(g)?(e=new c({inputString:g,property:d,parent:b,scheme:this}),a[d]={__validator:e}):f.isObject(g)&&(e=new c({inputString:"object",property:d,parent:b,scheme:this}),a[d]=this.parseConfigObj(g,e)));return a.__validator=b,a},a.prototype.addParentValidator=function(a,b,c){switch(b){case"__validate":a.addValidations(c);break;case"__additionalProperty":f.isFunction(c)&&(a.otherPropertyValidator=c);break;default:return!1}return!0},a.prototype.writeProperty=function(a){return b.test(a)?"."+a:"['"+a+"']"},a}()},{"./property_validator":2,"./type":4,"./validation_errors":5,"./validators":6}],4:[function(a,b){var c,d;c=Object.prototype.toString,b.exports=d={isObject:function(a){var b;return b=typeof a,"object"===b&&!!a&&!this.isArray(a)},isBoolean:function(a){return a===!0||a===!1||"[object Boolean]"===c.call(a)}},["Function","String","Number","Date","RegExp","Array"].forEach(function(a){return d["is"+a]=function(b){return c.call(b)==="[object "+a+"]"}}),Array.isArray&&(d.isArray=Array.isArray)},{}],5:[function(a,b){var c,d;d=a("./type"),b.exports=c=function(){function a(){}return a.prototype.hasErrors=function(){return null!=this.errors},a.prototype.setRoot=function(a){return this.root=a,this},a.prototype.add=function(b,c){var e,f,g,h;if(h=null!=c?c:{},g=h.location,e=h.defaultMessage,b===!1&&(b=e),void 0===b&&(b="validator returned undefined. Check your validator implementation."),null==this.errors&&(this.errors=[]),d.isString(b))this.errors.push({path:g,message:b});else if(b instanceof a)this.join(b,{location:g});else{if(!b.path||!b.message)throw new Error("ValidationError.add() unknown error type");f=b,this.errors.push({path:g+f.path,message:f.message})}return!1},a.prototype.join=function(a,b){var c,d,e,f,g,h;if(d=a.errors,e=(null!=b?b:{}).location,null!=d&&d.length){for(null==this.errors&&(this.errors=[]),h=[],f=0,g=d.length;g>f;f++)c=d[f],h.push(this.errors.push({path:(e||"")+c.path,message:c.message}));return h}},a.prototype.getMessages=function(){var a,b,c,d,e;for(b=[],e=this.errors||[],c=0,d=e.length;d>c;c++)a=e[c],b.push(""+(this.root||"")+a.path+": "+a.message);return b},a}()},{"./type":4}],6:[function(a,b){var c;c=a("./type"),b.exports={object:function(a){return c.isObject(a)},string:function(a){return c.isString(a)},"boolean":function(a){return c.isBoolean(a)},number:function(a){return c.isNumber(a)},"function":function(a){return c.isFunction(a)},date:function(a){return c.isDate(a)},regexp:function(a){return c.isRegExp(a)},array:function(a){return c.isArray(a)},falsy:function(a){return!!a==!1},truthy:function(a){return!!a==!0},"not empty":function(a){return!!a==!0},deprecated:function(){return!0}}},{"./type":4}]},{},[1]);