schema-to-yup
Advanced tools
Comparing version 1.11.2 to 1.11.4
# Changelog | ||
# 1.11.4 | ||
Added new `refValue` field to allow validating via previous property value | ||
## 1.11.3 | ||
Fixed bug when constraint value is 0 (such as in Person schema example!) | ||
Added better and more detailed logging and logging options | ||
## 1.10.4 | ||
@@ -4,0 +13,0 @@ |
@@ -1,2 +0,2 @@ | ||
var t=require("yup"),e=require("uniq"),n=require("dashify"),r=require("uppercamelcase");function i(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}function s(t){if(t&&t.__esModule)return t;var e=Object.create(null);return t&&Object.keys(t).forEach(function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,r.get?r:{enumerable:!0,get:function(){return t[n]}})}}),e.default=t,e}var a=/*#__PURE__*/s(t),o=/*#__PURE__*/i(e),u=/*#__PURE__*/i(n),c=/*#__PURE__*/i(r);function h(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function f(t,e,n){return e&&h(t.prototype,e),n&&h(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t}function l(){return l=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t},l.apply(this,arguments)}function p(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,y(t,e)}function d(t){return d=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)},d(t)}function y(t,e){return y=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},y(t,e)}function v(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch(t){return!1}}function m(t,e,n){return m=v()?Reflect.construct:function(t,e,n){var r=[null];r.push.apply(r,e);var i=new(Function.bind.apply(t,r));return n&&y(i,n.prototype),i},m.apply(null,arguments)}function g(t){var e="function"==typeof Map?new Map:void 0;return g=function(t){if(null===t||-1===Function.toString.call(t).indexOf("[native code]"))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,n)}function n(){return m(t,arguments,d(this).constructor)}return n.prototype=Object.create(t.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),y(n,t)},g(t)}var b={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&&b.hasDateFormat(t.format)},isNumber:function(t){return t&&("number"===t.type||b.isInteger(t))},isObject:function(t){return t&&"object"===t.type},isRequired:function(t){return t&&t.required}},w={getProps:function(t){return t.properties},getType:function(t){return t.type},getName:function(t){return t.name||t.title},getConstraints:function(t){return t},isString:function(t){return"string"===t.type&&!w.hasDateFormat(t)},isArray:function(t){return"array"===t.type},isBoolean:function(t){return"boolean"===t.type},isInteger:function(t){return"integer"===t.type},hasDateFormat:function(t){var e;return["date","date-time"].includes(null==t||null==(e=t.value)?void 0:e.format)},isDate:function(t){return"string"===t.type&&w.hasDateFormat(t)},isNumber:function(t){return"number"===t.type||w.isInteger(t)},isObject:function(t){return"object"===t.type},isRequired:function(t){return t.required}},k={getProps:function(t){return t&&t.fields},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&&"int"===t.type},isFloat:function(t){return t&&"float"===t.type},isDouble:function(t){return t&&"double"===t.type},isBoolean:function(t){return t&&"boolean"===t.type},isDate:function(t){return t&&"int"===t.type&&"date"===t.logicalType},isNumber:function(t){return t&&(k.isInteger(t)||k.isFloat(t)||k.isDouble(t))},isObject:function(t){return t&&"record"===t.type},isRequired:function(t){return t&&t.required}},C={"json-schema":b,"type-def":w,avro:k},E=/*#__PURE__*/function(t){function e(e){return void 0===e&&(e={}),t.call(this,e)||this}p(e,t);var n=e.prototype;return n.isNothing=function(t){return null==t},n.isPresent=function(t){return!this.isNothing(t)},n.toNumber=function(t){return Number(t)},n.isNumberLike=function(t){return!isNaN(this.toNumber(t))},n.isObjectType=function(t){return t===Object(t)},n.isArrayType=function(t){return Array.isArray(t)},n.isNumberType=function(t){return!isNaN(t)},n.isStringType=function(t){return"string"==typeof t},n.isFunctionType=function(t){return"function"==typeof t},n.isDateType=function(t){return t instanceof Date},e}(/*#__PURE__*/function(){function t(t){void 0===t&&(t={}),this.config=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}var e=t.prototype;return e.error=function(t,e){if(!1!==this.enable.error)throw this.err&&(e?this.err(t,e):this.err(t)),t},e.warn=function(t,e){this.enable.warn&&this.logInfo("WARNING: "+t,e)},e.logInfo=function(t,e){this.enable.log&&this.log&&(e?this.log(t,e):this.log(t))},t}()),O=/*#__PURE__*/function(){function t(t,e){this.schemaParserMap=t,this.name=e}var e=t.prototype;return e.lookup=function(t){return void 0===t&&(t=this.name),this.schemaParserMap[t]},e.build=function(){var t=this.lookup();if(t.extends){var e=this.extend(t.extends);if(e)return e}return t},e.extend=function(t){var e=this.lookup(t);e&&(entry=l({},e,this.lookup()))},t}(),T=/*#__PURE__*/function(t){function e(e){var n;void 0===e&&(e={}),n=t.call(this,e)||this;var r=(e.createSchemaParserBuilder||new O(e.schemaParserMap||C,e.schemaType||"json-schema")).build();return n.config=l({},r,e),n}return p(e,t),e.prototype.createSchemaParserBuilder=function(t,e){return new SchemaParserBuilder(t,e)},e}(E);function M(t){return t===Object(t)}var N=/*#__PURE__*/function(){function t(t,e){void 0===e&&(e={}),this.whenEntryObj=t;var n=e.properties,r=e.config,i=e.key,s=e.when,a=e.type;this.schema=e.schema,this.when=s,this.properties=n||{},this.key=i,this.type=a,this.config=r}var e=t.prototype;return e.validateAndConfigure=function(t){if(!M(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' or 'else' constraint: "+e,t),!1):(this.warn("validateAndConfigure: when entry constraint missing 'is' constraint: "+e,t),!1)},e.createYupSchemaEntry=function(t){return this.config.createYupSchemaEntry(t)},e.createValue=function(t,e){var n;return"string"==typeof t&&((n={})[t]=!0,t=n),M(t)||this.error("createValue: "+e+" must be a schema object"),l({key:this.key,type:this.type},t)},e.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}},e.createEntry=function(t,e){var n=this.createEntryOpts(t,e);return this.createYupSchemaEntry(n)},e.hasKey=function(t,e){return t.find(function(t){return t===e})},e.hasAnyKey=function(t,e){return t.find(function(t){return e.includes(t)})},e.whenEntryFor=function(t,e,n){var r;if(n=n||e,"string"==typeof t&&((r={})[t]=!0,t=r),!M(t))throw"whenEntryFor: Invalid when object "+t;var i=l({},t[n]);return delete t[n],i?(t[e]=this.createEntry(i,e),t):t},e.calcEntryObj=function(){var t=l({},this.whenEntryObj);return"otherwise"in(t=this.whenEntryFor(t,"then"))&&(t=this.whenEntryFor(t,"otherwise")),"else"in t&&(t=this.whenEntryFor(t,"else")),t},e.warn=function(t,e){console.error("[WhenEntry] WARNING",t,e)},e.error=function(t,e){throw console.error("[WhenEntry] ERROR",t,e),t},f(t,[{key:"entryObj",get:function(){return this.validateAndConfigure()&&this.calcEntryObj()}}]),t}();function x(t){return t===Object(t)}var j=/*#__PURE__*/function(){function t(t){void 0===t&&(t={});var e=t.type,n=t.key,r=t.value,i=t.when,s=t.schema,a=t.properties,o=t.config;this.opts=t,this.when=i,this.key=n,this.type=e,this.value=r,this.schema=s,this.properties=a,this.config=o,this.validate()}var e=t.prototype;return e.validate=function(){"string"!=typeof this.type&&this.error("validate: invalid or mising type: "+this.type,this.opts),x(this.when)||this.error("validate: invalid or mising when: "+this.when,this.opts)},e.validateAndConfigure=function(t){if(!x(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)},e.createWhenEntry=function(t,e){return function(t,e){return void 0===e&&(e={}),new N(t,e)}(t,e)},e.accumulate=function(t,e){var n=this.when[e];if(!x(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},e.warn=function(t,e){console.error("[WhenCondition] WARNING",t,e)},e.error=function(t,e){throw console.error("[WhenCondition] ERROR",t,e),t},f(t,[{key:"constraintObj",get:function(){return this.whenKeys?this.whenKeys.reduce(this.accumulate.bind(this),{}):{}}},{key:"keyVal",get:function(){var t=this.whenKeys||[];return 1===t.length?t[0]:t}},{key:"constraintValue",get:function(){return!!this.keyVal&&[this.keyVal,this.constraintObj]}},{key:"constraint",get:function(){return this.validateAndConfigure()&&this.constraintValue}}]),t}(),S=function(t){return new j(t)},V=/*#__PURE__*/function(t){function e(e,n){var r;return void 0===n&&(n={}),(r=t.call(this,n)||this).typeHandler=e,r.constraintsAdded={},r.delegators.map(function(t){r[t]=e[t]}),r}p(e,t);var n=e.prototype;return n.build=function(t,e){void 0===e&&(e={});var n=e.constraintName,r=e.constraintValue,i=e.method,s=e.yup,a=e.value,o=e.values,u=e.errName;if(s=s||this.base,this.isNothing(r=r||e.propValue||this.constraints[t]))return this.warn("no prop value"),!1;n=n||t;var c=this.aliasMap[i=i||n]||i;if(!s[c])return this.warn("Yup has no such API method: "+c),!1;for(var h,f=s[c].bind(s),l=this.valErrMessage(n),p=u&&this.valErrMessage(u),d={constraintName:n,yup:s,constraintFn:f,errFn:l||p},y=0,v=["multiValueConstraint","presentConstraintValue","nonPresentConstraintValue"];y<v.length&&!(h=this[v[y]](a||o,d));y++);return h?(this.base=h,h):(this.warn("buildConstraint: missing value or values options"),!1)},n.nonPresentConstraintValue=function(t,e){var n=e.constraintName,r=e.constraintFn,i=e.errFn;if(!this.isPresent(t))return this.onConstraintAdded({name:n}),r(i)},n.presentConstraintValue=function(t,e){var n=e.constraintName,r=e.constraintFn,i=e.errFn;if(this.isPresent(t))return this.onConstraintAdded({name:n,value:t}),this.isNoValueConstraint(n)?r(i):r(t,i)},n.multiValueConstraint=function(t,e){var n=e.constraintFn,r=e.constraintName,i=e.errFn;if(this.isPresent(t)){if(Array.isArray(t))return this.onConstraintAdded({name:r,value:t}),this.callConstraintFn(n,r,t,i);this.warn("buildConstraint: values option must be an array of arguments")}},n.callConstraintFn=function(t,e,n,r){return this.isMultiArgsCall(e)?t.apply(void 0,n.concat([r])):t(n,r)},n.isMultiArgsCall=function(t){return this.multiArgsValidatorMethods[t]},n.isNoValueConstraint=function(t){return this.noValueConstraints.includes(t)},n.addValueConstraint=function(t,e){var n=void 0===e?{}:e;return this.addConstraint(t,{constraintName:n.constraintName,value:!0,errName:n.errName})},n.addConstraint=function(t,e){var n=this.build(t,e);return!!n&&(this.typeHandler.base=n,n)},n.onConstraintAdded=function(t){return this.constraintsAdded[t.name]=t.value,this.typeHandler},n.valErrMessage=function(t){return this.errorMessageHandler.valErrMessage(t)},f(e,[{key:"delegators",get:function(){return["errMessages","base","key","constraints","errorMessageHandler"]}},{key:"multiArgsValidatorMethods",get:function(){return this.config.multiArgsValidatorMethods||{when:!0}}},{key:"noValueConstraints",get:function(){return["required","email","url","format"]}},{key:"constraintsMap",get:function(){return{simple:["required","notRequired","nullable"],value:["default","strict"]}}},{key:"aliasMap",get:function(){return{oneOf:"oneOf",enum:"oneOf",anyOf:"oneOf"}}}]),e}(E),A=/*#__PURE__*/function(t){function e(e,n){var r;return void 0===n&&(n={}),(r=t.call(this,n)||this).typeHandler=e,r.constraints=e.constraints,r.errMessages=e.errMessages,r.key=e.key,r.type=e.type,r}p(e,t);var n=e.prototype;return n.valErrMessage=function(t){var e=this.constraints,n=this.errMessageFor(t);return"function"==typeof n?n(e):n},n.errMessageFor=function(t){var e=this.errMessages,n=e[this.key];return n?n[t]:e["$"+t]},e}(E),I=/*#__PURE__*/function(t){function e(){return t.apply(this,arguments)||this}return p(e,t),e}(/*#__PURE__*/g(Error)),F=/*#__PURE__*/function(t){function e(e){var n;return void 0===e&&(e={}),(n=t.call(this,e.config)||this).init(e),n}p(e,t);var n=e.prototype;return n.init=function(t){var e=t.schema,n=t.key,r=t.value,i=t.config,s=t.entryHandler;i=i||{},e=e||{},this.validateOnCreate(n,r,t),this.opts=t,this.entryHandler=s,this.validator=this.getValidator(),this.key=n,this.schema=e,this.properties=e.properties||{},this.value=r,this.constraints=this.getConstraints(),this.format=r.format||this.constraints.format,this.config=i||{},this.type=this.baseType,this.mixedConfig=this.config.mixedEnabled||{},this.typeConfig=this.config[this.type]||{},this.errMessages=i.errMessages||{},this.configureTypeConfig(),this.constraintsAdded={},this.base=this.getBase()},n.getBase=function(){return this.customBaseValidator||this.validatorInstance},n.getValidator=function(){return this.opts.validator||this.config.validator||this.entryHandler&&this.entryHandler.validator||a},n.configureTypeConfig=function(){this.typeConfig.enabled||this.typeConfig.extends||this.typeConfig.convert&&(this.typeConfig.extends=Object.keys(this.typeConfig.convert))},n.isRequired=function(t){return!0===(t=t||this.value).required},n.disabledMode=function(t){var e=this.mode[t];return!!this.disableFlags.find(function(t){return e===t})},n.enabledMode=function(t){var e=this.mode[t];return!!this.enableFlags.find(function(t){return e===t})},n.preProcessedConstraintValue=function(t){return this.shouldPreProcessValue?this.isRequired(t)?t:l({},t,{notRequired:!0}):t},n.initHelpers=function(){var t=this.config;this.errorMessageHandler=(this.config.createErrorMessageHandler||this.createErrorMessageHandler)(this,t),this.constraintBuilder=(this.config.createConstraintBuilder||this.createConstraintBuilder)(this,t),this.rebind("addConstraint","addValueConstraint")},n.createConstraintBuilder=function(t,e){return void 0===e&&(e={}),new V(t,e)},n.createErrorMessageHandler=function(t,e){return void 0===e&&(e={}),new A(t,e)},n.rebind=function(){var t=this;[].slice.call(arguments).map(function(e){var n=t[e];t[e]=t.isFunctionType(n)?n.bind(t):n})},n.validateOnCreate=function(t,e,n){t||this.error("create: missing key "+JSON.stringify(n)),e||this.error("create: missing value "+JSON.stringify(n))},n.convertEnabled=function(){var t=this;this.enabled.map(function(e){var n=t.convertFnFor(e);n&&n(t)})},n.convertFnFor=function(t){return this.customConvertFnFor(t,this)||this.builtInConvertFnFor(t)},n.customConvertFnFor=function(t){return(this.typeConfig.convert||{})[t]},n.builtInConvertFnFor=function(t){return this[t].bind(this)},n.getConstraints=function(){return this.config.getConstraints(this.value)},n.createSchemaEntry=function(){return this.convert().base},n.convert=function(){return this.initHelpers(),this.addMappedConstraints(),this.convertEnabled(),this},n.apply=function(t){var e,n=[].slice.call(arguments,1);if("string"!=typeof t)throw new TypeError("[Mixed] apply must take a method name available on the validator instance as first argument");return this.base=n&&n.length&&(e=this.base)[t].apply(e,n)||this.base,this},n.addValueConstraint=function(t,e){var n=this.constraintBuilder.addValueConstraint(t,e);return n&&(this.base=n.base),this},n.addConstraint=function(t,e){if(!this.constraintBuilder)throw new Error("[YupMixed] addConstraint: Missing constraintBuilder in "+this.constructor.name);var n=this.constraintBuilder.addConstraint(t,e);return n&&(this.base=n),this},n.addMappedConstraints=function(){var t=Object.keys(this.constraintsMap),e=this.addMappedConstraint.bind(this);return t.map(e),this},n.addMappedConstraint=function(t){var e=this["value"===t?"addValueConstraint":"addConstraint"];this.constraintsMap[t].map(function(t){e(t)})},n.oneOf=function(){var t=this,e=this.constraints.enum||this.constraints.oneOf||this.constraints.anyOf;if(this.isNothing(e))return this;e=Array.isArray(e)?e:[e];var n=["oneOf","enum","anyOf"].find(function(e){return void 0!==t.constraints[e]});return this.addConstraint(n,{values:e})},n.notOneOf=function(){var t=this.constraints,e=t.not,n=t.notOneOf||e&&(e.enum||e.oneOf);return this.isNothing(n)?this:(n=Array.isArray(n)?n:[n],this.addConstraint("notOneOf",{values:n}))},n.valErrMessage=function(t){return this.errorMessageHandler.valErrMessage(t)},n.createWhenConditionFor=function(t){return(this.config.createWhenCondition||S)({key:this.key,type:this.type,value:this.value,schema:this.schema,properties:this.properties,config:this.config,when:t})},n.label=function(){var t=this.value,e=t.title||t.label;return this.base=e&&this.base.label(e)||this.base,this},n.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)},n.isType=function(){return this.addConstraint("isType",{value:this.constraints.isType,errName:"notOneOf"}),this},n.nullable=function(){var t=this.constraints;return this.addConstraint("nullable",{value:t.nullable||t.isNullable,errName:"notOneOf"}),this},n.message=function(){return config.messages[this.key]||config.messages[this.type]||{}},n.errMessage=function(t){return void 0===t&&(t="default"),this.message[t]||"error"},n.toValidJSONSchema=function(){},n.normalize=function(){},n.deNormalize=function(){},n.errorMsg=function(t){this.throwError(t)},n.error=function(t,e){var n=["["+t+"]",e].join(" ");this.errorMsg(n)},n.throwError=function(t){throw t},f(e,[{key:"customBaseValidator",get:function(){return this.config.validatorFor&&this.config.validatorFor(this.type)}},{key:"baseType",get:function(){return"mixed"}},{key:"validatorInstance",get:function(){return this.validator.mixed()}},{key:"mode",get:function(){return this.config.mode||{}}},{key:"disableFlags",get:function(){return[!1,"disabled","no","off"]}},{key:"enableFlags",get:function(){return[!0,"enabled","yes","on"]}},{key:"shouldPreProcessValue",get:function(){return!this.disabledMode("notRequired")}},{key:"value",get:function(){return this._value},set:function(t){this._value=this.preProcessedConstraintValue(t)}},{key:"mixedEnabled",get:function(){return this.mixedConfig.enabled||["oneOf","notOneOf","when","nullable","isType","label"]}},{key:"typeEnabled",get:function(){return[]}},{key:"$typeExtends",get:function(){if(Array.isArray(this.typeConfig.extends))return o.default([].concat(this.typeConfig.extends,this.typeEnabled))}},{key:"configuredTypeEnabled",get:function(){return Array.isArray(this.typeConfig.enabled)?this.typeConfig.enabled:this.typeEnabled}},{key:"$typeEnabled",get:function(){return this.$typeExtends||this.configuredTypeEnabled}},{key:"enabled",get:function(){return[].concat(this.mixedEnabled,this.$typeEnabled)}},{key:"constraintsMap",get:function(){return{simple:["required","notRequired","nullable"],value:["default","strict"]}}}]),e}(T),P=["oneOf","enum","required","notRequired","minDate","min","maxDate","max","trim","lowercase","uppercase","email","url","minLength","maxLength","pattern","matches","regex","integer","positive","minimum","maximum"],D={errMessages:function(t){return void 0===t&&(t=P),t.reduce(function(t,e){return t[e]=function(t){var e=t.value;return t.key+": invalid for "+(e.name||e.title)},t},{})}},R=/*#__PURE__*/function(t){function e(e){var n;return(n=t.call(this,e)||this).type=n.baseType,n.base=n.validatorInstance,n.createYupSchemaEntry=n.config.createYupSchemaEntry,n}p(e,t),e.create=function(t){return new e(t)};var n=e.prototype;return n.convert=function(){return t.prototype.convert.call(this),this},n.ensureItems=function(){return this.addConstraint("ensure")},n.compact=function(){return this.addConstraint("compact")},n.itemsOf=function(){var t=this.constraints,e=t.items||t.itemsOf||this.constraints.of;if(!this.isNothing(e))if(Array.isArray(e))this.error("itemsOf","does not (yet) support an Array of schemas");else if(this.isObjectType(e)){if(this.createYupSchemaEntry){try{var n=this.createYupSchemaEntry({key:this.key,value:e,config:this.config});return this.addConstraint("of",{constraintValue:n,propValue:e})}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 e)},n.maxItems=function(){var t=this.constraints,e=t.maxItems||t.max;if(!this.isNumberType(e))return this;if(!this.isValidSize(e))return this.handleInvalidSize("maxItems",e);var n=e&&this.base.max(e);return this.base=n||this.base,this},n.minItems=function(){var t=this.constraints,e=t.minItems||t.min;if(!this.isNumberType(e))return this;if(!this.isValidSize(e))return this.handleInvalidSize("minItems",e);var n=e&&this.base.min(e);return this.base=n||this.base,this},n.$items=function(){return this},n.$additionalItems=function(){return this},n.$uniqueItems=function(){return this},n.$contains=function(){return this},n.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)},n.isValidSize=function(t){return this.isNumberType(t)&&t>=0},f(e,[{key:"baseType",get:function(){return"array"}},{key:"validatorInstance",get:function(){return this.validator.array()}},{key:"typeEnabled",get:function(){return["maxItems","minItems","ensureItems","compact","itemsOf"]}}]),e}(F),Y=/*#__PURE__*/function(t){function e(e){return t.call(this,e)||this}p(e,t);var n=e.prototype;return n.isArray=function(t){return this.config.isArray||this.error("ArrayHandler: mising isArray in config",this.config),this.config.isArray(t)},n.handle=function(t){return this.isArray(t)&&R.create(t).createSchemaEntry()},e}(T);function q(t,e){return void 0===e&&(e={}),t&&new Y(e).handle(t)}var H=/*#__PURE__*/function(t){function e(e){var n;return(n=t.call(this,e)||this).type=n.baseType,n.base=n.validatorInstance,n}return p(e,t),e.create=function(t){return new e(t)},f(e,[{key:"baseType",get:function(){return"boolean"}},{key:"validatorInstance",get:function(){return this.validator.boolean()}}]),e}(F),B=/*#__PURE__*/function(){function t(t){this.config=t}var e=t.prototype;return e.isBoolean=function(t){return this.config.isBoolean(t)},e.handle=function(t){return this.isBoolean(t)&&H.create(t).createSchemaEntry()},t}();function L(t,e){return void 0===e&&(e={}),t&&new B(e).handle(t)}var z=/*#__PURE__*/function(t){function e(e){return t.call(this,e)||this}return p(e,t),f(e,[{key:"$map",get:function(){return{moreThan:["exclusiveMinimum","moreThan"],lessThan:["exclusiveMaximum","lessThan"],max:["maximum","max"],min:["minimum","min"]}}}]),e}(/*#__PURE__*/function(t){function e(e){return t.call(this,e)||this}p(e,t);var n=e.prototype;return n.transform=function(t){return this.typer.toNumber(t)},n.isValidConstraint=function(t){return this.typer.isNumberLike(t)},f(e,[{key:"explainConstraintValidMsg",get:function(){return"Must be a number or convertible to a number"}}]),e}(/*#__PURE__*/function(t){function e(e,n){var r;return(r=t.call(this,e.config)||this).map=n||r.$map||{},r.typer=e,r.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}),r}p(e,t);var n=e.prototype;return n.isStringType=function(t){return"string"==typeof t},n.add=function(){var t=this,e=this.map;Object.keys(e).map(function(n){var r=t.entryNames(e[n]);t.addConstraints(n,r)})},n.entryNames=function(t){return Array.isArray(t)?t:[t]},n.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},n.validateAndTransform=function(t){var e=this.constraints[t];return this.validate(e),this.transform(e)},n.invalidMsg=function(t,e){return"invalid constraint for "+t+", was "+e+"."},n.invalidConstraintMsg=function(t,e){return[this.invalidMsg(t,e),this.explainConstraintValidMsg].join("\n")},n.validate=function(t){return this.isNothing(t)?this:this.isValidConstraint(t)?void 0:this.handleInvalidConstraint(name,t)},n.isValidConstraint=function(t){return!0},n.handleInvalidConstraint=function(t,e){var n=this.invalidConstraintMsg(t,e);return this.config.warnOnInvalid?(this.warn(n),this):(this.error(n,e),this)},f(e,[{key:"delegates",get:function(){return["constraints","addConstraint","constraintsAdded"]}},{key:"explainConstraintValidMsg",get:function(){return""}}]),e}(E))),_=/*#__PURE__*/function(t){function e(e,n){return t.call(this,e,n)||this}return p(e,t),e.prototype.isValid=function(){return this.config.isNumber(this.obj)},e}(/*#__PURE__*/function(t){function e(e,n){var r;return(r=t.call(this,n)||this).obj=e,r}p(e,t);var n=e.prototype;return n.isValid=function(){return!1},n.verify=function(){return this.isPresent(this.obj)&&this.isValid(this.obj)},e}(T)),$=function(t,e){return void 0===e&&(e={}),function(t,e){return new _(t,e)}(t,e).verify()};function K(t,e){return void 0===e&&(e={}),$(t,e)&&function(t){return U.schemaEntryFor(t)}(t)}var U=/*#__PURE__*/function(t){function e(e){var n,r;return(n=t.call(this,e)||this).type=n.baseType,n.base=n.validatorInstance,n.rangeConstraint=(r=function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(n),new z(r)),n}p(e,t);var n=e.prototype;return n.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.convert=function(){return t.prototype.convert.call(this),this},n.range=function(){this.rangeConstraint.add()},n.truncate=function(){return this.addConstraint("truncate")},n.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},n.posNeg=function(){this.positive(),this.negative()},n.integer=function(){return this.isInteger&&this.addConstraint("integer"),this},n.positive=function(){return this.addConstraint("positive")},n.negative=function(){return this.addConstraint("negative")},n.normalize=function(){this.constraints.maximum=this.constraints.maximum||this.constraints.max,this.constraints.minimum=this.constraints.minimum||this.constraints.min},f(e,[{key:"baseType",get:function(){return this.normalizeNumType(this.opts.type)}},{key:"validatorInstance",get:function(){return this.validator.number()}},{key:"typeEnabled",get:function(){return["range","posNeg","integer"]}},{key:"isInteger",get:function(){return this.config.isInteger(this.type)}},{key:"isNegative",get:function(){var t=this.constraints,e=t.exclusiveMaximum;return!!t.negative||void 0!==e&&0===e}},{key:"isPositive",get:function(){var t=this.constraints,e=t.exclusiveMinimum;return!!t.positive||void 0!==e&&0===e}}]),e}(F),W=/*#__PURE__*/function(t){function e(e){var n;return(n=t.call(this,e)||this).type=n.baseType,n.base=n.validatorInstance,n.properties=n.value.properties,n}p(e,t),e.create=function(t){return new e(t)};var n=e.prototype;return n.convert=function(){if(!this.properties)return this;t.prototype.convert.call(this);var e=this.value,n=this.config;if(e){n.buildYup||this.error("convert","Missing buildYup function from config",n);var r=this.config.buildYup(e,n);this.base=r}return this},n.camelCase=function(){return this.addConstraint("camelCase")},n.constantCase=function(){return this.addConstraint("constantCase")},n.noUnknown=function(){var t=this.value,e=t.noUnknown||t.propertyNames,n=e&&this.base.noUnknown(e,this.valErrMessage("propertyNames")||this.valErrMessage("noUnknown"));return this.base=n||this.base,this},f(e,[{key:"baseType",get:function(){return"object"}},{key:"validatorInstance",get:function(){return this.validator.object()}},{key:"typeEnabled",get:function(){return["noUnknown","camelCase","constantCase"]}}]),e}(F),J=function(t){return t&&"object"===t.type},G=/*#__PURE__*/function(){function t(t){void 0===t&&(t={}),(t=t||{}).isObject=t.isObject||J,this.config=t,this.schema=t.schema}var e=t.prototype;return e.isObject=function(t){return this.config.isObject(t.value)},e.handle=function(t){return this.isObject(t)&&W.create(l({},t,{config:this.config})).createSchemaEntry()},t}();function Q(t,e){return void 0===e&&(e={}),t&&new G(e).handle(t)}var X=/*#__PURE__*/function(t){function e(e){var n;return(n=t.call(this,e)||this).type=n.baseType,n.base=n.validatorInstance,n}p(e,t),e.create=function(t){return new e(t)};var n=e.prototype;return n.convert=function(){return t.prototype.convert.call(this),this},n.trim=function(){return this.addConstraint("trim")},n.lowercase=function(){return this.addConstraint("lowercase")},n.uppercase=function(){return this.addConstraint("uppercase")},n.genericFormat=function(){1!=!this.config.format&&this.validator.prototype[this.format]&&this.addConstraint(this.format)},n.email=function(){if(!this.isEmail)return this;var t=this.constraintNameFor("email","format"),e="email";return this.addConstraint("email",{constraintValue:!0,constraintName:t,method:e,errName:e}),this},n.constraintNameFor=function(){var t=this;return[].slice.call(arguments).find(function(e){return t.constraints[e]})},n.url=function(){if(!this.isUrl)return this;var t=this.constraintNameFor("url","format");return this.addConstraint("url",{constraintValue:!0,constraintName:t,method:"url",errName:"url"}),this},n.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},n.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},n.pattern=function(){var t=this.constraints,e=t.pattern;if(!e)return this;var n=new RegExp(e,t.flags),r=this.valErrMessage("pattern")||this.valErrMessage("matches")||this.valErrMessage("regex"),i=n&&this.base.matches(n,r);return this.base=i||this.base,this},n.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},f(e,[{key:"baseType",get:function(){return"string"}},{key:"validatorInstance",get:function(){return this.validator.string()}},{key:"typeEnabled",get:function(){return["normalize","minLength","maxLength","pattern","lowercase","uppercase","email","url","genericFormat"]}},{key:"isEmail",get:function(){return this.constraints.email||"email"===this.format}},{key:"isUrl",get:function(){return this.constraints.url||"url"===this.format}}]),e}(F),Z=/*#__PURE__*/function(){function t(t){this.config=t}var e=t.prototype;return e.isString=function(t){return this.config.isString(t)},e.handle=function(t){return this.isString(t)&&X.create(l({config:this.config},t)).createSchemaEntry()},t}();function tt(t,e){return void 0===e&&(e={}),t&&new Z(e).handle(t)}var et=/*#__PURE__*/function(t){function e(e){var n;return(n=t.call(this,e)||this).type=n.baseType,n.base=n.validatorInstance,n}p(e,t),e.create=function(t){return new e(t)};var n=e.prototype;return n.convert=function(){return t.prototype.convert.call(this),this},n.toDate=function(t){return new Date(t)},n.isValidDateType=function(t){return this.isStringType(t)||this.isDateType(t)},n.isValidDate=function(t){return!!this.isValidDateType(t)&&(!this.isStringType(t)||Boolean(Date.parse(t)))},n.transformToDate=function(t){return this.isNumberType(t)?new Date(t):t},n.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},n.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},n.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)},f(e,[{key:"baseType",get:function(){return"date"}},{key:"validatorInstance",get:function(){return this.validator.date()}},{key:"typeEnabled",get:function(){return["minDate","maxDate"]}}]),e}(F),nt=/*#__PURE__*/function(){function t(t){this.config=t}var e=t.prototype;return e.isDate=function(t){return this.config.isDate(t)},e.handle=function(t){return this.isDate(t)&&et.create(t).createSchemaEntry()},t}();function rt(t,e){return void 0===e&&(e={}),t&&new nt(e).handle(t)}var it={__proto__:null,errValKeys:P,defaults:D,YupArray:R,toYupArray:q,YupBoolean:H,toYupBoolean:L,YupNumber:U,toYupNumberSchemaEntry:K,toYupNumber:function(t,e){return void 0===e&&(e={}),$(t,e)&&function(t){return U.create(t)}(t)},YupObject:W,toYupObject:Q,YupString:X,toYupString:tt,YupDate:et,toYupDate:rt,YupMixed:F,ConvertYupSchemaError:I,Base:T},st=/*#__PURE__*/function(t){function e(){return t.apply(this,arguments)||this}return p(e,t),e}(/*#__PURE__*/g(Error)),at=/*#__PURE__*/function(t){function e(e,n){var r;r=t.call(this,n)||this;var i=e.value,s=e.type,a=e.kind,o=e.name,u=e.key,c=e.schema,h=e.types;return r.opts=e,r.kind=a,r.value=i,r.schema=c,r.key=u,r.value=i||{},r.name=o,r.type=s,r.types=h,r}p(e,t);var n=e.prototype;return n.error=function(t,e){var n,r,i=this.opts;throw e?(n=console).error.apply(n,[t,e].concat(i)):(r=console).error.apply(r,[t].concat(i)),new st(t)},n.resolve=function(){throw"Must be implemented by subclass"},f(e,[{key:"obj",get:function(){return{schema:this.schema,key:this.key,value:this.value,type:this.type,kind:this.kind,config:this.config}}}]),e}(T),ot=/*#__PURE__*/function(t){function e(e,n){return t.call(this,e,n)||this}return p(e,t),e.prototype.resolve=function(){if(Array.isArray(this.value)){var t=this.config.toMultiType;return t?t(this):void 0}},e}(at),ut=/*#__PURE__*/function(t){function e(e,n){return t.call(this,e,n)||this}return p(e,t),e.prototype.resolve=function(){if(!Array.isArray(this.value)){var t=this.config.toSingleType;if(t)return t(this);for(var e,n=this.obj,r=this.config,i=0,s=Object.keys(this.types);i<s.length;i++){var a=this.types[s[i]];if(a&&(e=a(n,r)),e)break}return e}},e}(at),ct=function(t,e,n){return new ht(t,e,n)},ht=/*#__PURE__*/function(t){function e(e,n){var r;return(r=t.call(this,e,n)||this).initResolvers(),r}p(e,t);var n=e.prototype;return n.initResolvers=function(){var t=this.opts,e=this.config,n=e.createMultiTypeResolver||this.createMultiTypeResolver.bind(this);this.multiTypeResolver=n(t,e);var r=e.createSingleTypeResolver||this.createSingleTypeResolver.bind(this);this.singleTypeResolver=r(t,e)},n.createMultiTypeResolver=function(){return new ot(this.opts,this.config)},n.createSingleTypeResolver=function(){return new ut(this.opts,this.config)},n.resolve=function(){return this.toMultiType()||this.toSingleType()||this.toDefaultEntry()},n.toMultiType=function(){return(this.config.toMultiType||this.singleTypeResolver.resolve.bind(this.singleTypeResolver))(this)},n.toSingleType=function(){return(this.config.toSingleType||this.singleTypeResolver.resolve.bind(this.singleTypeResolver))(this)},n.toDefaultEntry=function(){return this.defaultType()},n.defaultType=function(){this.error("toEntry: unknown type",this.type)},e}(at),ft=/*#__PURE__*/function(t){function e(){return t.apply(this,arguments)||this}return p(e,t),e}(/*#__PURE__*/g(Error)),lt=/*#__PURE__*/function(t){function e(e){var n;n=t.call(this,e.config)||this;var r=e.schema,i=e.name,s=e.key,a=e.value,o=e.config;n.builder=e.builder,n.opts=e,n.schema=r,n.key=s,n.value=a||{},n.config=o||{},n.name=i;var u=Array.isArray(a)?"array":a.type;return n.kind="array"===u?"multi":"single",n.type=u,n.setTypeHandlers(),n.setPropertyHandler(),n}p(e,t);var n=e.prototype;return n.setPropertyHandler=function(){var t=this.config;this.propertyValueHandler=(t.createPropertyValueHandler||this.createPropertyValueHandler)(this.propertyHandlerOpts,t)},n.createPropertyValueHandler=function(t,e){return ct(t,e)},n.setTypeHandlers=function(){this.types=this.config.types||this.typeHandlers},n.isValidSchema=function(){return this.isStringType(this.type)},n.error=function(t,e){var n,r,i=this.opts;throw e?(n=console).error.apply(n,[t,e].concat(i)):(r=console).error.apply(r,[t].concat(i)),new ft(t)},n.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)}return this.propertyValueHandler.resolve(this.opts,this.config)},f(e,[{key:"validator",get:function(){return this.builder&&this.builder.validator}},{key:"propertyHandlerOpts",get:function(){return{type:this.type,kind:this.kind,types:this.types,value:this.value,name:this.name,key:this.key,schema:this.schema,entryHandler:this}}},{key:"defaultTypeHandlerMap",get:function(){return{string:tt,number:K,boolean:L,array:q,object:Q,date:rt}}},{key:"typeHandlers",get:function(){return l({},this.defaultTypeHandlerMap,this.config.typeHandlers||{})}}]),e}(T);function pt(t){return void 0===t&&(t={}),new lt(t).toEntry()}function dt(t,e){return void 0===e&&(e={}),new vt(t,l({},e)).yupSchema}function yt(t){return t===Object(t)}var vt=/*#__PURE__*/function(t){function e(e,n){var r;return void 0===n&&(n={}),(r=t.call(this,n)||this).init(e,n),r}p(e,t);var n=e.prototype;return n.init=function(t,e){e.buildYup=dt,e.createYupSchemaEntry=e.createYupSchemaEntry||pt,this.config=Object.assign(this.config,e),this.schema=t;var n=this.getType(t),r=this.getProps(t);if(this.type=n,this.properties=r,this.additionalProps=this.getAdditionalProperties(t),this.required=this.getRequired(t),this.setLocale(),("function"==typeof e.init?e.init:function(){}).bind(this)(t,e),function(t){return t&&"object"===t}(n))if(yt(r)){var i=this.getName(t),s=this.normalizeRequired(t),a=this.propsToShape({properties:s,name:i,config:e});this.shapeConfig=a,this.validSchema=!0}else{var o=JSON.stringify(s);this.error("invalid schema: must have a properties object: "+o)}else this.error('invalid schema: must be type: "object", was type: '+n)},n.setLocale=function(){this.config.locale&&a.setLocale(this.config.locale)},n.getAdditionalProperties=function(t){return t.additionalProperties},n.getRequired=function(t){var e=this.config.getRequired;return e?e(t):t.required||[]},n.getProps=function(t){return this.config.getProps(t)},n.getType=function(t){return this.config.getType(t)},n.getName=function(t){return this.config.getName(t)},n.normalizeRequired=function(){var t=this,e=l({},this.properties),n=[].concat(this.required)||[];return Object.keys(e).reduce(function(r,i){var s=e[i],a=n.indexOf(i)>=0;return yt(s)?s.required=t.isRequired(s)||a:t.warn("Bad value: "+s+" must be an object"),r[i]=s,r},{})},n.isRequired=function(t){return this.config.isRequired(t)},n.propsToShape=function(t){void 0===t&&(t={});var e=this.objPropsToShape(t);return this.objPropsShape=e,this.addPropsShape=this.additionalPropsToShape(t,e),e},n.additionalPropsToShape=function(t,e){return e},n.objPropsToShape=function(t){var e=this,n=t.name,r=l({},this.properties);return Object.keys(r).reduce(function(t,i){var s=e.propToYupSchemaEntry({name:n,key:i,value:r[i]});return e.logInfo("propsToShape",{key:i,yupSchemaEntry:s}),t[i]=s,t},{})},n.propToYupSchemaEntry=function(t){var e=t.value;return this.createYupSchemaEntry({schema:this.schema,name:t.name,key:t.key,value:void 0===e?{}:e,config:this.config,builder:this})},n.createYupSchemaEntry=function(t){return void 0===t&&(t={}),this.config.createYupSchemaEntry(t)},f(e,[{key:"validator",get:function(){return a}},{key:"yupSchema",get:function(){return a.object().shape(this.shapeConfig)}}]),e}(T),mt={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:{}},gt={createValidatorName:function(t,e){return"is"+(t=(t=c.default(t||e)).replace(/Uri$/,"URI")).replace(/Id$/,"ID")},createTestName:function(t,e){return u.default(t||e)}},bt={isMagnetURI:function(t,e){return/magnet:\?xt=urn:[a-z0-9]+:[a-z0-9]{32}/i.test(t)}};exports.ConstraintBuilder=V,exports.ErrorMessageHandler=A,exports.PropertyValueResolver=ht,exports.YupBuilder=vt,exports.YupSchemaEntry=lt,exports.YupSchemaEntryError=ft,exports.buildYup=dt,exports.createPropertyValueResolver=ct,exports.createYupSchemaEntry=pt,exports.extendYupApi=function(e){var n,r=this,i=void 0===e?{}:e,s=i.constraints,a=i.override,o=void 0!==a&&a,u=i.validator,c=i.createValidatorName,h=i.createTestName;if(!u)throw"extendYupApi: missing validator option";Array.isArray(s)&&(void 0===n&&(n={}),s=s.reduce(function(t,e){if("string"!=typeof e&&!(e instanceof Object)){if(!1!==n.throws)throw"toConstraintsMap: invalid entry "+e;return t}if("string"==typeof e)t[name]={};else{if(!e.name){if(!1!==n.throws)throw"toConstraintsMap: invalid entry "+e+" missing name";return t}t[e.name]=e}return t},{})),s=o?s||mt:l({},mt,s||{}),c=c||gt.createValidatorName,h=h||gt.createTestName,Object.keys(s).map(function(e){var n=s[e],i=n.testName,a=n.optsKey,o=n.logging,f=c(n.validatorName,e);i=h(i,e),t.addMethod(t.string,e,function(n){void 0===n&&(n={});var s=n.message,c=n[a];return t.string().test(i,s,function(t){var n=r.path,a=r.createError,h=u[f];if("function"!=typeof(h=h||bt[f]))throw Error("No method named ${validatorName} on validator");var l=h(t,c);return!0===o&&console.log("Yup validator bridge",{key:e,fullValidatorName:f,testName:i,value:t,valid:l}),l||a({path:n,message:s})})})})},exports.types=it; | ||
var t=require("yup"),e=require("uniq"),s=require("dashify"),r=require("uppercamelcase");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}function i(t){if(t&&t.__esModule)return t;var e=Object.create(null);return t&&Object.keys(t).forEach(function(s){if("default"!==s){var r=Object.getOwnPropertyDescriptor(t,s);Object.defineProperty(e,s,r.get?r:{enumerable:!0,get:function(){return t[s]}})}}),e.default=t,e}var a=/*#__PURE__*/i(t),o=/*#__PURE__*/n(e),h=/*#__PURE__*/n(s),c=/*#__PURE__*/n(r);const l={getProps:t=>t&&t.properties,getType:t=>t&&t.type,getName:t=>t&&(t.name||t.title),getConstraints:t=>t,isString:t=>t&&"string"===t.type,isArray:t=>t&&"array"===t.type,isInteger:t=>t&&("integer"===t.type||"int"===t.type),isBoolean:t=>t&&"boolean"===t.type,hasDateFormat:t=>t&&["date","date-time"].find(e=>e===t.format),isDate:t=>t&&"string"===t.type&&l.hasDateFormat(t.format),isNumber:t=>t&&("number"===t.type||l.isInteger(t)),isObject:t=>t&&"object"===t.type,isRequired:t=>t&&t.required},u={getProps:t=>t.properties,getType:t=>t.type,getName:t=>t.name||t.title,getConstraints:t=>t,isString:t=>"string"===t.type&&!u.hasDateFormat(t),isArray:t=>"array"===t.type,isBoolean:t=>"boolean"===t.type,isInteger:t=>"integer"===t.type,hasDateFormat:t=>["date","date-time"].includes(t?.value?.format),isDate:t=>"string"===t.type&&u.hasDateFormat(t),isNumber:t=>"number"===t.type||u.isInteger(t),isObject:t=>"object"===t.type,isRequired:t=>t.required},d={getProps:t=>t&&t.fields,getType:t=>t&&t.type,getName:t=>t&&(t.name||t.title),getConstraints:t=>t,isString:t=>t&&"string"===t.type,isArray:t=>t&&"array"===t.type,isInteger:t=>t&&"int"===t.type,isFloat:t=>t&&"float"===t.type,isDouble:t=>t&&"double"===t.type,isBoolean:t=>t&&"boolean"===t.type,isDate:t=>t&&"int"===t.type&&"date"===t.logicalType,isNumber:t=>t&&(d.isInteger(t)||d.isFloat(t)||d.isDouble(t)),isObject:t=>t&&"record"===t.type,isRequired:t=>t&&t.required},p={"json-schema":l,"type-def":u,avro:d};class m extends class{constructor(t={}){this.config=t;const{log:e,error:s}=t,r=t.enable||{};!0===t.logging&&(r.log=!0),!1===t.logging&&(r.log=!1),this.enable=r,this.log="function"==typeof e?e:console.log,this.err="function"==typeof s?s:console.error}error(t){var e=[].slice.call(arguments,1);if(!1!==this.enable.error&&this.err)throw e&&e.length?this.err(t,...e):this.err(t),t}warn(t){this.enable.warn&&this.logInfo("WARNING: "+t,...[].slice.call(arguments,1))}logInfo(t){var e=[].slice.call(arguments,1);this.enable.log&&this.log&&(e&&e.length?this.log(t,...e):this.log(t))}}{constructor(t={}){super(t)}isNothing(t){return null==t}isPresent(t){return!this.isNothing(t)}toNumber(t){return Number(t)}isNumberLike(t){return!isNaN(this.toNumber(t))}isObjectType(t){return t===Object(t)}isArrayType(t){return Array.isArray(t)}isNumberType(t){return!isNaN(t)}isStringType(t){return"string"==typeof t}isFunctionType(t){return"function"==typeof t}isDateType(t){return t instanceof Date}}class g{constructor(t,e){this.schemaParserMap=t,this.name=e}lookup(t=this.name){return this.schemaParserMap[t]}build(){const t=this.lookup();if(t.extends){const e=this.extend(t.extends);if(e)return e}return t}extend(t){const e=this.lookup(t);e&&(entry={...e,...this.lookup()})}}class y extends m{constructor(t={}){super(t);const e=(t.createSchemaParserBuilder||new g(t.schemaParserMap||p,t.schemaType||"json-schema")).build();this.config={...e,...t}}createSchemaParserBuilder(t,e){return new SchemaParserBuilder(t,e)}}function f(t){return t===Object(t)}class b{constructor(t,e={}){this.whenEntryObj=t;const{schema:s,properties:r,config:n,key:i,when:a,type:o}=e;this.schema=s,this.when=a,this.properties=r||{},this.key=i,this.type=o,this.config=n}validateAndConfigure(t){if(!f(t=t||this.whenEntryObj))return this.warn("invalid or missing when entry constraint object",t),!1;const 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' or 'else' constraint: ${e}`,t),!1):(this.warn(`validateAndConfigure: when entry constraint missing 'is' constraint: ${e}`,t),!1)}createYupSchemaEntry(t){return this.config.createYupSchemaEntry(t)}createValue(t,e){return"string"==typeof t&&(t={[t]:!0}),f(t)||this.error(`createValue: ${e} must be a schema object`),{key:this.key,type:this.type,...t}}createEntryOpts(t,e){const s=this.createValue(t,e);return{schema:this.schema,properties:this.properties,key:this.key,type:this.type,value:s,config:this.config}}createEntry(t,e){const s=this.createEntryOpts(t,e);return this.createYupSchemaEntry(s)}hasKey(t,e){return t.find(t=>t===e)}hasAnyKey(t,e){return t.find(t=>e.includes(t))}whenEntryFor(t,e,s){if(s=s||e,"string"==typeof t&&(t={[t]:!0}),!f(t))throw`whenEntryFor: Invalid when object ${t}`;const r={...t[s]};return delete t[s],r?(t[e]=this.createEntry(r,e),t):t}calcEntryObj(){let t={...this.whenEntryObj};return t=this.whenEntryFor(t,"then"),"otherwise"in t&&(t=this.whenEntryFor(t,"otherwise")),"else"in t&&(t=this.whenEntryFor(t,"else")),t}get entryObj(){return this.validateAndConfigure()&&this.calcEntryObj()}warn(t,e){console.error("[WhenEntry] WARNING",t,e)}error(t,e){throw console.error("[WhenEntry] ERROR",t,e),t}}function v(t){return t===Object(t)}class C{constructor(t={}){const{type:e,key:s,value:r,when:n,schema:i,properties:a,config:o}=t;this.opts=t,this.when=n,this.key=s,this.type=e,this.value=r,this.schema=i,this.properties=a,this.config=o,this.validate()}validate(){"string"!=typeof this.type&&this.error(`validate: invalid or mising type: ${this.type}`,this.opts),v(this.when)||this.error(`validate: invalid or mising when: ${this.when}`,this.opts)}validateAndConfigure(t){if(!v(t=t||this.when))return this.warn("invalid or missing when constraint",t),!1;const 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)}createWhenEntry(t,e){return((t,e={})=>new b(t,e))(t,e)}accumulate(t,e){let s=this.when[e];if(!v(s))return this.warn(`invalid when entry constraint object ${s} for ${e}`),t;const r={type:this.type,key:this.key,schema:this.schema,properties:this.properties,config:this.config},{entryObj:n}=this.createWhenEntry(s,r);return n?t=Object.assign(t,n):t}get constraintObj(){return this.whenKeys?this.whenKeys.reduce(this.accumulate.bind(this),{}):{}}get keyVal(){const t=this.whenKeys||[];return 1===t.length?t[0]:t}get constraintValue(){return!!this.keyVal&&[this.keyVal,this.constraintObj]}get constraint(){return this.validateAndConfigure()&&this.constraintValue}warn(t,e){console.error("[WhenCondition] WARNING",t,e)}error(t,e){throw console.error("[WhenCondition] ERROR",t,e),t}}const w=t=>new C(t);class E extends m{constructor(t,e={}){super(e),this.typeHandler=t,this.builder=t.builder,this.constraintsAdded={},this.delegators.map(e=>{this[e]=t[e]})}get delegators(){return["errMessages","base","key","type","constraints","errorMessageHandler","logInfo","warn"]}build(t,e={}){let{constraintName:s,constraintValue:r,propValue:n,method:i,yup:a,value:o,values:h,errName:c}=e;if(a=a||this.base,r=this.getFirstValue([r,n,this.constraints[t]]),s=s||t,i=i||s,this.idObj={propName:t,method:i,key:this.key},this.logDetailed("build",e,{resolved:{constraintValue:r,constraintName:s}}),this.isNothing(r))return this.warn("no prop value",{constraintValue:r}),!1;const l=this.aliasMap[i]||i;if(!a[l])return this.warn(`Yup has no such API method: ${l}`),!1;const u=a[l].bind(a),d=this.valErrMessage(s),p=c&&this.valErrMessage(c),m={constraintName:s,yup:a,constraintFn:u,errFn:d||p},g=["multiValueConstraint","presentConstraintValue","nonPresentConstraintValue"];let y;for(let t of g)if(y=this[t].bind(this)(this.getFirstValue([o,h]),m),y)break;return y?(this.base=y,y):(this.warn("buildConstraint: missing value or values options"),!1)}getFirstValue(t){const e=this.isPresent.bind(this);return t.filter(e)[0]}nonPresentConstraintValue(t,{constraintName:e,constraintFn:s,errFn:r}){if(!this.isPresent(t))return this.logInfo("nonPresentConstraintValue",{constraintValue:t}),this.onConstraintAdded({method:"nonPresentConstraintValue",name:e}),s(r)}presentConstraintValue(t,{constraintName:e,constraintFn:s,errFn:r}){if(this.isPresent(t))return this.logInfo("presentConstraintValue",{constraintName:e,constraintValue:t}),this.onConstraintAdded({method:"presentConstraintValue",name:e,value:t}),this.isNoValueConstraint(e)?(this.logInfo("isNoValueConstraint",{constraintName:e}),s(r)):(this.logInfo("presentConstraintValue: apply validator function",{constraintName:e,constraintValue:t}),s(t,r));this.logInfo("presentConstraintValue: value not present",{constraintName:e,constraintValue:t})}multiValueConstraint(t,{constraintFn:e,constraintName:s,errFn:r}){if(this.isPresent(t)){if(this.logInfo("multiValueConstraint",{constraintName:s,values:t}),Array.isArray(t))return this.onConstraintAdded({method:"multiValueConstraint",name:s,value:t}),this.logInfo("multiValueConstraint: apply validator function",{constraintName:s,value:t}),this.callConstraintFn(e,s,t,r);this.warn("buildConstraint: values option must be an array of arguments")}}callConstraintFn(t,e,s,r){return this.isMultiArgsCall(e)?t(...s,r):t(s,r)}isMultiArgsCall(t){return this.multiArgsValidatorMethods[t]}get multiArgsValidatorMethods(){return this.config.multiArgsValidatorMethods||{when:!0}}isNoValueConstraint(t){return this.noValueConstraints.includes(t)}get noValueConstraints(){return["required","email","url","format"]}addValueConstraint(t,{constraintName:e,errName:s}={}){return this.addConstraint(t,{constraintName:e,value:!0,errName:s})}addConstraint(t,e){const s=this.build(t,e);return!!s&&(this.typeHandler.base=s,s)}onConstraintAdded({method:t,name:e,value:s}){if(this.constraintsAdded[e]=s,this.builder)return this.builder.onConstraintAdded({type:this.type,method:t,name:e,value:s}),this.typeHandler;this.logInfo("no builder set to notify in ConstraintBuilder")}get constraintsMap(){return{simple:["required","notRequired","nullable"],value:["default","strict"]}}logDetailed(t){const e=this.idObj,s=this.config.logDetailed||[];s.length&&s.find(t=>!(t.key&&e.key!==t.key||t.propName&&e.propName!==t.propName||t.method&&e.method!==t.method))&&this.logInfo(t,e,...[].slice.call(arguments,1))}valErrMessage(t){return this.errorMessageHandler.valErrMessage(t)}get aliasMap(){return{oneOf:"oneOf",enum:"oneOf",anyOf:"oneOf"}}}class N extends m{constructor(t,e={}){super(e),this.typeHandler=t,this.constraints=t.constraints,this.errMessages=t.errMessages,this.key=t.key,this.type=t.type}valErrMessage(t){const{constraints:e}=this,s=this.errMessageFor(t);return"function"==typeof s?s(e):s}errMessageFor(t){const{errMessages:e,key:s}=this,r=e[s];return r?r[t]:e[`$${t}`]}}class x extends Error{}class T extends y{constructor(t={}){super(t.config),this.init(t)}init(t){let{schema:e,key:s,value:r,config:n,entryHandler:i}=t;n=n||{},e=e||{},this.validateOnCreate(s,r,t),this.opts=t,this.entryHandler=i,this.validator=this.getValidator(),this.key=s,this.schema=e,this.properties=e.properties||{},this.value=r,this.constraints=this.getConstraints(),this.format=r.format||this.constraints.format,this.config=n||{},this.type=this.baseType,this.mixedConfig=this.config.mixedEnabled||{},this.typeConfig=this.config[this.type]||{},this.errMessages=n.errMessages||{},this.configureTypeConfig(),this.constraintsAdded={},this.base=this.getBase()}get builder(){return this.entryHandler&&this.entryHandler.builder}getBase(){return this.customBaseValidator||this.validatorInstance}get customBaseValidator(){return this.config.validatorFor&&this.config.validatorFor(this.type)}getValidator(){return this.opts.validator||this.config.validator||this.builder&&this.builder.validator||a}get baseType(){return"mixed"}get validatorInstance(){return this.validator.mixed()}configureTypeConfig(){this.typeConfig.enabled||this.typeConfig.extends||this.typeConfig.convert&&(this.typeConfig.extends=Object.keys(this.typeConfig.convert))}isRequired(t){return!0===(t=t||this.value).required}get mode(){return this.config.mode||{}}get disableFlags(){return[!1,"disabled","no","off"]}get enableFlags(){return[!0,"enabled","yes","on"]}disabledMode(t){const e=this.mode[t];return!!this.disableFlags.find(t=>e===t)}enabledMode(t){const e=this.mode[t];return!!this.enableFlags.find(t=>e===t)}get shouldPreProcessValue(){return!this.disabledMode("notRequired")}preProcessedConstraintValue(t){return this.shouldPreProcessValue?this.isRequired(t)?t:{...t,notRequired:!0}:t}set value(t){this._value=this.preProcessedConstraintValue(t)}get value(){return this._value}initHelpers(){const{config:t}=this;this.errorMessageHandler=(this.config.createErrorMessageHandler||this.createErrorMessageHandler)(this,t),this.constraintBuilder=(this.config.createConstraintBuilder||this.createConstraintBuilder)(this,t),this.rebind("addConstraint","addValueConstraint")}createConstraintBuilder(t,e={}){return new E(t,e)}createErrorMessageHandler(t,e={}){return new N(t,e)}rebind(){[].slice.call(arguments).map(t=>{const e=this[t];this[t]=this.isFunctionType(e)?e.bind(this):e})}validateOnCreate(t,e,s){t||this.error(`create: missing key ${JSON.stringify(s)}`),e||this.error(`create: missing value ${JSON.stringify(s)}`)}get mixedEnabled(){return this.mixedConfig.enabled||["oneOf","notOneOf","when","nullable","isType","label","refValueFor"]}get typeEnabled(){return[]}get $typeExtends(){if(Array.isArray(this.typeConfig.extends))return o.default([...this.typeConfig.extends,...this.typeEnabled])}get configuredTypeEnabled(){return Array.isArray(this.typeConfig.enabled)?this.typeConfig.enabled:this.typeEnabled}get $typeEnabled(){return this.$typeExtends||this.configuredTypeEnabled}get enabled(){return[...this.mixedEnabled,...this.$typeEnabled]}convertEnabled(){this.enabled.map(t=>{const e=this.convertFnFor(t);e&&e(this)})}convertFnFor(t){return this.customConvertFnFor(t,this)||this.builtInConvertFnFor(t)}customConvertFnFor(t){return(this.typeConfig.convert||{})[t]}builtInConvertFnFor(t){return this[t].bind(this)}getConstraints(){return this.config.getConstraints(this.value)}createSchemaEntry(){return this.convert().base}convert(){return this.initHelpers(),this.addMappedConstraints(),this.convertEnabled(),this}apply(t){var e=[].slice.call(arguments,1);if("string"!=typeof t)throw new TypeError("[Mixed] apply must take a method name available on the validator instance as first argument");return this.base=e&&e.length&&this.base[t](...e)||this.base,this}addValueConstraint(t,e){const s=this.constraintBuilder.addValueConstraint(t,e);if(s){const{base:t}=s;this.base=t}return this}addConstraint(t,e){if(!this.constraintBuilder)throw new Error(`[YupMixed] addConstraint: Missing constraintBuilder in ${this.constructor.name}`);const s=this.constraintBuilder.addConstraint(t,e);return s&&(this.base=s),this}addMappedConstraints(){const t=Object.keys(this.constraintsMap),e=this.addMappedConstraint.bind(this);return t.map(e),this}addMappedConstraint(t){const{constraintsMap:e}=this,s=this["value"===t?"addValueConstraint":"addConstraint"];e[t].map(t=>{s(t)})}get constraintsMap(){return{simple:["required","notRequired","nullable"],value:["default","strict"]}}refValueFor(){let t=this.constraints.refValueFor;return this.isNothing(t)?this:(this.logInfo("refValueFor",{propRefName:t}),this.apply("when",(e,s)=>e?s.required().oneOf([a.ref(t)]):s))}oneOf(){let t=this.constraints.enum||this.constraints.oneOf||this.constraints.anyOf;if(this.isNothing(t))return this;t=Array.isArray(t)?t:[t];const e=["oneOf","enum","anyOf"].find(t=>void 0!==this.constraints[t]);return this.addConstraint(e,{values:t})}notOneOf(){const{not:t,notOneOf:e}=this.constraints;let s=e||t&&(t.enum||t.oneOf);return this.isNothing(s)?this:(s=Array.isArray(s)?s:[s],this.addConstraint("notOneOf",{values:s}))}valErrMessage(t){return this.errorMessageHandler.valErrMessage(t)}createWhenConditionFor(t){return(this.config.createWhenCondition||w)({key:this.key,type:this.type,value:this.value,schema:this.schema,properties:this.properties,config:this.config,when:t})}label(){const t=this.value,e=t.title||t.label;return this.base=e&&this.base.label(e)||this.base,this}when(){const t=this.constraints.when;if((e=t)!==Object(e))return this;var e;const{constraint:s}=this.createWhenConditionFor(t);return s?(this.logInfo(`Adding when constraint for ${this.key}`,s),this.addConstraint("when",{values:s,errName:"when"}),this):(this.warn(`Invalid when constraint for: ${t}`),this)}isType(){return this.addConstraint("isType",{value:this.constraints.isType,errName:"notOneOf"}),this}nullable(){const{nullable:t,isNullable:e}=this.constraints;return this.addConstraint("nullable",{value:t||e,errName:"notOneOf"}),this}message(){return config.messages[this.key]||config.messages[this.type]||{}}errMessage(t="default"){return this.message[t]||"error"}toValidJSONSchema(){}normalize(){}deNormalize(){}errorMsg(t){this.throwError(t)}error(t,e){const s=[`[${t}]`,e].join(" ");this.errorMsg(s)}throwError(t){throw t}}const O=["oneOf","enum","required","notRequired","minDate","min","maxDate","max","trim","lowercase","uppercase","email","url","minLength","maxLength","pattern","matches","regex","integer","positive","minimum","maximum"],M={errMessages:(t=O)=>t.reduce((t,e)=>(t[e]=({key:t,value:e})=>`${t}: invalid for ${e.name||e.title}`,t),{})};class V extends T{constructor(t){super(t),this.type=this.baseType,this.base=this.validatorInstance,this.createYupSchemaEntry=this.config.createYupSchemaEntry}get baseType(){return"array"}get validatorInstance(){return this.validator.array()}static create(t){return new V(t)}convert(){return super.convert(),this}get typeEnabled(){return["maxItems","minItems","ensureItems","compact","itemsOf"]}ensureItems(){return this.addConstraint("ensure")}compact(){return this.addConstraint("compact")}itemsOf(){const{items:t,itemsOf:e}=this.constraints,s=t||e||this.constraints.of;if(!this.isNothing(s))if(Array.isArray(s))this.error("itemsOf","does not (yet) support an Array of schemas");else if(this.isObjectType(s)){if(this.createYupSchemaEntry){try{const t=this.createYupSchemaEntry({key:this.key,value:s,config:this.config});return this.addConstraint("of",{constraintValue:t,propValue:s})}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 s)}maxItems(){const{maxItems:t,max:e}=this.constraints,s=t||e;if(!this.isNumberType(s))return this;if(!this.isValidSize(s))return this.handleInvalidSize("maxItems",s);const r=s&&this.base.max(s);return this.base=r||this.base,this}minItems(){const{minItems:t,min:e}=this.constraints,s=t||e;if(!this.isNumberType(s))return this;if(!this.isValidSize(s))return this.handleInvalidSize("minItems",s);const r=s&&this.base.min(s);return this.base=r||this.base,this}$items(){return this}$additionalItems(){return this}$uniqueItems(){return this}$contains(){return this}handleInvalidSize(t,e){const s=`invalid array size constraint for ${t}, was ${e}. Must be a number >= 0`;return this.config.warnOnInvalid?(this.warn(s),this):(this.error(s,e),this)}isValidSize(t){return this.isNumberType(t)&&t>=0}}class k extends y{constructor(t){super(t)}isArray(t){return this.config.isArray||this.error("ArrayHandler: mising isArray in config",this.config),this.config.isArray(t)}handle(t){return this.isArray(t)&&V.create(t).createSchemaEntry()}}function S(t,e={}){return t&&new k(e).handle(t)}class I extends T{constructor(t){super(t),this.type=this.baseType,this.base=this.validatorInstance}get baseType(){return"boolean"}get validatorInstance(){return this.validator.boolean()}static create(t){return new I(t)}}class j{constructor(t){this.config=t}isBoolean(t){return this.config.isBoolean(t)}handle(t){return this.isBoolean(t)&&I.create(t).createSchemaEntry()}}function A(t,e={}){return t&&new j(e).handle(t)}class F extends m{constructor(t,e){super(t.config),this.map=e||this.$map||{},this.typeHandler=t,this.delegates.map(e=>{const s=t[e];s||this.error(`missing delegate: ${e}`,{typeHandler:t}),this[e]=this.isFunctionType(s)?s.bind(t):s})}isStringType(t){return"string"==typeof t}get delegates(){return["constraints","addConstraint","constraintsAdded"]}add(){const t=this.map;Object.keys(t).map(e=>{const s=this.entryNames(t[e]);this.addConstraints(e,s)})}entryNames(t){return Array.isArray(t)?t:[t]}addConstraints(t,e=[]){return e.map(e=>{const s=this.validateAndTransform(e);this.addConstraint(e,{method:t,value:s})}),this}validateAndTransform(t){const e=this.constraints[t];return this.validate(e),this.transform(e)}invalidMsg(t,e){return`invalid constraint for ${t}, was ${e}.`}get explainConstraintValidMsg(){return""}invalidConstraintMsg(t,e){return[this.invalidMsg(t,e),this.explainConstraintValidMsg].join("\n")}validate(t){return this.isNothing(t)?this:this.isValidConstraint(t)?void 0:this.handleInvalidConstraint(name,t)}isValidConstraint(t){return!0}handleInvalidConstraint(t,e){const s=this.invalidConstraintMsg(t,e);return this.config.warnOnInvalid?(this.warn(s),this):(this.error(s,e),this)}}class D extends F{constructor(t){super(t)}transform(t){return this.typeHandler.toNumber(t)}isValidConstraint(t){return this.typeHandler.isNumberLike(t)}get explainConstraintValidMsg(){return"Must be a number or convertible to a number"}}class P extends D{constructor(t){super(t)}get $map(){return{moreThan:["exclusiveMinimum","moreThan"],lessThan:["exclusiveMaximum","lessThan"],max:["maximum","max"],min:["minimum","min"]}}}class $ extends y{constructor(t,e){super(e),this.obj=t}isValid(){return!1}verify(){return this.isPresent(this.obj)&&this.isValid(this.obj)}}class H extends ${constructor(t,e){super(t,e)}isValid(){return this.config.isNumber(this.obj)}}const R=(t,e={})=>function(t,e){return new H(t,e)}(t,e).verify();function Y(t,e={}){return R(t,e)&&function(t){return q.schemaEntryFor(t)}(t)}class q extends T{constructor(t){super(t),this.type=this.baseType,this.base=this.validatorInstance,this.rangeConstraint=new P(this)}get baseType(){return this.normalizeNumType(this.opts.type)}get validatorInstance(){return this.validator.number()}normalizeNumType(t){return"int"===t?"integer":t}static create(t){return new q(t)}static schemaEntryFor(t){return q.create(t).createSchemaEntry()}get typeEnabled(){return["range","posNeg","integer"]}convert(){return super.convert(),this}range(){this.rangeConstraint.add()}truncate(){return this.addConstraint("truncate")}round(){const{round:t}=this.constraints;if(this.isNothing(t))return this;const e=this.isStringType(t)?t:"round";return t&&this.base.round(e),this}posNeg(){this.positive(),this.negative()}integer(){return this.isInteger&&this.addConstraint("integer"),this}get isInteger(){return this.config.isInteger(this.type)}positive(){return this.addConstraint("positive")}negative(){return this.addConstraint("negative")}get isNegative(){const{exclusiveMaximum:t,negative:e}=this.constraints;return!!e||void 0!==t&&0===t}get isPositive(){const{exclusiveMinimum:t,positive:e}=this.constraints;return!!e||void 0!==t&&0===t}normalize(){this.constraints.maximum=this.constraints.maximum||this.constraints.max,this.constraints.minimum=this.constraints.minimum||this.constraints.min}}class B extends T{constructor(t){super(t),this.type=this.baseType,this.base=this.validatorInstance,this.properties=this.value.properties}get baseType(){return"object"}get validatorInstance(){return this.validator.object()}static create(t){return new B(t)}get typeEnabled(){return["noUnknown","camelCase","constantCase"]}convert(){if(!this.properties)return this;super.convert();const t=this.value,e=this.config;if(t){e.buildYup||this.error("convert","Missing buildYup function from config",e);const s=this.config.buildYup(t,e);this.base=s}return this}camelCase(){return this.addConstraint("camelCase")}constantCase(){return this.addConstraint("constantCase")}noUnknown(){const{noUnknown:t,propertyNames:e}=this.value,s=t||e,r=s&&this.base.noUnknown(s,this.valErrMessage("propertyNames")||this.valErrMessage("noUnknown"));return this.base=r||this.base,this}}const L=t=>t&&"object"===t.type;class z{constructor(t={}){(t=t||{}).isObject=t.isObject||L,this.config=t,this.schema=t.schema}isObject(t){return this.config.isObject(t.value)}handle(t){return this.isObject(t)&&B.create({...t,config:this.config}).createSchemaEntry()}}function K(t,e={}){return t&&new z(e).handle(t)}class U extends T{constructor(t){super(t),this.type=this.baseType,this.base=this.validatorInstance}get baseType(){return"string"}get validatorInstance(){return this.validator.string()}static create(t){return new U(t)}convert(){return super.convert(),this}get typeEnabled(){return["normalize","minLength","maxLength","pattern","lowercase","uppercase","email","url","genericFormat"]}trim(){return this.addConstraint("trim")}lowercase(){return this.addConstraint("lowercase")}uppercase(){return this.addConstraint("uppercase")}genericFormat(){1!=!this.config.format&&this.validator.prototype[this.format]&&this.addConstraint(this.format)}email(){if(!this.isEmail)return this;const t=this.constraintNameFor("email","format"),e="email";return this.addConstraint("email",{constraintValue:!0,constraintName:t,method:e,errName:e}),this}constraintNameFor(){return[].slice.call(arguments).find(t=>this.constraints[t])}get isEmail(){return this.constraints.email||"email"===this.format}url(){if(!this.isUrl)return this;const t=this.constraintNameFor("url","format");return this.addConstraint("url",{constraintValue:!0,constraintName:t,method:"url",errName:"url"}),this}get isUrl(){return this.constraints.url||"url"===this.format}minLength(){const{minLength:t}=this.constraints,e=this.valErrMessage("minLength")||this.valErrMessage("min"),s=t&&this.base.min(t,e);return this.base=s||this.base,this}maxLength(){const{maxLength:t}=this.constraints,e=this.valErrMessage("maxLength")||this.valErrMessage("max"),s=t&&this.base.max(t,e);return this.base=s||this.base,this}pattern(){const{pattern:t,flags:e}=this.constraints;if(!t)return this;const s=new RegExp(t,e),r=this.valErrMessage("pattern")||this.valErrMessage("matches")||this.valErrMessage("regex"),n=s&&this.base.matches(s,r);return this.base=n||this.base,this}normalize(){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}}class W{constructor(t){this.config=t}isString(t){return this.config.isString(t)}handle(t){return this.isString(t)&&U.create({config:this.config,...t}).createSchemaEntry()}}function _(t,e={}){return t&&new W(e).handle(t)}class J extends T{constructor(t){super(t),this.type=this.baseType,this.base=this.validatorInstance}get baseType(){return"date"}get validatorInstance(){return this.validator.date()}static create(t){return new J(t)}get typeEnabled(){return["minDate","maxDate"]}convert(){return super.convert(),this}toDate(t){return new Date(t)}isValidDateType(t){return this.isStringType(t)||this.isDateType(t)}isValidDate(t){return!!this.isValidDateType(t)&&(!this.isStringType(t)||Boolean(Date.parse(t)))}transformToDate(t){return this.isNumberType(t)?new Date(t):t}minDate(){const t=this.constraints.minDate||this.constraints.min;if(this.isNothing(t))return this;const e=this.transformToDate(t);if(!this.isValidDateType(e))return this.handleInvalidDate("minDate",e);const s=e&&this.base.min(this.toDate(e),this.valErrMessage("minDate")||this.valErrMessage("min"));return this.base=s||this.base,this}maxDate(){const t=this.constraints.maxDate||this.constraints.max;if(this.isNothing(t))return this;const e=this.transformToDate(t);if(!this.isValidDateType(e))return this.handleInvalidDate("maxDate",e);const s=e&&this.base.max(this.toDate(e),this.valErrMessage("maxDate")||this.valErrMessage("max"));return this.base=s||this.base,this}handleInvalidDate(t,e){const s=`invalid constraint for ${t}, was ${e}. Must be a number, string (valid date format) or a Date instance`;return this.config.warnOnInvalid?(this.warn(s),this):(this.error(s,e),this)}}class G{constructor(t){this.config=t}isDate(t){return this.config.isDate(t)}handle(t){return this.isDate(t)&&J.create(t).createSchemaEntry()}}function Q(t,e={}){return t&&new G(e).handle(t)}var X={__proto__:null,errValKeys:O,defaults:M,YupArray:V,toYupArray:S,YupBoolean:I,toYupBoolean:A,YupNumber:q,toYupNumberSchemaEntry:Y,toYupNumber:function(t,e={}){return R(t,e)&&function(t){return q.create(t)}(t)},YupObject:B,toYupObject:K,YupString:U,toYupString:_,YupDate:J,toYupDate:Q,YupMixed:T,ConvertYupSchemaError:x,Base:y};class Z extends Error{}class tt extends y{constructor(t,e,s){super(e);const{value:r,type:n,kind:i,name:a,key:o,schema:h,types:c}=t;this.entryHandler=s||t.entryHandler,this.opts=t,this.kind=i,this.value=r,this.schema=h,this.key=o,this.value=r||{},this.name=a,this.type=n,this.types=c}error(t,e){const{opts:s}=this;throw e?console.error(t,e,...s):console.error(t,...s),new Z(t)}resolve(){throw"Must be implemented by subclass"}get obj(){const{schema:t,key:e,value:s,type:r,kind:n,config:i,entryHandler:a}=this;return{schema:t,key:e,value:s,type:r,kind:n,config:i,entryHandler:a}}}class et extends tt{constructor(t,e,s){super(t,e,s)}resolve(){const{value:t}=this;if(!Array.isArray(t))return;const e=this.config.toMultiType;return e?e(this):void 0}}class st extends tt{constructor(t,e,s){super(t,e,s)}resolve(){const{value:t}=this;if(Array.isArray(t))return;const e=this.config.toSingleType;if(e)return e(this);const{obj:s,config:r,entryHandler:n}=this,i=Object.keys(this.types);let a;for(let t of i){const e=this.types[t];if(e&&(a=e(s,r,n)),a)break}return a}}const rt=(t,e,s)=>new nt(t,e,s);class nt extends tt{constructor(t,e,s){super(t,e,s),this.initResolvers()}initResolvers(){const{opts:t,config:e,entryHandler:s}=this,r=e.createMultiTypeResolver||this.createMultiTypeResolver.bind(this);this.multiTypeResolver=r(t,e,s);const n=e.createSingleTypeResolver||this.createSingleTypeResolver.bind(this);this.singleTypeResolver=n(t,e,s)}createMultiTypeResolver(){const{opts:t,config:e,entryHandler:s}=this;return new et(t,e,s)}createSingleTypeResolver(){const{opts:t,config:e,entryHandler:s}=this;return new st(t,e,s)}resolve(){return this.toMultiType()||this.toSingleType()||this.toDefaultEntry()}toMultiType(){return(this.config.toMultiType||this.singleTypeResolver.resolve.bind(this.singleTypeResolver))(this)}toSingleType(){return(this.config.toSingleType||this.singleTypeResolver.resolve.bind(this.singleTypeResolver))(this)}toDefaultEntry(){return this.defaultType()}defaultType(){this.error("toEntry: unknown type",this.type)}}class it extends Error{}class at extends y{constructor(t){super(t.config);const{schema:e,name:s,key:r,value:n,config:i,builder:a}=t;this.builder=a,this.opts=t,this.schema=e,this.key=r,this.value=n||{},this.config=i||{},this.name=s;const o=Array.isArray(n)?"array":n.type;this.kind="array"===o?"multi":"single",this.type=o,this.setTypeHandlers(),this.setPropertyHandler()}get validator(){return this.builder&&this.builder.validator}setPropertyHandler(){const{config:t}=this;this.propertyValueHandler=(t.createPropertyValueHandler||this.createPropertyValueHandler)(this.propertyHandlerOpts,t)}get propertyHandlerOpts(){const{types:t,value:e,name:s,key:r,type:n,kind:i,schema:a}=this;return{type:n,kind:i,types:t,value:e,name:s,key:r,schema:a,entryHandler:this}}createPropertyValueHandler(t,e){return rt(t,e,this)}get defaultTypeHandlerMap(){return{string:_,number:Y,boolean:A,array:S,object:K,date:Q}}setTypeHandlers(){this.types=this.config.types||this.typeHandlers}get typeHandlers(){return{...this.defaultTypeHandlerMap,...this.config.typeHandlers||{}}}isValidSchema(){const{type:t}=this;return this.isStringType(t)}error(t,e){const{opts:s}=this;throw e?console.error(t,e,...s):console.error(t,...s),new it(t)}toEntry(){if(!this.isValidSchema()){const t=JSON.stringify(this.schema);this.error(`Not a valid schema: type ${this.type} must be a string, was ${typeof this.type} ${t}`)}const{opts:t,config:e}=this;return this.propertyValueHandler.resolve(t,e)}}function ot(t={}){return new at(t).toEntry()}function ht(t,e={}){return new lt(t,{...e}).yupSchema}function ct(t){return t===Object(t)}class lt extends y{constructor(t,e={}){super(e),this.init(t,e)}init(t,e){e.buildYup=ht,e.createYupSchemaEntry=e.createYupSchemaEntry||ot,this.config=Object.assign(this.config,e),this.schema=t;const s=this.getType(t),r=this.getProps(t);if(this.type=s,this.properties=r,this.additionalProps=this.getAdditionalProperties(t),this.required=this.getRequired(t),this.setLocale(),("function"==typeof e.init?e.init:()=>{}).bind(this)(t,e),!function(t){return t&&"object"===t}(s))return void this.error(`invalid schema: must be type: "object", was type: ${s}`);if(!ct(r)){const t=JSON.stringify(i);return void this.error(`invalid schema: must have a properties object: ${t}`)}const n=this.getName(t),i=this.normalizeRequired(t),a=this.propsToShape({properties:i,name:n,config:e});this.shapeConfig=a,this.validSchema=!0}get validator(){return a}setLocale(){this.config.locale&&a.setLocale(this.config.locale)}getAdditionalProperties(t){return t.additionalProperties}getRequired(t){const{getRequired:e}=this.config;return e?e(t):t.required||[]}getProps(t){return this.config.getProps(t)}getType(t){return this.config.getType(t)}getName(t){return this.config.getName(t)}get yupSchema(){return a.object().shape(this.shapeConfig)}normalizeRequired(){const t={...this.properties},e=[...this.required]||[];return Object.keys(t).reduce((s,r)=>{const n=t[r],i=e.indexOf(r)>=0;return ct(n)?n.required=this.isRequired(n)||i:this.warn(`Bad value: ${n} must be an object`),s[r]=n,s},{})}isRequired(t){return this.config.isRequired(t)}propsToShape(t={}){const e=this.objPropsToShape(t);return this.objPropsShape=e,this.addPropsShape=this.additionalPropsToShape(t,e),e}additionalPropsToShape(t,e){return e}objPropsToShape({name:t}){const e={...this.properties};return Object.keys(e).reduce((s,r)=>{const n=this.propToYupSchemaEntry({name:t,key:r,value:e[r]});return this.logInfo("propsToShape",{key:r,yupSchemaEntry:n}),s[r]=n,s},{})}propToYupSchemaEntry({name:t,key:e,value:s={}}){return this.createYupSchemaEntry({schema:this.schema,name:t,key:e,value:s,config:this.config,builder:this})}createYupSchemaEntry(t={}){return this.config.createYupSchemaEntry(t)}onConstraintAdded(t){this.logInfo("Constraint Added",t)}}const ut={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:{}},dt={createValidatorName:(t,e)=>`is${t=(t=(t=c.default(t||e)).replace(/Uri$/,"URI")).replace(/Id$/,"ID")}`,createTestName:(t,e)=>h.default(t||e)},pt={isMagnetURI:(t,e)=>/magnet:\?xt=urn:[a-z0-9]+:[a-z0-9]{32}/i.test(t)};exports.ConstraintBuilder=E,exports.ErrorMessageHandler=N,exports.PropertyValueResolver=nt,exports.YupBuilder=lt,exports.YupSchemaEntry=at,exports.YupSchemaEntryError=it,exports.buildYup=ht,exports.createPropertyValueResolver=rt,exports.createYupSchemaEntry=ot,exports.extendYupApi=function({constraints:e,override:s=!1,validator:r,createValidatorName:n,createTestName:i}={}){if(!r)throw"extendYupApi: missing validator option";Array.isArray(e)&&(e=((t,e={})=>t.reduce((t,s)=>{if("string"!=typeof s&&!(s instanceof Object)){if(!1!==e.throws)throw`toConstraintsMap: invalid entry ${s}`;return t}if("string"==typeof s)t[name]={};else{if(!s.name){if(!1!==e.throws)throw`toConstraintsMap: invalid entry ${s} missing name`;return t}t[s.name]=s}return t},{}))(e)),e=s?e||ut:{...ut,...e||{}},n=n||dt.createValidatorName,i=i||dt.createTestName,Object.keys(e).map(s=>{let{testName:a,optsKey:o,validatorName:h,logging:c}=e[s];const l=n(h,s);a=i(a,s),t.addMethod(t.string,s,(e={})=>{const{message:n}=e,i=e[o];return t.string().test(a,n,t=>{const{path:e,createError:o}=this;let h=r[l];if(h=h||pt[l],"function"!=typeof h)throw Error("No method named ${validatorName} on validator");const u=h(t,i);return!0===c&&console.log("Yup validator bridge",{key:s,fullValidatorName:l,testName:a,value:t,valid:u}),u||o({path:e,message:n})})})})},exports.types=X; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "schema-to-yup", | ||
"version": "1.11.2", | ||
"version": "1.11.4", | ||
"publishConfig": { | ||
@@ -35,7 +35,2 @@ "access": "public" | ||
}, | ||
"babel": { | ||
"presets": [ | ||
"@babel/preset-env" | ||
] | ||
}, | ||
"keywords": [ | ||
@@ -42,0 +37,0 @@ "yup", |
@@ -7,9 +7,26 @@ # Schema to Yup schema | ||
- JSON | ||
- GraphQL | ||
- Avro | ||
- Custom | ||
### JSON schema | ||
- [AJV: JSON Schema keywords](https://ajv.js.org/keywords.html) | ||
- [Learn JsonSchema](https://cswr.github.io/JsonSchema/) | ||
The builder currently supports the most commonly used [JSON Schema layout](https://json-schema.org/) and GraphQL type definition exports using [graphSchemaToJson](https://github.com/kristianmandrup/graphSchemaToJson) (see [GraphQL schema](#graphql-schema)). | ||
The builder currently supports the most commonly used [JSON Schema layout](https://json-schema.org/) | ||
Recently basic [Avro](https://avro.apache.org/docs/current/spec.html) schema support has been added as well. You can easily add support for your own custom schema formats. | ||
### GraphQL schema | ||
GraphQL type definition exports using [graphSchemaToJson](https://github.com/kristianmandrup/graphSchemaToJson) (see [GraphQL schema](#graphql-schema)). | ||
### Avro schema | ||
Recently basic [Avro](https://avro.apache.org/docs/current/spec.html) schema support has been added as well. | ||
### Custom/Alternative schemas | ||
You can easily add support for your own custom schema formats. | ||
We also supports some extra convenience schema properties that make it more "smooth" to define validation requirements declaratively (see below). | ||
@@ -19,2 +36,4 @@ | ||
## Custom/Alternative validators | ||
You can use the building blocks of this library to support alternative validators other than Yup. See [Supporting alternative validators](#supporting-alternative-validators) | ||
@@ -33,2 +52,22 @@ | ||
## Build and Sample run | ||
`$ yarn run build` | ||
Sample runs (uses built file in `dist`) | ||
`node sample-runs/person-schema.mjs` | ||
This sample run is configured with detailed logging in the `config` object: | ||
```js | ||
{ | ||
logging: true, | ||
logDetailed: [{ | ||
propName: 'exclusiveMinimum', | ||
key: 'age' | ||
}], | ||
} | ||
``` | ||
## Quick start | ||
@@ -211,5 +250,6 @@ | ||
- `notOneOf` | ||
- `when` _NEW_ | ||
- `isType` _NEW_ | ||
- `nullable` (`isNullable`) _NEW_ | ||
- `refValue` for confirm password scenario | ||
- `when` | ||
- `isType` | ||
- `nullable` (`isNullable`) | ||
@@ -266,2 +306,19 @@ ### Array | ||
## Confirm password | ||
You can use the special `refValue` constraint for password confirmation scenarios. | ||
```js | ||
"password": { | ||
"minLength": 6, | ||
"type": "string" | ||
}, | ||
"confirmPassword": { | ||
"refValue": "password", | ||
"type": "string" | ||
} | ||
``` | ||
See the sample test case in `test/confirm-password.test.js` | ||
## Multi-type constraints | ||
@@ -567,3 +624,3 @@ | ||
getValidator() { | ||
return this.opts.validator || (this.entryHandler && this.entryHandler.validator) || yup; | ||
return this.opts.validator || this.builder.validator || yup; | ||
} | ||
@@ -970,6 +1027,7 @@ ``` | ||
You can enable logging py passing a `log` option in the `config` argument. If set to true, it will by default assign the internal log function to `console.log` | ||
You can enable logging py passing a `log` option in the `config.enable` object. If set to `true`, it will by default assign the internal log function to `console.log` | ||
You can enable/disable warnings in a similar fashion with `enable.warn` | ||
```js | ||
const schema = buildYup(nameJsonSchema, { log: true }); | ||
const schema = buildYup(nameJsonSchema, { enable: { log: true, warn: true } }); | ||
``` | ||
@@ -993,2 +1051,15 @@ | ||
Logging can also be enabled simply by passing `logging:true`. In addition, detailed logging can be enabled for specific matches, such as whenever a combination of a specific `key`, `propName` and `method` are encountered by the `ConstraintBuilder` | ||
```js | ||
{ | ||
logging: true, | ||
logDetailed: [{ | ||
propName: 'exclusiveMinimum', // property name in JSON schema | ||
key: 'age' // key in JSON schema | ||
}, { | ||
method: 'lessThan' // method that propName is resolved to | ||
}], | ||
``` | ||
## Localised error messages | ||
@@ -995,0 +1066,0 @@ |
@@ -6,5 +6,7 @@ import { Base } from "./types"; | ||
export class BasePropertyValueResolver extends Base { | ||
constructor(opts, config) { | ||
constructor(opts, config, entryHandler) { | ||
super(config); | ||
const { value, type, kind, name, key, schema, types } = opts; | ||
// this.logInfo('BasePropertyValueResolver', opts) | ||
this.entryHandler = entryHandler || opts.entryHandler | ||
this.opts = opts; | ||
@@ -32,3 +34,3 @@ this.kind = kind; | ||
get obj() { | ||
const { schema, key, value, type, kind, config } = this; | ||
const { schema, key, value, type, kind, config, entryHandler } = this; | ||
return { | ||
@@ -40,5 +42,6 @@ schema, | ||
kind, | ||
config | ||
config, | ||
entryHandler | ||
}; | ||
} | ||
} |
@@ -6,3 +6,4 @@ import { TypeMatcher } from "./types/_type-matcher"; | ||
super(config); | ||
this.typeHandler = typeHandler; | ||
this.typeHandler = typeHandler | ||
this.builder = typeHandler.builder | ||
this.constraintsAdded = {}; | ||
@@ -15,3 +16,3 @@ this.delegators.map(name => { | ||
get delegators() { | ||
return ["errMessages", "base", "key", "constraints", "errorMessageHandler"]; | ||
return ["errMessages", "base", "key", "type", "constraints", "errorMessageHandler", "logInfo", "warn"]; | ||
} | ||
@@ -30,13 +31,20 @@ | ||
} = opts; | ||
yup = yup || this.base; | ||
yup = yup || this.base; | ||
constraintValue = | ||
constraintValue || propValue || this.constraints[propName]; | ||
// find the first value that is present (must not be undefined or null) | ||
const potentialValues = [constraintValue, propValue, this.constraints[propName]]; | ||
constraintValue = this.getFirstValue(potentialValues) | ||
constraintName = constraintName || propName; | ||
method = method || constraintName; | ||
this.idObj = {propName, method, key: this.key} | ||
this.logDetailed("build", opts, { resolved: { constraintValue, constraintName}}) | ||
// this.logInfo("build", { opts, constraintValue }) | ||
if (this.isNothing(constraintValue)) { | ||
this.warn("no prop value"); | ||
this.warn("no prop value", { constraintValue }); | ||
return false; | ||
} | ||
constraintName = constraintName || propName; | ||
method = method || constraintName; | ||
@@ -72,3 +80,5 @@ const yupConstraintMethodName = this.aliasMap[method] || method; | ||
for (let name of constrainFnNames) { | ||
newBase = this[name](value || values, constrOpts); | ||
const fnName = this[name].bind(this) | ||
const constrValue = this.getFirstValue([value, values]) | ||
newBase = fnName(constrValue, constrOpts); | ||
if (newBase) break; | ||
@@ -88,2 +98,7 @@ } | ||
getFirstValue(potentialValues) { | ||
const isDefined = this.isPresent.bind(this) | ||
return potentialValues.filter(isDefined)[0] | ||
} | ||
nonPresentConstraintValue( | ||
@@ -94,4 +109,5 @@ constraintValue, | ||
if (this.isPresent(constraintValue)) return; | ||
this.logInfo("nonPresentConstraintValue", { constraintValue }) | ||
this.onConstraintAdded({ name: constraintName }); | ||
this.onConstraintAdded({ method: 'nonPresentConstraintValue', name: constraintName }); | ||
@@ -106,12 +122,16 @@ const newBase = constraintFn(errFn); | ||
) { | ||
if (!this.isPresent(constraintValue)) return; | ||
if (!this.isPresent(constraintValue)) { | ||
this.logInfo("presentConstraintValue: value not present", { constraintName, constraintValue }) | ||
return; | ||
} | ||
this.logInfo("presentConstraintValue", { constraintName, constraintValue }) | ||
this.onConstraintAdded({ name: constraintName, value: constraintValue }); | ||
this.onConstraintAdded({ method: 'presentConstraintValue', name: constraintName, value: constraintValue }); | ||
if (this.isNoValueConstraint(constraintName)) { | ||
this.logInfo("isNoValueConstraint", { constraintName }) | ||
let specialNewBase = constraintFn(errFn); | ||
return specialNewBase; | ||
} | ||
// console.log("presentConstraintValue", { constraintName, constraintValue }); | ||
this.logInfo("presentConstraintValue: apply validator function", { constraintName, constraintValue }) | ||
const newBase = constraintFn(constraintValue, errFn); | ||
@@ -124,2 +144,3 @@ return newBase; | ||
this.logInfo("multiValueConstraint", { constraintName, values }) | ||
// call yup constraint function with multiple arguments | ||
@@ -131,4 +152,5 @@ if (!Array.isArray(values)) { | ||
this.onConstraintAdded({ name: constraintName, value: values }); | ||
this.onConstraintAdded({ method: 'multiValueConstraint', name: constraintName, value: values }); | ||
// console.log(constraintFn, { constraintName, values }); | ||
this.logInfo("multiValueConstraint: apply validator function", { constraintName, value: values }) | ||
@@ -188,4 +210,9 @@ return this.callConstraintFn(constraintFn, constraintName, values, errFn); | ||
onConstraintAdded({ name, value }) { | ||
onConstraintAdded({ method, name, value }) { | ||
this.constraintsAdded[name] = value; | ||
if (!this.builder) { | ||
this.logInfo('no builder set to notify in ConstraintBuilder') | ||
return | ||
} | ||
this.builder.onConstraintAdded({ type: this.type, method, name, value }) | ||
return this.typeHandler; | ||
@@ -201,2 +228,16 @@ } | ||
// propName, method, key | ||
logDetailed(label, ...values) { | ||
const idObj = this.idObj | ||
const matchIdList = this.config.logDetailed || [] | ||
if (!matchIdList.length) return | ||
const found = matchIdList.find(matchIds => { | ||
if (matchIds.key && idObj.key !== matchIds.key) return false | ||
if (matchIds.propName && idObj.propName !== matchIds.propName) return false | ||
if (matchIds.method && idObj.method !== matchIds.method) return false | ||
return true | ||
}) | ||
found && this.logInfo(label, idObj, ...values) | ||
} | ||
valErrMessage(msgName) { | ||
@@ -203,0 +244,0 @@ return this.errorMessageHandler.valErrMessage(msgName); |
@@ -60,3 +60,3 @@ import { | ||
createPropertyValueHandler(opts, config) { | ||
return createPropertyValueResolver(opts, config); | ||
return createPropertyValueResolver(opts, config, this); | ||
} | ||
@@ -63,0 +63,0 @@ |
@@ -8,4 +8,4 @@ import { BasePropertyValueResolver } from "./base-property-value-resolver"; | ||
export class MultiPropertyValueResolver extends BasePropertyValueResolver { | ||
constructor(opts, config) { | ||
super(opts, config); | ||
constructor(opts, config, entryHandler) { | ||
super(opts, config, entryHandler); | ||
} | ||
@@ -12,0 +12,0 @@ |
@@ -10,4 +10,4 @@ import { BasePropertyValueResolver } from "./base-property-value-resolver"; | ||
export class PropertyValueResolver extends BasePropertyValueResolver { | ||
constructor(opts, config) { | ||
super(opts, config); | ||
constructor(opts, config, entryHandler) { | ||
super(opts, config, entryHandler); | ||
this.initResolvers(); | ||
@@ -17,20 +17,20 @@ } | ||
initResolvers() { | ||
const { opts, config } = this; | ||
const { opts, config, entryHandler } = this; | ||
const createMultiTypeResolverFn = | ||
config.createMultiTypeResolver || this.createMultiTypeResolver.bind(this); | ||
this.multiTypeResolver = createMultiTypeResolverFn(opts, config); | ||
this.multiTypeResolver = createMultiTypeResolverFn(opts, config, entryHandler); | ||
const createSingleTypeResolverFn = | ||
config.createSingleTypeResolver || | ||
this.createSingleTypeResolver.bind(this); | ||
this.singleTypeResolver = createSingleTypeResolverFn(opts, config); | ||
this.singleTypeResolver = createSingleTypeResolverFn(opts, config, entryHandler); | ||
} | ||
createMultiTypeResolver() { | ||
const { opts, config } = this; | ||
return new MultiPropertyValueResolver(opts, config); | ||
const { opts, config, entryHandler } = this; | ||
return new MultiPropertyValueResolver(opts, config, entryHandler); | ||
} | ||
createSingleTypeResolver() { | ||
const { opts, config } = this; | ||
return new SinglePropertyValueResolver(opts, config); | ||
const { opts, config, entryHandler } = this; | ||
return new SinglePropertyValueResolver(opts, config, entryHandler); | ||
} | ||
@@ -37,0 +37,0 @@ |
@@ -8,4 +8,4 @@ import { BasePropertyValueResolver } from "./base-property-value-resolver"; | ||
export class SinglePropertyValueResolver extends BasePropertyValueResolver { | ||
constructor(opts, config) { | ||
super(opts, config); | ||
constructor(opts, config, entryHandler) { | ||
super(opts, config, entryHandler); | ||
} | ||
@@ -21,3 +21,3 @@ | ||
const { obj, config } = this; | ||
const { obj, config, entryHandler } = this; | ||
const typeHandlerNames = Object.keys(this.types); | ||
@@ -29,3 +29,3 @@ let result; | ||
if (typeFn) { | ||
result = typeFn(obj, config); | ||
result = typeFn(obj, config, entryHandler); | ||
} | ||
@@ -32,0 +32,0 @@ if (result) break; |
class Loggable { | ||
constructor(config = {}) { | ||
this.config = config; | ||
const { log, error } = config; | ||
const { log, error } = config; | ||
const enable = config.enable || {}; | ||
if (config.logging === true) { | ||
enable.log = true | ||
} | ||
if (config.logging === false) { | ||
enable.log = false | ||
} | ||
this.enable = enable; | ||
@@ -12,17 +18,19 @@ // what type of logger to use | ||
error(errMsg, value) { | ||
error(errMsg, ...values) { | ||
// only disable if directly disabled | ||
if (this.enable.error === false) return; | ||
this.err && (value ? this.err(errMsg, value) : this.err(errMsg)); | ||
if (!this.err) return | ||
values && values.length ? this.err(errMsg, ...values) : this.err(errMsg); | ||
throw errMsg; | ||
} | ||
warn(warnMsg, value) { | ||
warn(warnMsg, ...values) { | ||
if (!this.enable.warn) return; | ||
this.logInfo("WARNING: " + warnMsg, value); | ||
this.logInfo("WARNING: " + warnMsg, ...values); | ||
} | ||
logInfo(name, value) { | ||
logInfo(name, ...values) { | ||
if (!this.enable.log) return; | ||
this.log && (value ? this.log(name, value) : this.log(name)); | ||
if (!this.log) return | ||
values && values.length ? this.log(name, ...values) : this.log(name); | ||
} | ||
@@ -29,0 +37,0 @@ } |
import { TypeMatcher } from '../_type-matcher'; | ||
class Constraint extends TypeMatcher { | ||
constructor(typer, map) { | ||
super(typer.config); | ||
constructor(typeHandler, map) { | ||
super(typeHandler.config); | ||
this.map = map || this.$map || {}; | ||
this.typer = typer; | ||
this.typeHandler = typeHandler; | ||
this.delegates.map(name => { | ||
const delegate = typer[name]; | ||
const delegate = typeHandler[name]; | ||
if (!delegate) { | ||
this.error(`missing delegate: ${name}`, { | ||
typer | ||
typeHandler | ||
}); | ||
} | ||
this[name] = this.isFunctionType(delegate) | ||
? delegate.bind(typer) | ||
? delegate.bind(typeHandler) | ||
: delegate; | ||
@@ -18,0 +18,0 @@ }); |
@@ -8,12 +8,12 @@ import { Constraint } from '../constraints/base'; | ||
class NumericConstraint extends Constraint { | ||
constructor(typer) { | ||
super(typer); | ||
constructor(typeHandler) { | ||
super(typeHandler); | ||
} | ||
transform(value) { | ||
return this.typer.toNumber(value); | ||
return this.typeHandler.toNumber(value); | ||
} | ||
isValidConstraint(value) { | ||
return this.typer.isNumberLike(value); | ||
return this.typeHandler.isNumberLike(value); | ||
} | ||
@@ -20,0 +20,0 @@ |
@@ -8,8 +8,8 @@ import { Constraint } from './base'; | ||
class StringConstraint extends Constraint { | ||
constructor(typer, map = {}) { | ||
super(typer, map); | ||
constructor(typeHandler, map = {}) { | ||
super(typeHandler, map); | ||
} | ||
isValidConstraint(value) { | ||
return this.typer.isStringType(value); | ||
return this.typeHandler.isStringType(value); | ||
} | ||
@@ -16,0 +16,0 @@ |
@@ -22,3 +22,3 @@ import * as yup from "yup"; | ||
init(opts) { | ||
let { schema, key, value, config, validator, entryHandler } = opts; | ||
let { schema, key, value, config, entryHandler } = opts; | ||
config = config || {}; | ||
@@ -46,2 +46,6 @@ schema = schema || {}; | ||
get builder() { | ||
return this.entryHandler && this.entryHandler.builder | ||
} | ||
getBase() { | ||
@@ -56,3 +60,3 @@ return this.customBaseValidator || this.validatorInstance; | ||
getValidator() { | ||
return this.opts.validator || this.config.validator || (this.entryHandler && this.entryHandler.validator) || yup; | ||
return this.opts.validator || this.config.validator || (this.builder && this.builder.validator) || yup; | ||
} | ||
@@ -174,3 +178,4 @@ | ||
"isType", | ||
"label" | ||
"label", | ||
"refValueFor" | ||
] | ||
@@ -297,2 +302,12 @@ ); | ||
refValueFor() { | ||
let propRefName = | ||
this.constraints.refValueFor | ||
if (this.isNothing(propRefName)) return this; | ||
this.logInfo("refValueFor", { propRefName }) | ||
return this.apply('when', (propRefName, (refValueFor, field) => | ||
refValueFor ? field.required().oneOf([yup.ref(propRefName)]) : field | ||
)) | ||
} | ||
oneOf() { | ||
@@ -299,0 +314,0 @@ let values = |
@@ -168,2 +168,6 @@ import * as yup from "yup"; | ||
} | ||
onConstraintAdded(constraint) { | ||
this.logInfo('Constraint Added', constraint) | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
286218
75
3364
1448