schema-to-yup
Advanced tools
Comparing version 1.9.0 to 1.9.1
@@ -1,2 +0,2 @@ | ||
function t(t){return t&&"object"==typeof t&&"default"in t?t.default:t}var e=t(require("dashify")),n=t(require("uppercamelcase")),r=require("yup"),i={getProps:function(t){return t&&t.properties},getType:function(t){return t&&t.type},getName:function(t){return t&&t.name||t.title},getConstraints:function(t){return t},isString:function(t){return t&&"string"===t.type},isArray:function(t){return t&&"array"===t.type},isInteger:function(t){return t&&"integer"===t.type||"int"===t.type},isBoolean:function(t){return t&&"boolean"===t.type},hasDateFormat:function(t){return t&&["date","date-time"].find(function(e){return e===t.format})},isDate:function(t){return t&&"string"===t.type&&i.hasDateFormat(t.format)},isNumber:function(t){return t&&("number"===t.type||i.isInteger(t))},isObject:function(t){return t&&"object"===t.type},isRequired:function(t){return t&&t.required}},o={"json-schema":i,"type-def":{getProps:function(t){return t&&t.fields},getType:function(t){return t&&t.type},getName:function(t){return t&&t.name},getConstraints:function(t){return t&&(t.directives||{}).constraints||{}},isString:function(t){return t&&"String"===t.type},isArray:function(t){return t&&t.isList},isInteger:function(t){return t&&"Int"===t.type},isBoolean:function(t){return t&&"Boolean"===t.type},isDate:function(t){return t&&"Date"===t.type||t.directives.date},isNumber:function(t){return t&&"Int"===t.type||"Float"===t.type},isObject:function(t){return t&&"Object"===t.type},isRequired:function(t){return t&&!t.isNullable}}},s=function(t){void 0===t&&(t={});var e=t.log,n=t.error;this.enable=t.enable||{},this.log="function"==typeof e?e:console.log,this.err="function"==typeof n?n:console.error};s.prototype.error=function(t,e){if(!1!==this.enable.error)throw this.err&&(e?this.err(t,e):this.err(t)),t},s.prototype.warn=function(t,e){this.enable.warn&&this.logInfo("WARNING: "+t,e)},s.prototype.logInfo=function(t,e){this.enable.log&&this.log&&(e?this.log(t,e):this.log(t))};var a=function(t){function e(e){void 0===e&&(e={}),t.call(this,e)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.isNothing=function(t){return null==t},e.prototype.isPresent=function(t){return!this.isNothing(t)},e.prototype.toNumber=function(t){return Number(t)},e.prototype.isNumberLike=function(t){return!isNaN(this.toNumber(t))},e.prototype.isNumberType=function(t){return!isNaN(t)},e.prototype.isStringType=function(t){return"string"==typeof t},e.prototype.isFunctionType=function(t){return"function"==typeof t},e.prototype.isDateType=function(t){return t instanceof Date},e}(s),u=function(t){function e(e){void 0===e&&(e={}),t.call(this,e),this.config=Object.assign({},o[e.schemaType||"json-schema"],e)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e}(a);function c(t){return t===Object(t)}var p=function(t,e){void 0===e&&(e={}),this.whenEntryObj=t;var n=e.properties,r=e.config,i=e.key,o=e.when,s=e.type;this.schema=e.schema,this.when=o,this.properties=n||{},this.key=i,this.type=s,this.config=r},h={entryObj:{configurable:!0}};p.prototype.validateAndConfigure=function(t){if(!c(t=t||this.whenEntryObj))return this.warn("invalid or missing when entry constraint object",t),!1;var e=Object.keys(t);return e.length<2?(this.warn("validateAndConfigure: when entry constraint must have at least 2 keys: "+e,t),!1):this.hasKey(e,"is")?!!this.hasKey(e,"then")||(this.warn("validateAndConfigure: when entry constraint missing 'then' constraint: "+e,t),!1):(this.warn("validateAndConfigure: when entry constraint missing 'is' constraint: "+e,t),!1)},p.prototype.createYupSchemaEntry=function(t){return this.config.createYupSchemaEntry(t)},p.prototype.createValue=function(t,e){return"string"==typeof t&&((t={})[t]=!0),c(t)||this.error("createValue: "+e+" must be a schema object"),Object.assign({},{key:this.key,type:this.type},t)},p.prototype.createEntryOpts=function(t,e){var n=this.createValue(t,e);return{schema:this.schema,properties:this.properties,key:this.key,type:this.type,value:n,config:this.config}},p.prototype.createEntry=function(t,e){var n=this.createEntryOpts(t,e);return this.createYupSchemaEntry(n)},p.prototype.hasKey=function(t,e){return t.find(function(t){return t===e})},p.prototype.whenEntryFor=function(t,e){var n=t[e];return n?(t[e]=this.createEntry(n,e),t):t},p.prototype.calcEntryObj=function(){var t=Object.assign({},this.whenEntryObj);return t=this.whenEntryFor(t,"then"),t=this.whenEntryFor(t,"otherwise")},h.entryObj.get=function(){return this.validateAndConfigure()&&this.calcEntryObj()},p.prototype.warn=function(t,e){console.error("[WhenEntry] WARNING",t,e)},p.prototype.error=function(t,e){throw console.error("[WhenEntry] ERROR",t,e),t},Object.defineProperties(p.prototype,h);function f(t){return t===Object(t)}var y=function(t){void 0===t&&(t={});var e=t.type,n=t.key,r=t.value,i=t.when,o=t.schema,s=t.properties,a=t.config;this.opts=t,this.when=i,this.key=n,this.type=e,this.value=r,this.schema=o,this.properties=s,this.config=a,this.validate()},l={constraintObj:{configurable:!0},keyVal:{configurable:!0},constraintValue:{configurable:!0},constraint:{configurable:!0}};y.prototype.validate=function(){"string"!=typeof this.type&&this.error("validate: invalid or mising type: "+this.type,this.opts),f(this.when)||this.error("validate: invalid or mising when: "+this.when,this.opts)},y.prototype.validateAndConfigure=function(t){if(!f(t=t||this.when))return this.warn("invalid or missing when constraint",t),!1;var e=Object.keys(t);return e.length<1?(this.warn("when constraint must have at least 1 key: "+e,t),!1):(this.whenKeys=e,!0)},y.prototype.createWhenEntry=function(t,e){return function(t,e){return void 0===e&&(e={}),new p(t,e)}(t,e)},y.prototype.accumulate=function(t,e){var n=this.when[e];if(!f(n))return this.warn("invalid when entry constraint object "+n+" for "+e),t;var r=this.createWhenEntry(n,{type:this.type,key:this.key,schema:this.schema,properties:this.properties,config:this.config}).entryObj;return r?t=Object.assign(t,r):t},l.constraintObj.get=function(){return this.whenKeys?this.whenKeys.reduce(this.accumulate.bind(this),{}):{}},l.keyVal.get=function(){var t=this.whenKeys||[];return 1===t.length?t[0]:t},l.constraintValue.get=function(){return!!this.keyVal&&[this.keyVal,this.constraintObj]},l.constraint.get=function(){return this.validateAndConfigure()&&this.constraintValue},y.prototype.warn=function(t,e){console.error("[WhenCondition] WARNING",t,e)},y.prototype.error=function(t,e){throw console.error("[WhenCondition] ERROR",t,e),t},Object.defineProperties(y.prototype,l);var m=function(t){function e(){t.apply(this,arguments)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e}(Error),d=["oneOf","enum","required","notRequired","minDate","min","maxDate","max","trim","lowercase","uppercase","email","url","minLength","maxLength","pattern","matches","regex","integer","positive","minimum","maximum"],g={errMessages:function(t){return void 0===t&&(t=d),t.reduce(function(t,e){return t[e]=function(t){var e=t.value;return t.key+": invalid for "+(e.name||e.title)},t},{})}};var v=function(t){function e(e){void 0===e&&(e={}),t.call(this,e.config);var n=e.schema,i=e.key,o=e.value,s=e.config;this.validateOnCreate(i,o,e),this.yup=r,this.key=i,this.schema=n,this.properties=n.properties,this.value=o,this.constraints=this.getConstraints(),this.format=o.format||this.constraints.format,this.config=s||{},this.type="mixed",this.base=r.mixed(),this.errMessages=s.errMessages||{},this.constraintsAdded={},this.rebind("addConstraint","addValueConstraint")}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={enabled:{configurable:!0},constraintsMap:{configurable:!0}};return e.prototype.rebind=function(){for(var t=this,e=[],n=arguments.length;n--;)e[n]=arguments[n];e.map(function(e){var n=t[e];t[e]=t.isFunctionType(n)?n.bind(t):n})},e.prototype.validateOnCreate=function(t,e,n){t||this.error("create: missing key "+JSON.stringify(n)),e||this.error("create: missing value "+JSON.stringify(n))},n.enabled.get=function(){},e.prototype.convertEnabled=function(){var t=this;this.enabled.map(function(e){t[e]&&t[e]()})},e.prototype.getConstraints=function(){return this.config.getConstraints(this.value)},e.prototype.createSchemaEntry=function(){return this.convert().base},e.prototype.convert=function(){return this.addMappedConstraints(),this.oneOf().notOneOf(),this.when(),this},e.prototype.addValueConstraint=function(t,e){return void 0===e&&(e={}),this.addConstraint(t,{constraintName:e.constraintName,value:!0,errName:e.errName})},e.prototype.buildConstraint=function(t,e){void 0===e&&(e={});var n=e.constraintName,r=e.method,i=e.yup,o=e.value,s=e.values,a=e.errName;i=i||this.base;var u=this.constraints[t];if(!u)return i;if(n=n||t,!i[r=r||n])return this.warn("Yup has no such API method: "+r),this;var c=i[r].bind(i),p=this.valErrMessage(n)||a&&this.valErrMessage(a);if(o){var h=!0===o?u:o;return this.onConstraintAdded({name:n,value:h}),h?c(h,p):c(p)}return s?Array.isArray(s)?(this.onConstraintAdded({name:n,value:s}),c.apply(void 0,s.concat([p]))):(this.warn("buildConstraint: values option must be an array of arguments"),i):(this.warn("buildConstraint: missing value or values options"),i)},e.prototype.addConstraint=function(t,e){var n=this.buildConstraint(t,e);return this.base=n||this.base,this},e.prototype.onConstraintAdded=function(t){return this.constraintsAdded[t.name]=t.value,this},e.prototype.addMappedConstraints=function(){var t=this,e=this.constraintsMap;return Object.keys(e).map(function(n){e[n].map(t["value"===n?"addValueConstraint":"addConstraint"])}),this},n.constraintsMap.get=function(){return{simple:["required","notRequired","nullable"],value:["default","strict"]}},e.prototype.oneOf=function(){return this.addConstraint("oneOf",{value:this.constraints.enum||this.constraints.oneOf,errName:"enum"})},e.prototype.notOneOf=function(){var t=this.constraints,e=t.not,n=t.notOneOf;return this.addConstraint("notOneOf",{value:n||e&&(e.enum||e.oneOf)})},e.prototype.valErrMessage=function(t){var e=this.errMessages[this.key]?this.errMessages[this.key][t]:void 0;return"function"==typeof e?e(this.constraints):e},e.prototype.createWhenConditionFor=function(t){return function(t){return new y(t)}({key:this.key,type:this.type,value:this.value,schema:this.schema,properties:this.properties,config:this.config,when:t})},e.prototype.when=function(){var t,e=this.constraints.when;if((t=e)!==Object(t))return this;var n=this.createWhenConditionFor(e).constraint;return n?(this.logInfo("Adding when constraint for "+this.key,n),this.addConstraint("when",{values:n,errName:"when"}),this):(this.warn("Invalid when constraint for: "+e),this)},e.prototype.$const=function(){return this},e.prototype.$allOf=function(){return this},e.prototype.$anyOf=function(){return this},e.prototype.$oneOf=function(){return this},e.prototype.$not=function(){return this},e.prototype.$if=function(){return this},e.prototype.$then=function(){return this},e.prototype.$else=function(){return this},e.prototype.message=function(){return config.messages[this.key]||config.messages[this.type]||{}},e.prototype.errMessage=function(t){return void 0===t&&(t="default"),this.message[t]||"error"},e.prototype.toValidJSONSchema=function(){},e.prototype.normalize=function(){},e.prototype.deNormalize=function(){},e.prototype.errorMsg=function(t){this.throwError(t)},e.prototype.error=function(t,e){var n=["["+t+"]",e].join(" ");this.errorMsg(n)},e.prototype.throwError=function(t){throw t},Object.defineProperties(e.prototype,n),e}(u),b=function(t){function e(e){t.call(this,e)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.isArray=function(t){return this.config.isArray||this.error("ArrayHandler: mising isArray in config",this.config),this.config.isArray(t)},e.prototype.handle=function(t){return this.isArray(t)&&O.create(t).createSchemaEntry()},e}(u);function w(t,e){return void 0===e&&(e={}),t&&new b(e).handle(t)}var O=function(t){function e(e){t.call(this,e),this.type="array",this.base=this.yup.array(),this.createYupSchemaEntry=this.config.createYupSchemaEntry}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.create=function(t){return new e(t)},e.prototype.convert=function(){return this.maxItems(),this.minItems(),this.ensureItems(),this.compact(),t.prototype.convert.call(this),this},e.prototype.ensureItems=function(){return this.addConstraint("ensure")},e.prototype.compact=function(){return this.addConstraint("compact")},e.prototype.itemsOf=function(){return this},e.prototype.maxItems=function(){var t=this.constraints,e=t.maxItems,n=e||t.max;if(!this.isNumberType(n))return this;if(!this.isValidSize(n))return this.handleInvalidSize("maxItems",n);var r=n&&this.base.max(n);return this.base=r||this.base,this},e.prototype.minItems=function(){var t=this.constraints,e=t.minItems,n=e||t.min;if(!this.isNumberType(n))return this;if(!this.isValidSize(n))return this.handleInvalidSize("minItems",n);var r=n&&this.base.min(n);return this.base=r||this.base,this},e.prototype.$items=function(){return this},e.prototype.$additionalItems=function(){return this},e.prototype.$uniqueItems=function(){return this},e.prototype.$contains=function(){return this},e.prototype.handleInvalidSize=function(t,e){var n="invalid array size constraint for "+t+", was "+e+". Must be a number >= 0";return this.config.warnOnInvalid?(this.warn(n),this):(this.error(n,e),this)},e.prototype.isValidSize=function(t){return this.isNumberType(t)&&t>=0},e}(v),j=function(t){this.config=t};function C(t,e){return void 0===e&&(e={}),t&&new j(e).handle(t)}j.prototype.isBoolean=function(t){return this.config.isBoolean(t)},j.prototype.handle=function(t){return this.isBoolean(t)&&E.create(t).createSchemaEntry()};var E=function(t){function e(e){t.call(this,e),this.type="boolean",this.base=this.yup.boolean()}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.create=function(t){return new e(t)},e}(v);var N=function(t){function e(e){t.call(this,e)}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={$map:{configurable:!0}};return n.$map.get=function(){return{moreThan:["exclusiveMinimum","moreThan"],lessThan:["exclusiveMaximum","lessThan"],max:["maximum","max"],min:["minimum","min"]}},Object.defineProperties(e.prototype,n),e}(function(t){function e(e){t.call(this,e)}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={explainConstraintValidMsg:{configurable:!0}};return e.prototype.transform=function(t){return this.typer.toNumber(t)},e.prototype.isValidConstraint=function(t){return this.typer.isNumberLike(t)},n.explainConstraintValidMsg.get=function(){return"Must be a number or convertible to a number"},Object.defineProperties(e.prototype,n),e}(function(t){function e(e,n){var r=this;t.call(this,e.config),this.map=n||this.$map||{},this.typer=e,this.delegates.map(function(t){var n=e[t];n||r.error("missing delegate: "+t,{typer:e}),r[t]=r.isFunctionType(n)?n.bind(e):n})}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={delegates:{configurable:!0},explainConstraintValidMsg:{configurable:!0}};return e.prototype.isStringType=function(t){return"string"==typeof t},n.delegates.get=function(){return["constraints","addConstraint","constraintsAdded"]},e.prototype.add=function(){var t=this,e=this.map;Object.keys(e).map(function(n){var r=t.entryNames(e[n]);t.addConstraints(n,r)})},e.prototype.entryNames=function(t){return Array.isArray(t)?t:[t]},e.prototype.addConstraints=function(t,e){var n=this;return void 0===e&&(e=[]),e.map(function(e){var r=n.validateAndTransform(e);n.addConstraint(e,{method:t,value:r})}),this},e.prototype.validateAndTransform=function(t){var e=this.constraints[t];return this.validate(e),this.transform(e)},e.prototype.invalidMsg=function(t,e){return"invalid constraint for "+t+", was "+e+"."},n.explainConstraintValidMsg.get=function(){return""},e.prototype.invalidConstraintMsg=function(t,e){return[this.invalidMsg(t,e),this.explainConstraintValidMsg].join("\n")},e.prototype.validate=function(t){return this.isNothing(t)?this:this.isValidConstraint(t)?void 0:this.handleInvalidConstraint(name,t)},e.prototype.isValidConstraint=function(t){return!0},e.prototype.handleInvalidConstraint=function(t,e){var n=this.invalidConstraintMsg(t,e);return this.config.warnOnInvalid?(this.warn(n),this):(this.error(n,e),this)},Object.defineProperties(e.prototype,n),e}(a))),_=function(t){function e(e,n){t.call(this,e,n)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.isValid=function(){return this.config.isNumber(this.obj)},e}(function(t){function e(e,n){t.call(this,n),this.obj=e}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.isValid=function(){return!1},e.prototype.verify=function(){return this.isPresent(this.obj)&&this.isValid(this.obj)},e}(u));var x=function(t,e){return void 0===e&&(e={}),function(t,e){return new _(t,e)}(t,e).verify()};function S(t,e){return void 0===e&&(e={}),x(t,e)&&function(t){return k.schemaEntryFor(t)}(t)}var k=function(t){function e(e){t.call(this,e),this.type=this.normalizeNumType(e.type),this.base=this.yup.number(),this.range=new N(this)}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={enabled:{configurable:!0},isInteger:{configurable:!0},isNegative:{configurable:!0},isPositive:{configurable:!0}};return e.prototype.normalizeNumType=function(t){return"int"===t?"integer":t},e.create=function(t){return new e(t)},e.schemaEntryFor=function(t){return e.create(t).createSchemaEntry()},n.enabled.get=function(){return["range","posNeg","integer"]},e.prototype.convert=function(){var e=this;return this.enabled.map(function(t){return e.processConstraint(t)}),t.prototype.convert.call(this),this},e.prototype.processConstraint=function(t){var e=this[t];e&&"function"==typeof e?e.bind(this)():e.add()},e.prototype.truncate=function(){return this.addConstraint("truncate")},e.prototype.round=function(){var t=this.constraints.round;if(this.isNothing(t))return this;var e=this.isStringType(t)?t:"round";return t&&this.base.round(e),this},e.prototype.posNeg=function(){this.positive(),this.negative()},e.prototype.integer=function(){return this.isInteger&&this.addConstraint("integer"),this},n.isInteger.get=function(){return this.config.isInteger(this.type)},e.prototype.positive=function(){return this.addConstraint("positive")},e.prototype.negative=function(){return this.addConstraint("negative")},n.isNegative.get=function(){var t=this.constraints,e=t.exclusiveMaximum;return!!t.negative||void 0!==e&&0===e},n.isPositive.get=function(){var t=this.constraints,e=t.exclusiveMinimum;return!!t.positive||void 0!==e&&0===e},e.prototype.normalize=function(){this.constraints.maximum=this.constraints.maximum||this.constraints.max,this.constraints.minimum=this.constraints.minimum||this.constraints.min},Object.defineProperties(e.prototype,n),e}(v),M=function(t){this.config=t};function T(t,e){return void 0===e&&(e={}),t&&new M(e).handle(t)}M.prototype.isObject=function(t){return this.config.isObject(t)},M.prototype.handle=function(t){return this.isObject(t)&&I.create(t).createSchemaEntry()};var I=function(t){function e(e){t.call(this,e),this.type="object",this.base=this.yup.object(),this.properties=this.value.properties}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.create=function(t){return new e(t)},e.prototype.convert=function(){if(!this.properties)return this;if(this.noUnknown(),this.camelCase().constantCase(),this.value){var t=this.config.buildYup(this.value);this.base.shape(t)}return this},e.prototype.camelCase=function(){return this.addConstraint("camelCase")},e.prototype.constantCase=function(){return this.addConstraint("constantCase")},e.prototype.noUnknown=function(){var t=this.value,e=t.noUnknown,n=e||t.propertyNames,r=n&&this.base.noUnknown(n,this.valErrMessage("propertyNames")||this.valErrMessage("noUnknown"));return this.base=r||this.base,this},e}(v),D=function(t){this.config=t};function V(t,e){return void 0===e&&(e={}),t&&new D(e).handle(t)}D.prototype.isString=function(t){return this.config.isString(t)},D.prototype.handle=function(t){return this.isString(t)&&A.create(Object.assign({},{config:this.config},t)).createSchemaEntry()};var A=function(t){function e(e){t.call(this,e),this.type="string",this.base=this.yup.string()}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={isEmail:{configurable:!0},isUrl:{configurable:!0}};return e.create=function(t){return new e(t)},e.prototype.convert=function(){return t.prototype.convert.call(this),this.normalize(),this.minLength().maxLength().pattern(),this.lowercase().uppercase(),this.email(),this.url(),this.genericFormat(),this},e.prototype.trim=function(){return this.addConstraint("trim")},e.prototype.lowercase=function(){return this.addConstraint("lowercase")},e.prototype.uppercase=function(){return this.addConstraint("uppercase")},e.prototype.genericFormat=function(){!0!=!this.config.format&&(this.yup.prototype[this.format]&&this.addConstraint(this.format))},e.prototype.email=function(){return this.isEmail&&this.addConstraint("email"),this},n.isEmail.get=function(){return this.constraints.email||"email"===this.format},e.prototype.url=function(){return this.isUrl&&this.addConstraint("url"),this},n.isUrl.get=function(){return this.constraints.url||"url"===this.format},e.prototype.minLength=function(){var t=this.constraints.minLength,e=this.valErrMessage("minLength")||this.valErrMessage("min"),n=t&&this.base.min(t,e);return this.base=n||this.base,this},e.prototype.maxLength=function(){var t=this.constraints.maxLength,e=this.valErrMessage("maxLength")||this.valErrMessage("max"),n=t&&this.base.max(t,e);return this.base=n||this.base,this},e.prototype.pattern=function(){var t=this.constraints.pattern;if(!t)return this;var e=new RegExp(t),n=this.valErrMessage("pattern")||this.valErrMessage("matches")||this.valErrMessage("regex"),r=e&&this.base.matches(e,n);return this.base=r||this.base,this},e.prototype.normalize=function(){this.constraints.pattern=this.constraints.pattern||this.constraints.matches||this.constraints.regex,this.constraints.maxLength=this.constraints.maxLength||this.constraints.max,this.constraints.minLength=this.constraints.minLength||this.constraints.min},Object.defineProperties(e.prototype,n),e}(v),Y=function(t){this.config=t};function R(t,e){return void 0===e&&(e={}),t&&new Y(e).handle(t)}Y.prototype.isDate=function(t){return this.config.isDate(t)},Y.prototype.handle=function(t){return this.isDate(t)&&q.create(t).createSchemaEntry()};var q=function(t){function e(e){t.call(this,e),this.type="date",this.base=this.yup.date()}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.create=function(t){return new e(t)},e.prototype.convert=function(){return this.minDate().maxDate(),t.prototype.convert.call(this),this},e.prototype.toDate=function(t){return new Date(t)},e.prototype.isValidDateType=function(t){return this.isStringType(t)||this.isDateType(t)},e.prototype.isValidDate=function(t){return!!this.isValidDateType(t)&&(!this.isStringType(t)||Boolean(Date.parse(t)))},e.prototype.transformToDate=function(t){return this.isNumberType(t)?new Date(t):t},e.prototype.minDate=function(){var t=this.constraints.minDate||this.constraints.min;if(this.isNothing(t))return this;var e=this.transformToDate(t);if(!this.isValidDateType(e))return this.handleInvalidDate("minDate",e);var n=e&&this.base.min(this.toDate(e),this.valErrMessage("minDate")||this.valErrMessage("min"));return this.base=n||this.base,this},e.prototype.maxDate=function(){var t=this.constraints.maxDate||this.constraints.max;if(this.isNothing(t))return this;var e=this.transformToDate(t);if(!this.isValidDateType(e))return this.handleInvalidDate("maxDate",e);var n=e&&this.base.max(this.toDate(e),this.valErrMessage("maxDate")||this.valErrMessage("max"));return this.base=n||this.base,this},e.prototype.handleInvalidDate=function(t,e){var n="invalid constraint for "+t+", was "+e+". Must be a number, string (valid date format) or a Date instance";return this.config.warnOnInvalid?(this.warn(n),this):(this.error(n,e),this)},e}(v),P={errValKeys:d,defaults:g,YupArray:O,toYupArray:w,YupBoolean:E,toYupBoolean:C,YupNumber:k,toYupNumberSchemaEntry:S,toYupNumber:function(t,e){return void 0===e&&(e={}),x(t,e)&&function(t){return k.create(t)}(t)},YupObject:I,toYupObject:T,YupString:A,toYupString:V,YupDate:q,toYupDate:R,YupMixed:v,ConvertYupSchemaError:m,Base:u},z=function(t){function e(){t.apply(this,arguments)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e}(Error),$=function(t){function e(e){var n=e.schema,r=e.name,i=e.key,o=e.value,s=e.config;t.call(this,s),this.schema=n,this.key=i,this.value=o,this.config=s,this.name=r,this.type=o.type,this.types={string:V,number:S,boolean:C,array:w,object:T,date:R}}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={obj:{configurable:!0}};return e.prototype.isValidSchema=function(){return"string"==typeof this.type},e.prototype.error=function(t){throw new z(t)},e.prototype.toEntry=function(){if(!this.isValidSchema()){var t=JSON.stringify(this.schema);this.error("Not a valid schema: type "+this.type+" must be a string, was "+typeof this.type+" "+t)}var e=this.obj;return this.string(e)||this.number(e)||this.boolean(e)||this.array(e)||this.object(e)||this.date(e)||this.defaultType(e)},e.prototype.defaultType=function(t){console.error({type:t.type}),this.error("toEntry: unknown type",t)},n.obj.get=function(){return{schema:this.schema,key:this.key,value:this.value,type:this.type,config:this.config}},e.prototype.string=function(t){return V(t||this.obj,this.config)},e.prototype.number=function(t){return S(t||this.obj,this.config)},e.prototype.boolean=function(t){return C(t||this.obj,this.config)},e.prototype.array=function(t){return w(t||this.obj,this.config)},e.prototype.object=function(t){return T(t||this.obj,this.config)},e.prototype.date=function(t){return R(t||this.obj,this.config)},Object.defineProperties(e.prototype,n),e}(u);function L(t){void 0===t&&(t={});return new $(t).toEntry()}var F=function(t,e){return void 0===e&&(e={}),t.reduce(function(t,n){if("string"!=typeof n&&!(n instanceof Object)){if(!1!==e.throws)throw"toConstraintsMap: invalid entry "+n;return t}if("string"==typeof n)t[name]={};else{if(!n.name){if(!1!==e.throws)throw"toConstraintsMap: invalid entry "+n+" missing name";return t}t[n.name]=n}return t},{})},U={alphanumeric:{optsKey:"locale"},alpha:{optsKey:"locale"},ascii:{},byte:{},creditCard:{},currency:{opts:"currencyOpts"},dataUri:{},dateTime:{},date:{},domainName:{opts:"domainOpts"},hash:{opts:"hashAlgo"},hexColor:{},ipv4:{},ipv6:{},isbn:{},magnetUri:{},mimeType:{},mobilePhone:{},mongoId:{},postalCode:{},uuid:{}},B={createValidatorName:function(t,e){return"is"+(t=(t=(t=n(t||e)).replace(/Uri$/,"URI")).replace(/Id$/,"ID"))},createTestName:function(t,n){return e(t||n)}},K={isMagnetURI:function(t,e){return/magnet:\?xt=urn:[a-z0-9]+:[a-z0-9]{32}/i.test(t)}};function W(t,e){return void 0===e&&(e={}),new G(t,e).yupSchema}function J(t){return t===Object(t)}var G=function(t){function e(e,n){void 0===n&&(n={}),t.call(this,n),n.buildYup=W,n.createYupSchemaEntry=n.createYupSchemaEntry||L,this.config=Object.assign(this.config,n),this.schema=e;var r=this.getType(e),i=this.getProps(e);if(this.type=r,this.properties=i,this.required=this.getRequired(e),function(t){return t&&"object"===t}(r))if(J(i)){var o=this.getName(e),s=this.normalizeRequired(e),a=this.propsToShape({properties:s,name:o,config:n});this.shapeConfig=a,this.validSchema=!0}else{var u=JSON.stringify(s);this.error("invalid schema: must have a properties object: "+u)}else this.error('invalid schema: must be type: "object", was type: '+r)}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={yupSchema:{configurable:!0}};return e.prototype.getRequired=function(t){var e=this.config.getRequired;return e?e(t):t.required||[]},e.prototype.getProps=function(t){return this.config.getProps(t)},e.prototype.getType=function(t){return this.config.getType(t)},e.prototype.getName=function(t){return this.config.getName(t)},n.yupSchema.get=function(){return r.object().shape(this.shapeConfig)},e.prototype.normalizeRequired=function(){var t=this,e=Object.assign({},this.properties),n=[].concat(this.required)||[];return Object.keys(e).reduce(function(r,i){var o=e[i],s=n.indexOf(i)>=0;return J(o)?o.required=t.isRequired(o)||s:t.warn("Bad value: "+o+" must be an object"),r[i]=o,r},{})},e.prototype.isRequired=function(t){return this.config.isRequired(t)},e.prototype.propsToShape=function(t){var e=this,n=t.name,r=Object.assign({},this.properties);return Object.keys(r).reduce(function(t,i){var o=e.propToYupSchemaEntry({name:n,key:i,value:r[i]});return e.logInfo("propsToShape",{key:i,yupSchemaEntry:o}),t[i]=o,t},{})},e.prototype.propToYupSchemaEntry=function(t){var e=t.value;return void 0===e&&(e={}),(this.createYupSchemaEntry||this.config.createYupSchemaEntry)({schema:this.schema,name:t.name,key:t.key,value:e,config:this.config})},e.prototype.createYupSchemaEntry=function(t){var e=t.config;return e.createYupSchemaEntry({schema:t.schema,name:t.name,key:t.key,value:t.value,config:e})},Object.defineProperties(e.prototype,n),e}(u);exports.buildYup=W,exports.YupBuilder=G,exports.YupSchemaEntry=$,exports.YupSchemaEntryError=z,exports.types=P,exports.createYupSchemaEntry=L,exports.extendYupApi=function(t){var e=this;void 0===t&&(t={});var n=t.constraints,i=t.override;void 0===i&&(i=!1);var o=t.validator,s=t.createValidatorName,a=t.createTestName;if(!o)throw"extendYupApi: missing validator option";Array.isArray(n)&&(n=F(n)),n=i?n||U:Object.assign({},U,n||{}),s=s||B.createValidatorName,a=a||B.createTestName,Object.keys(n).map(function(t){var i=n[t],u=i.testName,c=i.optsKey,p=i.logging,h=s(i.validatorName,t);u=a(u,t),r.addMethod(r.string,t,function(n){void 0===n&&(n={});var i=n.message,s=n[c];return r.string().test(u,i,function(n){var r=e.path,a=e.createError,c=o[h];if("function"!=typeof(c=c||K[h]))throw Error("No method named ${validatorName} on validator");var f=c(n,s);return!0===p&&console.log("Yup validator bridge",{key:t,fullValidatorName:h,testName:u,value:n,valid:f}),f||a({path:r,message:i})})})})}; | ||
function t(t){return t&&"object"==typeof t&&"default"in t?t.default:t}var e=t(require("dashify")),n=t(require("uppercamelcase")),r=require("yup"),i={getProps:function(t){return t&&t.properties},getType:function(t){return t&&t.type},getName:function(t){return t&&t.name||t.title},getConstraints:function(t){return t},isString:function(t){return t&&"string"===t.type},isArray:function(t){return t&&"array"===t.type},isInteger:function(t){return t&&"integer"===t.type||"int"===t.type},isBoolean:function(t){return t&&"boolean"===t.type},hasDateFormat:function(t){return t&&["date","date-time"].find(function(e){return e===t.format})},isDate:function(t){return t&&"string"===t.type&&i.hasDateFormat(t.format)},isNumber:function(t){return t&&("number"===t.type||i.isInteger(t))},isObject:function(t){return t&&"object"===t.type},isRequired:function(t){return t&&t.required}},o={"json-schema":i,"type-def":{getProps:function(t){return t&&t.fields},getType:function(t){return t&&t.type},getName:function(t){return t&&t.name},getConstraints:function(t){return t&&(t.directives||{}).constraints||{}},isString:function(t){return t&&"String"===t.type},isArray:function(t){return t&&t.isList},isInteger:function(t){return t&&"Int"===t.type},isBoolean:function(t){return t&&"Boolean"===t.type},isDate:function(t){return t&&"Date"===t.type||t.directives.date},isNumber:function(t){return t&&"Int"===t.type||"Float"===t.type},isObject:function(t){return t&&"Object"===t.type},isRequired:function(t){return t&&!t.isNullable}}},s=function(t){void 0===t&&(t={});var e=t.log,n=t.error;this.enable=t.enable||{},this.log="function"==typeof e?e:console.log,this.err="function"==typeof n?n:console.error};s.prototype.error=function(t,e){if(!1!==this.enable.error)throw this.err&&(e?this.err(t,e):this.err(t)),t},s.prototype.warn=function(t,e){this.enable.warn&&this.logInfo("WARNING: "+t,e)},s.prototype.logInfo=function(t,e){this.enable.log&&this.log&&(e?this.log(t,e):this.log(t))};var a=function(t){function e(e){void 0===e&&(e={}),t.call(this,e)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.isNothing=function(t){return null==t},e.prototype.isPresent=function(t){return!this.isNothing(t)},e.prototype.toNumber=function(t){return Number(t)},e.prototype.isNumberLike=function(t){return!isNaN(this.toNumber(t))},e.prototype.isObjectType=function(t){return t===Object(t)},e.prototype.isNumberType=function(t){return!isNaN(t)},e.prototype.isStringType=function(t){return"string"==typeof t},e.prototype.isFunctionType=function(t){return"function"==typeof t},e.prototype.isDateType=function(t){return t instanceof Date},e}(s),u=function(t){function e(e){void 0===e&&(e={}),t.call(this,e),this.config=Object.assign({},o[e.schemaType||"json-schema"],e)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e}(a);function c(t){return t===Object(t)}var p=function(t,e){void 0===e&&(e={}),this.whenEntryObj=t;var n=e.properties,r=e.config,i=e.key,o=e.when,s=e.type;this.schema=e.schema,this.when=o,this.properties=n||{},this.key=i,this.type=s,this.config=r},h={entryObj:{configurable:!0}};p.prototype.validateAndConfigure=function(t){if(!c(t=t||this.whenEntryObj))return this.warn("invalid or missing when entry constraint object",t),!1;var e=Object.keys(t);return e.length<2?(this.warn("validateAndConfigure: when entry constraint must have at least 2 keys: "+e,t),!1):this.hasKey(e,"is")?!!this.hasKey(e,"then")||(this.warn("validateAndConfigure: when entry constraint missing 'then' constraint: "+e,t),!1):(this.warn("validateAndConfigure: when entry constraint missing 'is' constraint: "+e,t),!1)},p.prototype.createYupSchemaEntry=function(t){return this.config.createYupSchemaEntry(t)},p.prototype.createValue=function(t,e){return"string"==typeof t&&((t={})[t]=!0),c(t)||this.error("createValue: "+e+" must be a schema object"),Object.assign({},{key:this.key,type:this.type},t)},p.prototype.createEntryOpts=function(t,e){var n=this.createValue(t,e);return{schema:this.schema,properties:this.properties,key:this.key,type:this.type,value:n,config:this.config}},p.prototype.createEntry=function(t,e){var n=this.createEntryOpts(t,e);return this.createYupSchemaEntry(n)},p.prototype.hasKey=function(t,e){return t.find(function(t){return t===e})},p.prototype.whenEntryFor=function(t,e){var n=t[e];return n?(t[e]=this.createEntry(n,e),t):t},p.prototype.calcEntryObj=function(){var t=Object.assign({},this.whenEntryObj);return t=this.whenEntryFor(t,"then"),t=this.whenEntryFor(t,"otherwise")},h.entryObj.get=function(){return this.validateAndConfigure()&&this.calcEntryObj()},p.prototype.warn=function(t,e){console.error("[WhenEntry] WARNING",t,e)},p.prototype.error=function(t,e){throw console.error("[WhenEntry] ERROR",t,e),t},Object.defineProperties(p.prototype,h);function f(t){return t===Object(t)}var y=function(t){void 0===t&&(t={});var e=t.type,n=t.key,r=t.value,i=t.when,o=t.schema,s=t.properties,a=t.config;this.opts=t,this.when=i,this.key=n,this.type=e,this.value=r,this.schema=o,this.properties=s,this.config=a,this.validate()},l={constraintObj:{configurable:!0},keyVal:{configurable:!0},constraintValue:{configurable:!0},constraint:{configurable:!0}};y.prototype.validate=function(){"string"!=typeof this.type&&this.error("validate: invalid or mising type: "+this.type,this.opts),f(this.when)||this.error("validate: invalid or mising when: "+this.when,this.opts)},y.prototype.validateAndConfigure=function(t){if(!f(t=t||this.when))return this.warn("invalid or missing when constraint",t),!1;var e=Object.keys(t);return e.length<1?(this.warn("when constraint must have at least 1 key: "+e,t),!1):(this.whenKeys=e,!0)},y.prototype.createWhenEntry=function(t,e){return function(t,e){return void 0===e&&(e={}),new p(t,e)}(t,e)},y.prototype.accumulate=function(t,e){var n=this.when[e];if(!f(n))return this.warn("invalid when entry constraint object "+n+" for "+e),t;var r=this.createWhenEntry(n,{type:this.type,key:this.key,schema:this.schema,properties:this.properties,config:this.config}).entryObj;return r?t=Object.assign(t,r):t},l.constraintObj.get=function(){return this.whenKeys?this.whenKeys.reduce(this.accumulate.bind(this),{}):{}},l.keyVal.get=function(){var t=this.whenKeys||[];return 1===t.length?t[0]:t},l.constraintValue.get=function(){return!!this.keyVal&&[this.keyVal,this.constraintObj]},l.constraint.get=function(){return this.validateAndConfigure()&&this.constraintValue},y.prototype.warn=function(t,e){console.error("[WhenCondition] WARNING",t,e)},y.prototype.error=function(t,e){throw console.error("[WhenCondition] ERROR",t,e),t},Object.defineProperties(y.prototype,l);var m=function(t){function e(){t.apply(this,arguments)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e}(Error),d=["oneOf","enum","required","notRequired","minDate","min","maxDate","max","trim","lowercase","uppercase","email","url","minLength","maxLength","pattern","matches","regex","integer","positive","minimum","maximum"],g={errMessages:function(t){return void 0===t&&(t=d),t.reduce(function(t,e){return t[e]=function(t){var e=t.value;return t.key+": invalid for "+(e.name||e.title)},t},{})}};var v=function(t){function e(e){void 0===e&&(e={}),t.call(this,e.config);var n=e.schema,i=e.key,o=e.value,s=e.config;n=n||{},this.validateOnCreate(i,o,e),this.yup=r,this.key=i,this.schema=n,this.properties=n.properties||{},this.value=o,this.constraints=this.getConstraints(),this.format=o.format||this.constraints.format,this.config=s||{},this.type="mixed",this.base=r.mixed(),this.errMessages=s.errMessages||{},this.constraintsAdded={},this.rebind("addConstraint","addValueConstraint")}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={enabled:{configurable:!0},constraintsMap:{configurable:!0}};return e.prototype.rebind=function(){for(var t=this,e=[],n=arguments.length;n--;)e[n]=arguments[n];e.map(function(e){var n=t[e];t[e]=t.isFunctionType(n)?n.bind(t):n})},e.prototype.validateOnCreate=function(t,e,n){t||this.error("create: missing key "+JSON.stringify(n)),e||this.error("create: missing value "+JSON.stringify(n))},n.enabled.get=function(){},e.prototype.convertEnabled=function(){var t=this;this.enabled.map(function(e){t[e]&&t[e]()})},e.prototype.getConstraints=function(){return this.config.getConstraints(this.value)},e.prototype.createSchemaEntry=function(){return this.convert().base},e.prototype.convert=function(){return this.addMappedConstraints(),this.oneOf().notOneOf(),this.when(),this.nullable().isType(),this},e.prototype.addValueConstraint=function(t,e){return void 0===e&&(e={}),this.addConstraint(t,{constraintName:e.constraintName,value:!0,errName:e.errName})},e.prototype.buildConstraint=function(t,e){void 0===e&&(e={});var n=e.constraintName,r=e.propValue,i=e.method,o=e.yup,s=e.value,a=e.values,u=e.errName;if(o=o||this.base,!(r=r||this.constraints[t]))return this.warn("no prop value"),o;if(n=n||t,!o[i=i||n])return this.warn("Yup has no such API method: "+i),this;var c=o[i].bind(o),p=this.valErrMessage(n)||u&&this.valErrMessage(u);if(s){var h=!0===s?r:s;return this.onConstraintAdded({name:n,value:h}),h?c(h,p):c(p)}return a?Array.isArray(a)?(this.onConstraintAdded({name:n,value:a}),c.apply(void 0,a.concat([p]))):(this.warn("buildConstraint: values option must be an array of arguments"),o):(this.warn("buildConstraint: missing value or values options"),o)},e.prototype.addConstraint=function(t,e){var n=this.buildConstraint(t,e);return this.base=n||this.base,this},e.prototype.onConstraintAdded=function(t){return this.constraintsAdded[t.name]=t.value,this},e.prototype.addMappedConstraints=function(){var t=this,e=this.constraintsMap;return Object.keys(e).map(function(n){e[n].map(t["value"===n?"addValueConstraint":"addConstraint"])}),this},n.constraintsMap.get=function(){return{simple:["required","notRequired","nullable"],value:["default","strict"]}},e.prototype.oneOf=function(){var t=this.constraints.enum||this.constraints.oneOf||this.constraints.anyOf;return null===t?(this.error("oneOf","should not be null"),this):this.isNothing(t)?this:(t=Array.isArray(t)?t:[t],this.addConstraint("oneOf",{value:t,errName:"enum"}))},e.prototype.notOneOf=function(){var t=this.constraints,e=t.not,n=t.notOneOf,r=n||e&&(e.enum||e.oneOf);return r=Array.isArray(r)?r:[r],this.addConstraint("notOneOf",{value:r})},e.prototype.valErrMessage=function(t){var e=this.errMessages[this.key]?this.errMessages[this.key][t]:void 0;return"function"==typeof e?e(this.constraints):e},e.prototype.createWhenConditionFor=function(t){return function(t){return new y(t)}({key:this.key,type:this.type,value:this.value,schema:this.schema,properties:this.properties,config:this.config,when:t})},e.prototype.when=function(){var t,e=this.constraints.when||this.constraints.if;if((t=e)!==Object(t))return this;var n=this.createWhenConditionFor(e).constraint;return n?(this.logInfo("Adding when constraint for "+this.key,n),this.addConstraint("when",{values:n,errName:"when"}),this):(this.warn("Invalid when constraint for: "+e),this)},e.prototype.isType=function(){return this.addConstraint("isType",{value:this.constraints.isType,errName:"notOneOf"}),this},e.prototype.nullable=function(){var t=this.constraints,e=t.nullable;return this.addConstraint("nullable",{value:e||t.isNullable,errName:"notOneOf"}),this},e.prototype.$const=function(){return this},e.prototype.$allOf=function(){return this},e.prototype.$anyOf=function(){return this},e.prototype.$oneOf=function(){return this},e.prototype.$if=function(){return this},e.prototype.$then=function(){return this},e.prototype.$else=function(){return this},e.prototype.message=function(){return config.messages[this.key]||config.messages[this.type]||{}},e.prototype.errMessage=function(t){return void 0===t&&(t="default"),this.message[t]||"error"},e.prototype.toValidJSONSchema=function(){},e.prototype.normalize=function(){},e.prototype.deNormalize=function(){},e.prototype.errorMsg=function(t){this.throwError(t)},e.prototype.error=function(t,e){var n=["["+t+"]",e].join(" ");this.errorMsg(n)},e.prototype.throwError=function(t){throw t},Object.defineProperties(e.prototype,n),e}(u),b=function(t){function e(e){t.call(this,e)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.isArray=function(t){return this.config.isArray||this.error("ArrayHandler: mising isArray in config",this.config),this.config.isArray(t)},e.prototype.handle=function(t){return this.isArray(t)&&w.create(t).createSchemaEntry()},e}(u);function O(t,e){return void 0===e&&(e={}),t&&new b(e).handle(t)}var w=function(t){function e(e){t.call(this,e),this.type="array",this.base=this.yup.array(),this.createYupSchemaEntry=this.config.createYupSchemaEntry}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.create=function(t){return new e(t)},e.prototype.convert=function(){return this.maxItems(),this.minItems(),this.ensureItems(),this.compact(),this.itemsOf(),t.prototype.convert.call(this),this},e.prototype.ensureItems=function(){return this.addConstraint("ensure")},e.prototype.compact=function(){return this.addConstraint("compact")},e.prototype.itemsOf=function(){var t=this.constraints,e=t.itemsOf,n=t.items||e||this.constraints.of;if(!this.isNothing(n))if(Array.isArray(n))this.error("itemsOf","does not (yet) support an Array of schemas");else if(this.isObjectType(n)){if(this.createYupSchemaEntry){try{var r=this.createYupSchemaEntry({key:this.key,value:n,config:this.config});return this.addConstraint("of",{value:r,propValue:n})}catch(t){this.error("itemsOf: Error",t)}return this}this.warn("missing createYupSchemaEntry in config, needed for recursive validation")}else this.error("itemsOf","must be a schema object, was "+typeof n)},e.prototype.maxItems=function(){var t=this.constraints,e=t.maxItems,n=e||t.max;if(!this.isNumberType(n))return this;if(!this.isValidSize(n))return this.handleInvalidSize("maxItems",n);var r=n&&this.base.max(n);return this.base=r||this.base,this},e.prototype.minItems=function(){var t=this.constraints,e=t.minItems,n=e||t.min;if(!this.isNumberType(n))return this;if(!this.isValidSize(n))return this.handleInvalidSize("minItems",n);var r=n&&this.base.min(n);return this.base=r||this.base,this},e.prototype.$items=function(){return this},e.prototype.$additionalItems=function(){return this},e.prototype.$uniqueItems=function(){return this},e.prototype.$contains=function(){return this},e.prototype.handleInvalidSize=function(t,e){var n="invalid array size constraint for "+t+", was "+e+". Must be a number >= 0";return this.config.warnOnInvalid?(this.warn(n),this):(this.error(n,e),this)},e.prototype.isValidSize=function(t){return this.isNumberType(t)&&t>=0},e}(v),j=function(t){this.config=t};function C(t,e){return void 0===e&&(e={}),t&&new j(e).handle(t)}j.prototype.isBoolean=function(t){return this.config.isBoolean(t)},j.prototype.handle=function(t){return this.isBoolean(t)&&N.create(t).createSchemaEntry()};var N=function(t){function e(e){t.call(this,e),this.type="boolean",this.base=this.yup.boolean()}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.create=function(t){return new e(t)},e}(v);var E=function(t){function e(e){t.call(this,e)}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={$map:{configurable:!0}};return n.$map.get=function(){return{moreThan:["exclusiveMinimum","moreThan"],lessThan:["exclusiveMaximum","lessThan"],max:["maximum","max"],min:["minimum","min"]}},Object.defineProperties(e.prototype,n),e}(function(t){function e(e){t.call(this,e)}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={explainConstraintValidMsg:{configurable:!0}};return e.prototype.transform=function(t){return this.typer.toNumber(t)},e.prototype.isValidConstraint=function(t){return this.typer.isNumberLike(t)},n.explainConstraintValidMsg.get=function(){return"Must be a number or convertible to a number"},Object.defineProperties(e.prototype,n),e}(function(t){function e(e,n){var r=this;t.call(this,e.config),this.map=n||this.$map||{},this.typer=e,this.delegates.map(function(t){var n=e[t];n||r.error("missing delegate: "+t,{typer:e}),r[t]=r.isFunctionType(n)?n.bind(e):n})}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={delegates:{configurable:!0},explainConstraintValidMsg:{configurable:!0}};return e.prototype.isStringType=function(t){return"string"==typeof t},n.delegates.get=function(){return["constraints","addConstraint","constraintsAdded"]},e.prototype.add=function(){var t=this,e=this.map;Object.keys(e).map(function(n){var r=t.entryNames(e[n]);t.addConstraints(n,r)})},e.prototype.entryNames=function(t){return Array.isArray(t)?t:[t]},e.prototype.addConstraints=function(t,e){var n=this;return void 0===e&&(e=[]),e.map(function(e){var r=n.validateAndTransform(e);n.addConstraint(e,{method:t,value:r})}),this},e.prototype.validateAndTransform=function(t){var e=this.constraints[t];return this.validate(e),this.transform(e)},e.prototype.invalidMsg=function(t,e){return"invalid constraint for "+t+", was "+e+"."},n.explainConstraintValidMsg.get=function(){return""},e.prototype.invalidConstraintMsg=function(t,e){return[this.invalidMsg(t,e),this.explainConstraintValidMsg].join("\n")},e.prototype.validate=function(t){return this.isNothing(t)?this:this.isValidConstraint(t)?void 0:this.handleInvalidConstraint(name,t)},e.prototype.isValidConstraint=function(t){return!0},e.prototype.handleInvalidConstraint=function(t,e){var n=this.invalidConstraintMsg(t,e);return this.config.warnOnInvalid?(this.warn(n),this):(this.error(n,e),this)},Object.defineProperties(e.prototype,n),e}(a))),_=function(t){function e(e,n){t.call(this,e,n)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.isValid=function(){return this.config.isNumber(this.obj)},e}(function(t){function e(e,n){t.call(this,n),this.obj=e}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.isValid=function(){return!1},e.prototype.verify=function(){return this.isPresent(this.obj)&&this.isValid(this.obj)},e}(u));var x=function(t,e){return void 0===e&&(e={}),function(t,e){return new _(t,e)}(t,e).verify()};function S(t,e){return void 0===e&&(e={}),x(t,e)&&function(t){return k.schemaEntryFor(t)}(t)}var k=function(t){function e(e){t.call(this,e),this.type=this.normalizeNumType(e.type),this.base=this.yup.number(),this.range=new E(this)}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={enabled:{configurable:!0},isInteger:{configurable:!0},isNegative:{configurable:!0},isPositive:{configurable:!0}};return e.prototype.normalizeNumType=function(t){return"int"===t?"integer":t},e.create=function(t){return new e(t)},e.schemaEntryFor=function(t){return e.create(t).createSchemaEntry()},n.enabled.get=function(){return["range","posNeg","integer"]},e.prototype.convert=function(){var e=this;return this.enabled.map(function(t){return e.processConstraint(t)}),t.prototype.convert.call(this),this},e.prototype.processConstraint=function(t){var e=this[t];e&&"function"==typeof e?e.bind(this)():e.add()},e.prototype.truncate=function(){return this.addConstraint("truncate")},e.prototype.round=function(){var t=this.constraints.round;if(this.isNothing(t))return this;var e=this.isStringType(t)?t:"round";return t&&this.base.round(e),this},e.prototype.posNeg=function(){this.positive(),this.negative()},e.prototype.integer=function(){return this.isInteger&&this.addConstraint("integer"),this},n.isInteger.get=function(){return this.config.isInteger(this.type)},e.prototype.positive=function(){return this.addConstraint("positive")},e.prototype.negative=function(){return this.addConstraint("negative")},n.isNegative.get=function(){var t=this.constraints,e=t.exclusiveMaximum;return!!t.negative||void 0!==e&&0===e},n.isPositive.get=function(){var t=this.constraints,e=t.exclusiveMinimum;return!!t.positive||void 0!==e&&0===e},e.prototype.normalize=function(){this.constraints.maximum=this.constraints.maximum||this.constraints.max,this.constraints.minimum=this.constraints.minimum||this.constraints.min},Object.defineProperties(e.prototype,n),e}(v),T=function(t){this.config=t};function M(t,e){return void 0===e&&(e={}),t&&new T(e).handle(t)}T.prototype.isObject=function(t){return this.config.isObject(t)},T.prototype.handle=function(t){return this.isObject(t)&&A.create(t).createSchemaEntry()};var A=function(t){function e(e){t.call(this,e),this.type="object",this.base=this.yup.object(),this.properties=this.value.properties}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.create=function(t){return new e(t)},e.prototype.convert=function(){if(!this.properties)return this;if(this.noUnknown(),this.camelCase().constantCase(),this.value){var t=this.config.buildYup(this.value);this.base.shape(t)}return this},e.prototype.camelCase=function(){return this.addConstraint("camelCase")},e.prototype.constantCase=function(){return this.addConstraint("constantCase")},e.prototype.noUnknown=function(){var t=this.value,e=t.noUnknown,n=e||t.propertyNames,r=n&&this.base.noUnknown(n,this.valErrMessage("propertyNames")||this.valErrMessage("noUnknown"));return this.base=r||this.base,this},e}(v),I=function(t){this.config=t};function D(t,e){return void 0===e&&(e={}),t&&new I(e).handle(t)}I.prototype.isString=function(t){return this.config.isString(t)},I.prototype.handle=function(t){return this.isString(t)&&V.create(Object.assign({},{config:this.config},t)).createSchemaEntry()};var V=function(t){function e(e){t.call(this,e),this.type="string",this.base=this.yup.string()}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={isEmail:{configurable:!0},isUrl:{configurable:!0}};return e.create=function(t){return new e(t)},e.prototype.convert=function(){return t.prototype.convert.call(this),this.normalize(),this.minLength().maxLength().pattern(),this.lowercase().uppercase(),this.email(),this.url(),this.genericFormat(),this},e.prototype.trim=function(){return this.addConstraint("trim")},e.prototype.lowercase=function(){return this.addConstraint("lowercase")},e.prototype.uppercase=function(){return this.addConstraint("uppercase")},e.prototype.genericFormat=function(){!0!=!this.config.format&&(this.yup.prototype[this.format]&&this.addConstraint(this.format))},e.prototype.email=function(){return this.isEmail&&this.addConstraint("email"),this},n.isEmail.get=function(){return this.constraints.email||"email"===this.format},e.prototype.url=function(){return this.isUrl&&this.addConstraint("url"),this},n.isUrl.get=function(){return this.constraints.url||"url"===this.format},e.prototype.minLength=function(){var t=this.constraints.minLength,e=this.valErrMessage("minLength")||this.valErrMessage("min"),n=t&&this.base.min(t,e);return this.base=n||this.base,this},e.prototype.maxLength=function(){var t=this.constraints.maxLength,e=this.valErrMessage("maxLength")||this.valErrMessage("max"),n=t&&this.base.max(t,e);return this.base=n||this.base,this},e.prototype.pattern=function(){var t=this.constraints.pattern;if(!t)return this;var e=new RegExp(t),n=this.valErrMessage("pattern")||this.valErrMessage("matches")||this.valErrMessage("regex"),r=e&&this.base.matches(e,n);return this.base=r||this.base,this},e.prototype.normalize=function(){this.constraints.pattern=this.constraints.pattern||this.constraints.matches||this.constraints.regex,this.constraints.maxLength=this.constraints.maxLength||this.constraints.max,this.constraints.minLength=this.constraints.minLength||this.constraints.min},Object.defineProperties(e.prototype,n),e}(v),Y=function(t){this.config=t};function R(t,e){return void 0===e&&(e={}),t&&new Y(e).handle(t)}Y.prototype.isDate=function(t){return this.config.isDate(t)},Y.prototype.handle=function(t){return this.isDate(t)&&q.create(t).createSchemaEntry()};var q=function(t){function e(e){t.call(this,e),this.type="date",this.base=this.yup.date()}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.create=function(t){return new e(t)},e.prototype.convert=function(){return this.minDate().maxDate(),t.prototype.convert.call(this),this},e.prototype.toDate=function(t){return new Date(t)},e.prototype.isValidDateType=function(t){return this.isStringType(t)||this.isDateType(t)},e.prototype.isValidDate=function(t){return!!this.isValidDateType(t)&&(!this.isStringType(t)||Boolean(Date.parse(t)))},e.prototype.transformToDate=function(t){return this.isNumberType(t)?new Date(t):t},e.prototype.minDate=function(){var t=this.constraints.minDate||this.constraints.min;if(this.isNothing(t))return this;var e=this.transformToDate(t);if(!this.isValidDateType(e))return this.handleInvalidDate("minDate",e);var n=e&&this.base.min(this.toDate(e),this.valErrMessage("minDate")||this.valErrMessage("min"));return this.base=n||this.base,this},e.prototype.maxDate=function(){var t=this.constraints.maxDate||this.constraints.max;if(this.isNothing(t))return this;var e=this.transformToDate(t);if(!this.isValidDateType(e))return this.handleInvalidDate("maxDate",e);var n=e&&this.base.max(this.toDate(e),this.valErrMessage("maxDate")||this.valErrMessage("max"));return this.base=n||this.base,this},e.prototype.handleInvalidDate=function(t,e){var n="invalid constraint for "+t+", was "+e+". Must be a number, string (valid date format) or a Date instance";return this.config.warnOnInvalid?(this.warn(n),this):(this.error(n,e),this)},e}(v),P={errValKeys:d,defaults:g,YupArray:w,toYupArray:O,YupBoolean:N,toYupBoolean:C,YupNumber:k,toYupNumberSchemaEntry:S,toYupNumber:function(t,e){return void 0===e&&(e={}),x(t,e)&&function(t){return k.create(t)}(t)},YupObject:A,toYupObject:M,YupString:V,toYupString:D,YupDate:q,toYupDate:R,YupMixed:v,ConvertYupSchemaError:m,Base:u},z=function(t){function e(){t.apply(this,arguments)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e}(Error),L=function(t){function e(e){var n=e.schema,r=e.name,i=e.key,o=e.value,s=e.config;t.call(this,s),this.schema=n,this.key=i,this.value=o,this.config=s,this.name=r,this.type=o.type,this.types={string:D,number:S,boolean:C,array:O,object:M,date:R}}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={obj:{configurable:!0}};return e.prototype.isValidSchema=function(){return"string"==typeof this.type},e.prototype.error=function(t){throw new z(t)},e.prototype.toEntry=function(){if(!this.isValidSchema()){var t=JSON.stringify(this.schema);this.error("Not a valid schema: type "+this.type+" must be a string, was "+typeof this.type+" "+t)}var e=this.obj;return this.string(e)||this.number(e)||this.boolean(e)||this.array(e)||this.object(e)||this.date(e)||this.defaultType(e)},e.prototype.defaultType=function(t){console.error({type:t.type}),this.error("toEntry: unknown type",t)},n.obj.get=function(){return{schema:this.schema,key:this.key,value:this.value,type:this.type,config:this.config}},e.prototype.string=function(t){return D(t||this.obj,this.config)},e.prototype.number=function(t){return S(t||this.obj,this.config)},e.prototype.boolean=function(t){return C(t||this.obj,this.config)},e.prototype.array=function(t){return O(t||this.obj,this.config)},e.prototype.object=function(t){return M(t||this.obj,this.config)},e.prototype.date=function(t){return R(t||this.obj,this.config)},Object.defineProperties(e.prototype,n),e}(u);function $(t){void 0===t&&(t={});return new L(t).toEntry()}var F=function(t,e){return void 0===e&&(e={}),t.reduce(function(t,n){if("string"!=typeof n&&!(n instanceof Object)){if(!1!==e.throws)throw"toConstraintsMap: invalid entry "+n;return t}if("string"==typeof n)t[name]={};else{if(!n.name){if(!1!==e.throws)throw"toConstraintsMap: invalid entry "+n+" missing name";return t}t[n.name]=n}return t},{})},U={alphanumeric:{optsKey:"locale"},alpha:{optsKey:"locale"},ascii:{},byte:{},creditCard:{},currency:{opts:"currencyOpts"},dataUri:{},dateTime:{},date:{},domainName:{opts:"domainOpts"},hash:{opts:"hashAlgo"},hexColor:{},ipv4:{},ipv6:{},isbn:{},magnetUri:{},mimeType:{},mobilePhone:{},mongoId:{},postalCode:{},uuid:{}},B={createValidatorName:function(t,e){return"is"+(t=(t=(t=n(t||e)).replace(/Uri$/,"URI")).replace(/Id$/,"ID"))},createTestName:function(t,n){return e(t||n)}},K={isMagnetURI:function(t,e){return/magnet:\?xt=urn:[a-z0-9]+:[a-z0-9]{32}/i.test(t)}};function W(t,e){return void 0===e&&(e={}),new G(t,e).yupSchema}function J(t){return t===Object(t)}var G=function(t){function e(e,n){void 0===n&&(n={}),t.call(this,n),n.buildYup=W,n.createYupSchemaEntry=n.createYupSchemaEntry||$,this.config=Object.assign(this.config,n),this.schema=e;var r=this.getType(e),i=this.getProps(e);if(this.type=r,this.properties=i,this.required=this.getRequired(e),function(t){return t&&"object"===t}(r))if(J(i)){var o=this.getName(e),s=this.normalizeRequired(e),a=this.propsToShape({properties:s,name:o,config:n});this.shapeConfig=a,this.validSchema=!0}else{var u=JSON.stringify(s);this.error("invalid schema: must have a properties object: "+u)}else this.error('invalid schema: must be type: "object", was type: '+r)}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={yupSchema:{configurable:!0}};return e.prototype.getRequired=function(t){var e=this.config.getRequired;return e?e(t):t.required||[]},e.prototype.getProps=function(t){return this.config.getProps(t)},e.prototype.getType=function(t){return this.config.getType(t)},e.prototype.getName=function(t){return this.config.getName(t)},n.yupSchema.get=function(){return r.object().shape(this.shapeConfig)},e.prototype.normalizeRequired=function(){var t=this,e=Object.assign({},this.properties),n=[].concat(this.required)||[];return Object.keys(e).reduce(function(r,i){var o=e[i],s=n.indexOf(i)>=0;return J(o)?o.required=t.isRequired(o)||s:t.warn("Bad value: "+o+" must be an object"),r[i]=o,r},{})},e.prototype.isRequired=function(t){return this.config.isRequired(t)},e.prototype.propsToShape=function(t){var e=this,n=t.name,r=Object.assign({},this.properties);return Object.keys(r).reduce(function(t,i){var o=e.propToYupSchemaEntry({name:n,key:i,value:r[i]});return e.logInfo("propsToShape",{key:i,yupSchemaEntry:o}),t[i]=o,t},{})},e.prototype.propToYupSchemaEntry=function(t){var e=t.value;return void 0===e&&(e={}),(this.createYupSchemaEntry||this.config.createYupSchemaEntry)({schema:this.schema,name:t.name,key:t.key,value:e,config:this.config})},e.prototype.createYupSchemaEntry=function(t){var e=t.config;return e.createYupSchemaEntry({schema:t.schema,name:t.name,key:t.key,value:t.value,config:e})},Object.defineProperties(e.prototype,n),e}(u);exports.buildYup=W,exports.YupBuilder=G,exports.YupSchemaEntry=L,exports.YupSchemaEntryError=z,exports.types=P,exports.createYupSchemaEntry=$,exports.extendYupApi=function(t){var e=this;void 0===t&&(t={});var n=t.constraints,i=t.override;void 0===i&&(i=!1);var o=t.validator,s=t.createValidatorName,a=t.createTestName;if(!o)throw"extendYupApi: missing validator option";Array.isArray(n)&&(n=F(n)),n=i?n||U:Object.assign({},U,n||{}),s=s||B.createValidatorName,a=a||B.createTestName,Object.keys(n).map(function(t){var i=n[t],u=i.testName,c=i.optsKey,p=i.logging,h=s(i.validatorName,t);u=a(u,t),r.addMethod(r.string,t,function(n){void 0===n&&(n={});var i=n.message,s=n[c];return r.string().test(u,i,function(n){var r=e.path,a=e.createError,c=o[h];if("function"!=typeof(c=c||K[h]))throw Error("No method named ${validatorName} on validator");var f=c(n,s);return!0===p&&console.log("Yup validator bridge",{key:t,fullValidatorName:h,testName:u,value:n,valid:f}),f||a({path:r,message:i})})})})}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "schema-to-yup", | ||
"version": "1.9.0", | ||
"version": "1.9.1", | ||
"source": "src/index.js", | ||
"main": "dist/index.js", | ||
"description": "Build a Yup schema from a JSON Schema. Also supports custom/alternative schema models such as GraphQL type defs", | ||
"description": "Build a Yup schema object to validate models from a domain model schema (JSON or GraphQL)", | ||
"license": "MIT", | ||
@@ -53,2 +53,2 @@ "author": "Kristian Mandrup <kmandrup@gmail.com>", | ||
} | ||
} | ||
} |
@@ -109,4 +109,7 @@ # Schema to Yup schema | ||
- `notRequired` | ||
- `oneOf` (`enum`) | ||
- `oneOf` (`enum`, `anyOf`) | ||
- `notOneOf` | ||
- `when` (`if`) _NEW_ | ||
- `isType` _NEW_ | ||
- `nullable` (`isNullable`) _NEW_ | ||
@@ -120,2 +123,3 @@ ### Array | ||
- `minItems` (`min`) | ||
- `itemsOf` (`of`) _NEW_ | ||
@@ -160,10 +164,58 @@ ### Boolean | ||
## Conditional logic support | ||
## Conditional logic | ||
Basic support for [when conditions](https://github.com/jquense/yup#mixedwhenkeys-string--arraystring-builder-object--value-schema-schema-schema) as requested and outlined in [this issue](https://github.com/kristianmandrup/schema-to-yup/issues/14) are now included in the [when-condition](https://github.com/kristianmandrup/schema-to-yup/tree/when-condition) branch. | ||
Basic support for [when conditions](https://github.com/jquense/yup#mixedwhenkeys-string--arraystring-builder-object--value-schema-schema-schema) as requested and outlined in [this issue](https://github.com/kristianmandrup/schema-to-yup/issues/14) is now included. | ||
This will be merged in the next major release, most likely 2.0. | ||
Work will continue in the [when-condition](https://github.com/kristianmandrup/schema-to-yup/tree/when-condition) branch. | ||
You are most welcome to continue the effort to support more conditional schema logic by continuing on this branch and making PRs. | ||
Sample schema using simple `when` constraint: | ||
```js | ||
const biggyJson = { | ||
title: "biggy", | ||
type: "object", | ||
properties: { | ||
isBig: { | ||
type: "boolean" | ||
}, | ||
count: { | ||
type: "number", | ||
when: { | ||
isBig: { | ||
is: true, | ||
then: { | ||
min: 5 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
``` | ||
Sample valid and invalid values with respect to `biggyJson` schema | ||
```js | ||
const bigJson = { | ||
valid: { | ||
isBig: true, | ||
count: 5 // since isBig is set, must be >= 5 | ||
}, | ||
invalid: { | ||
isBig: true, | ||
count: 4 // since isBig is set, must be >= 5 | ||
} | ||
}; | ||
``` | ||
Currently basic support is included in `schema-to-yup@1.9.0` on [npmjs](https://www.npmjs.com) | ||
More advanced conditionals support will likely be included the next major release: `2.0`. | ||
You are welcome to continue the effort to support more conditional schema logic by continuing on this branch and making PRs. | ||
Support for `if` `then` and `else` constraints in JSON schema can be added using the `when` condition (ie. transalating to equivalent: `when`, `then` and `otherwise`). | ||
Likewise, `not` can be implemented via a single `notOneOf`. | ||
## Complex example | ||
@@ -170,0 +222,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { Loggable } from './_loggable'; | ||
import { Loggable } from "./_loggable"; | ||
@@ -24,2 +24,6 @@ class TypeMatcher extends Loggable { | ||
isObjectType(obj) { | ||
return obj === Object(obj); | ||
} | ||
isNumberType(num) { | ||
@@ -42,4 +46,2 @@ return !isNaN(num); | ||
export { | ||
TypeMatcher | ||
}; | ||
export { TypeMatcher }; |
// See: | ||
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.4 | ||
import { YupMixed } from './mixed'; | ||
import { Base } from './base'; | ||
import { YupMixed } from "./mixed"; | ||
import { Base } from "./base"; | ||
@@ -50,3 +50,3 @@ class ArrayHandler extends Base { | ||
// this.itemsOf() | ||
this.itemsOf(); | ||
@@ -65,26 +65,42 @@ super.convert(); | ||
// TODO: not yet implemented | ||
itemsOf() { | ||
return this; | ||
// const { items, itemsOf } = this.constraints; | ||
// const $itemsOfSchema = items || itemsOf || this.constraints.of; | ||
const { items, itemsOf } = this.constraints; | ||
const $of = items || itemsOf || this.constraints.of; | ||
// if (Array.isArray($itemsOfSchema)) { | ||
// this.error("itemsOf", "does not (yet) support an Array of schemas"); | ||
// } | ||
if (this.isNothing($of)) return; | ||
// if (!this.createYupSchemaEntry) { | ||
// this.warn( | ||
// "missing createYupSchemaEntry in config, needed for recursive validation" | ||
// ); | ||
// return; | ||
// } | ||
// this.createYupSchemaEntry({ | ||
// key: this.key, | ||
// value: $itemsOfSchema, | ||
// type: this.type, | ||
// config: this.config | ||
// }); | ||
// $of && this.base.of($max); | ||
// return this; | ||
if (Array.isArray($of)) { | ||
this.error("itemsOf", "does not (yet) support an Array of schemas"); | ||
return; | ||
} | ||
if (!this.isObjectType($of)) { | ||
this.error("itemsOf", `must be a schema object, was ${typeof $of}`); | ||
return; | ||
} | ||
if (!this.createYupSchemaEntry) { | ||
this.warn( | ||
"missing createYupSchemaEntry in config, needed for recursive validation" | ||
); | ||
return; | ||
} | ||
try { | ||
const schemaConf = { | ||
key: this.key, | ||
value: $of, | ||
config: this.config | ||
}; | ||
const schemaEntry = this.createYupSchemaEntry(schemaConf); | ||
return this.addConstraint("of", { | ||
value: schemaEntry, | ||
propValue: $of | ||
}); | ||
} catch (ex) { | ||
this.error("itemsOf: Error", ex); | ||
} | ||
return this; | ||
} | ||
@@ -153,6 +169,2 @@ | ||
export { | ||
toYupArray, | ||
YupArray, | ||
ArrayHandler | ||
}; | ||
export { toYupArray, YupArray, ArrayHandler }; |
@@ -52,3 +52,4 @@ import * as yup from "yup"; | ||
super(opts.config); | ||
const { schema, key, value, config } = opts; | ||
let { schema, key, value, config } = opts; | ||
schema = schema || {}; | ||
this.validateOnCreate(key, value, opts); | ||
@@ -58,3 +59,3 @@ this.yup = yup; | ||
this.schema = schema; | ||
this.properties = schema.properties; | ||
this.properties = schema.properties || {}; | ||
this.value = value; | ||
@@ -114,2 +115,3 @@ this.constraints = this.getConstraints(); | ||
this.when(); | ||
this.nullable().isType(); | ||
return this; | ||
@@ -127,6 +129,16 @@ } | ||
buildConstraint(propName, opts = {}) { | ||
let { constraintName, method, yup, value, values, errName } = opts; | ||
let { | ||
constraintName, | ||
propValue, | ||
method, | ||
yup, | ||
value, | ||
values, | ||
errName | ||
} = opts; | ||
yup = yup || this.base; | ||
const propValue = this.constraints[propName]; | ||
propValue = propValue || this.constraints[propName]; | ||
if (!propValue) { | ||
this.warn("no prop value"); | ||
return yup; | ||
@@ -136,2 +148,3 @@ } | ||
method = method || constraintName; | ||
if (!yup[method]) { | ||
@@ -141,2 +154,3 @@ this.warn(`Yup has no such API method: ${method}`); | ||
} | ||
const constraintFn = yup[method].bind(yup); | ||
@@ -170,4 +184,2 @@ const errFn = | ||
// console.log("constraint", { method, values }); | ||
const newBase = constraintFn(...values, errFn); | ||
@@ -210,3 +222,10 @@ return newBase; | ||
oneOf() { | ||
const value = this.constraints.enum || this.constraints.oneOf; | ||
let value = | ||
this.constraints.enum || this.constraints.oneOf || this.constraints.anyOf; | ||
if (value === null) { | ||
this.error("oneOf", "should not be null"); | ||
return this; | ||
} | ||
if (this.isNothing(value)) return this; | ||
value = Array.isArray(value) ? value : [value]; | ||
return this.addConstraint("oneOf", { value, errName: "enum" }); | ||
@@ -217,3 +236,4 @@ } | ||
const { not, notOneOf } = this.constraints; | ||
const value = notOneOf || (not && (not.enum || not.oneOf)); | ||
let value = notOneOf || (not && (not.enum || not.oneOf)); | ||
value = Array.isArray(value) ? value : [value]; | ||
return this.addConstraint("notOneOf", { value }); | ||
@@ -243,3 +263,3 @@ } | ||
when() { | ||
const when = this.constraints.when; | ||
const when = this.constraints.when || this.constraints.if; | ||
if (!isObjectType(when)) return this; | ||
@@ -260,2 +280,15 @@ const { constraint } = this.createWhenConditionFor(when); | ||
isType() { | ||
const value = this.constraints.isType; | ||
this.addConstraint("isType", { value, errName: "notOneOf" }); | ||
return this; | ||
} | ||
nullable() { | ||
const { nullable, isNullable } = this.constraints; | ||
const value = nullable || isNullable; | ||
this.addConstraint("nullable", { value, errName: "notOneOf" }); | ||
return this; | ||
} | ||
$const() { | ||
@@ -280,7 +313,2 @@ return this; | ||
// conditions https://ajv.js.org/keywords.html#not | ||
$not() { | ||
return this; | ||
} | ||
$if() { | ||
@@ -287,0 +315,0 @@ return this; |
@@ -1,4 +0,4 @@ | ||
import { YupMixed } from '../mixed'; | ||
import { createRangeConstraint, RangeConstraint } from './range-constraint'; | ||
import { createNumberGuard, NumberGuard } from './guard'; | ||
import { YupMixed } from "../mixed"; | ||
import { createRangeConstraint, RangeConstraint } from "./range-constraint"; | ||
import { createNumberGuard, NumberGuard } from "./guard"; | ||
@@ -5,0 +5,0 @@ const proceed = (obj, config = {}) => { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
177149
2039
740