@jaysalvat/smart-model
Advanced tools
Comparing version 0.2.5 to 0.2.6
/**! | ||
* smartModel | ||
* SmartModel | ||
* Javascript object model | ||
* https://github.com/jaysalvat/smart-model | ||
* @version 0.2.5 built 2021-02-19 18:00:24 | ||
* @version 0.2.6 built 2021-02-19 20:48:27 | ||
* @license ISC | ||
@@ -8,0 +8,0 @@ * @author Jay Salvat http://jaysalvat.com |
@@ -1,2 +0,2 @@ | ||
/*! smartModel v0.2.5 */ | ||
/*! SmartModel v0.2.6 */ | ||
function e(e){return void 0===e}function t(t){return""===t||null===t||e(t)}function r(e){return"function"==typeof e}function n(e){return e?JSON.parse(JSON.stringify(e)):e}function o(e){return[].concat([],e)}function s(e,r,n){const s=[];return e.required&&t(n)?(s.push({message:`Invalid value 'required' on property '${r}'`,code:"required"}),s):(void 0===n||(!e.type||!e.required&&t(n)||o(e.type).some((e=>function(e,t){return typeof e==typeof t()||e instanceof t}(n,e)))||s.push({message:`Invalid type '${typeof n}' on property '${r}'`,code:"type"}),e.rule&&Object.keys(e.rule).forEach((t=>{(0,e.rule[t])(n)&&s.push({message:`Invalid value '${t}' on property '${r}'`,code:t})}))),s)}class c extends Error{constructor(e){super(e.message),Object.assign(this,e)}}class u extends class{constructor(e={},t={}){return new Proxy(this,{set(r,o,u){const i=n(r[o]),f=(a=u,p=i,!(JSON.stringify(a)===JSON.stringify(p)));var a,p;const l=e[o];function d(t,n){return Reflect.apply(t,r,n||[o,u,i,e])}if(!l)return t.strict||(r[o]=u),!0;if(d(r.onBeforeSet),f&&d(r.onBeforeUpdate),l.transform&&(u=d(l.transform,[u,e])),t.exceptions){const e=s(l,o,u);e.length&&e.forEach((e=>{throw new c({message:e.message,property:o,code:e.code})}))}return r[o]=u,d(r.onSet),f&&d(r.onUpdate),!0},get(t,n){const o=e[n];let s=t[n];if(!o)return t[n];function c(r,o){return Reflect.apply(r,t,o||[n,s,e])}return c(t.onBeforeGet),r(o)&&(s=c(o,[t,e])),r(o.format)&&(s=c(o.format,[s,e])),c(t.onGet),s},deleteProperty(t,r){const o=n(t[r]);function s(n,s){return Reflect.apply(n,t,s||[r,o,e])}if(e[r].required)throw new c({message:`Invalid delete on required propery ${r}`,property:r,code:"required"});return s(t.onBeforeDelete),Reflect.deleteProperty(t,r),s(t.onDelete),s(t.onUpdate),!0}})}}{constructor(t={},r={},n={}){super(t,n),Object.keys(t).forEach((n=>{e(r[n])&&(t[n].default?this[n]=t[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 c={[e]:class extends u{constructor(e){super(t,e,r)}}}[e];return c.checkErrors=function(e,r){const n={};return Object.keys(t).forEach((c=>{let u=s(t[c],c,e[c]);u.length&&(r&&(u=u.filter((e=>!o(r).includes(e.code)))),u.length&&(n[c]=u))})),!!Object.keys(n).length&&n},c.hydrate=function(e){return t=e,Array.isArray(t)?e.map((e=>new c(e))):new c(e);var t},Object.assign(c.prototype,n),c};export default u; |
/**! | ||
* smartModel | ||
* SmartModel | ||
* Javascript object model | ||
* https://github.com/jaysalvat/smart-model | ||
* @version 0.2.5 built 2021-02-19 18:00:24 | ||
* @version 0.2.6 built 2021-02-19 20:48:27 | ||
* @license ISC | ||
@@ -13,3 +13,3 @@ * @author Jay Salvat http://jaysalvat.com | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.smartModel = {})); | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.SmartModel = {})); | ||
}(this, (function (exports) { 'use strict'; | ||
@@ -16,0 +16,0 @@ |
@@ -1,2 +0,2 @@ | ||
/*! smartModel v0.2.5 */ | ||
!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";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 e?JSON.parse(JSON.stringify(e)):e}function s(e){return[].concat([],e)}function c(e,t,n){const o=[];return e.required&&r(n)?(o.push({message:`Invalid value 'required' on property '${t}'`,code:"required"}),o):(void 0===n||(!e.type||!e.required&&r(n)||s(e.type).some((e=>function(e,t){return typeof e==typeof t()||e instanceof t}(n,e)))||o.push({message:`Invalid type '${typeof n}' on property '${t}'`,code:"type"}),e.rule&&Object.keys(e.rule).forEach((r=>{(0,e.rule[r])(n)&&o.push({message:`Invalid value '${r}' on property '${t}'`,code:r})}))),o)}class i extends Error{constructor(e){super(e.message),Object.assign(this,e)}}class u extends class{constructor(e={},t={}){return new Proxy(this,{set(r,n,s){const u=o(r[n]),f=(a=s,p=u,!(JSON.stringify(a)===JSON.stringify(p)));var a,p;const d=e[n];function l(t,o){return Reflect.apply(t,r,o||[n,s,u,e])}if(!d)return t.strict||(r[n]=s),!0;if(l(r.onBeforeSet),f&&l(r.onBeforeUpdate),d.transform&&(s=l(d.transform,[s,e])),t.exceptions){const e=c(d,n,s);e.length&&e.forEach((e=>{throw new i({message:e.message,property:n,code:e.code})}))}return r[n]=s,l(r.onSet),f&&l(r.onUpdate),!0},get(t,r){const o=e[r];let s=t[r];if(!o)return t[r];function c(n,o){return Reflect.apply(n,t,o||[r,s,e])}return c(t.onBeforeGet),n(o)&&(s=c(o,[t,e])),n(o.format)&&(s=c(o.format,[s,e])),c(t.onGet),s},deleteProperty(t,r){const n=o(t[r]);function s(o,s){return Reflect.apply(o,t,s||[r,n,e])}if(e[r].required)throw new i({message:`Invalid delete on required propery ${r}`,property:r,code:"required"});return s(t.onBeforeDelete),Reflect.deleteProperty(t,r),s(t.onDelete),s(t.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 o={[e]:class extends u{constructor(e){super(t,e,r)}}}[e];return o.checkErrors=function(e,r){const n={};return Object.keys(t).forEach((o=>{let i=c(t[o],o,e[o]);i.length&&(r&&(i=i.filter((e=>!s(r).includes(e.code)))),i.length&&(n[o]=i))})),!!Object.keys(n).length&&n},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},e.default=u,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
/*! SmartModel v0.2.6 */ | ||
!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";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 e?JSON.parse(JSON.stringify(e)):e}function s(e){return[].concat([],e)}function c(e,t,n){const o=[];return e.required&&r(n)?(o.push({message:`Invalid value 'required' on property '${t}'`,code:"required"}),o):(void 0===n||(!e.type||!e.required&&r(n)||s(e.type).some((e=>function(e,t){return typeof e==typeof t()||e instanceof t}(n,e)))||o.push({message:`Invalid type '${typeof n}' on property '${t}'`,code:"type"}),e.rule&&Object.keys(e.rule).forEach((r=>{(0,e.rule[r])(n)&&o.push({message:`Invalid value '${r}' on property '${t}'`,code:r})}))),o)}class i extends Error{constructor(e){super(e.message),Object.assign(this,e)}}class u extends class{constructor(e={},t={}){return new Proxy(this,{set(r,n,s){const u=o(r[n]),f=(a=s,p=u,!(JSON.stringify(a)===JSON.stringify(p)));var a,p;const d=e[n];function l(t,o){return Reflect.apply(t,r,o||[n,s,u,e])}if(!d)return t.strict||(r[n]=s),!0;if(l(r.onBeforeSet),f&&l(r.onBeforeUpdate),d.transform&&(s=l(d.transform,[s,e])),t.exceptions){const e=c(d,n,s);e.length&&e.forEach((e=>{throw new i({message:e.message,property:n,code:e.code})}))}return r[n]=s,l(r.onSet),f&&l(r.onUpdate),!0},get(t,r){const o=e[r];let s=t[r];if(!o)return t[r];function c(n,o){return Reflect.apply(n,t,o||[r,s,e])}return c(t.onBeforeGet),n(o)&&(s=c(o,[t,e])),n(o.format)&&(s=c(o.format,[s,e])),c(t.onGet),s},deleteProperty(t,r){const n=o(t[r]);function s(o,s){return Reflect.apply(o,t,s||[r,n,e])}if(e[r].required)throw new i({message:`Invalid delete on required propery ${r}`,property:r,code:"required"});return s(t.onBeforeDelete),Reflect.deleteProperty(t,r),s(t.onDelete),s(t.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 o={[e]:class extends u{constructor(e){super(t,e,r)}}}[e];return o.checkErrors=function(e,r){const n={};return Object.keys(t).forEach((o=>{let i=c(t[o],o,e[o]);i.length&&(r&&(i=i.filter((e=>!s(r).includes(e.code)))),i.length&&(n[o]=i))})),!!Object.keys(n).length&&n},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},e.default=u,Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@jaysalvat/smart-model", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"description": "Javascript object model", | ||
@@ -11,2 +11,3 @@ "main": "./build/smart-model.umd.js", | ||
"test": "mocha", | ||
"test:browser": "open http://localhost:5500/test/index.html && light-server -s . -p 5500", | ||
"test:watch": "mocha ./test/test.js --parallel --watch", | ||
@@ -13,0 +14,0 @@ "lint": "eslint .", |
import { terser } from 'rollup-plugin-terser' | ||
import pkg from './package.json' | ||
const NAME = 'smartModel' | ||
const NAME = 'SmartModel' | ||
const FILENAME = 'smart-model' | ||
@@ -6,0 +6,0 @@ const SRC = './src' |
599
test/test.js
@@ -1,601 +0,8 @@ | ||
/* eslint-disable prefer-reflect */ | ||
/* eslint-disable no-new */ | ||
/* eslint-env mocha */ | ||
import chai from 'chai' | ||
import SmartModel from '../src/SmartModel.js' | ||
import chai from '../node_modules/chai/chai.js' | ||
import test from './test.shared.js' | ||
const { expect } = chai | ||
let undef | ||
describe('SmartModel', function () { | ||
// Init | ||
describe('Init', function () { | ||
it('should create a Model', function () { | ||
const Model = SmartModel.create('Model', {}) | ||
const model = new Model() | ||
expect(Model).to. be.an.instanceOf(Function) | ||
expect(model).to. be.an.instanceOf(SmartModel) | ||
expect(model).to. be.an.instanceOf(Model) | ||
}) | ||
it('should populate with a schema', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop1: {}, prop2: {}, prop3: {} | ||
}) | ||
const model = new Model() | ||
expect(Object.keys(model).length).to.be.equal(3) | ||
}) | ||
it('should add default values', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop1: { default: 'Default prop1' }, | ||
prop2: { } | ||
}) | ||
const model = new Model() | ||
expect(model.prop1).to.be.equal('Default prop1') | ||
expect(model.prop2).to.be.equal(undef) | ||
}) | ||
}) | ||
// Required | ||
describe('Required', function () { | ||
it('should throw an exception if a required prop is not set on init', function () { | ||
let code, property | ||
const Model = SmartModel.create('Model', { | ||
prop: { required: true } | ||
}) | ||
try { | ||
new Model() | ||
} catch (e) { | ||
property = e.property | ||
code = e.code | ||
} | ||
expect(property).to.be.equal('prop') | ||
expect(code).to.be.equal('required') | ||
}) | ||
it('should not throw an exception if a required prop is set on init', function () { | ||
let code, property | ||
const Model = SmartModel.create('Model', { | ||
prop: { required: true } | ||
}) | ||
try { | ||
new Model({ prop: 'ok' }) | ||
} catch (e) { | ||
property = e.property | ||
code = e.code | ||
} | ||
expect(property).to.be.equal(undef) | ||
expect(code).to.be.equal(undef) | ||
}) | ||
it('should not throw an exception if a required prop is not set but have default value on init', function () { | ||
let code, property | ||
const Model = SmartModel.create('Model', { | ||
prop: { required: true, default: 'ok' } | ||
}) | ||
try { | ||
new Model() | ||
} catch (e) { | ||
property = e.property | ||
code = e.code | ||
} | ||
expect(property).to.be.equal(undef) | ||
expect(code).to.be.equal(undef) | ||
}) | ||
it('should throw an exception if a required prop is set to empty', function () { | ||
let code, property | ||
const Model = SmartModel.create('Model', { | ||
prop: { required: true, default: 'ok' } | ||
}) | ||
try { | ||
const model = new Model() | ||
model.prop = null | ||
} catch (e) { | ||
property = e.property | ||
code = e.code | ||
} | ||
expect(property).to.be.equal('prop') | ||
expect(code).to.be.equal('required') | ||
}) | ||
it('should throw an exception if a required prop is deleted', function () { | ||
let code, property | ||
const Model = SmartModel.create('Model', { | ||
prop: { required: true, default: 'ok' } | ||
}) | ||
try { | ||
const model = new Model() | ||
delete model.prop | ||
} catch (e) { | ||
property = e.property | ||
code = e.code | ||
} | ||
expect(property).to.be.equal('prop') | ||
expect(code).to.be.equal('required') | ||
}) | ||
}) | ||
// Type | ||
describe('Type', function () { | ||
it('should throw an exception if a typed prop is not set properly', function () { | ||
let code, property | ||
const Model = SmartModel.create('Model', { | ||
prop: { required: true, type: String } | ||
}) | ||
try { | ||
new Model({ | ||
prop: 0 | ||
}) | ||
} catch (e) { | ||
property = e.property | ||
code = e.code | ||
} | ||
expect(property).to.be.equal('prop') | ||
expect(code).to.be.equal('type') | ||
}) | ||
it('should not throw an exception if a typed prop is set properly', function () { | ||
let code, property | ||
const Model = SmartModel.create('Model', { | ||
prop: { required: true, type: String } | ||
}) | ||
try { | ||
new Model({ | ||
prop: 'ok' | ||
}) | ||
} catch (e) { | ||
property = e.property | ||
code = e.code | ||
} | ||
expect(property).to.be.equal(undef) | ||
expect(code).to.be.equal(undef) | ||
}) | ||
it('should not throw an exception if multiple typed props is set properly', function () { | ||
let code, property | ||
const Model = SmartModel.create('Model', { | ||
prop: { required: true, type: [ String, Array ] } | ||
}) | ||
try { | ||
const model = new Model({ prop: 'ok' }) | ||
model.prop = [ 'ok' ] | ||
} catch (e) { | ||
property = e.property | ||
code = e.code | ||
} | ||
expect(property).to.be.equal(undef) | ||
expect(code).to.be.equal(undef) | ||
}) | ||
it('should not throw an exception if a typed prop is set on a not required property', function () { | ||
let code, property | ||
const Model = SmartModel.create('Model', { | ||
prop: { type: String } | ||
}) | ||
try { | ||
new Model() | ||
} catch (e) { | ||
property = e.property | ||
code = e.code | ||
} | ||
expect(property).to.be.equal(undef) | ||
expect(code).to.be.equal(undef) | ||
}) | ||
}) | ||
// Rule | ||
describe('Rule', function () { | ||
it('should throw an exception if a ruled prop is not set properly', function () { | ||
let code, property | ||
const Model = SmartModel.create('Model', { | ||
prop: { required: true, rule: { | ||
min: (value) => value < 5 | ||
} } | ||
}) | ||
try { | ||
new Model({ | ||
prop: 0 | ||
}) | ||
} catch (e) { | ||
property = e.property | ||
code = e.code | ||
} | ||
expect(property).to.be.equal('prop') | ||
expect(code).to.be.equal('min') | ||
}) | ||
it('should not throw an exception if a ruled prop is set properly', function () { | ||
let code, property | ||
const Model = SmartModel.create('Model', { | ||
prop: { required: true, rule: { | ||
min: (value) => value < 5 | ||
} } | ||
}) | ||
try { | ||
new Model({ prop: 10 }) | ||
} catch (e) { | ||
property = e.property | ||
code = e.code | ||
} | ||
expect(property).to.be.equal(undef) | ||
expect(code).to.be.equal(undef) | ||
}) | ||
it('should not throw an exception if a ruled prop is set on an empty not required property', function () { | ||
let code, property | ||
const Model = SmartModel.create('Model', { | ||
prop: { rule: { | ||
min: (value) => value < 5 | ||
} } | ||
}) | ||
try { | ||
new Model() | ||
} catch (e) { | ||
property = e.property | ||
code = e.code | ||
} | ||
expect(property).to.be.equal(undef) | ||
expect(code).to.be.equal(undef) | ||
}) | ||
}) | ||
// Format / Transfor | ||
describe('Format / Transform', function () { | ||
it('should transform a value', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop: { transform: (value) => 'transform: ' + value } | ||
}) | ||
const model = new Model({ prop: 'ok' }) | ||
expect(model.prop).to.be.equal('transform: ok') | ||
}) | ||
it('should format a value', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop: { format: (value) => 'format: ' + value } | ||
}) | ||
const model = new Model({ prop: 'ok' }) | ||
expect(model.prop).to.be.equal('format: ok') | ||
}) | ||
it('should transform and format a value', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop: { | ||
transform: (value) => 'transform: ' + value, | ||
format: (value) => 'format: ' + value | ||
} | ||
}) | ||
const model = new Model({ prop: 'ok' }) | ||
expect(model.prop).to.be.equal('format: transform: ok') | ||
}) | ||
}) | ||
// Virtual property | ||
describe('Virtual property', function () { | ||
it('should get a virtual property', function () { | ||
const Model = SmartModel.create('Model', { | ||
today: () => new Date() | ||
}) | ||
const model = new Model() | ||
expect(model.today.toString()).to.be.equal(new Date().toString()) | ||
}) | ||
it('should get a virtual property with context', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop1: { default: 'ok' }, | ||
prop2: (model) => 'virtual: ' + model.prop1 | ||
}) | ||
const model = new Model() | ||
expect(model.prop1).to.be.equal('ok') | ||
expect(model.prop2).to.be.equal('virtual: ok') | ||
}) | ||
}) | ||
describe('Events', function () { | ||
it('should trigger onBeforeDelete', testTrigger('onBeforeDelete')) | ||
it('should trigger onBeforeGet', testTrigger('onBeforeGet')) | ||
it('should trigger onBeforeSet', testTrigger('onBeforeSet')) | ||
it('should trigger onBeforeUpdate', testTrigger('onBeforeUpdate')) | ||
it('should trigger onDelete', testTrigger('onDelete')) | ||
it('should trigger onGet', testTrigger('onGet')) | ||
it('should trigger onSet', testTrigger('onSet')) | ||
it('should trigger onUpdate', testTrigger('onUpdate')) | ||
}) | ||
// Methods | ||
describe('Statics methods', function () { | ||
describe('CheckErrors', function () { | ||
it('should return an array of model errors', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop1: { required: true }, | ||
prop2: { type: String }, | ||
prop3: { type: String }, | ||
prop4: { | ||
rule: { | ||
min: (value) => value < 5, | ||
pos: (value) => value < 0 | ||
} | ||
} | ||
}) | ||
const errrors = Model.checkErrors({ | ||
prop2: 0, | ||
prop3: 'ok', | ||
prop4: -1 | ||
}) | ||
const errorProperties = Object.keys(errrors) | ||
expect(errorProperties.length).to.be.equal(3) | ||
expect(errrors.prop1[0].code).to.be.equal('required') | ||
expect(errrors.prop2[0].code).to.be.equal('type') | ||
expect(errrors.prop4[0].code).to.be.equal('min') | ||
expect(errrors.prop4[1].code).to.be.equal('pos') | ||
}) | ||
it('should return an array of model errors without type', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop1: { type: String, required: true }, | ||
prop2: { type: String }, | ||
prop3: { type: String, required: true }, | ||
prop4: { type: String } | ||
}) | ||
const errrors = Model.checkErrors({ | ||
prop2: 0, | ||
prop4: 0 | ||
}, [ 'type' ]) | ||
const errorProperties = Object.keys(errrors) | ||
expect(errorProperties.length).to.be.equal(2) | ||
expect(errrors.prop1[0].code).to.be.equal('required') | ||
expect(errrors.prop3[0].code).to.be.equal('required') | ||
}) | ||
it('should not return an array of model errors', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop1: { type: String, required: true }, | ||
prop2: { type: String }, | ||
prop3: { type: String, required: true }, | ||
prop4: { type: String } | ||
}) | ||
const errrors = Model.checkErrors({ | ||
prop1: 'ok', | ||
prop3: 'ok' | ||
}) | ||
expect(errrors).to.be.equal(false) | ||
}) | ||
}) | ||
describe('Hydrate', function () { | ||
it('should hydrate a object', function () { | ||
const obj = { | ||
prop1: 'ok' | ||
} | ||
const Model = SmartModel.create('Model', { | ||
prop1: { format: (value) => 'format: ' + value }, | ||
prop2: { default: 'default' } | ||
}) | ||
const model = Model.hydrate(obj) | ||
expect(model.prop1).to.be.equal('format: ok') | ||
expect(model.prop2).to.be.equal('default') | ||
}) | ||
it('should hydrate an array of objects', function () { | ||
const objs = [ | ||
{ prop1: 'ok 1' }, | ||
{ prop1: 'ok 2' }, | ||
{ prop1: 'ok 3' } | ||
] | ||
const Model = SmartModel.create('Model', { | ||
prop1: { format: (value) => 'format: ' + value }, | ||
prop2: { default: 'default' } | ||
}) | ||
const models = Model.hydrate(objs) | ||
models.forEach((model, i) => { | ||
expect(model.prop1).to.be.equal('format: ok ' + (i + 1)) | ||
expect(model.prop2).to.be.equal('default') | ||
}) | ||
}) | ||
}) | ||
// Settings | ||
describe('Settings', function () { | ||
it('should set settings for all instances', function () { | ||
SmartModel.settings.strict = true | ||
const Model1 = SmartModel.create('Model1', { | ||
prop1: { type: String } | ||
}) | ||
const Model2 = SmartModel.create('Model2', { | ||
prop1: { type: String } | ||
}, { strict: false }) | ||
const Model3 = SmartModel.create('Model3', { | ||
prop1: { type: String } | ||
}) | ||
const model1 = new Model1({ | ||
prop1: 'ok', | ||
prop2: 'ok' | ||
}) | ||
const model2 = new Model2({ | ||
prop1: 'ok', | ||
prop2: 'ok' | ||
}) | ||
const model3 = new Model3({ | ||
prop1: 'ok', | ||
prop2: 'ok' | ||
}) | ||
expect(model1.prop1).to.be.equal('ok') | ||
expect(model1.prop2).to.be.equal(undef) | ||
expect(model2.prop1).to.be.equal('ok') | ||
expect(model2.prop2).to.be.equal('ok') | ||
expect(model3.prop1).to.be.equal('ok') | ||
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 model = new Model({ | ||
prop1: 'ok', | ||
prop2: 'ok' | ||
}) | ||
expect(model.prop1).to.be.equal('ok') | ||
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 model = new Model({ | ||
prop1: 'ok', | ||
prop2: 'ok' | ||
}) | ||
expect(model.prop1).to.be.equal('ok') | ||
expect(model.prop2).to.be.equal('ok') | ||
}) | ||
}) | ||
describe('exceptions', function () { | ||
it('should not throw exceptions if exceptions:false', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop1: { required: true } | ||
}, { | ||
exceptions: false | ||
}) | ||
expect(() => { | ||
new Model() | ||
}).to.not.throw(Error) | ||
}) | ||
it('should throw exceptions if exceptions:true', function () { | ||
const Model = SmartModel.create('Model', { | ||
prop1: { required: true } | ||
}, { | ||
exceptions: true | ||
}) | ||
expect(() => { | ||
new Model() | ||
}).to.throw(Error) | ||
}) | ||
}) | ||
}) | ||
}) | ||
function testTrigger(name) { | ||
return function () { | ||
let property, value, oldValue | ||
const Model = SmartModel.create('Model', { | ||
prop: { default: 'old value' } | ||
}, {}, { | ||
[name](prop, val, oldVal) { | ||
property = prop | ||
value = val | ||
oldValue = oldVal | ||
} | ||
}) | ||
const model = new Model() | ||
model.prop = 'new value' | ||
if ([ 'onBeforeDelete', 'onDelete' ].includes(name)) { | ||
delete model.prop | ||
expect(property).to.be.equal('prop') | ||
} else if ([ 'onBeforeGet', 'onGet' ].includes(name)) { | ||
model.prop = model.prop | ||
expect(property).to.be.equal('prop') | ||
expect(value).to.be.equal('new value') | ||
} else { | ||
expect(property).to.be.equal('prop') | ||
expect(value).to.be.equal('new value') | ||
expect(oldValue).to.be.equal('old value') | ||
} | ||
} | ||
} | ||
}) | ||
test(expect, SmartModel) |
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
48659
20
1282