@jaysalvat/smart-model
Advanced tools
Comparing version 0.2.13 to 0.2.14
@@ -6,3 +6,3 @@ | ||
* https://github.com/jaysalvat/smart-model | ||
* @version 0.2.13 built 2021-02-21 11:54:48 | ||
* @version 0.2.14 built 2021-02-21 15:21:56 | ||
* @license ISC | ||
@@ -44,16 +44,26 @@ * @author Jay Salvat http://jaysalvat.com | ||
function isPlainObject(value) { | ||
return value.toString() === '[object Object]' | ||
} | ||
function isType(value, Type) { | ||
if (typeof Type === 'object') { | ||
Type = Object; | ||
const match = Type && Type.toString().match(/^\s*function (\w+)/); | ||
const type = (match ? match[1] : 'object').toLowerCase(); | ||
if (type === 'date' && value instanceof Type) { | ||
return true | ||
} | ||
if (!isClass(Type) && typeof value === typeof Type()) { | ||
if (type === 'array' && isArray(value)) { | ||
return true | ||
} | ||
// if (isClass(Type) && typeof value === typeof new Type({}, { exceptions: false })) { | ||
// return true | ||
// } | ||
if (value instanceof Type || typeof value === typeof Type) { | ||
if (type === 'object') { | ||
if (isClass(Type) && value instanceof Type) { | ||
return true | ||
} | ||
if (!isClass(Type) && typeof value === type) { | ||
return true | ||
} | ||
} else if (typeof value === type) { | ||
return true | ||
@@ -125,3 +135,3 @@ } | ||
const Child = entry.type.prototype instanceof SmartModel ? entry.type : false; | ||
const schema = typeof entry.type === 'object' ? entry.type : false; | ||
const schema = isPlainObject(entry.type) ? entry.type : false; | ||
@@ -256,3 +266,3 @@ if (Child || schema) { | ||
if (isUndef(data[key])) { | ||
if (schema[key].default) { | ||
if (!isUndef(schema[key].default)) { | ||
this[key] = schema[key].default; | ||
@@ -259,0 +269,0 @@ } else { |
@@ -1,2 +0,2 @@ | ||
/*! SmartModel v0.2.13 */ | ||
class e extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function t(e){return void 0===e}function r(e){return""===e||null===e||t(e)}function n(e){return"function"==typeof e}function o(e){return[].concat([],e)}function s(e,t,n){const s=[];return e.required&&r(n)?(s.push({message:`Invalid value 'required' on property '${t}'`,code:"required"}),s):(void 0===n||(!e.type||!e.required&&r(n)||o(e.type).some((e=>function(e,t){return"object"==typeof t&&(t=Object),!function(e){return e.toString().startsWith("class")}(t)&&typeof e==typeof t()||e instanceof t||typeof e==typeof t}(n,e)))||s.push({message:`Invalid type '${typeof n}' on property '${t}'`,code:"type"}),e.rule&&Object.keys(e.rule).forEach((r=>{(0,e.rule[r])(n)&&s.push({message:`Invalid value '${r}' on property '${t}'`,code:r})}))),s)}function c(e={},t,r){if(!e.type)return!1;const n=e.type.prototype instanceof u&&e.type,o="object"==typeof e.type&&e.type;return!(!n&&!o)&&(n||u.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),o,r))}class u extends class{constructor(t,r){return new Proxy(this,{set(o,u,i){let a=t[u];const f=o[u],p=(l=i,y=f,!(JSON.stringify(l)===JSON.stringify(y)));var l,y;const d=c(a,u,r);function h(e,r){return Reflect.apply(e,o,r||[u,i,f,t])}if(!a){if(r.strict)return!0;a={}}if(h(o.onBeforeSet),p&&h(o.onBeforeUpdate),n(a.transform)&&(i=h(a.transform,[i,t])),r.exceptions){const t=s(a,u,i);if(t.length)throw new e({message:t[0].message,property:u,code:t[0].code,source:o.constructor.name})}return d&&(i=new d(i instanceof Object?i:{})),o[u]=i,h(o.onSet),p&&h(o.onUpdate),!0},get(e,r){const o=t[r];let s=e[r];if(!o)return e[r];function c(n,o){return Reflect.apply(n,e,o||[r,s,t])}return c(e.onBeforeGet),n(o)&&(s=c(o,[e,t])),n(o.format)&&(s=c(o.format,[s,t])),c(e.onGet),s},deleteProperty(r,n){const o=r[n];function s(e,s){return Reflect.apply(e,r,s||[n,o,t])}if(t[n].required)throw new e({message:`Invalid delete on required propery ${n}`,property:n,code:"required"});return s(r.onBeforeDelete),Reflect.deleteProperty(r,n),s(r.onDelete),s(r.onUpdate),!0}})}}{constructor(e={},r={},n){super(e,n),Object.keys(e).forEach((n=>{t(r[n])&&(e[n].default?this[n]=e[n].default:this[n]=r[n])})),this.feed(r)}feed(e){Object.keys(e).forEach((t=>{this[t]=e[t]}))}onBeforeGet(){}onBeforeSet(){}onBeforeUpdate(){}onDelete(){}onGet(){}onBeforeDelete(){}onSet(){}onUpdate(){}}u.settings={strict:!1,exceptions:!0},u.create=function(e,t,r,n){r=Object.assign({},u.settings,r);const i={[e]:class extends u{constructor(e){super(t,e,r)}}}[e];return i.checkErrors=function(e,n){const u={};return Object.keys(t).forEach((i=>{let a;const f=e[i],p=t[i],l=c(p,i,r);l&&(a=l.checkErrors(f,n));let y=s(p,i,f);a?u[i]=a:y.length&&(n&&(y=y.filter((e=>!o(n).includes(e.code)))),y.length&&(u[i]=y))})),!!Object.keys(u).length&&u},i.hydrate=function(e){return t=e,Array.isArray(t)?e.map((e=>new i(e))):new i(e);var t},Object.assign(i.prototype,n),i.schema=t,i};export default u; | ||
/*! SmartModel v0.2.14 */ | ||
class e extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function t(e){return Array.isArray(e)}function r(e){return void 0===e}function n(e){return""===e||null===e||r(e)}function o(e){return"function"==typeof e}function s(e){return e.toString().startsWith("class")}function c(e){return[].concat([],e)}function i(e,r,o){const i=[];return e.required&&n(o)?(i.push({message:`Invalid value 'required' on property '${r}'`,code:"required"}),i):(void 0===o||(!e.type||!e.required&&n(o)||c(e.type).some((e=>function(e,r){const n=r&&r.toString().match(/^\s*function (\w+)/),o=(n?n[1]:"object").toLowerCase();if("date"===o&&e instanceof r)return!0;if("array"===o&&t(e))return!0;if("object"===o){if(s(r)&&e instanceof r)return!0;if(!s(r)&&typeof e===o)return!0}else if(typeof e===o)return!0;return!1}(o,e)))||i.push({message:`Invalid type '${typeof o}' on property '${r}'`,code:"type"}),e.rule&&Object.keys(e.rule).forEach((t=>{(0,e.rule[t])(o)&&i.push({message:`Invalid value '${t}' on property '${r}'`,code:t})}))),i)}function u(e={},t,r){if(!e.type)return!1;const n=e.type.prototype instanceof a&&e.type,o="[object Object]"===e.type.toString()&&e.type;return!(!n&&!o)&&(n||a.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),o,r))}class a extends class{constructor(t,r){return new Proxy(this,{set(n,s,c){let a=t[s];const f=n[s],p=(l=c,d=f,!(JSON.stringify(l)===JSON.stringify(d)));var l,d;const y=u(a,s,r);function h(e,r){return Reflect.apply(e,n,r||[s,c,f,t])}if(!a){if(r.strict)return!0;a={}}if(h(n.onBeforeSet),p&&h(n.onBeforeUpdate),o(a.transform)&&(c=h(a.transform,[c,t])),r.exceptions){const t=i(a,s,c);if(t.length)throw new e({message:t[0].message,property:s,code:t[0].code,source:n.constructor.name})}return y&&(c=new y(c instanceof Object?c:{})),n[s]=c,h(n.onSet),p&&h(n.onUpdate),!0},get(e,r){const n=t[r];let s=e[r];if(!n)return e[r];function c(n,o){return Reflect.apply(n,e,o||[r,s,t])}return c(e.onBeforeGet),o(n)&&(s=c(n,[e,t])),o(n.format)&&(s=c(n.format,[s,t])),c(e.onGet),s},deleteProperty(r,n){const o=r[n];function s(e,s){return Reflect.apply(e,r,s||[n,o,t])}if(t[n].required)throw new e({message:`Invalid delete on required propery ${n}`,property:n,code:"required"});return s(r.onBeforeDelete),Reflect.deleteProperty(r,n),s(r.onDelete),s(r.onUpdate),!0}})}}{constructor(e={},t={},n){super(e,n),Object.keys(e).forEach((n=>{r(t[n])&&(r(e[n].default)?this[n]=t[n]:this[n]=e[n].default)})),this.feed(t)}feed(e){Object.keys(e).forEach((t=>{this[t]=e[t]}))}onBeforeGet(){}onBeforeSet(){}onBeforeUpdate(){}onDelete(){}onGet(){}onBeforeDelete(){}onSet(){}onUpdate(){}}a.settings={strict:!1,exceptions:!0},a.create=function(e,r,n,o){n=Object.assign({},a.settings,n);const s={[e]:class extends a{constructor(e){super(r,e,n)}}}[e];return s.checkErrors=function(e,t){const o={};return Object.keys(r).forEach((s=>{let a;const f=e[s],p=r[s],l=u(p,s,n);l&&(a=l.checkErrors(f,t));let d=i(p,s,f);a?o[s]=a:d.length&&(t&&(d=d.filter((e=>!c(t).includes(e.code)))),d.length&&(o[s]=d))})),!!Object.keys(o).length&&o},s.hydrate=function(e){return t(e)?e.map((e=>new s(e))):new s(e)},Object.assign(s.prototype,o),s.schema=r,s};export default a; |
@@ -6,3 +6,3 @@ | ||
* https://github.com/jaysalvat/smart-model | ||
* @version 0.2.13 built 2021-02-21 11:54:48 | ||
* @version 0.2.14 built 2021-02-21 15:21:56 | ||
* @license ISC | ||
@@ -50,16 +50,26 @@ * @author Jay Salvat http://jaysalvat.com | ||
function isPlainObject(value) { | ||
return value.toString() === '[object Object]' | ||
} | ||
function isType(value, Type) { | ||
if (typeof Type === 'object') { | ||
Type = Object; | ||
const match = Type && Type.toString().match(/^\s*function (\w+)/); | ||
const type = (match ? match[1] : 'object').toLowerCase(); | ||
if (type === 'date' && value instanceof Type) { | ||
return true | ||
} | ||
if (!isClass(Type) && typeof value === typeof Type()) { | ||
if (type === 'array' && isArray(value)) { | ||
return true | ||
} | ||
// if (isClass(Type) && typeof value === typeof new Type({}, { exceptions: false })) { | ||
// return true | ||
// } | ||
if (value instanceof Type || typeof value === typeof Type) { | ||
if (type === 'object') { | ||
if (isClass(Type) && value instanceof Type) { | ||
return true | ||
} | ||
if (!isClass(Type) && typeof value === type) { | ||
return true | ||
} | ||
} else if (typeof value === type) { | ||
return true | ||
@@ -131,3 +141,3 @@ } | ||
const Child = entry.type.prototype instanceof SmartModel ? entry.type : false; | ||
const schema = typeof entry.type === 'object' ? entry.type : false; | ||
const schema = isPlainObject(entry.type) ? entry.type : false; | ||
@@ -262,3 +272,3 @@ if (Child || schema) { | ||
if (isUndef(data[key])) { | ||
if (schema[key].default) { | ||
if (!isUndef(schema[key].default)) { | ||
this[key] = schema[key].default; | ||
@@ -265,0 +275,0 @@ } else { |
@@ -1,2 +0,2 @@ | ||
/*! SmartModel v0.2.13 */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).SmartModel={})}(this,(function(e){"use strict";class t extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function r(e){return void 0===e}function n(e){return""===e||null===e||r(e)}function o(e){return"function"==typeof e}function s(e){return[].concat([],e)}function c(e,t,r){const o=[];return e.required&&n(r)?(o.push({message:`Invalid value 'required' on property '${t}'`,code:"required"}),o):(void 0===r||(!e.type||!e.required&&n(r)||s(e.type).some((e=>function(e,t){return"object"==typeof t&&(t=Object),!function(e){return e.toString().startsWith("class")}(t)&&typeof e==typeof t()||e instanceof t||typeof e==typeof t}(r,e)))||o.push({message:`Invalid type '${typeof r}' on property '${t}'`,code:"type"}),e.rule&&Object.keys(e.rule).forEach((n=>{(0,e.rule[n])(r)&&o.push({message:`Invalid value '${n}' on property '${t}'`,code:n})}))),o)}function i(e={},t,r){if(!e.type)return!1;const n=e.type.prototype instanceof u&&e.type,o="object"==typeof e.type&&e.type;return!(!n&&!o)&&(n||u.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),o,r))}class u extends class{constructor(e,r){return new Proxy(this,{set(n,s,u){let f=e[s];const a=n[s],p=(l=u,d=a,!(JSON.stringify(l)===JSON.stringify(d)));var l,d;const y=i(f,s,r);function h(t,r){return Reflect.apply(t,n,r||[s,u,a,e])}if(!f){if(r.strict)return!0;f={}}if(h(n.onBeforeSet),p&&h(n.onBeforeUpdate),o(f.transform)&&(u=h(f.transform,[u,e])),r.exceptions){const e=c(f,s,u);if(e.length)throw new t({message:e[0].message,property:s,code:e[0].code,source:n.constructor.name})}return y&&(u=new y(u instanceof Object?u:{})),n[s]=u,h(n.onSet),p&&h(n.onUpdate),!0},get(t,r){const n=e[r];let s=t[r];if(!n)return t[r];function c(n,o){return Reflect.apply(n,t,o||[r,s,e])}return c(t.onBeforeGet),o(n)&&(s=c(n,[t,e])),o(n.format)&&(s=c(n.format,[s,e])),c(t.onGet),s},deleteProperty(r,n){const o=r[n];function s(t,s){return Reflect.apply(t,r,s||[n,o,e])}if(e[n].required)throw new t({message:`Invalid delete on required propery ${n}`,property:n,code:"required"});return s(r.onBeforeDelete),Reflect.deleteProperty(r,n),s(r.onDelete),s(r.onUpdate),!0}})}}{constructor(e={},t={},n){super(e,n),Object.keys(e).forEach((n=>{r(t[n])&&(e[n].default?this[n]=e[n].default:this[n]=t[n])})),this.feed(t)}feed(e){Object.keys(e).forEach((t=>{this[t]=e[t]}))}onBeforeGet(){}onBeforeSet(){}onBeforeUpdate(){}onDelete(){}onGet(){}onBeforeDelete(){}onSet(){}onUpdate(){}}u.settings={strict:!1,exceptions:!0},u.create=function(e,t,r,n){r=Object.assign({},u.settings,r);const o={[e]:class extends u{constructor(e){super(t,e,r)}}}[e];return o.checkErrors=function(e,n){const o={};return Object.keys(t).forEach((u=>{let f;const a=e[u],p=t[u],l=i(p,u,r);l&&(f=l.checkErrors(a,n));let d=c(p,u,a);f?o[u]=f:d.length&&(n&&(d=d.filter((e=>!s(n).includes(e.code)))),d.length&&(o[u]=d))})),!!Object.keys(o).length&&o},o.hydrate=function(e){return t=e,Array.isArray(t)?e.map((e=>new o(e))):new o(e);var t},Object.assign(o.prototype,n),o.schema=t,o},e.default=u,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
/*! SmartModel v0.2.14 */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).SmartModel={})}(this,(function(e){"use strict";class t extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function r(e){return Array.isArray(e)}function n(e){return void 0===e}function o(e){return""===e||null===e||n(e)}function s(e){return"function"==typeof e}function c(e){return e.toString().startsWith("class")}function i(e){return[].concat([],e)}function u(e,t,n){const s=[];return e.required&&o(n)?(s.push({message:`Invalid value 'required' on property '${t}'`,code:"required"}),s):(void 0===n||(!e.type||!e.required&&o(n)||i(e.type).some((e=>function(e,t){const n=t&&t.toString().match(/^\s*function (\w+)/),o=(n?n[1]:"object").toLowerCase();if("date"===o&&e instanceof t)return!0;if("array"===o&&r(e))return!0;if("object"===o){if(c(t)&&e instanceof t)return!0;if(!c(t)&&typeof e===o)return!0}else if(typeof e===o)return!0;return!1}(n,e)))||s.push({message:`Invalid type '${typeof n}' on property '${t}'`,code:"type"}),e.rule&&Object.keys(e.rule).forEach((r=>{(0,e.rule[r])(n)&&s.push({message:`Invalid value '${r}' on property '${t}'`,code:r})}))),s)}function f(e={},t,r){if(!e.type)return!1;const n=e.type.prototype instanceof a&&e.type,o="[object Object]"===e.type.toString()&&e.type;return!(!n&&!o)&&(n||a.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),o,r))}class a extends class{constructor(e,r){return new Proxy(this,{set(n,o,c){let i=e[o];const a=n[o],p=(l=c,d=a,!(JSON.stringify(l)===JSON.stringify(d)));var l,d;const y=f(i,o,r);function h(t,r){return Reflect.apply(t,n,r||[o,c,a,e])}if(!i){if(r.strict)return!0;i={}}if(h(n.onBeforeSet),p&&h(n.onBeforeUpdate),s(i.transform)&&(c=h(i.transform,[c,e])),r.exceptions){const e=u(i,o,c);if(e.length)throw new t({message:e[0].message,property:o,code:e[0].code,source:n.constructor.name})}return y&&(c=new y(c instanceof Object?c:{})),n[o]=c,h(n.onSet),p&&h(n.onUpdate),!0},get(t,r){const n=e[r];let o=t[r];if(!n)return t[r];function c(n,s){return Reflect.apply(n,t,s||[r,o,e])}return c(t.onBeforeGet),s(n)&&(o=c(n,[t,e])),s(n.format)&&(o=c(n.format,[o,e])),c(t.onGet),o},deleteProperty(r,n){const o=r[n];function s(t,s){return Reflect.apply(t,r,s||[n,o,e])}if(e[n].required)throw new t({message:`Invalid delete on required propery ${n}`,property:n,code:"required"});return s(r.onBeforeDelete),Reflect.deleteProperty(r,n),s(r.onDelete),s(r.onUpdate),!0}})}}{constructor(e={},t={},r){super(e,r),Object.keys(e).forEach((r=>{n(t[r])&&(n(e[r].default)?this[r]=t[r]:this[r]=e[r].default)})),this.feed(t)}feed(e){Object.keys(e).forEach((t=>{this[t]=e[t]}))}onBeforeGet(){}onBeforeSet(){}onBeforeUpdate(){}onDelete(){}onGet(){}onBeforeDelete(){}onSet(){}onUpdate(){}}a.settings={strict:!1,exceptions:!0},a.create=function(e,t,n,o){n=Object.assign({},a.settings,n);const s={[e]:class extends a{constructor(e){super(t,e,n)}}}[e];return s.checkErrors=function(e,r){const o={};return Object.keys(t).forEach((s=>{let c;const a=e[s],p=t[s],l=f(p,s,n);l&&(c=l.checkErrors(a,r));let d=u(p,s,a);c?o[s]=c:d.length&&(r&&(d=d.filter((e=>!i(r).includes(e.code)))),d.length&&(o[s]=d))})),!!Object.keys(o).length&&o},s.hydrate=function(e){return r(e)?e.map((e=>new s(e))):new s(e)},Object.assign(s.prototype,o),s.schema=t,s},e.default=a,Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@jaysalvat/smart-model", | ||
"version": "0.2.13", | ||
"version": "0.2.14", | ||
"description": "Javascript object model", | ||
@@ -5,0 +5,0 @@ "main": "./build/smart-model.umd.js", |
@@ -1,2 +0,2 @@ | ||
import { pascalCase } from './utils.js' | ||
import { pascalCase, isPlainObject } from './utils.js' | ||
import SmartModel from './SmartModel.js' | ||
@@ -10,3 +10,3 @@ | ||
const Child = entry.type.prototype instanceof SmartModel ? entry.type : false | ||
const schema = typeof entry.type === 'object' ? entry.type : false | ||
const schema = isPlainObject(entry.type) ? entry.type : false | ||
@@ -13,0 +13,0 @@ if (Child || schema) { |
@@ -13,3 +13,3 @@ | ||
if (isUndef(data[key])) { | ||
if (schema[key].default) { | ||
if (!isUndef(schema[key].default)) { | ||
this[key] = schema[key].default | ||
@@ -16,0 +16,0 @@ } else { |
@@ -27,16 +27,26 @@ /* eslint-disable new-cap */ | ||
export function isPlainObject(value) { | ||
return value.toString() === '[object Object]' | ||
} | ||
export function isType(value, Type) { | ||
if (typeof Type === 'object') { | ||
Type = Object | ||
const match = Type && Type.toString().match(/^\s*function (\w+)/) | ||
const type = (match ? match[1] : 'object').toLowerCase() | ||
if (type === 'date' && value instanceof Type) { | ||
return true | ||
} | ||
if (!isClass(Type) && typeof value === typeof Type()) { | ||
if (type === 'array' && isArray(value)) { | ||
return true | ||
} | ||
// if (isClass(Type) && typeof value === typeof new Type({}, { exceptions: false })) { | ||
// return true | ||
// } | ||
if (value instanceof Type || typeof value === typeof Type) { | ||
if (type === 'object') { | ||
if (isClass(Type) && value instanceof Type) { | ||
return true | ||
} | ||
if (!isClass(Type) && typeof value === type) { | ||
return true | ||
} | ||
} else if (typeof value === type) { | ||
return true | ||
@@ -43,0 +53,0 @@ } |
@@ -61,3 +61,3 @@ /* eslint-disable prefer-reflect */ | ||
it('should not throw an error if a required prop is set on init', function () { | ||
it('should not throw an error if a required prop is set on init', function () { | ||
const Model = SmartModel.create('Model', { | ||
@@ -75,3 +75,3 @@ prop: { required: true } | ||
it('should not throw an error if a required prop is not set but have default value on init', function () { | ||
it('should not throw an error if a required prop is not set but have default value on init', function () { | ||
const Model = SmartModel.create('Model', { | ||
@@ -89,4 +89,3 @@ prop: { required: true, default: 'string' } | ||
it('should throw an error if a required prop is set to empty', function () { | ||
it('should throw an error if a required prop is set to empty', function () { | ||
const Model = SmartModel.create('Model', { | ||
@@ -105,4 +104,3 @@ prop: { required: true, default: 'string' } | ||
it('should throw an error if a required prop is deleted', function () { | ||
it('should throw an error if a required prop is deleted', function () { | ||
const Model = SmartModel.create('Model', { | ||
@@ -125,3 +123,3 @@ prop: { required: true, default: 'string' } | ||
describe('Type', function () { | ||
it('should throw an error if a typed prop is not set properly', function () { | ||
it('should throw an error if a typed prop is not set properly', function () { | ||
const Model = SmartModel.create('Model', { | ||
@@ -139,3 +137,3 @@ prop: { required: true, type: String } | ||
it('should not throw an error if a typed prop is set properly', function () { | ||
it('should not throw an error if a typed prop is set properly', function () { | ||
const Model = SmartModel.create('Model', { | ||
@@ -153,3 +151,3 @@ prop: { required: true, type: String } | ||
it('should not throw an error if multiple typed props is set properly', function () { | ||
it('should not throw an error if multiple typed props is set properly', function () { | ||
const Model = SmartModel.create('Model', { | ||
@@ -169,3 +167,3 @@ prop: { type: [ String, Array ] } | ||
it('should not throw an error if a typed prop is set on a not required property', function () { | ||
it('should not throw an error if a typed prop is set on a not required property', function () { | ||
const Model = SmartModel.create('Model', { | ||
@@ -182,8 +180,43 @@ prop: { type: String } | ||
}) | ||
it('should be alright with all those type checks', function () { | ||
const TestModel = SmartModel.create('Test', {}) | ||
const testModel = new TestModel() | ||
const types = [ | ||
{ type: Date, fail: 100, success: new Date() }, | ||
{ type: Array, fail: {}, success: [] }, | ||
{ type: String, fail: 0, success: 'string' }, | ||
{ type: Object, fail: 'string', success: {} }, | ||
{ type: Number, fail: 'string', success: 100 }, | ||
{ type: Boolean, fail: 'string', success: true }, | ||
{ type: Function, fail: 'string', success: function () { } }, | ||
{ type: SmartModel, fail: {}, success: testModel }, | ||
{ type: TestModel, fail: 'string', success: testModel } | ||
] | ||
types.forEach((type) => { | ||
expect(() => { | ||
const Model = SmartModel.create('Model', { | ||
prop: { type: type.type, default: type.fail } | ||
}) | ||
new Model() | ||
}).to.throw('type') | ||
expect(() => { | ||
const Model = SmartModel.create('Model', { | ||
prop: { type: type.type, default: type.success } | ||
}) | ||
new Model() | ||
}).to.not.throw('type') | ||
}) | ||
}) | ||
}) | ||
// Rule | ||
// // Rule | ||
describe('Rule', function () { | ||
it('should throw an error if a ruled prop is not set properly', function () { | ||
it('should throw an error if a ruled prop is not set properly', function () { | ||
const Model = SmartModel.create('Model', { | ||
@@ -203,3 +236,3 @@ prop: { required: true, rule: { | ||
it('should not throw an error if a ruled prop is set properly', function () { | ||
it('should not throw an error if a ruled prop is set properly', function () { | ||
const Model = SmartModel.create('Model', { | ||
@@ -219,3 +252,3 @@ prop: { required: true, rule: { | ||
it('should not throw an error if a ruled prop is set on an empty not required property', function () { | ||
it('should not throw an error if a ruled prop is set on an empty not required property', function () { | ||
let err = {} | ||
@@ -222,0 +255,0 @@ const Model = SmartModel.create('Model', { |
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
61064
1612