@jaysalvat/smart-model
Advanced tools
Comparing version 0.3.3 to 0.3.4
@@ -5,3 +5,3 @@ /**! | ||
* https://github.com/jaysalvat/smart-model | ||
* @version 0.3.3 built 2021-02-23 12:22:54 | ||
* @version 0.3.4 built 2021-02-23 18:42:31 | ||
* @license ISC | ||
@@ -266,3 +266,3 @@ * @author Jay Salvat http://jaysalvat.com | ||
this[key] = schema[key].default; | ||
} else { | ||
} else if (!isFn(schema[key])) { | ||
this[key] = data[key]; | ||
@@ -302,10 +302,2 @@ } | ||
} | ||
$onBeforeGet() {} | ||
$onBeforeSet() {} | ||
$onBeforeUpdate() {} | ||
$onDelete() {} | ||
$onGet() {} | ||
$onBeforeDelete() {} | ||
$onSet() {} | ||
$onUpdate() {} | ||
} | ||
@@ -322,6 +314,16 @@ | ||
type: true | ||
}, | ||
methods: { | ||
$onBeforeGet: () => {}, | ||
$onBeforeSet: () => {}, | ||
$onBeforeUpdate: () => {}, | ||
$onDelete: () => {}, | ||
$onGet: () => {}, | ||
$onBeforeDelete: () => {}, | ||
$onSet: () => {}, | ||
$onUpdate: () => {} | ||
} | ||
}; | ||
SmartModel.create = function(name, schema, settings, prototype) { | ||
SmartModel.create = function(name, schema, settings) { | ||
settings = merge(SmartModel.settings, settings); | ||
@@ -366,3 +368,3 @@ const Model = { | ||
}; | ||
Object.assign(Model.prototype, prototype); | ||
Object.assign(Model.prototype, settings.methods); | ||
Model.schema = schema; | ||
@@ -369,0 +371,0 @@ return Model; |
@@ -1,2 +0,2 @@ | ||
/*! SmartModel v0.3.3 */ | ||
function e(e){return Array.isArray(e)}function t(e){return void 0===e}function r(e){return"function"==typeof e}function n(e){return e&&e.toString().startsWith("class")}function o(e){return e.prototype instanceof l||e instanceof l}function s(e){return e&&"[object Object]"===e.toString()}function c(e,t=function(){}){return Object.keys(e).map(t)}function i(e){return[].concat([],e)}function u(e,t){return t=Object.assign({},e,t),c(e,(r=>{s(e[r])&&s(t[r])&&(t[r]=Object.assign({},e[r],u(e[r],t[r])))})),t}class a extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function f(t,r,u,a,f){const p=[];return!f.strict||t&&c(t).length||p.push({message:`Property "${r}" can't be set in strict mode`,code:"strict"}),t.required&&f.empty(u)?(p.push({message:`Property "${r}" is "required"`,code:"required"}),p):t.readonly&&!a?(p.push({message:`Property '${r}' is 'readonly'`,code:"readonly"}),p):(void 0===u||(!t.type||!t.required&&f.empty(u)||o(t.type)&&s(u)||i(t.type).some((t=>function(t,r){const o=r&&r.toString().match(/^\s*function (\w+)/),s=(o?o[1]:"object").toLowerCase();if("date"===s&&t instanceof r)return!0;if("array"===s&&e(t))return!0;if("object"===s){if(n(r)&&t instanceof r)return!0;if(!n(r)&&typeof t===s)return!0}else if(typeof t===s)return!0;return!1}(u,t)))||p.push({message:`Property "${r}" has an invalid type "${typeof u}"`,code:"type"}),t.rule&&c(t.rule,(e=>{(0,t.rule[e])(u)&&p.push({message:`Property "${r}" breaks the "${e}" rule`,code:"rule:"+e})}))),p)}function p(e={},t,r){if(!e.type)return!1;const n=!!o(e.type)&&e.type,c=!!s(e.type)&&e.type;if(n||c){const o=n||l.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z1-9]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),c,r);return e.type=o,o}return!1}a.throw=function(e,t,r,n,o){const c=t.split(":")[0];if(!0===e.exceptions||s(e.exceptions)&&e.exceptions[c])throw new a({message:r,property:n,code:t,source:o&&o.constructor.name})};class l extends class{constructor(e,n){return new Proxy(this,{set(o,s,i){const u=e[s]||{},l=o[s],y=t(l),d=!(y||(h=i,$=l,JSON.stringify(h)===JSON.stringify($)));var h,$;const g=p(u,s,n);function m(r,n){const c=Reflect.apply(r,o,n||[s,i,l,e]);return t(c)?i:c}i=m(o.$onBeforeSet),d&&(i=m(o.$onBeforeUpdate)),r(u.transform)&&(i=m(u.transform,[i,e]));const b=f(u,s,i,y,n);if(b.length){if(!n.exceptions)return!0;a.throw(n,b[0].code,b[0].message,s,o)}return n.strict&&!c(u).length||(g&&(i=new g(i)),o[s]=i,m(o.$onSet),d&&m(o.$onUpdate)),!0},get(n,s){const i=e[s];let u=n[s];if("$eject"===s)return function(){return function(e){return c(e=Object.assign({},e),(t=>{o(e[t])&&(e[t]=e[t].$eject())})),e}(n)};if(!i)return n[s];function a(r,o){const c=Reflect.apply(r,n,o||[s,u,e]);return t(c)?u:c}return u=a(n.$onBeforeGet),r(i)&&(u=a(i,[n,e])),r(i.format)&&(u=a(i.format,[u,e])),u=a(n.$onGet),u},deleteProperty(t,r){const o=t[r];function s(n,s){return Reflect.apply(n,t,s||[r,o,e])}return(e[r]||{}).required&&a.throw(n,"required",`Property "${r}" is "required"`,r,t),s(t.$onBeforeDelete),Reflect.deleteProperty(t,r),s(t.$onDelete),s(t.$onUpdate),!0}})}}{constructor(e={},r={},n){super(e,n),c(e,(n=>{t(r[n])&&(this[n]=t(e[n].default)?r[n]:e[n].default)})),this.$patch(r)}$patch(e){c(e,(t=>{this[t]=e[t]}))}$put(e){c(this,(t=>{e[t]?o(this[t])?this[t].$put(e[t]):this[t]=e[t]:this.$delete(t)})),c(e,(t=>{this[t]||(this[t]=e[t])}))}$delete(e){i(e).forEach((e=>{Reflect.deleteProperty(this,e)}))}$onBeforeGet(){}$onBeforeSet(){}$onBeforeUpdate(){}$onDelete(){}$onGet(){}$onBeforeDelete(){}$onSet(){}$onUpdate(){}}l.settings={empty:e=>""===e||null===e||t(e),strict:!1,exceptions:{readonly:!1,required:!0,rule:!0,strict:!1,type:!0}},l.create=function(t,r,n,o){n=u(l.settings,n);const s={[t]:class extends l{constructor(e){super(r,e,n)}}}[t];return s.checkErrors=function(e,t){const o={};return c(r,(s=>{let c;const u=e[s],a=r[s],l=p(a,s,n);l&&(c=l.checkErrors(u,t));let y=f(a,s,u,!1,n);c?o[s]=c:y.length&&(t&&(y=y.filter((e=>!i(t).includes(e.code)))),y.length&&(o[s]=y))})),!!c(o).length&&o},s.hydrate=function(t){return e(t)?t.map((e=>new s(e))):new s(t)},Object.assign(s.prototype,o),s.schema=r,s};export default l; | ||
/*! SmartModel v0.3.4 */ | ||
function e(e){return Array.isArray(e)}function t(e){return void 0===e}function r(e){return"function"==typeof e}function n(e){return e&&e.toString().startsWith("class")}function o(e){return e.prototype instanceof l||e instanceof l}function s(e){return e&&"[object Object]"===e.toString()}function c(e,t=function(){}){return Object.keys(e).map(t)}function i(e){return[].concat([],e)}function u(e,t){return t=Object.assign({},e,t),c(e,(r=>{s(e[r])&&s(t[r])&&(t[r]=Object.assign({},e[r],u(e[r],t[r])))})),t}class a extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function f(t,r,u,a,f){const p=[];return!f.strict||t&&c(t).length||p.push({message:`Property "${r}" can't be set in strict mode`,code:"strict"}),t.required&&f.empty(u)?(p.push({message:`Property "${r}" is "required"`,code:"required"}),p):t.readonly&&!a?(p.push({message:`Property '${r}' is 'readonly'`,code:"readonly"}),p):(void 0===u||(!t.type||!t.required&&f.empty(u)||o(t.type)&&s(u)||i(t.type).some((t=>function(t,r){const o=r&&r.toString().match(/^\s*function (\w+)/),s=(o?o[1]:"object").toLowerCase();if("date"===s&&t instanceof r)return!0;if("array"===s&&e(t))return!0;if("object"===s){if(n(r)&&t instanceof r)return!0;if(!n(r)&&typeof t===s)return!0}else if(typeof t===s)return!0;return!1}(u,t)))||p.push({message:`Property "${r}" has an invalid type "${typeof u}"`,code:"type"}),t.rule&&c(t.rule,(e=>{(0,t.rule[e])(u)&&p.push({message:`Property "${r}" breaks the "${e}" rule`,code:"rule:"+e})}))),p)}function p(e={},t,r){if(!e.type)return!1;const n=!!o(e.type)&&e.type,c=!!s(e.type)&&e.type;if(n||c){const o=n||l.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z1-9]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),c,r);return e.type=o,o}return!1}a.throw=function(e,t,r,n,o){const c=t.split(":")[0];if(!0===e.exceptions||s(e.exceptions)&&e.exceptions[c])throw new a({message:r,property:n,code:t,source:o&&o.constructor.name})};class l extends class{constructor(e,n){return new Proxy(this,{set(o,s,i){const u=e[s]||{},l=o[s],y=t(l),d=!(y||(h=i,$=l,JSON.stringify(h)===JSON.stringify($)));var h,$;const g=p(u,s,n);function m(r,n){const c=Reflect.apply(r,o,n||[s,i,l,e]);return t(c)?i:c}i=m(o.$onBeforeSet),d&&(i=m(o.$onBeforeUpdate)),r(u.transform)&&(i=m(u.transform,[i,e]));const b=f(u,s,i,y,n);if(b.length){if(!n.exceptions)return!0;a.throw(n,b[0].code,b[0].message,s,o)}return n.strict&&!c(u).length||(g&&(i=new g(i)),o[s]=i,m(o.$onSet),d&&m(o.$onUpdate)),!0},get(n,s){const i=e[s];let u=n[s];if("$eject"===s)return function(){return function(e){return c(e=Object.assign({},e),(t=>{o(e[t])&&(e[t]=e[t].$eject())})),e}(n)};if(!i)return n[s];function a(r,o){const c=Reflect.apply(r,n,o||[s,u,e]);return t(c)?u:c}return u=a(n.$onBeforeGet),r(i)&&(u=a(i,[n,e])),r(i.format)&&(u=a(i.format,[u,e])),u=a(n.$onGet),u},deleteProperty(t,r){const o=t[r];function s(n,s){return Reflect.apply(n,t,s||[r,o,e])}return(e[r]||{}).required&&a.throw(n,"required",`Property "${r}" is "required"`,r,t),s(t.$onBeforeDelete),Reflect.deleteProperty(t,r),s(t.$onDelete),s(t.$onUpdate),!0}})}}{constructor(e={},n={},o){super(e,o),c(e,(o=>{t(n[o])&&(t(e[o].default)?r(e[o])||(this[o]=n[o]):this[o]=e[o].default)})),this.$patch(n)}$patch(e){c(e,(t=>{this[t]=e[t]}))}$put(e){c(this,(t=>{e[t]?o(this[t])?this[t].$put(e[t]):this[t]=e[t]:this.$delete(t)})),c(e,(t=>{this[t]||(this[t]=e[t])}))}$delete(e){i(e).forEach((e=>{Reflect.deleteProperty(this,e)}))}}l.settings={empty:e=>""===e||null===e||t(e),strict:!1,exceptions:{readonly:!1,required:!0,rule:!0,strict:!1,type:!0},methods:{$onBeforeGet:()=>{},$onBeforeSet:()=>{},$onBeforeUpdate:()=>{},$onDelete:()=>{},$onGet:()=>{},$onBeforeDelete:()=>{},$onSet:()=>{},$onUpdate:()=>{}}},l.create=function(t,r,n){n=u(l.settings,n);const o={[t]:class extends l{constructor(e){super(r,e,n)}}}[t];return o.checkErrors=function(e,t){const o={};return c(r,(s=>{let c;const u=e[s],a=r[s],l=p(a,s,n);l&&(c=l.checkErrors(u,t));let y=f(a,s,u,!1,n);c?o[s]=c:y.length&&(t&&(y=y.filter((e=>!i(t).includes(e.code)))),y.length&&(o[s]=y))})),!!c(o).length&&o},o.hydrate=function(t){return e(t)?t.map((e=>new o(e))):new o(t)},Object.assign(o.prototype,n.methods),o.schema=r,o};export default l; |
@@ -5,3 +5,3 @@ /**! | ||
* https://github.com/jaysalvat/smart-model | ||
* @version 0.3.3 built 2021-02-23 12:22:54 | ||
* @version 0.3.4 built 2021-02-23 18:42:31 | ||
* @license ISC | ||
@@ -250,3 +250,3 @@ * @author Jay Salvat http://jaysalvat.com | ||
this[key] = schema[key].default; | ||
} else { | ||
} else if (!isFn(schema[key])) { | ||
this[key] = data[key]; | ||
@@ -286,10 +286,2 @@ } | ||
} | ||
$onBeforeGet() {} | ||
$onBeforeSet() {} | ||
$onBeforeUpdate() {} | ||
$onDelete() {} | ||
$onGet() {} | ||
$onBeforeDelete() {} | ||
$onSet() {} | ||
$onUpdate() {} | ||
} | ||
@@ -305,5 +297,15 @@ SmartModel.settings = { | ||
type: true | ||
}, | ||
methods: { | ||
$onBeforeGet: () => {}, | ||
$onBeforeSet: () => {}, | ||
$onBeforeUpdate: () => {}, | ||
$onDelete: () => {}, | ||
$onGet: () => {}, | ||
$onBeforeDelete: () => {}, | ||
$onSet: () => {}, | ||
$onUpdate: () => {} | ||
} | ||
}; | ||
SmartModel.create = function(name, schema, settings, prototype) { | ||
SmartModel.create = function(name, schema, settings) { | ||
settings = merge(SmartModel.settings, settings); | ||
@@ -348,3 +350,3 @@ const Model = { | ||
}; | ||
Object.assign(Model.prototype, prototype); | ||
Object.assign(Model.prototype, settings.methods); | ||
Model.schema = schema; | ||
@@ -351,0 +353,0 @@ return Model; |
@@ -1,2 +0,2 @@ | ||
/*! SmartModel v0.3.3 */ | ||
var SmartModel=function(){"use strict";function e(e){return Array.isArray(e)}function t(e){return void 0===e}function r(e){return"function"==typeof e}function n(e){return e&&e.toString().startsWith("class")}function o(e){return e.prototype instanceof l||e instanceof l}function s(e){return e&&"[object Object]"===e.toString()}function c(e,t=function(){}){return Object.keys(e).map(t)}function i(e){return[].concat([],e)}function u(e,t){return t=Object.assign({},e,t),c(e,(r=>{s(e[r])&&s(t[r])&&(t[r]=Object.assign({},e[r],u(e[r],t[r])))})),t}class a extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function f(t,r,u,a,f){const p=[];return!f.strict||t&&c(t).length||p.push({message:`Property "${r}" can't be set in strict mode`,code:"strict"}),t.required&&f.empty(u)?(p.push({message:`Property "${r}" is "required"`,code:"required"}),p):t.readonly&&!a?(p.push({message:`Property '${r}' is 'readonly'`,code:"readonly"}),p):(void 0===u||(!t.type||!t.required&&f.empty(u)||o(t.type)&&s(u)||i(t.type).some((t=>function(t,r){const o=r&&r.toString().match(/^\s*function (\w+)/),s=(o?o[1]:"object").toLowerCase();if("date"===s&&t instanceof r)return!0;if("array"===s&&e(t))return!0;if("object"===s){if(n(r)&&t instanceof r)return!0;if(!n(r)&&typeof t===s)return!0}else if(typeof t===s)return!0;return!1}(u,t)))||p.push({message:`Property "${r}" has an invalid type "${typeof u}"`,code:"type"}),t.rule&&c(t.rule,(e=>{(0,t.rule[e])(u)&&p.push({message:`Property "${r}" breaks the "${e}" rule`,code:"rule:"+e})}))),p)}function p(e={},t,r){if(!e.type)return!1;const n=!!o(e.type)&&e.type,c=!!s(e.type)&&e.type;if(n||c){const o=n||l.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z1-9]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),c,r);return e.type=o,o}return!1}a.throw=function(e,t,r,n,o){const c=t.split(":")[0];if(!0===e.exceptions||s(e.exceptions)&&e.exceptions[c])throw new a({message:r,property:n,code:t,source:o&&o.constructor.name})};class l extends class{constructor(e,n){return new Proxy(this,{set(o,s,i){const u=e[s]||{},l=o[s],y=t(l),d=!(y||(h=i,$=l,JSON.stringify(h)===JSON.stringify($)));var h,$;const g=p(u,s,n);function m(r,n){const c=Reflect.apply(r,o,n||[s,i,l,e]);return t(c)?i:c}i=m(o.$onBeforeSet),d&&(i=m(o.$onBeforeUpdate)),r(u.transform)&&(i=m(u.transform,[i,e]));const b=f(u,s,i,y,n);if(b.length){if(!n.exceptions)return!0;a.throw(n,b[0].code,b[0].message,s,o)}return n.strict&&!c(u).length||(g&&(i=new g(i)),o[s]=i,m(o.$onSet),d&&m(o.$onUpdate)),!0},get(n,s){const i=e[s];let u=n[s];if("$eject"===s)return function(){return function(e){return c(e=Object.assign({},e),(t=>{o(e[t])&&(e[t]=e[t].$eject())})),e}(n)};if(!i)return n[s];function a(r,o){const c=Reflect.apply(r,n,o||[s,u,e]);return t(c)?u:c}return u=a(n.$onBeforeGet),r(i)&&(u=a(i,[n,e])),r(i.format)&&(u=a(i.format,[u,e])),u=a(n.$onGet),u},deleteProperty(t,r){const o=t[r];function s(n,s){return Reflect.apply(n,t,s||[r,o,e])}return(e[r]||{}).required&&a.throw(n,"required",`Property "${r}" is "required"`,r,t),s(t.$onBeforeDelete),Reflect.deleteProperty(t,r),s(t.$onDelete),s(t.$onUpdate),!0}})}}{constructor(e={},r={},n){super(e,n),c(e,(n=>{t(r[n])&&(this[n]=t(e[n].default)?r[n]:e[n].default)})),this.$patch(r)}$patch(e){c(e,(t=>{this[t]=e[t]}))}$put(e){c(this,(t=>{e[t]?o(this[t])?this[t].$put(e[t]):this[t]=e[t]:this.$delete(t)})),c(e,(t=>{this[t]||(this[t]=e[t])}))}$delete(e){i(e).forEach((e=>{Reflect.deleteProperty(this,e)}))}$onBeforeGet(){}$onBeforeSet(){}$onBeforeUpdate(){}$onDelete(){}$onGet(){}$onBeforeDelete(){}$onSet(){}$onUpdate(){}}return l.settings={empty:e=>""===e||null===e||t(e),strict:!1,exceptions:{readonly:!1,required:!0,rule:!0,strict:!1,type:!0}},l.create=function(t,r,n,o){n=u(l.settings,n);const s={[t]:class extends l{constructor(e){super(r,e,n)}}}[t];return s.checkErrors=function(e,t){const o={};return c(r,(s=>{let c;const u=e[s],a=r[s],l=p(a,s,n);l&&(c=l.checkErrors(u,t));let y=f(a,s,u,!1,n);c?o[s]=c:y.length&&(t&&(y=y.filter((e=>!i(t).includes(e.code)))),y.length&&(o[s]=y))})),!!c(o).length&&o},s.hydrate=function(t){return e(t)?t.map((e=>new s(e))):new s(t)},Object.assign(s.prototype,o),s.schema=r,s},l}(); | ||
/*! SmartModel v0.3.4 */ | ||
var SmartModel=function(){"use strict";function e(e){return Array.isArray(e)}function t(e){return void 0===e}function r(e){return"function"==typeof e}function n(e){return e&&e.toString().startsWith("class")}function o(e){return e.prototype instanceof l||e instanceof l}function s(e){return e&&"[object Object]"===e.toString()}function c(e,t=function(){}){return Object.keys(e).map(t)}function i(e){return[].concat([],e)}function u(e,t){return t=Object.assign({},e,t),c(e,(r=>{s(e[r])&&s(t[r])&&(t[r]=Object.assign({},e[r],u(e[r],t[r])))})),t}class a extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function f(t,r,u,a,f){const p=[];return!f.strict||t&&c(t).length||p.push({message:`Property "${r}" can't be set in strict mode`,code:"strict"}),t.required&&f.empty(u)?(p.push({message:`Property "${r}" is "required"`,code:"required"}),p):t.readonly&&!a?(p.push({message:`Property '${r}' is 'readonly'`,code:"readonly"}),p):(void 0===u||(!t.type||!t.required&&f.empty(u)||o(t.type)&&s(u)||i(t.type).some((t=>function(t,r){const o=r&&r.toString().match(/^\s*function (\w+)/),s=(o?o[1]:"object").toLowerCase();if("date"===s&&t instanceof r)return!0;if("array"===s&&e(t))return!0;if("object"===s){if(n(r)&&t instanceof r)return!0;if(!n(r)&&typeof t===s)return!0}else if(typeof t===s)return!0;return!1}(u,t)))||p.push({message:`Property "${r}" has an invalid type "${typeof u}"`,code:"type"}),t.rule&&c(t.rule,(e=>{(0,t.rule[e])(u)&&p.push({message:`Property "${r}" breaks the "${e}" rule`,code:"rule:"+e})}))),p)}function p(e={},t,r){if(!e.type)return!1;const n=!!o(e.type)&&e.type,c=!!s(e.type)&&e.type;if(n||c){const o=n||l.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z1-9]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),c,r);return e.type=o,o}return!1}a.throw=function(e,t,r,n,o){const c=t.split(":")[0];if(!0===e.exceptions||s(e.exceptions)&&e.exceptions[c])throw new a({message:r,property:n,code:t,source:o&&o.constructor.name})};class l extends class{constructor(e,n){return new Proxy(this,{set(o,s,i){const u=e[s]||{},l=o[s],y=t(l),d=!(y||(h=i,$=l,JSON.stringify(h)===JSON.stringify($)));var h,$;const g=p(u,s,n);function m(r,n){const c=Reflect.apply(r,o,n||[s,i,l,e]);return t(c)?i:c}i=m(o.$onBeforeSet),d&&(i=m(o.$onBeforeUpdate)),r(u.transform)&&(i=m(u.transform,[i,e]));const b=f(u,s,i,y,n);if(b.length){if(!n.exceptions)return!0;a.throw(n,b[0].code,b[0].message,s,o)}return n.strict&&!c(u).length||(g&&(i=new g(i)),o[s]=i,m(o.$onSet),d&&m(o.$onUpdate)),!0},get(n,s){const i=e[s];let u=n[s];if("$eject"===s)return function(){return function(e){return c(e=Object.assign({},e),(t=>{o(e[t])&&(e[t]=e[t].$eject())})),e}(n)};if(!i)return n[s];function a(r,o){const c=Reflect.apply(r,n,o||[s,u,e]);return t(c)?u:c}return u=a(n.$onBeforeGet),r(i)&&(u=a(i,[n,e])),r(i.format)&&(u=a(i.format,[u,e])),u=a(n.$onGet),u},deleteProperty(t,r){const o=t[r];function s(n,s){return Reflect.apply(n,t,s||[r,o,e])}return(e[r]||{}).required&&a.throw(n,"required",`Property "${r}" is "required"`,r,t),s(t.$onBeforeDelete),Reflect.deleteProperty(t,r),s(t.$onDelete),s(t.$onUpdate),!0}})}}{constructor(e={},n={},o){super(e,o),c(e,(o=>{t(n[o])&&(t(e[o].default)?r(e[o])||(this[o]=n[o]):this[o]=e[o].default)})),this.$patch(n)}$patch(e){c(e,(t=>{this[t]=e[t]}))}$put(e){c(this,(t=>{e[t]?o(this[t])?this[t].$put(e[t]):this[t]=e[t]:this.$delete(t)})),c(e,(t=>{this[t]||(this[t]=e[t])}))}$delete(e){i(e).forEach((e=>{Reflect.deleteProperty(this,e)}))}}return l.settings={empty:e=>""===e||null===e||t(e),strict:!1,exceptions:{readonly:!1,required:!0,rule:!0,strict:!1,type:!0},methods:{$onBeforeGet:()=>{},$onBeforeSet:()=>{},$onBeforeUpdate:()=>{},$onDelete:()=>{},$onGet:()=>{},$onBeforeDelete:()=>{},$onSet:()=>{},$onUpdate:()=>{}}},l.create=function(t,r,n){n=u(l.settings,n);const o={[t]:class extends l{constructor(e){super(r,e,n)}}}[t];return o.checkErrors=function(e,t){const o={};return c(r,(s=>{let c;const u=e[s],a=r[s],l=p(a,s,n);l&&(c=l.checkErrors(u,t));let y=f(a,s,u,!1,n);c?o[s]=c:y.length&&(t&&(y=y.filter((e=>!i(t).includes(e.code)))),y.length&&(o[s]=y))})),!!c(o).length&&o},o.hydrate=function(t){return e(t)?t.map((e=>new o(e))):new o(t)},Object.assign(o.prototype,n.methods),o.schema=r,o},l}(); |
@@ -5,3 +5,3 @@ /**! | ||
* https://github.com/jaysalvat/smart-model | ||
* @version 0.3.3 built 2021-02-23 12:22:54 | ||
* @version 0.3.4 built 2021-02-23 18:42:31 | ||
* @license ISC | ||
@@ -253,3 +253,3 @@ * @author Jay Salvat http://jaysalvat.com | ||
this[key] = schema[key].default; | ||
} else { | ||
} else if (!isFn(schema[key])) { | ||
this[key] = data[key]; | ||
@@ -289,10 +289,2 @@ } | ||
} | ||
$onBeforeGet() {} | ||
$onBeforeSet() {} | ||
$onBeforeUpdate() {} | ||
$onDelete() {} | ||
$onGet() {} | ||
$onBeforeDelete() {} | ||
$onSet() {} | ||
$onUpdate() {} | ||
} | ||
@@ -308,5 +300,15 @@ SmartModel.settings = { | ||
type: true | ||
}, | ||
methods: { | ||
$onBeforeGet: () => {}, | ||
$onBeforeSet: () => {}, | ||
$onBeforeUpdate: () => {}, | ||
$onDelete: () => {}, | ||
$onGet: () => {}, | ||
$onBeforeDelete: () => {}, | ||
$onSet: () => {}, | ||
$onUpdate: () => {} | ||
} | ||
}; | ||
SmartModel.create = function(name, schema, settings, prototype) { | ||
SmartModel.create = function(name, schema, settings) { | ||
settings = merge(SmartModel.settings, settings); | ||
@@ -351,3 +353,3 @@ const Model = { | ||
}; | ||
Object.assign(Model.prototype, prototype); | ||
Object.assign(Model.prototype, settings.methods); | ||
Model.schema = schema; | ||
@@ -354,0 +356,0 @@ return Model; |
@@ -1,2 +0,2 @@ | ||
/*! SmartModel v0.3.3 */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).SmartModel=t()}(this,(function(){"use strict";function e(e){return Array.isArray(e)}function t(e){return void 0===e}function r(e){return"function"==typeof e}function n(e){return e&&e.toString().startsWith("class")}function o(e){return e.prototype instanceof l||e instanceof l}function s(e){return e&&"[object Object]"===e.toString()}function c(e,t=function(){}){return Object.keys(e).map(t)}function i(e){return[].concat([],e)}function u(e,t){return t=Object.assign({},e,t),c(e,(r=>{s(e[r])&&s(t[r])&&(t[r]=Object.assign({},e[r],u(e[r],t[r])))})),t}class f extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function a(t,r,u,f,a){const p=[];return!a.strict||t&&c(t).length||p.push({message:`Property "${r}" can't be set in strict mode`,code:"strict"}),t.required&&a.empty(u)?(p.push({message:`Property "${r}" is "required"`,code:"required"}),p):t.readonly&&!f?(p.push({message:`Property '${r}' is 'readonly'`,code:"readonly"}),p):(void 0===u||(!t.type||!t.required&&a.empty(u)||o(t.type)&&s(u)||i(t.type).some((t=>function(t,r){const o=r&&r.toString().match(/^\s*function (\w+)/),s=(o?o[1]:"object").toLowerCase();if("date"===s&&t instanceof r)return!0;if("array"===s&&e(t))return!0;if("object"===s){if(n(r)&&t instanceof r)return!0;if(!n(r)&&typeof t===s)return!0}else if(typeof t===s)return!0;return!1}(u,t)))||p.push({message:`Property "${r}" has an invalid type "${typeof u}"`,code:"type"}),t.rule&&c(t.rule,(e=>{(0,t.rule[e])(u)&&p.push({message:`Property "${r}" breaks the "${e}" rule`,code:"rule:"+e})}))),p)}function p(e={},t,r){if(!e.type)return!1;const n=!!o(e.type)&&e.type,c=!!s(e.type)&&e.type;if(n||c){const o=n||l.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z1-9]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),c,r);return e.type=o,o}return!1}f.throw=function(e,t,r,n,o){const c=t.split(":")[0];if(!0===e.exceptions||s(e.exceptions)&&e.exceptions[c])throw new f({message:r,property:n,code:t,source:o&&o.constructor.name})};class l extends class{constructor(e,n){return new Proxy(this,{set(o,s,i){const u=e[s]||{},l=o[s],d=t(l),y=!(d||(h=i,$=l,JSON.stringify(h)===JSON.stringify($)));var h,$;const g=p(u,s,n);function m(r,n){const c=Reflect.apply(r,o,n||[s,i,l,e]);return t(c)?i:c}i=m(o.$onBeforeSet),y&&(i=m(o.$onBeforeUpdate)),r(u.transform)&&(i=m(u.transform,[i,e]));const b=a(u,s,i,d,n);if(b.length){if(!n.exceptions)return!0;f.throw(n,b[0].code,b[0].message,s,o)}return n.strict&&!c(u).length||(g&&(i=new g(i)),o[s]=i,m(o.$onSet),y&&m(o.$onUpdate)),!0},get(n,s){const i=e[s];let u=n[s];if("$eject"===s)return function(){return function(e){return c(e=Object.assign({},e),(t=>{o(e[t])&&(e[t]=e[t].$eject())})),e}(n)};if(!i)return n[s];function f(r,o){const c=Reflect.apply(r,n,o||[s,u,e]);return t(c)?u:c}return u=f(n.$onBeforeGet),r(i)&&(u=f(i,[n,e])),r(i.format)&&(u=f(i.format,[u,e])),u=f(n.$onGet),u},deleteProperty(t,r){const o=t[r];function s(n,s){return Reflect.apply(n,t,s||[r,o,e])}return(e[r]||{}).required&&f.throw(n,"required",`Property "${r}" is "required"`,r,t),s(t.$onBeforeDelete),Reflect.deleteProperty(t,r),s(t.$onDelete),s(t.$onUpdate),!0}})}}{constructor(e={},r={},n){super(e,n),c(e,(n=>{t(r[n])&&(this[n]=t(e[n].default)?r[n]:e[n].default)})),this.$patch(r)}$patch(e){c(e,(t=>{this[t]=e[t]}))}$put(e){c(this,(t=>{e[t]?o(this[t])?this[t].$put(e[t]):this[t]=e[t]:this.$delete(t)})),c(e,(t=>{this[t]||(this[t]=e[t])}))}$delete(e){i(e).forEach((e=>{Reflect.deleteProperty(this,e)}))}$onBeforeGet(){}$onBeforeSet(){}$onBeforeUpdate(){}$onDelete(){}$onGet(){}$onBeforeDelete(){}$onSet(){}$onUpdate(){}}return l.settings={empty:e=>""===e||null===e||t(e),strict:!1,exceptions:{readonly:!1,required:!0,rule:!0,strict:!1,type:!0}},l.create=function(t,r,n,o){n=u(l.settings,n);const s={[t]:class extends l{constructor(e){super(r,e,n)}}}[t];return s.checkErrors=function(e,t){const o={};return c(r,(s=>{let c;const u=e[s],f=r[s],l=p(f,s,n);l&&(c=l.checkErrors(u,t));let d=a(f,s,u,!1,n);c?o[s]=c:d.length&&(t&&(d=d.filter((e=>!i(t).includes(e.code)))),d.length&&(o[s]=d))})),!!c(o).length&&o},s.hydrate=function(t){return e(t)?t.map((e=>new s(e))):new s(t)},Object.assign(s.prototype,o),s.schema=r,s},l})); | ||
/*! SmartModel v0.3.4 */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).SmartModel=t()}(this,(function(){"use strict";function e(e){return Array.isArray(e)}function t(e){return void 0===e}function r(e){return"function"==typeof e}function n(e){return e&&e.toString().startsWith("class")}function o(e){return e.prototype instanceof l||e instanceof l}function s(e){return e&&"[object Object]"===e.toString()}function c(e,t=function(){}){return Object.keys(e).map(t)}function i(e){return[].concat([],e)}function u(e,t){return t=Object.assign({},e,t),c(e,(r=>{s(e[r])&&s(t[r])&&(t[r]=Object.assign({},e[r],u(e[r],t[r])))})),t}class f extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function a(t,r,u,f,a){const p=[];return!a.strict||t&&c(t).length||p.push({message:`Property "${r}" can't be set in strict mode`,code:"strict"}),t.required&&a.empty(u)?(p.push({message:`Property "${r}" is "required"`,code:"required"}),p):t.readonly&&!f?(p.push({message:`Property '${r}' is 'readonly'`,code:"readonly"}),p):(void 0===u||(!t.type||!t.required&&a.empty(u)||o(t.type)&&s(u)||i(t.type).some((t=>function(t,r){const o=r&&r.toString().match(/^\s*function (\w+)/),s=(o?o[1]:"object").toLowerCase();if("date"===s&&t instanceof r)return!0;if("array"===s&&e(t))return!0;if("object"===s){if(n(r)&&t instanceof r)return!0;if(!n(r)&&typeof t===s)return!0}else if(typeof t===s)return!0;return!1}(u,t)))||p.push({message:`Property "${r}" has an invalid type "${typeof u}"`,code:"type"}),t.rule&&c(t.rule,(e=>{(0,t.rule[e])(u)&&p.push({message:`Property "${r}" breaks the "${e}" rule`,code:"rule:"+e})}))),p)}function p(e={},t,r){if(!e.type)return!1;const n=!!o(e.type)&&e.type,c=!!s(e.type)&&e.type;if(n||c){const o=n||l.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z1-9]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),c,r);return e.type=o,o}return!1}f.throw=function(e,t,r,n,o){const c=t.split(":")[0];if(!0===e.exceptions||s(e.exceptions)&&e.exceptions[c])throw new f({message:r,property:n,code:t,source:o&&o.constructor.name})};class l extends class{constructor(e,n){return new Proxy(this,{set(o,s,i){const u=e[s]||{},l=o[s],d=t(l),y=!(d||(h=i,$=l,JSON.stringify(h)===JSON.stringify($)));var h,$;const g=p(u,s,n);function m(r,n){const c=Reflect.apply(r,o,n||[s,i,l,e]);return t(c)?i:c}i=m(o.$onBeforeSet),y&&(i=m(o.$onBeforeUpdate)),r(u.transform)&&(i=m(u.transform,[i,e]));const b=a(u,s,i,d,n);if(b.length){if(!n.exceptions)return!0;f.throw(n,b[0].code,b[0].message,s,o)}return n.strict&&!c(u).length||(g&&(i=new g(i)),o[s]=i,m(o.$onSet),y&&m(o.$onUpdate)),!0},get(n,s){const i=e[s];let u=n[s];if("$eject"===s)return function(){return function(e){return c(e=Object.assign({},e),(t=>{o(e[t])&&(e[t]=e[t].$eject())})),e}(n)};if(!i)return n[s];function f(r,o){const c=Reflect.apply(r,n,o||[s,u,e]);return t(c)?u:c}return u=f(n.$onBeforeGet),r(i)&&(u=f(i,[n,e])),r(i.format)&&(u=f(i.format,[u,e])),u=f(n.$onGet),u},deleteProperty(t,r){const o=t[r];function s(n,s){return Reflect.apply(n,t,s||[r,o,e])}return(e[r]||{}).required&&f.throw(n,"required",`Property "${r}" is "required"`,r,t),s(t.$onBeforeDelete),Reflect.deleteProperty(t,r),s(t.$onDelete),s(t.$onUpdate),!0}})}}{constructor(e={},n={},o){super(e,o),c(e,(o=>{t(n[o])&&(t(e[o].default)?r(e[o])||(this[o]=n[o]):this[o]=e[o].default)})),this.$patch(n)}$patch(e){c(e,(t=>{this[t]=e[t]}))}$put(e){c(this,(t=>{e[t]?o(this[t])?this[t].$put(e[t]):this[t]=e[t]:this.$delete(t)})),c(e,(t=>{this[t]||(this[t]=e[t])}))}$delete(e){i(e).forEach((e=>{Reflect.deleteProperty(this,e)}))}}return l.settings={empty:e=>""===e||null===e||t(e),strict:!1,exceptions:{readonly:!1,required:!0,rule:!0,strict:!1,type:!0},methods:{$onBeforeGet:()=>{},$onBeforeSet:()=>{},$onBeforeUpdate:()=>{},$onDelete:()=>{},$onGet:()=>{},$onBeforeDelete:()=>{},$onSet:()=>{},$onUpdate:()=>{}}},l.create=function(t,r,n){n=u(l.settings,n);const o={[t]:class extends l{constructor(e){super(r,e,n)}}}[t];return o.checkErrors=function(e,t){const o={};return c(r,(s=>{let c;const u=e[s],f=r[s],l=p(f,s,n);l&&(c=l.checkErrors(u,t));let d=a(f,s,u,!1,n);c?o[s]=c:d.length&&(t&&(d=d.filter((e=>!i(t).includes(e.code)))),d.length&&(o[s]=d))})),!!c(o).length&&o},o.hydrate=function(t){return e(t)?t.map((e=>new o(e))):new o(t)},Object.assign(o.prototype,n.methods),o.schema=r,o},l})); |
{ | ||
"name": "@jaysalvat/smart-model", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"description": "Javascript object model", | ||
@@ -5,0 +5,0 @@ "main": "./build/smart-model.cjs.min.cjs", |
126
README.md
``` | ||
________ _____ ______ ________ ________ _________ _____ ______ ________ ________ _______ ___ | ||
|\ ____\|\ _ \ _ \|\ __ \|\ __ \|\___ ___\\ _ \ _ \|\ __ \|\ ___ \|\ ___ \ |\ \ | ||
\ \ \___|\ \ \\\__\ \ \ \ \|\ \ \ \|\ \|___ \ \_\ \ \\\__\ \ \ \ \|\ \ \ \_|\ \ \ __/|\ \ \ | ||
\ \_____ \ \ \\|__| \ \ \ __ \ \ _ _\ \ \ \ \ \ \\|__| \ \ \ \\\ \ \ \ \\ \ \ \_|/_\ \ \ | ||
\|____|\ \ \ \ \ \ \ \ \ \ \ \ \\ \| \ \ \ \ \ \ \ \ \ \ \\\ \ \ \_\\ \ \ \_|\ \ \ \____ | ||
____\_\ \ \__\ \ \__\ \__\ \__\ \__\\ _\ \ \__\ \ \__\ \ \__\ \_______\ \_______\ \_______\ \_______\ | ||
|\_________\|__| \|__|\|__|\|__|\|__|\|__| \|__| \|__| \|__|\|_______|\|_______|\|_______|\|_______| | ||
\|_________| | ||
___ __ __ __ ____ ____ __ __ _____ ____ ____ __ | ||
/ __)( \/ ) /__\ ( _ \(_ _)( \/ )( _ )( _ \( ___)( ) | ||
\__ \ ) ( /(__)\ ) / )( ) ( )(_)( )(_) ))__) )(__ | ||
(___/(_/\/\_)(__)(__)(_)\_) (__) (_/\/\_)(_____)(____/(____)(____) | ||
``` | ||
@@ -19,3 +16,3 @@ | ||
- [x] 1Kb+ gzipped | ||
- [x] ~1.5Kb gzipped | ||
- [x] Value transformation | ||
@@ -67,3 +64,3 @@ - [x] Value format | ||
```javascript | ||
function readingtime(text) { /*...*/ } | ||
function readingTime(text) { /*...*/ } | ||
@@ -79,4 +76,4 @@ const Post = SmartModel.create('Post', { | ||
rule: { | ||
'tooShort': (calue) => value.length < 100, | ||
'tooLong': (calue) => value.length > 1000, | ||
'tooShort': (value) => value.length < 100, | ||
'tooLong': (value) => value.length > 1000, | ||
} | ||
@@ -105,4 +102,7 @@ }, | ||
lastname: { | ||
type: String | ||
} | ||
required: true, | ||
type: String, | ||
transform: (value) => value.toUpperCase() | ||
}, | ||
fullname: (author) => author.firstname + ' ' + author.lastname | ||
} | ||
@@ -117,8 +117,8 @@ }, | ||
strict: false, | ||
exceptions: true | ||
} | ||
// Events | ||
{ | ||
onUpdate() { | ||
this.updatedAt = new Date() | ||
exceptions: true, | ||
// Events | ||
methods: { | ||
$onUpdate() { | ||
this.updatedAt = new Date() | ||
} | ||
} | ||
@@ -132,6 +132,6 @@ } | ||
title: 'My new post', | ||
body: 'Lorem ipsum...', | ||
body: 'Very long post...', | ||
author: { | ||
firstname: 'Brad', | ||
lastname: 'Pitt' | ||
firstname: 'James', | ||
lastname: 'Hetfield' | ||
} | ||
@@ -141,2 +141,31 @@ }) | ||
```javascript | ||
console.log(post) | ||
``` | ||
```ssh | ||
Post { | ||
createdAt: 2020-01-31T09:50:00.000Z, | ||
updatedAt: 2020-01-31T09:50:00.000Z, | ||
title: 'My new post', | ||
body: 'Very long post...', | ||
author: Author { | ||
firstname: 'James', | ||
lastname: 'Hetfield' | ||
} | ||
} | ||
``` | ||
```javascript | ||
console.log(post.author.fullname) | ||
console.log(post.createdAt) | ||
console.log(post.readingTime) | ||
``` | ||
```ssh | ||
James HETFIELD | ||
01/31/2020, 9:50:00 AM | ||
12 minutes | ||
``` | ||
## Documentation | ||
@@ -149,3 +178,3 @@ | ||
| Option | Type | Description | ||
| ----------- | ------- | --- | ||
| :---------- | :------ | :-- | ||
| type | any | The required type (*) of a value. You can set a schema or another model (*) in order to nest models | ||
@@ -237,7 +266,7 @@ | required | bool | The value is required. See `settings.empty` for the empty check function | ||
| Option | Type | Default | Description | ||
| ----------- | ----------- | ------------- | --- | ||
| strict | bool | false | Allow to set property not present in the schema | ||
| empty | fn | fn (***) | Function to check if a value is empty if required | ||
| exceptions | bool/object | object (****) | Throw exceptions on errors. can be `boolean` or òbject` for advanced settings | ||
| Option | Type | Default | Description | ||
| :---------- | :---------- | :------------- | :-- | ||
| strict | bool | false | Allow to set property not present in the schema | ||
| empty | fn | fn (***) | Function to check if a value is empty if required | ||
| exceptions | bool/object | object (****) | Throw exceptions on errors. can be `boolean` or òbject` for advanced settings | ||
@@ -255,3 +284,3 @@ #### [***] Empty check function | ||
| Option | Type | Default | ||
| --------- | ---- | ------- | ||
| :-------- | :--- | :------ | ||
| readonly | bool | false | ||
@@ -267,8 +296,9 @@ | required | bool | true | ||
Replace the model properties. Existing properties are deleted. | ||
```javascript | ||
const article = new Article() | ||
article.put({ | ||
title: 'My article', | ||
article.$put({ | ||
title: 'My article' | ||
}) | ||
@@ -286,3 +316,3 @@ ``` | ||
const json = article.eject() | ||
const json = article.$eject() | ||
``` | ||
@@ -299,5 +329,7 @@ | ||
} | ||
}, {}, { | ||
excerpt(limit = 10) { | ||
return this.body.substr(0, limit) + '…' | ||
}, { | ||
methods: { | ||
excerpt(limit = 10) { | ||
return this.body.substr(0, limit) + '…' | ||
} | ||
} | ||
@@ -316,11 +348,13 @@ }) | ||
} | ||
}, {}, { | ||
$onBeforeGet() {} | ||
$onBeforeSet() {} | ||
$onBeforeUpdate() {} | ||
$onDelete() {} | ||
$onGet() {} | ||
$onBeforeDelete() {} | ||
$onSet() {} | ||
$onUpdate() {} | ||
}, { | ||
methods: { | ||
$onBeforeGet() {} | ||
$onBeforeSet() {} | ||
$onBeforeUpdate() {} | ||
$onDelete() {} | ||
$onGet() {} | ||
$onBeforeDelete() {} | ||
$onSet() {} | ||
$onUpdate() {} | ||
} | ||
}) | ||
@@ -327,0 +361,0 @@ ``` |
@@ -5,3 +5,3 @@ | ||
import checkErrors from './checkErrors.js' | ||
import { toArray, isSmartModel, keys, merge, isArray, isUndef } from './utils.js' | ||
import { toArray, keys, merge, isSmartModel, isFn, isArray, isUndef } from './utils.js' | ||
@@ -16,3 +16,3 @@ class SmartModel extends SmartModelProxy { | ||
this[key] = schema[key].default | ||
} else { | ||
} else if (!isFn(schema[key])) { | ||
this[key] = data[key] | ||
@@ -57,11 +57,2 @@ } | ||
} | ||
$onBeforeGet() {} | ||
$onBeforeSet() {} | ||
$onBeforeUpdate() {} | ||
$onDelete() {} | ||
$onGet() {} | ||
$onBeforeDelete() {} | ||
$onSet() {} | ||
$onUpdate() {} | ||
} | ||
@@ -78,6 +69,16 @@ | ||
type: true | ||
}, | ||
methods: { | ||
$onBeforeGet: () => {}, | ||
$onBeforeSet: () => {}, | ||
$onBeforeUpdate: () => {}, | ||
$onDelete: () => {}, | ||
$onGet: () => {}, | ||
$onBeforeDelete: () => {}, | ||
$onSet: () => {}, | ||
$onUpdate: () => {} | ||
} | ||
} | ||
SmartModel.create = function (name, schema, settings, prototype) { | ||
SmartModel.create = function (name, schema, settings) { | ||
settings = merge(SmartModel.settings, settings) | ||
@@ -130,3 +131,3 @@ | ||
Object.assign(Model.prototype, prototype) | ||
Object.assign(Model.prototype, settings.methods) | ||
@@ -133,0 +134,0 @@ Model.schema = schema |
@@ -351,3 +351,3 @@ /* eslint-disable prefer-reflect */ | ||
describe('Events', function () { | ||
describe('Hooks', function () { | ||
it('should trigger $onBeforeDelete', testTrigger('$onBeforeDelete')) | ||
@@ -366,9 +366,11 @@ it('should trigger $onBeforeGet', testTrigger('$onBeforeGet')) | ||
prop: { default: 'default string' } | ||
}, {}, { | ||
$onBeforeSet() { | ||
return 'INTERCEPTED' | ||
}, | ||
}, { | ||
methods: { | ||
$onBeforeSet() { | ||
return 'INTERCEPTED' | ||
}, | ||
$onSet(_, val) { | ||
isIntercepted = val === 'INTERCEPTED' | ||
$onSet(_, val) { | ||
isIntercepted = val === 'INTERCEPTED' | ||
} | ||
} | ||
@@ -387,9 +389,11 @@ }) | ||
prop: { default: 'default string' } | ||
}, {}, { | ||
$onBeforeUpdate() { | ||
return 'INTERCEPTED' | ||
}, | ||
}, { | ||
methods: { | ||
$onBeforeUpdate() { | ||
return 'INTERCEPTED' | ||
}, | ||
$onUpdate(_, val) { | ||
isIntercepted = val === 'INTERCEPTED' | ||
$onUpdate(_, val) { | ||
isIntercepted = val === 'INTERCEPTED' | ||
} | ||
} | ||
@@ -410,9 +414,11 @@ }) | ||
prop: { default: 'default string' } | ||
}, {}, { | ||
$onBeforeGet() { | ||
return 'INTERCEPTED' | ||
}, | ||
}, { | ||
methods: { | ||
$onBeforeGet() { | ||
return 'INTERCEPTED' | ||
}, | ||
$onGet(_, val) { | ||
isIntercepted = val === 'INTERCEPTED' | ||
$onGet(_, val) { | ||
isIntercepted = val === 'INTERCEPTED' | ||
} | ||
} | ||
@@ -543,3 +549,3 @@ }) | ||
expect(obj).to.be.deep.equal(model) | ||
expect(JSON.stringify(obj)).to.be.deep.equal(JSON.stringify(model)) | ||
expect(model).to.be.instanceOf(SmartModel) | ||
@@ -553,3 +559,3 @@ expect(model.prop2).to.be.instanceOf(SmartModel) | ||
describe('Statics methods', function () { | ||
describe('Static methods', function () { | ||
describe('CheckErrors', function () { | ||
@@ -670,23 +676,57 @@ it('should return an array of model errors', function () { | ||
}) | ||
}) | ||
// Settings | ||
// Settings | ||
describe('Settings', function () { | ||
it('should set settings for all instances', function () { | ||
SmartModel.settings.strict = true | ||
SmartModel.settings.exceptions.strict = false | ||
describe('Settings', function () { | ||
it('should set settings for all instances', function () { | ||
SmartModel.settings.strict = true | ||
SmartModel.settings.exceptions.strict = false | ||
const Model1 = SmartModel.create('Model1', { | ||
prop1: { type: String } | ||
}) | ||
const Model1 = SmartModel.create('Model1', { | ||
prop1: { type: String } | ||
}) | ||
const Model2 = SmartModel.create('Model2', { | ||
prop1: { type: String } | ||
}, { strict: false }) | ||
const Model2 = SmartModel.create('Model2', { | ||
prop1: { type: String } | ||
}, { strict: false }) | ||
const Model3 = SmartModel.create('Model3', { | ||
const Model3 = SmartModel.create('Model3', { | ||
prop1: { type: String } | ||
}) | ||
const model1 = new Model1({ | ||
prop1: 'string', | ||
prop2: 'string' | ||
}) | ||
const model2 = new Model2({ | ||
prop1: 'string', | ||
prop2: 'string' | ||
}) | ||
const model3 = new Model3({ | ||
prop1: 'string', | ||
prop2: 'string' | ||
}) | ||
expect(model1.prop1).to.be.equal('string') | ||
expect(model1.prop2).to.be.equal(undef) | ||
expect(model2.prop1).to.be.equal('string') | ||
expect(model2.prop2).to.be.equal('string') | ||
expect(model3.prop1).to.be.equal('string') | ||
expect(model3.prop2).to.be.equal(undef) | ||
}) | ||
describe('strict', function () { | ||
it('should not set undefined properties if strict:true', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop1: { type: String } | ||
}, { | ||
strict: true | ||
}) | ||
const model1 = new Model1({ | ||
const model = new Model({ | ||
prop1: 'string', | ||
@@ -696,8 +736,14 @@ prop2: 'string' | ||
const model2 = new Model2({ | ||
prop1: 'string', | ||
prop2: 'string' | ||
expect(model.prop1).to.be.equal('string') | ||
expect(model.prop2).to.be.equal(undef) | ||
}) | ||
it('should set undefined properties if strict:false', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop1: { type: String } | ||
}, { | ||
strict: false | ||
}) | ||
const model3 = new Model3({ | ||
const model = new Model({ | ||
prop1: 'string', | ||
@@ -707,126 +753,106 @@ prop2: 'string' | ||
expect(model1.prop1).to.be.equal('string') | ||
expect(model1.prop2).to.be.equal(undef) | ||
expect(model.prop1).to.be.equal('string') | ||
expect(model.prop2).to.be.equal('string') | ||
}) | ||
}) | ||
expect(model2.prop1).to.be.equal('string') | ||
expect(model2.prop2).to.be.equal('string') | ||
describe('exceptions', function () { | ||
it('should not throw errors if exceptions:false', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop: { required: true } | ||
}, { | ||
exceptions: false | ||
}) | ||
expect(model3.prop1).to.be.equal('string') | ||
expect(model3.prop2).to.be.equal(undef) | ||
expect(() => { | ||
new Model() | ||
}).to.not.throw(Error) | ||
}) | ||
describe('strict', function () { | ||
it('should not set undefined properties if strict:true', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop1: { type: String } | ||
}, { | ||
strict: true | ||
}) | ||
it('should throw errors if exceptions:true', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop: { required: true } | ||
}, { | ||
exceptions: true | ||
}) | ||
const model = new Model({ | ||
prop1: 'string', | ||
prop2: 'string' | ||
}) | ||
expect(() => { | ||
new Model() | ||
}).to.throw(Error) | ||
}) | ||
expect(model.prop1).to.be.equal('string') | ||
expect(model.prop2).to.be.equal(undef) | ||
it('should not throw errors if exceptions.required:false', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop: { required: true } | ||
}, { | ||
exceptions: { | ||
required: false | ||
} | ||
}) | ||
it('should set undefined properties if strict:false', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop1: { type: String } | ||
}, { | ||
strict: false | ||
}) | ||
expect(() => { | ||
new Model() | ||
}).to.not.throw(Error) | ||
}) | ||
const model = new Model({ | ||
prop1: 'string', | ||
prop2: 'string' | ||
}) | ||
it('should not throw errors if exceptions.type:false', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop: { type: String } | ||
}, { | ||
exceptions: { | ||
type: false | ||
} | ||
}) | ||
expect(model.prop1).to.be.equal('string') | ||
expect(model.prop2).to.be.equal('string') | ||
}) | ||
expect(() => { | ||
new Model({ prop: 0 }) | ||
}).to.not.throw(Error) | ||
}) | ||
describe('exceptions', function () { | ||
it('should not throw errors if exceptions:false', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop: { required: true } | ||
}, { | ||
exceptions: false | ||
}) | ||
expect(() => { | ||
new Model() | ||
}).to.not.throw(Error) | ||
it('should not throw errors if exceptions.strict:false', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop1: { type: String } | ||
}, { | ||
exceptions: { | ||
strict: false | ||
} | ||
}) | ||
it('should throw errors if exceptions:true', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop: { required: true } | ||
}, { | ||
exceptions: true | ||
}) | ||
expect(() => { | ||
new Model({ prop2: 'ok' }) | ||
}).to.not.throw(Error) | ||
}) | ||
expect(() => { | ||
new Model() | ||
}).to.throw(Error) | ||
it('should not throw errors if exceptions.rule:false', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop: { rule: { min: (value) => value < 2 } } | ||
}, { | ||
exceptions: { | ||
rule: false | ||
} | ||
}) | ||
it('should not throw errors if exceptions.required:false', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop: { required: true } | ||
}, { | ||
exceptions: { | ||
required: false | ||
} | ||
}) | ||
expect(() => { | ||
new Model({ prop: 1 }) | ||
}).to.not.throw(Error) | ||
}) | ||
}) | ||
expect(() => { | ||
new Model() | ||
}).to.not.throw(Error) | ||
}) | ||
it('should not throw errors if exceptions.type:false', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop: { type: String } | ||
}, { | ||
exceptions: { | ||
type: false | ||
describe('Methods', function () { | ||
it('should add methods to instance', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop: { type: String } | ||
}, { | ||
methods: { | ||
myMethods(param) { | ||
return this.prop + ':' + param | ||
} | ||
}) | ||
expect(() => { | ||
new Model({ prop: 0 }) | ||
}).to.not.throw(Error) | ||
} | ||
}) | ||
it('should not throw errors if exceptions.strict:false', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop1: { type: String } | ||
}, { | ||
exceptions: { | ||
strict: false | ||
} | ||
}) | ||
expect(() => { | ||
new Model({ prop2: 'ok' }) | ||
}).to.not.throw(Error) | ||
const model = new Model({ | ||
prop: 'string' | ||
}) | ||
it('should not throw errors if exceptions.rule:false', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop: { rule: { min: (value) => value < 2 } } | ||
}, { | ||
exceptions: { | ||
rule: false | ||
} | ||
}) | ||
expect(() => { | ||
new Model({ prop: 1 }) | ||
}).to.not.throw(Error) | ||
}) | ||
expect(model.myMethods('param')).to.be.equal('string:param') | ||
}) | ||
@@ -1022,7 +1048,9 @@ }) | ||
prop: { default: 'old value' } | ||
}, {}, { | ||
[name](prop, val, oldVal) { | ||
property = prop | ||
value = val | ||
oldValue = oldVal | ||
}, { | ||
methods: { | ||
[name](prop, val, oldVal) { | ||
property = prop | ||
value = val | ||
oldValue = oldVal | ||
} | ||
} | ||
@@ -1029,0 +1057,0 @@ }) |
Sorry, the diff of this file is not supported yet
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
115463
2879
406