UNPKG

json-schema-to-yup

Version:

Build a Yup schema from a JSON Schema. Also supports custom/alternative schema models such as GraphQL type defs

3 lines (2 loc) 25.5 kB
function t(t){return t&&"object"==typeof t&&"default"in t?t.default:t}var e=t(require("dashify")),n=t(require("uppercamelcase")),r=require("yup"),i={getProps:function(t){return t&&t.properties},getType:function(t){return t&&t.type},getName:function(t){return t&&t.name||t.title},getConstraints:function(t){return t},isString:function(t){return t&&"string"===t.type},isArray:function(t){return t&&"array"===t.type},isInteger:function(t){return t&&"integer"===t.type||"int"===t.type},isBoolean:function(t){return t&&"boolean"===t.type},hasDateFormat:function(t){return t&&["date","date-time"].find(function(e){return e===t.format})},isDate:function(t){return t&&"string"===t.type&&i.hasDateFormat(t.format)},isNumber:function(t){return t&&("number"===t.type||i.isInteger(t))},isObject:function(t){return t&&"object"===t.type},isRequired:function(t){return t&&t.required}},o={"json-schema":i,"type-def":{getProps:function(t){return t&&t.fields},getType:function(t){return t&&t.type},getName:function(t){return t&&t.name},getConstraints:function(t){return t&&(t.directives||{}).constraints||{}},isString:function(t){return t&&"String"===t.type},isArray:function(t){return t&&t.isList},isInteger:function(t){return t&&"Int"===t.type},isBoolean:function(t){return t&&"Boolean"===t.type},isDate:function(t){return t&&"Date"===t.type||t.directives.date},isNumber:function(t){return t&&"Int"===t.type||"Float"===t.type},isObject:function(t){return t&&"Object"===t.type},isRequired:function(t){return t&&!t.isNullable}}};function s(t){var e=t.config,n=(e||{}).YupSchemaEntry;if(!n)throw"missing YupSchemaEntry class in config";return new n({name:t.name,key:t.key,value:t.value,config:e}).toEntry()}var a=function(t){void 0===t&&(t={});var e=t.log,n=t.error;this.enable=t.enable||{},this.log="function"==typeof e?e:console.log,this.err="function"==typeof n?n:console.error};a.prototype.error=function(t,e){if(!1!==this.enable.error)throw this.err&&(e?this.err(t,e):this.err(t)),t},a.prototype.warn=function(t,e){this.enable.warn&&this.logInfo("WARNING: "+t,e)},a.prototype.logInfo=function(t,e){this.enable.log&&this.log&&(e?this.log(t,e):this.log(t))};var u=function(t){function e(e){void 0===e&&(e={}),t.call(this,e)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.isNothing=function(t){return null==t},e.prototype.isPresent=function(t){return!this.isNothing(t)},e.prototype.toNumber=function(t){return Number(t)},e.prototype.isNumberLike=function(t){return!isNaN(this.toNumber(t))},e.prototype.isNumberType=function(t){return!isNaN(t)},e.prototype.isStringType=function(t){return"string"==typeof t},e.prototype.isFunctionType=function(t){return"function"==typeof t},e.prototype.isDateType=function(t){return t instanceof Date},e}(a),c=function(t){function e(e){void 0===e&&(e={}),t.call(this,e),e=Object.assign({},{createYupSchemaEntry:s},e),this.config=Object.assign({},o[e.schemaType||"json-schema"],e)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e}(u),p=function(t){function e(){t.apply(this,arguments)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e}(Error),h=["oneOf","enum","required","notRequired","minDate","min","maxDate","max","trim","lowercase","uppercase","email","url","minLength","maxLength","pattern","matches","regex","integer","positive","minimum","maximum"],f={errMessages:function(t){return void 0===t&&(t=h),t.reduce(function(t,e){return t[e]=function(t){var e=t.value;return t.key+": invalid for "+(e.name||e.title)},t},{})}},y=function(t){function e(e){void 0===e&&(e={});var n=e.key,i=e.value,o=e.config;t.call(this,o),this.validateOnCreate(n,i),this.yup=r,this.key=n,this.value=i,this.constraints=this.getConstraints(),this.format=i.format||this.constraints.format,this.config=o||{},this.type="mixed",this.base=r.mixed(),this.errMessages=o.errMessages||{},this.constraintsAdded={},this.rebind("addConstraint","addValueConstraint")}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={enabled:{configurable:!0},constraintsMap:{configurable:!0}};return e.prototype.rebind=function(){for(var t=this,e=[],n=arguments.length;n--;)e[n]=arguments[n];e.map(function(e){var n=t[e];t[e]=t.isFunctionType(n)?n.bind(t):n})},e.prototype.validateOnCreate=function(t,e){t||this.error("create: missing key"),e||this.error("create: missing value")},n.enabled.get=function(){},e.prototype.convertEnabled=function(){var t=this;this.enabled.map(function(e){t[e]&&t[e]()})},e.prototype.getConstraints=function(){return this.config.getConstraints(this.value)},e.prototype.createSchemaEntry=function(){return this.convert().base},e.prototype.convert=function(){return this.addMappedConstraints(),this.oneOf().notOneOf(),this},e.prototype.addValueConstraint=function(t,e){return void 0===e&&(e={}),this.addConstraint(t,{constraintName:e.constraintName,value:!0,errName:e.errName})},e.prototype.addConstraint=function(t,e){void 0===e&&(e={});var n=e.constraintName,r=e.method,i=e.value,o=e.errName,s=this.constraints[t];if(s){if(n=n||t,!this.base[r=r||n])return this.warn("Yup has no such API method: "+r),this;var a=this.base[r].bind(this.base),u=this.valErrMessage(n)||o&&this.valErrMessage(o),c=!0===i?s:i;this.onConstraintAdded({name:n,value:c});var p=c?a(c,u):a(u);this.base=p||this.base}return this},e.prototype.onConstraintAdded=function(t){return this.constraintsAdded[t.name]=t.value,this},e.prototype.addMappedConstraints=function(){var t=this,e=this.constraintsMap;return Object.keys(e).map(function(n){e[n].map(t["value"===n?"addValueConstraint":"addConstraint"])}),this},n.constraintsMap.get=function(){return{simple:["required","notRequired","nullable"],value:["default","strict"]}},e.prototype.oneOf=function(){return this.addConstraint("oneOf",{value:this.constraints.enum||this.constraints.oneOf,errName:"enum"})},e.prototype.notOneOf=function(){var t=this.constraints,e=t.not,n=t.notOneOf;return this.addConstraint("notOneOf",{value:n||e&&(e.enum||e.oneOf)})},e.prototype.valErrMessage=function(t){var e=this.errMessages[this.key]?this.errMessages[this.key][t]:void 0;return"function"==typeof e?e(this.constraints):e},e.prototype.$const=function(){return this},e.prototype.$allOf=function(){return this},e.prototype.$anyOf=function(){return this},e.prototype.$oneOf=function(){return this},e.prototype.$not=function(){return this},e.prototype.$if=function(){return this},e.prototype.$then=function(){return this},e.prototype.$else=function(){return this},e.prototype.message=function(){return config.messages[this.key]||config.messages[this.type]||{}},e.prototype.errMessage=function(t){return void 0===t&&(t="default"),this.message[t]||"error"},e.prototype.toValidJSONSchema=function(){},e.prototype.normalize=function(){},e.prototype.deNormalize=function(){},e.prototype.errorMsg=function(t){this.throwError(t)},e.prototype.error=function(t,e){var n=["["+t+"]",e].join(" ");this.errorMsg(n)},e.prototype.throwError=function(t){throw t},Object.defineProperties(e.prototype,n),e}(c),m=function(t){function e(e){t.call(this,e)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.isArray=function(t){return this.config.isArray||this.error("ArrayHandler: mising isArray in config",this.config),this.config.isArray(t)},e.prototype.handle=function(t){return this.isArray(t)&&d.create(t).createSchemaEntry()},e}(c);function l(t,e){return void 0===e&&(e={}),t&&new m(e).handle(t)}var d=function(t){function e(e){t.call(this,e),this.type="array",this.base=this.yup.array(),this.createYupSchemaEntry=this.config.createYupSchemaEntry}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.create=function(t){return new e(t)},e.prototype.convert=function(){return this.maxItems(),this.minItems(),this.ensureItems(),this.compact(),t.prototype.convert.call(this),this},e.prototype.ensureItems=function(){return this.addConstraint("ensure")},e.prototype.compact=function(){return this.addConstraint("compact")},e.prototype.itemsOf=function(){return this},e.prototype.maxItems=function(){var t=this.constraints,e=t.maxItems,n=e||t.max;if(!this.isNumberType(n))return this;if(!this.isValidSize(n))return this.handleInvalidSize("maxItems",n);var r=n&&this.base.max(n);return this.base=r||this.base,this},e.prototype.minItems=function(){var t=this.constraints,e=t.minItems,n=e||t.min;if(!this.isNumberType(n))return this;if(!this.isValidSize(n))return this.handleInvalidSize("minItems",n);var r=n&&this.base.min(n);return this.base=r||this.base,this},e.prototype.$items=function(){return this},e.prototype.$additionalItems=function(){return this},e.prototype.$uniqueItems=function(){return this},e.prototype.$contains=function(){return this},e.prototype.handleInvalidSize=function(t,e){var n="invalid array size constraint for "+t+", was "+e+". Must be a number >= 0";return this.config.warnOnInvalid?(this.warn(n),this):(this.error(n,e),this)},e.prototype.isValidSize=function(t){return this.isNumberType(t)&&t>=0},e}(y),g=function(t){this.config=t};function v(t,e){return void 0===e&&(e={}),t&&new g(e).handle(t)}g.prototype.isBoolean=function(t){return this.config.isBoolean(t)},g.prototype.handle=function(t){return this.isBoolean(t)&&b.create(t).createSchemaEntry()};var b=function(t){function e(e){t.call(this,e),this.type="boolean",this.base=this.yup.boolean()}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.create=function(t){return new e(t)},e}(y);var j=function(t){function e(e){t.call(this,e)}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={$map:{configurable:!0}};return n.$map.get=function(){return{moreThan:["exclusiveMinimum","moreThan"],lessThan:["exclusiveMaximum","lessThan"],max:["maximum","max"],min:["minimum","min"]}},Object.defineProperties(e.prototype,n),e}(function(t){function e(e){t.call(this,e)}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={explainConstraintValidMsg:{configurable:!0}};return e.prototype.transform=function(t){return this.typer.toNumber(t)},e.prototype.isValidConstraint=function(t){return this.typer.isNumberLike(t)},n.explainConstraintValidMsg.get=function(){return"Must be a number or convertible to a number"},Object.defineProperties(e.prototype,n),e}(function(t){function e(e,n){var r=this;t.call(this,e.config),this.map=n||this.$map||{},this.typer=e,this.delegates.map(function(t){var n=e[t];n||r.error("missing delegate: "+t,{typer:e}),r[t]=r.isFunctionType(n)?n.bind(e):n})}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={delegates:{configurable:!0},explainConstraintValidMsg:{configurable:!0}};return e.prototype.isStringType=function(t){return"string"==typeof t},n.delegates.get=function(){return["constraints","addConstraint","constraintsAdded"]},e.prototype.add=function(){var t=this,e=this.map;Object.keys(e).map(function(n){var r=t.entryNames(e[n]);t.addConstraints(n,r)})},e.prototype.entryNames=function(t){return Array.isArray(t)?t:[t]},e.prototype.addConstraints=function(t,e){var n=this;return void 0===e&&(e=[]),e.map(function(e){var r=n.validateAndTransform(e);n.addConstraint(e,{method:t,value:r})}),this},e.prototype.validateAndTransform=function(t){var e=this.constraints[t];return this.validate(e),this.transform(e)},e.prototype.invalidMsg=function(t,e){return"invalid constraint for "+t+", was "+e+"."},n.explainConstraintValidMsg.get=function(){return""},e.prototype.invalidConstraintMsg=function(t,e){return[this.invalidMsg(t,e),this.explainConstraintValidMsg].join("\n")},e.prototype.validate=function(t){return this.isNothing(t)?this:this.isValidConstraint(t)?void 0:this.handleInvalidConstraint(name,t)},e.prototype.isValidConstraint=function(t){return!0},e.prototype.handleInvalidConstraint=function(t,e){var n=this.invalidConstraintMsg(t,e);return this.config.warnOnInvalid?(this.warn(n),this):(this.error(n,e),this)},Object.defineProperties(e.prototype,n),e}(u))),O=function(t){function e(e,n){t.call(this,e,n)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.isValid=function(){return this.config.isNumber(this.obj)},e}(function(t){function e(e,n){t.call(this,n),this.obj=e}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.isValid=function(){return!1},e.prototype.verify=function(){return this.isPresent(this.obj)&&this.isValid(this.obj)},e}(c));var _=function(t,e){return void 0===e&&(e={}),function(t,e){return new O(t,e)}(t,e).verify()};function N(t,e){return void 0===e&&(e={}),_(t,e)&&function(t){return C.schemaEntryFor(t)}(t)}var C=function(t){function e(e){t.call(this,e),this.type=this.normalizeNumType(e.type),this.base=this.yup.number(),this.range=new j(this)}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={enabled:{configurable:!0},isInteger:{configurable:!0},isNegative:{configurable:!0},isPositive:{configurable:!0}};return e.prototype.normalizeNumType=function(t){return"int"===t?"integer":t},e.create=function(t){return new e(t)},e.schemaEntryFor=function(t){return e.create(t).createSchemaEntry()},n.enabled.get=function(){return["range","posNeg","integer"]},e.prototype.convert=function(){var e=this;return this.enabled.map(function(t){return e.processConstraint(t)}),t.prototype.convert.call(this),this},e.prototype.processConstraint=function(t){var e=this[t];e&&"function"==typeof e?e.bind(this)():e.add()},e.prototype.truncate=function(){return this.addConstraint("truncate")},e.prototype.round=function(){var t=this.constraints.round;if(this.isNothing(t))return this;var e=this.isStringType(t)?t:"round";return t&&this.base.round(e),this},e.prototype.posNeg=function(){this.positive(),this.negative()},e.prototype.integer=function(){return this.isInteger&&this.addConstraint("integer"),this},n.isInteger.get=function(){return this.config.isInteger(this.type)},e.prototype.positive=function(){return this.addConstraint("positive")},e.prototype.negative=function(){return this.addConstraint("negative")},n.isNegative.get=function(){var t=this.constraints,e=t.exclusiveMaximum;return!!t.negative||void 0!==e&&0===e},n.isPositive.get=function(){var t=this.constraints,e=t.exclusiveMinimum;return!!t.positive||void 0!==e&&0===e},e.prototype.normalize=function(){this.constraints.maximum=this.constraints.maximum||this.constraints.max,this.constraints.minimum=this.constraints.minimum||this.constraints.min},Object.defineProperties(e.prototype,n),e}(y),x=function(t){this.config=t};function E(t,e){return void 0===e&&(e={}),t&&new x(e).handle(t)}x.prototype.isObject=function(t){return this.config.isObject(t)},x.prototype.handle=function(t){return this.isObject(t)&&w.create(t).createSchemaEntry()};var w=function(t){function e(e){t.call(this,e),this.type="object",this.base=this.yup.object(),this.properties=this.value.properties}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.create=function(t){return new e(t)},e.prototype.convert=function(){if(!this.properties)return this;if(this.noUnknown(),this.camelCase().constantCase(),this.value){var t=L(this.value);this.base.shape(t)}return this},e.prototype.camelCase=function(){return this.addConstraint("camelCase")},e.prototype.constantCase=function(){return this.addConstraint("constantCase")},e.prototype.noUnknown=function(){var t=this.value,e=t.noUnknown,n=e||t.propertyNames,r=n&&this.base.noUnknown(n,this.valErrMessage("propertyNames")||this.valErrMessage("noUnknown"));return this.base=r||this.base,this},e}(y),S=function(t){this.config=t};function M(t,e){return void 0===e&&(e={}),t&&new S(e).handle(t)}S.prototype.isString=function(t){return this.config.isString(t)},S.prototype.handle=function(t){return this.isString(t)&&T.create(Object.assign({},{config:this.config},t)).createSchemaEntry()};var T=function(t){function e(e){t.call(this,e),this.type="string",this.base=this.yup.string()}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={isEmail:{configurable:!0},isUrl:{configurable:!0}};return e.create=function(t){return new e(t)},e.prototype.convert=function(){return t.prototype.convert.call(this),this.normalize(),this.minLength().maxLength().pattern(),this.lowercase().uppercase(),this.email(),this.url(),this.genericFormat(),this},e.prototype.trim=function(){return this.addConstraint("trim")},e.prototype.lowercase=function(){return this.addConstraint("lowercase")},e.prototype.uppercase=function(){return this.addConstraint("uppercase")},e.prototype.genericFormat=function(){!0!=!this.config.format&&(this.yup.prototype[this.format]&&this.addConstraint(this.format))},e.prototype.email=function(){return this.isEmail&&this.addConstraint("email"),this},n.isEmail.get=function(){return this.constraints.email||"email"===this.format},e.prototype.url=function(){return this.isUrl&&this.addConstraint("url"),this},n.isUrl.get=function(){return this.constraints.url||"url"===this.format},e.prototype.minLength=function(){var t=this.constraints.minLength,e=this.valErrMessage("minLength")||this.valErrMessage("min"),n=t&&this.base.min(t,e);return this.base=n||this.base,this},e.prototype.maxLength=function(){var t=this.constraints.maxLength,e=this.valErrMessage("maxLength")||this.valErrMessage("max"),n=t&&this.base.max(t,e);return this.base=n||this.base,this},e.prototype.pattern=function(){var t=this.constraints.pattern;if(!t)return this;var e=new RegExp(t),n=this.valErrMessage("pattern")||this.valErrMessage("matches")||this.valErrMessage("regex"),r=e&&this.base.matches(e,n);return this.base=r||this.base,this},e.prototype.normalize=function(){this.constraints.pattern=this.constraints.pattern||this.constraints.matches||this.constraints.regex,this.constraints.maxLength=this.constraints.maxLength||this.constraints.max,this.constraints.minLength=this.constraints.minLength||this.constraints.min},Object.defineProperties(e.prototype,n),e}(y),D=function(t){this.config=t};function I(t,e){return void 0===e&&(e={}),t&&new D(e).handle(t)}D.prototype.isDate=function(t){return this.config.isDate(t)},D.prototype.handle=function(t){return this.isDate(t)&&Y.create(t).createSchemaEntry()};var Y=function(t){function e(e){t.call(this,e),this.type="date",this.base=this.yup.date()}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.create=function(t){return new e(t)},e.prototype.convert=function(){return this.minDate().maxDate(),t.prototype.convert.call(this),this},e.prototype.toDate=function(t){return new Date(t)},e.prototype.isValidDateType=function(t){return this.isStringType(t)||this.isDateType(t)},e.prototype.isValidDate=function(t){return!!this.isValidDateType(t)&&(!this.isStringType(t)||Boolean(Date.parse(t)))},e.prototype.transformToDate=function(t){return this.isNumberType(t)?new Date(t):t},e.prototype.minDate=function(){var t=this.constraints.minDate||this.constraints.min;if(this.isNothing(t))return this;var e=this.transformToDate(t);if(!this.isValidDateType(e))return this.handleInvalidDate("minDate",e);var n=e&&this.base.min(this.toDate(e),this.valErrMessage("minDate")||this.valErrMessage("min"));return this.base=n||this.base,this},e.prototype.maxDate=function(){var t=this.constraints.maxDate||this.constraints.max;if(this.isNothing(t))return this;var e=this.transformToDate(t);if(!this.isValidDateType(e))return this.handleInvalidDate("maxDate",e);var n=e&&this.base.max(this.toDate(e),this.valErrMessage("maxDate")||this.valErrMessage("max"));return this.base=n||this.base,this},e.prototype.handleInvalidDate=function(t,e){var n="invalid constraint for "+t+", was "+e+". Must be a number, string (valid date format) or a Date instance";return this.config.warnOnInvalid?(this.warn(n),this):(this.error(n,e),this)},e}(y),k={errValKeys:h,defaults:f,YupArray:d,toYupArray:l,YupBoolean:b,toYupBoolean:v,YupNumber:C,toYupNumberSchemaEntry:N,toYupNumber:function(t,e){return void 0===e&&(e={}),_(t,e)&&function(t){return C.create(t)}(t)},YupObject:w,toYupObject:E,YupString:T,toYupString:M,YupDate:Y,toYupDate:I,YupMixed:y,ConvertYupSchemaError:p,Base:c},V=function(t){function e(){t.apply(this,arguments)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e}(Error),A=function(t){function e(e){var n=e.name,r=e.key,i=e.value,o=e.config;t.call(this,o),this.key=r,this.value=i,this.config=o,this.name=n,this.type=i.type,this.types={string:M,number:N,boolean:v,array:l,object:E,date:I}}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={obj:{configurable:!0}};return e.prototype.isValidSchema=function(){return"string"==typeof this.type},e.prototype.error=function(t){throw new V(t)},e.prototype.toEntry=function(){this.isValidSchema()||this.error("Not a valid schema");var t=this.obj;return this.string(t)||this.number(t)||this.boolean(t)||this.array(t)||this.object(t)||this.date(t)||this.mixed(t)},n.obj.get=function(){return{key:this.key,value:this.value,type:this.type,config:this.config}},e.prototype.string=function(t){return M(t||this.obj,this.config)},e.prototype.number=function(t){return N(t||this.obj,this.config)},e.prototype.boolean=function(t){return v(t||this.obj,this.config)},e.prototype.array=function(t){return l(t||this.obj,this.config)},e.prototype.object=function(t){return E(t||this.obj,this.config)},e.prototype.date=function(t){return I(t||this.obj,this.config)},Object.defineProperties(e.prototype,n),e}(c),q=function(t,e){return void 0===e&&(e={}),t.reduce(function(t,n){if("string"!=typeof n&&!(n instanceof Object)){if(!1!==e.throws)throw"toConstraintsMap: invalid entry "+n;return t}if("string"==typeof n)t[name]={};else{if(!n.name){if(!1!==e.throws)throw"toConstraintsMap: invalid entry "+n+" missing name";return t}t[n.name]=n}return t},{})},P={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:{}},z={createValidatorName:function(t,e){return"is"+(t=(t=(t=n(t||e)).replace(/Uri$/,"URI")).replace(/Id$/,"ID"))},createTestName:function(t,n){return e(t||n)}},$={isMagnetURI:function(t,e){return/magnet:\?xt=urn:[a-z0-9]+:[a-z0-9]{32}/i.test(t)}};function L(t,e){return void 0===e&&(e={}),new R(t,e).yupSchema}var R=function(t){function e(e,n){void 0===n&&(n={}),t.call(this,n),this.schema=e;var r,i=this.getType(e),o=this.getProps(e);if(this.type=i,this.properties=o,this.required=this.getRequired(e),function(t){return t&&"object"===t}(i)){if((r=o)===Object(r)){var s=this.getName(e),a=this.normalizeRequired(e),u=this.propsToShape({properties:a,name:s,config:n});return this.shapeConfig=u,void(this.validSchema=!0)}this.error("invalid schema: must have a properties object: "+JSON.stringify(properties))}else this.error("invalid schema: must be an object type, was: "+i)}t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e;var n={yupSchema:{configurable:!0}};return e.prototype.getRequired=function(t){var e=this.config.getRequired;return e?e(t):t.required||[]},e.prototype.getProps=function(t){return this.config.getProps(t)},e.prototype.getType=function(t){return this.config.getType(t)},e.prototype.getName=function(t){return this.config.getName(t)},n.yupSchema.get=function(){return r.object().shape(this.shapeConfig)},e.prototype.normalizeRequired=function(){var t=this,e=Object.assign({},this.properties),n=[].concat(this.required)||[];return Object.keys(e).reduce(function(r,i){var o=e[i],s=n.indexOf(i)>=0;return o.required=t.isRequired(o)||s,r[i]=o,r},{})},e.prototype.isRequired=function(t){return this.config.isRequired(t)},e.prototype.propsToShape=function(t){var e=this,n=t.name,r=Object.assign({},this.properties);return Object.keys(r).reduce(function(t,i){var o=e.propToYupSchemaEntry({name:n,key:i,value:r[i]});return e.logInfo("propsToShape",{key:i,yupSchemaEntry:o}),t[i]=o,t},{})},e.prototype.propToYupSchemaEntry=function(t){var e=t.value;return void 0===e&&(e={}),(this.createYupSchemaEntry||this.config.createYupSchemaEntry)({name:t.name,key:t.key,value:e,config:this.config})},e.prototype.createYupSchemaEntry=function(t){return new A({name:t.name,key:t.key,value:t.value,config:t.config}).toEntry()},Object.defineProperties(e.prototype,n),e}(c);exports.buildYup=L,exports.YupBuilder=R,exports.YupSchemaEntry=A,exports.YupSchemaEntryError=V,exports.types=k,exports.createYupSchemaEntry=s,exports.extendYupApi=function(t){var e=this;void 0===t&&(t={});var n=t.constraints,i=t.override;void 0===i&&(i=!1);var o=t.validator,s=t.createValidatorName,a=t.createTestName;if(!o)throw"extendYupApi: missing validator option";Array.isArray(n)&&(n=q(n)),n=i?n||P:Object.assign({},P,n||{}),s=s||z.createValidatorName,a=a||z.createTestName,Object.keys(n).map(function(t){var i=n[t],u=i.testName,c=i.optsKey,p=i.logging,h=s(i.validatorName,t);u=a(u,t),r.addMethod(r.string,t,function(n){void 0===n&&(n={});var i=n.message,s=n[c];return r.string().test(u,i,function(n){var r=e.path,a=e.createError,c=o[h];if("function"!=typeof(c=c||$[h]))throw Error("No method named ${validatorName} on validator");var f=c(n,s);return!0===p&&console.log("Yup validator bridge",{key:t,fullValidatorName:h,testName:u,value:n,valid:f}),f||a({path:r,message:i})})})})}; //# sourceMappingURL=index.js.map