Comparing version 2.2.0 to 2.2.1
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Morphism=t():e.Morphism=t()}(this,function(){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t,r){"use strict";function n(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),r.push.apply(r,n)}return r}function o(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}r.r(t);var a=Symbol("SchemaOptions");function i(e){return l(e)&&(e.hasOwnProperty("fn")||e.hasOwnProperty("path"))}function c(e){return h(e)}function s(e){return Array.isArray(e)&&e.every(c)}var u=(e,t)=>e.reduce((e,r)=>(m(e,r,v(t,r)),e),{});function p(e){return void 0===e}function l(e){var t=typeof e;return null!=e&&("object"===t||"function"===t)}function h(e){return"string"==typeof e||e instanceof String}function f(e){return"function"==typeof e}function d(e){if(Promise&&Promise.resolve)return Promise.resolve(e)==e;throw new Error("Promise not supported in your environment")}function v(e,t){for(var r=(t=(t=t.replace(/\[(\w+)\]/g,".$1")).replace(/^\./,"")).split("."),n=0,o=r.length;n<o;++n){var a=r[n];if(!(l(e)&&a in e))return;e=e[a]}return e}function y(e,t){return e.reduce((e,r,a)=>(function(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?n(r,!0).forEach(function(t){o(e,t,r[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):n(r).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))})}return e})({},e,{[r]:t[a]}),{})}function g(e,t){return"number"==typeof t&&Array.isArray(e)||function(e,t){return null!=e&&Object.prototype.hasOwnProperty.call(e,t)}(e,t)}function m(e,t,r,n){if("number"==typeof t&&(t=[t]),!t||0===t.length)return e;if("string"==typeof t)return m(e,t.split(".").map(b),r,n);var o=t[0],a=function(e,t){if(g(e,t))return e[t]}(e,o);return 1===t.length?(void 0!==a&&n||(e[o]=r),a):(void 0===a&&("number"==typeof t[1]?e[o]=[]:e[o]={}),m(e[o],t.slice(1),r,n))}function b(e){var t=parseInt(e);return t.toString()===e?t:e}function w(e){for(var t in e)if(Object.prototype.hasOwnProperty.call(e,t))return!1;return!0}var x=Symbol("errors");class P extends Error{constructor(e){super("Invalid value ".concat(e.value," supplied at property ").concat(e.targetProperty,". Expecting: ").concat(e.expect)),this.targetProperty=void 0,this.value=void 0,this.expect=void 0,this.targetProperty=e.targetProperty,this.value=e.value,this.expect=e.expect}}class O extends Error{constructor(e,t){super(),this.errors=void 0,this.reporter=void 0,this.target=void 0,this.errors=new Set,this.reporter=e,this.target=t}addError(e){this.errors.add(e);var t=this.reporter.report(this.target);t&&(this.message=t.join("\n"))}}function j(e){return e&&e[x]&&e[x].errors.size>0}function S(e){var{message:t}=e;return t}class A{constructor(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:S;this.formatter=e}report(e){var t=this.extractErrors(e);return t?[...t.errors.values()].map(this.formatter):null}extractErrors(e){return j(e)?e[x]:null}}var E,M=new A;class R extends Error{constructor(e){super(e.expect),this.value=void 0,this.expect=void 0,this.value=e.value,this.expect=e.expect}}function _(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),r.push.apply(r,n)}return r}function V(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?_(r,!0).forEach(function(t){N(e,t,r[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):_(r).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))})}return e}function N(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function T(e,t){return t&&!w(t)&&(e[a]=t),e}!function(e){e.Root="Root",e.Property="Property",e.ActionFunction="ActionFunction",e.ActionAggregator="ActionAggregator",e.ActionString="ActionString",e.ActionSelector="ActionSelector"}(E||(E={}));class D{constructor(e){this.schemaOptions=void 0,this.root=void 0,this.schema=void 0,this.schema=e,this.schemaOptions=D.getSchemaOptions(this.schema),this.root={data:{targetPropertyPath:"",propertyName:"MorphismTreeRoot",action:null,kind:E.Root},parent:null,children:[]},e&&this.parseSchema(e)}static getSchemaOptions(e){return V({},{class:{automapping:!0},undefinedValues:{strip:!1}},{},e?e[a]:void 0)}parseSchema(e,t,r){if((h(n=e)||f(n)||i(n)||s(n))&&t)this.add({propertyName:t,action:e},r),r=r?"".concat(r,".").concat(t):t;else{if(t){if(l(e)&&w(e))throw new Error("A value of a schema property can't be an empty object. Value ".concat(JSON.stringify(e)," found for property ").concat(t));this.add({propertyName:t,action:e},r),r=r?"".concat(r,".").concat(t):t}Array.isArray(e)?e.forEach((e,t)=>{this.parseSchema(e,t.toString(),r)}):l(e)&&Object.keys(e).forEach(t=>{this.parseSchema(e[t],t,r)})}var n}*traverseBFS(){var e=[];for(e.push(this.root);e.length>0;){var t=e.shift();if(t){for(var r=0,n=t.children.length;r<n;r++)e.push(t.children[r]);t.data.kind!==E.Root&&(yield t)}}}add(e,t){var r={data:V({},e,{kind:this.getActionKind(e),targetPropertyPath:""}),parent:null,children:[]};if(r.data.preparedAction=this.getPreparedAction(r.data),t)for(var n of this.traverseBFS())n.data.targetPropertyPath===t&&(r.parent=n,r.data.targetPropertyPath="".concat(n.data.targetPropertyPath,".").concat(r.data.propertyName),n.children.push(r));else r.parent=this.root,r.data.targetPropertyPath=r.data.propertyName,this.root.children.push(r)}getActionKind(e){if(c(e.action))return E.ActionString;if(f(e.action))return E.ActionFunction;if(i(e.action))return E.ActionSelector;if(s(e.action))return E.ActionAggregator;if(l(e.action))return E.Property;throw new Error("The action specified for ".concat(e.propertyName," is not supported."))}getPreparedAction(e){var{propertyName:t,action:r,kind:n}=e;return c(r)?e=>{var{object:t}=e;return v(t,r)}:f(r)?e=>{var{object:t,items:n,objectToCompute:o}=e;return r.call(void 0,t,n,o)}:s(r)?e=>{var{object:t}=e;return u(r,t)}:i(r)?e=>{var n,{object:o,items:a,objectToCompute:i}=e;if(r.path?Array.isArray(r.path)?n=u(r.path,o):h(r.path)&&(n=v(o,r.path)):n=o,r.fn)try{n=r.fn.call(void 0,n,o,a,i)}catch(e){throw e.message="Unable to set target property [".concat(t,"].\n \n An error occured when applying [").concat(r.fn.name,"] on property [").concat(r.path,"]\n \n Internal error: ").concat(e.message),e}if(r.validation)try{n=r.validation.validate(n)}catch(e){if(!(e instanceof R))throw e;var c=new P({targetProperty:t,expect:e.expect,value:e.value});j(i)?i[x].addError(c):(this.schemaOptions.validation&&this.schemaOptions.validation.reporter?i[x]=new O(this.schemaOptions.validation.reporter,i):i[x]=new O(M,i),i[x].addError(c))}return n}:n===E.Property?null:void 0}}function k(e){return(t,r,n)=>{var o=n.value;return"function"==typeof o&&(n.value=function(){for(var t=arguments.length,r=new Array(t),n=0;n<t;n++)r[n]=arguments[n];var a=o.apply(this,r);return d(a)?Promise.resolve(a).then(t=>e(t)):e(a)}),n}}class F{constructor(e){this.rules=new Map,this.validate=e=>[...this.rules.values()].reduce((e,t)=>t.test(e),e),this.addRule(e)}addRule(e){if(this.rules.has(e.name))throw new Error("Rule ".concat(e.name," has already been used"));this.rules.set(e.name,e)}}var q={get:(e,t)=>{if(t in e)return e[t];if(B.has(t))return B.get(t);throw new Error("The validator ".concat(t,"() does not exist. Did you forget to call Validation.addValidator(name, validator)"))}},B=new Map,C=new Proxy({addValidator:(e,t)=>{B.set(e,()=>new t)}},q);function I(e,t,r,n){var o=t.schemaOptions,a=[];for(var i of t.traverseBFS()){var{preparedAction:c,targetPropertyPath:s}=i.data;c&&a.push({targetPropertyPath:s,preparedAction:c({object:e,objectToCompute:n,items:r})})}return a.reduce((e,t)=>{var r=((e,t)=>p(t)?p(e)?void 0:e:t)(v(e,t.targetPropertyPath),t.preparedAction);return void 0===r?(o&&o.undefinedValues&&o.undefinedValues.strip?o.undefinedValues.default&&m(e,t.targetPropertyPath,o.undefinedValues.default(e,t.targetPropertyPath)):m(e,t.targetPropertyPath,r),z(o,e),e):(m(e,t.targetPropertyPath,r),z(o,e),e)},n)}function z(e,t){if(e&&e.validation&&e.validation.throw&&j(t)){var r;if(e.validation.reporter)r=e.validation.reporter.extractErrors(t);else r=M.extractErrors(t);if(r)throw r}}function J(e,t){var r,n=D.getSchemaOptions(e);if(t&&n.class&&n.class.automapping){var o=function(e,t){var r=Object.keys(new e),n=y(r,r);return Object.assign(n,t)}(t,e);r=new D(o)}else r=new D(e);return e=>{if(!e)return e;if(Array.isArray(e))return e.map(n=>{if(t){var o=new t;return I(n,r,e,o)}return I(n,r,e,{})});var n=e;if(t){var o=new t;return I(n,r,[n],o)}return I(n,r,[n],{})}}function K(e,t,r){switch(arguments.length){case 1:return J(e);case 2:return J(e)(t);case 3:if(r)return null!==t?J(e,r)(t):J(e,r);throw new Error("When using morphism(schema, items, type), type should be defined but value received is ".concat(r))}}function $(e,t){return k(J(e,t))}function H(e){return k(J(e))}function L(e,t){return k(J(e,t))}C.addValidator("string",class extends F{constructor(){super({name:"string",expect:"value to be typeof string",test:function(e){var t=e;if(!h(t))throw new R({value:e,expect:this.expect});return t}})}min(e){return this.addRule({name:"min",expect:"value to be greater or equal than ".concat(e),test:function(t){if(t.length<e)throw new R({value:t,expect:this.expect});return t}}),this}max(e){return this.addRule({name:"max",expect:"value to be less or equal than ".concat(e),test:function(t){if(t.length>e)throw new R({value:t,expect:this.expect});return t}}),this}length(e){return this.addRule({name:"length",expect:"value to be length of ".concat(e),test:function(t){if(t.length!==e)throw new R({value:t,expect:this.expect});return t}}),this}createRegexRule(e,t,r){return{name:e,expect:t,test:function(e){if(!r.test(e))throw new R({value:e,expect:this.expect});return e}}}regex(e){var t=this.createRegexRule("regex","value to match pattern: ".concat(e),e);return this.addRule(t),this}alphanum(){var e=this.createRegexRule("regex","value to contain only alphanumeric characters",/^[a-z0-9]+$/i);return this.addRule(e),this}}),C.addValidator("number",class extends F{constructor(){super({name:"number",expect:"value to be typeof number",test:function(e){var t=+e;if(isNaN(t))throw new R({value:e,expect:this.expect});return t}})}min(e){return this.addRule({name:"min",expect:"value to be greater or equal than ".concat(e),test:function(t){if(t<e)throw new R({value:t,expect:this.expect});return t}}),this}max(e){return this.addRule({name:"max",expect:"value to be less or equal than ".concat(e),test:function(t){if(t>e)throw new R({value:t,expect:this.expect});return t}}),this}}),C.addValidator("boolean",class extends F{constructor(){super({name:"boolean",expect:"value to be typeof boolean",test:function(e){if("boolean"==typeof e)return e;if(/true/i.test(e))return!0;if(/false/i.test(e))return!1;throw new R({value:e,expect:this.expect})}})}}),r.d(t,"morph",function(){return $}),r.d(t,"toJSObject",function(){return H}),r.d(t,"toClassObject",function(){return L}),r.d(t,"morphism",function(){return K}),r.d(t,"createSchema",function(){return T}),r.d(t,"Schema",function(){}),r.d(t,"StrictSchema",function(){}),r.d(t,"SchemaOptions",function(){}),r.d(t,"Mapper",function(){}),r.d(t,"SCHEMA_OPTIONS_SYMBOL",function(){return a}),r.d(t,"Reporter",function(){return A}),r.d(t,"reporter",function(){return M}),r.d(t,"Formatter",function(){}),r.d(t,"Validation",function(){}),r.d(t,"BaseValidator",function(){return F}),r.d(t,"Rule",function(){});var U=new class{constructor(){this._registry=null,this._registry={cache:new Map}}register(e,t){if(!e&&!t)throw new Error("type paramater is required when you register a mapping");if(this.exists(e))throw new Error("A mapper for ".concat(e.name," has already been registered"));var r;return r=K(t||{},null,e),this._registry.cache.set(e,r),r}map(e,t){if(!this.exists(e)){var r=this.register(e);if(void 0===t)return r}return this.getMapper(e)(t)}getMapper(e){return this._registry.cache.get(e)}setMapper(e,t){if(t){if(this.exists(e)){var r=K(t,null,e);return this._registry.cache.set(e,r),r}throw new Error("The type ".concat(e.name," is not registered. Register it using `Mophism.register(").concat(e.name,", schema)`"))}throw new Error("The schema must be an Object. Found ".concat(t))}deleteMapper(e){return this._registry.cache.delete(e)}exists(e){return this._registry.cache.has(e)}get mappers(){return this._registry.cache}},W=K;W.register=(e,t)=>U.register(e,t),W.map=(e,t)=>U.map(e,t),W.getMapper=e=>U.getMapper(e),W.setMapper=(e,t)=>U.setMapper(e,t),W.deleteMapper=e=>U.deleteMapper(e),W.mappers=U.mappers;var Y=W;t.default=Y}])}); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Morphism=t():e.Morphism=t()}(this,function(){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t,r){"use strict";function n(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),r.push.apply(r,n)}return r}function o(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}r.r(t);var a=Symbol("SchemaOptions");function i(e){return l(e)&&(e.hasOwnProperty("fn")||e.hasOwnProperty("path"))}function c(e){return h(e)}function s(e){return Array.isArray(e)&&e.every(c)}var u=(e,t)=>e.reduce((e,r)=>(m(e,r,v(t,r)),e),{});function p(e){return void 0===e}function l(e){var t=typeof e;return null!=e&&("object"===t||"function"===t)}function h(e){return"string"==typeof e||e instanceof String}function f(e){return"function"==typeof e}function d(e){if(Promise&&Promise.resolve)return Promise.resolve(e)==e;throw new Error("Promise not supported in your environment")}function v(e,t){for(var r=(t=(t=t.replace(/\[(\w+)\]/g,".$1")).replace(/^\./,"")).split("."),n=0,o=r.length;n<o;++n){var a=r[n];if(!(l(e)&&a in e))return;e=e[a]}return e}function y(e,t){return e.reduce((e,r,a)=>(function(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?n(r,!0).forEach(function(t){o(e,t,r[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):n(r).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))})}return e})({},e,{[r]:t[a]}),{})}function g(e,t){return"number"==typeof t&&Array.isArray(e)||function(e,t){return null!=e&&Object.prototype.hasOwnProperty.call(e,t)}(e,t)}function m(e,t,r,n){if("number"==typeof t&&(t=[t]),!t||0===t.length)return e;if("string"==typeof t)return m(e,t.split(".").map(b),r,n);var o=t[0],a=function(e,t){if(g(e,t))return e[t]}(e,o);return 1===t.length?(void 0!==a&&n||(e[o]=r),a):(void 0===a&&("number"==typeof t[1]?e[o]=[]:e[o]={}),m(e[o],t.slice(1),r,n))}function b(e){var t=parseInt(e);return t.toString()===e?t:e}function w(e){for(var t in e)if(Object.prototype.hasOwnProperty.call(e,t))return!1;return!0}var x=Symbol("errors");class P extends Error{constructor(e){super("Invalid value ".concat(e.value," supplied at property ").concat(e.targetProperty,". Expecting: ").concat(e.expect)),this.targetProperty=void 0,this.value=void 0,this.expect=void 0,this.targetProperty=e.targetProperty,this.value=e.value,this.expect=e.expect}}class O extends Error{constructor(e,t){super(),this.errors=void 0,this.reporter=void 0,this.target=void 0,this.errors=new Set,this.reporter=e,this.target=t}addError(e){this.errors.add(e);var t=this.reporter.report(this.target);t&&(this.message=t.join("\n"))}}function j(e){return e&&e[x]&&e[x].errors.size>0}function S(e){var{message:t}=e;return t}class A{constructor(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:S;this.formatter=e}report(e){var t=this.extractErrors(e);return t?[...t.errors.values()].map(this.formatter):null}extractErrors(e){return j(e)?e[x]:null}}var E,M=new A;class R extends Error{constructor(e){super(e.expect),this.value=void 0,this.expect=void 0,this.value=e.value,this.expect=e.expect}}function _(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),r.push.apply(r,n)}return r}function V(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?_(r,!0).forEach(function(t){N(e,t,r[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):_(r).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))})}return e}function N(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function T(e,t){return t&&!w(t)&&(e[a]=t),e}!function(e){e.Root="Root",e.Property="Property",e.ActionFunction="ActionFunction",e.ActionAggregator="ActionAggregator",e.ActionString="ActionString",e.ActionSelector="ActionSelector"}(E||(E={}));class D{constructor(e){this.schemaOptions=void 0,this.root=void 0,this.schema=void 0,this.schema=e,this.schemaOptions=D.getSchemaOptions(this.schema),this.root={data:{targetPropertyPath:"",propertyName:"MorphismTreeRoot",action:null,kind:E.Root},parent:null,children:[]},e&&this.parseSchema(e)}static getSchemaOptions(e){return V({},{class:{automapping:!0},undefinedValues:{strip:!1}},{},e?e[a]:void 0)}parseSchema(e,t,r){if((h(n=e)||f(n)||i(n)||s(n))&&t)this.add({propertyName:t,action:e},r),r=r?"".concat(r,".").concat(t):t;else{if(t){if(l(e)&&w(e))throw new Error("A value of a schema property can't be an empty object. Value ".concat(JSON.stringify(e)," found for property ").concat(t));this.add({propertyName:t,action:e},r),r=r?"".concat(r,".").concat(t):t}Array.isArray(e)?e.forEach((e,t)=>{this.parseSchema(e,t.toString(),r)}):l(e)&&Object.keys(e).forEach(t=>{this.parseSchema(e[t],t,r)})}var n}*traverseBFS(){var e=[];for(e.push(this.root);e.length>0;){var t=e.shift();if(t){for(var r=0,n=t.children.length;r<n;r++)e.push(t.children[r]);t.data.kind!==E.Root&&(yield t)}}}add(e,t){var r={data:V({},e,{kind:this.getActionKind(e),targetPropertyPath:""}),parent:null,children:[]};if(r.data.preparedAction=this.getPreparedAction(r.data),t)for(var n of this.traverseBFS())n.data.targetPropertyPath===t&&(r.parent=n,r.data.targetPropertyPath="".concat(n.data.targetPropertyPath,".").concat(r.data.propertyName),n.children.push(r));else r.parent=this.root,r.data.targetPropertyPath=r.data.propertyName,this.root.children.push(r)}getActionKind(e){if(c(e.action))return E.ActionString;if(f(e.action))return E.ActionFunction;if(i(e.action))return E.ActionSelector;if(s(e.action))return E.ActionAggregator;if(l(e.action))return E.Property;throw new Error("The action specified for ".concat(e.propertyName," is not supported."))}getPreparedAction(e){var{propertyName:t,action:r,kind:n}=e;return c(r)?e=>{var{object:t}=e;return v(t,r)}:f(r)?e=>{var{object:t,items:n,objectToCompute:o}=e;return r.call(void 0,t,n,o)}:s(r)?e=>{var{object:t}=e;return u(r,t)}:i(r)?e=>{var n,{object:o,items:a,objectToCompute:i}=e;if(r.path?Array.isArray(r.path)?n=u(r.path,o):h(r.path)&&(n=v(o,r.path)):n=o,r.fn)try{n=r.fn.call(void 0,n,o,a,i)}catch(e){throw e.message="Unable to set target property [".concat(t,"].\n \n An error occured when applying [").concat(r.fn.name,"] on property [").concat(r.path,"]\n \n Internal error: ").concat(e.message),e}if(r.validation)try{n=r.validation.validate(n)}catch(e){if(!(e instanceof R))throw e;var c=new P({targetProperty:t,expect:e.expect,value:e.value});j(i)?i[x].addError(c):(this.schemaOptions.validation&&this.schemaOptions.validation.reporter?i[x]=new O(this.schemaOptions.validation.reporter,i):i[x]=new O(M,i),i[x].addError(c))}return n}:n===E.Property?null:void 0}}function k(e){return(t,r,n)=>{var o=n.value;return"function"==typeof o&&(n.value=function(){for(var t=arguments.length,r=new Array(t),n=0;n<t;n++)r[n]=arguments[n];var a=o.apply(this,r);return d(a)?Promise.resolve(a).then(t=>e(t)):e(a)}),n}}class F{constructor(e){this.rules=new Map,this.validate=e=>[...this.rules.values()].reduce((e,t)=>t.test(e),e),this.addRule(e)}addRule(e){if(this.rules.has(e.name))throw new Error("Rule ".concat(e.name," has already been used"));this.rules.set(e.name,e)}}var q={get:(e,t)=>{if(t in e)return e[t];if(B.has(t))return B.get(t);throw new Error("The validator ".concat(t,"() does not exist. Did you forget to call Validation.addValidator(name, validator)"))}},B=new Map,C=new Proxy({addValidator:(e,t)=>{B.set(e,()=>new t)}},q);function I(e,t,r,n){var o=t.schemaOptions,a=[];for(var i of t.traverseBFS()){var{preparedAction:c,targetPropertyPath:s}=i.data;c&&a.push({targetPropertyPath:s,preparedAction:c({object:e,objectToCompute:n,items:r})})}return a.reduce((e,t)=>{var r=((e,t)=>p(t)?p(e)?void 0:e:t)(v(e,t.targetPropertyPath),t.preparedAction);return void 0===r?(o&&o.undefinedValues&&o.undefinedValues.strip?o.undefinedValues.default&&m(e,t.targetPropertyPath,o.undefinedValues.default(e,t.targetPropertyPath)):m(e,t.targetPropertyPath,r),z(o,e),e):(m(e,t.targetPropertyPath,r),z(o,e),e)},n)}function z(e,t){if(e&&e.validation&&e.validation.throw&&j(t)){var r;if(e.validation.reporter)r=e.validation.reporter.extractErrors(t);else r=M.extractErrors(t);if(r)throw r}}function J(e,t){var r,n=D.getSchemaOptions(e);if(t&&n.class&&n.class.automapping){var o=function(e,t){var r=Object.keys(new e),n=y(r,r);return Object.assign(n,t)}(t,e);r=new D(o)}else r=new D(e);return e=>{if(!e)return e;if(Array.isArray(e))return e.map(n=>{if(t){var o=new t;return I(n,r,e,o)}return I(n,r,e,{})});var n=e;if(t){var o=new t;return I(n,r,[n],o)}return I(n,r,[n],{})}}function K(e,t,r){switch(arguments.length){case 1:return J(e);case 2:return J(e)(t);case 3:if(r)return null!==t?J(e,r)(t):J(e,r);throw new Error("When using morphism(schema, items, type), type should be defined but value received is ".concat(r))}}function $(e,t){return k(J(e,t))}function H(e){return k(J(e))}function L(e,t){return k(J(e,t))}C.addValidator("string",class extends F{constructor(){super({name:"string",expect:"value to be typeof string",test:function(e){var t=e;if(!h(t))throw new R({value:e,expect:this.expect});return t}})}min(e){return this.addRule({name:"min",expect:"value to be greater or equal than ".concat(e),test:function(t){if(t.length<e)throw new R({value:t,expect:this.expect});return t}}),this}max(e){return this.addRule({name:"max",expect:"value to be less or equal than ".concat(e),test:function(t){if(t.length>e)throw new R({value:t,expect:this.expect});return t}}),this}length(e){return this.addRule({name:"length",expect:"value to be length of ".concat(e),test:function(t){if(t.length!==e)throw new R({value:t,expect:this.expect});return t}}),this}createRegexRule(e,t,r){return{name:e,expect:t,test:function(e){if(!r.test(e))throw new R({value:e,expect:this.expect});return e}}}regex(e){var t=this.createRegexRule("regex","value to match pattern: ".concat(e),e);return this.addRule(t),this}alphanum(){var e=this.createRegexRule("regex","value to contain only alphanumeric characters",/^[a-z0-9]+$/i);return this.addRule(e),this}}),C.addValidator("number",class extends F{constructor(){super({name:"number",expect:"value to be typeof number",test:function(e){var t=+e;if(isNaN(t))throw new R({value:e,expect:this.expect});return t}})}min(e){return this.addRule({name:"min",expect:"value to be greater or equal than ".concat(e),test:function(t){if(t<e)throw new R({value:t,expect:this.expect});return t}}),this}max(e){return this.addRule({name:"max",expect:"value to be less or equal than ".concat(e),test:function(t){if(t>e)throw new R({value:t,expect:this.expect});return t}}),this}}),C.addValidator("boolean",class extends F{constructor(){super({name:"boolean",expect:"value to be typeof boolean",test:function(e){if("boolean"==typeof e)return e;if(/true/i.test(e))return!0;if(/false/i.test(e))return!1;throw new R({value:e,expect:this.expect})}})}}),r.d(t,"morph",function(){return $}),r.d(t,"toJSObject",function(){return H}),r.d(t,"toClassObject",function(){return L}),r.d(t,"morphism",function(){return K}),r.d(t,"createSchema",function(){return T}),r.d(t,"Schema",function(){}),r.d(t,"StrictSchema",function(){}),r.d(t,"SchemaOptions",function(){}),r.d(t,"Mapper",function(){}),r.d(t,"SCHEMA_OPTIONS_SYMBOL",function(){return a}),r.d(t,"Reporter",function(){return A}),r.d(t,"reporter",function(){return M}),r.d(t,"Formatter",function(){}),r.d(t,"Validation",function(){return C}),r.d(t,"BaseValidator",function(){return F}),r.d(t,"Rule",function(){});var U=new class{constructor(){this._registry=null,this._registry={cache:new Map}}register(e,t){if(!e&&!t)throw new Error("type paramater is required when you register a mapping");if(this.exists(e))throw new Error("A mapper for ".concat(e.name," has already been registered"));var r;return r=K(t||{},null,e),this._registry.cache.set(e,r),r}map(e,t){if(!this.exists(e)){var r=this.register(e);if(void 0===t)return r}return this.getMapper(e)(t)}getMapper(e){return this._registry.cache.get(e)}setMapper(e,t){if(t){if(this.exists(e)){var r=K(t,null,e);return this._registry.cache.set(e,r),r}throw new Error("The type ".concat(e.name," is not registered. Register it using `Mophism.register(").concat(e.name,", schema)`"))}throw new Error("The schema must be an Object. Found ".concat(t))}deleteMapper(e){return this._registry.cache.delete(e)}exists(e){return this._registry.cache.has(e)}get mappers(){return this._registry.cache}},W=K;W.register=(e,t)=>U.register(e,t),W.map=(e,t)=>U.map(e,t),W.getMapper=e=>U.getMapper(e),W.setMapper=(e,t)=>U.setMapper(e,t),W.deleteMapper=e=>U.deleteMapper(e),W.mappers=U.mappers;var Y=W;t.default=Y}])}); | ||
//# sourceMappingURL=morphism.map |
@@ -8,4 +8,4 @@ /** | ||
import { IMorphismRegistry } from './MorphismRegistry'; | ||
import { Reporter, reporter as defaultReporter, Formatter, Validation } from './validation/reporter'; | ||
import { BaseValidator, Rule } from './validation/Validation'; | ||
import { Reporter, reporter as defaultReporter, Formatter } from './validation/reporter'; | ||
import { BaseValidator, Rule, Validation } from './validation/Validation'; | ||
/** | ||
@@ -12,0 +12,0 @@ * Currying function that either outputs a mapping function or the transformed data. |
{ | ||
"name": "morphism", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "Do not repeat anymore your objects transformations.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/nobrainr/morphism", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
117171
15053