Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@jaysalvat/smart-model

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jaysalvat/smart-model - npm Package Compare versions

Comparing version 0.2.0 to 0.2.1

src/ModelCreate.js

74

build/smart-model.esm.js

@@ -6,3 +6,3 @@

* https://github.com/jaysalvat/smart-model
* @version 0.2.0 built 2021-02-19 10:32:47
* @version 0.2.1 built 2021-02-19 12:37:35
* @license ISC

@@ -104,3 +104,3 @@ * @author Jay Salvat http://jaysalvat.com

const updated = !isEqual(value, oldValue);
let entry = schema[property];
const entry = schema[property];

@@ -111,12 +111,10 @@ function trigger(method) {

if (this.settings.strict && !entry) {
if (!entry) {
if (!this.settings.strict) {
target[property] = value;
}
return true
} else {
entry = {};
}
if (entry.transform) {
value = entry.transform(value);
}
trigger(target.onBeforeSet);

@@ -128,2 +126,6 @@

if (entry.transform) {
value = entry.transform(value);
}
if (this.settings.exceptions) {

@@ -251,43 +253,37 @@ const errors = checkErrors(entry, property, value);

SuperModel.checkErrors = function (payload, required) {
return Model.checkErrors(schema, payload, required)
};
SuperModel.checkErrors = function (payload, filters) {
const invalidations = {};
Object.assign(SuperModel.prototype, prototype);
Object.keys(schema).forEach((property) => {
const value = payload[property];
const entry = schema[property];
let errors = checkErrors(entry, property, value);
return SuperModel
};
if (errors.length) {
if (filters) {
errors = errors.filter((error) => !toArray(filters).includes(error.code));
}
Model.checkErrors = function (schema, payload, filters) {
const invalidations = {};
if (errors.length) {
invalidations[property] = errors;
}
}
});
Object.keys(schema).forEach((property) => {
const value = payload[property];
const entry = schema[property];
let errors = checkErrors(entry, property, value);
return Object.keys(invalidations).length ? invalidations : false
};
if (errors.length) {
if (filters) {
errors = errors.filter((error) => !toArray(filters).includes(error.code));
}
if (errors.length) {
invalidations[property] = errors;
}
SuperModel.hydrate = function (payload) {
if (isArray(payload)) {
return payload.map((item) => new SuperModel(item))
}
return
});
return new SuperModel(payload)
};
return Object.keys(invalidations).length ? invalidations : false
};
Object.assign(SuperModel.prototype, prototype);
Model.hydrate = function (ModelToHydrate, payload) {
if (isArray(payload)) {
return payload.map((item) => new ModelToHydrate(item))
}
return new ModelToHydrate(payload)
return SuperModel
};
export default Model;

@@ -1,2 +0,2 @@

/*! smartModel v0.2.0 */
function e(e){return""===e||null==e}function t(e){return"function"==typeof e}function r(e){return Array.isArray(e)}function n(e){return e?JSON.parse(JSON.stringify(e)):e}function o(t,n,o){const s=[];if(t.required&&e(o))return s.push({message:`Invalid value 'required' on property '${n}'`,code:"required"}),s;if(void 0===o)return s;if(t.type){let c;const i=r(t.type)?t.type:[t.type];c=!(t.required||!e(o))||i.some((e=>typeof o==typeof e()||o instanceof e)),c||s.push({message:`Invalid type '${typeof o}' on property '${n}'`,code:"type"})}return t.rule&&Object.keys(t.rule).forEach((e=>{(0,t.rule[e])(o)&&s.push({message:`Invalid value '${e}' on property '${n}'`,code:e})})),s}class s extends Error{constructor(e){super(e.message),Object.assign(this,e)}}class c{constructor(e={},t={}){this.schema=e,this.settings=t}set(e,t,r){const c=this.schema,i=n(e[t]),u=(f=r,a=i,!(JSON.stringify(f)===JSON.stringify(a)));var f,a;let p=c[t];function l(n){return Reflect.apply(n,e,[t,r,i,c])}if(this.settings.strict&&!p)return!0;if(p={},p.transform&&(r=p.transform(r)),l(e.onBeforeSet),u&&l(e.onBeforeUpdate),this.settings.exceptions){const e=o(p,t,r);e.length&&e.forEach((e=>{throw new s({message:e.message,property:t,code:e.code})}))}return e[t]=r,l(e.onSet),u&&l(e.onUpdate),!0}get(e,r){const n=this.schema,o=n[r];let s=e[r];if(!o)return e[r];function c(t,r){return Reflect.apply(t,e,r)}return c(e.onBeforeGet,[r,s,n]),t(o)&&(s=c(o,[e])),t(o.format)&&(s=c(o.format,[s,e])),c(e.onGet,[r,s,n]),s}deleteProperty(e,t){const r=n(e[t]),o=this.schema;function c(t,r){return Reflect.apply(t,e,r)}if(o[t].required)throw new s({message:`Invalid delete on required propery ${t}`,property:t,code:"delete"});return Reflect.deleteProperty(e,t),c(e.onDelete,[t,r,o]),c(e.onUpdate,[t,undefined,o]),!0}}class i{constructor(e={},t={}){Object.keys(e).forEach((t=>{e[t].default&&(this[t]=e[t].default)})),this.feed(t)}feed(e){Object.keys(e).forEach((t=>{this[t]=e[t]}))}onSet(){}onGet(){}onDelete(){}onUpdate(){}onBeforeSet(){}onBeforeGet(){}onBeforeUpdate(){}}i.settings={strict:!1,exceptions:!0},i.create=function(e,t,r,n={}){n=Object.assign({},i.settings,n);const o={[e]:class extends i{constructor(e){return super(t,e),new Proxy(this,new c(t,n))}}}[e];return o.checkErrors=function(e,r){return i.checkErrors(t,e,r)},Object.assign(o.prototype,r),o},i.checkErrors=function(e,t,r){const n={};return Object.keys(e).forEach((s=>{const c=t[s];let i=o(e[s],s,c);i.length&&(r&&(i=i.filter((e=>!function(e){return[].concat([],e)}(r).includes(e.code)))),i.length&&(n[s]=i))})),!!Object.keys(n).length&&n},i.hydrate=function(e,t){return r(t)?t.map((t=>new e(t))):new e(t)};export default i;
/*! smartModel v0.2.1 */
function e(e){return""===e||null==e}function t(e){return"function"==typeof e}function r(e){return Array.isArray(e)}function n(e){return e?JSON.parse(JSON.stringify(e)):e}function o(t,n,o){const s=[];if(t.required&&e(o))return s.push({message:`Invalid value 'required' on property '${n}'`,code:"required"}),s;if(void 0===o)return s;if(t.type){let c;const i=r(t.type)?t.type:[t.type];c=!(t.required||!e(o))||i.some((e=>typeof o==typeof e()||o instanceof e)),c||s.push({message:`Invalid type '${typeof o}' on property '${n}'`,code:"type"})}return t.rule&&Object.keys(t.rule).forEach((e=>{(0,t.rule[e])(o)&&s.push({message:`Invalid value '${e}' on property '${n}'`,code:e})})),s}class s extends Error{constructor(e){super(e.message),Object.assign(this,e)}}class c{constructor(e={},t={}){this.schema=e,this.settings=t}set(e,t,r){const c=this.schema,i=n(e[t]),u=(f=r,a=i,!(JSON.stringify(f)===JSON.stringify(a)));var f,a;const p=c[t];function l(n){return Reflect.apply(n,e,[t,r,i,c])}if(!p)return this.settings.strict||(e[t]=r),!0;if(l(e.onBeforeSet),u&&l(e.onBeforeUpdate),p.transform&&(r=p.transform(r)),this.settings.exceptions){const e=o(p,t,r);e.length&&e.forEach((e=>{throw new s({message:e.message,property:t,code:e.code})}))}return e[t]=r,l(e.onSet),u&&l(e.onUpdate),!0}get(e,r){const n=this.schema,o=n[r];let s=e[r];if(!o)return e[r];function c(t,r){return Reflect.apply(t,e,r)}return c(e.onBeforeGet,[r,s,n]),t(o)&&(s=c(o,[e])),t(o.format)&&(s=c(o.format,[s,e])),c(e.onGet,[r,s,n]),s}deleteProperty(e,t){const r=n(e[t]),o=this.schema;function c(t,r){return Reflect.apply(t,e,r)}if(o[t].required)throw new s({message:`Invalid delete on required propery ${t}`,property:t,code:"delete"});return Reflect.deleteProperty(e,t),c(e.onDelete,[t,r,o]),c(e.onUpdate,[t,undefined,o]),!0}}class i{constructor(e={},t={}){Object.keys(e).forEach((t=>{e[t].default&&(this[t]=e[t].default)})),this.feed(t)}feed(e){Object.keys(e).forEach((t=>{this[t]=e[t]}))}onSet(){}onGet(){}onDelete(){}onUpdate(){}onBeforeSet(){}onBeforeGet(){}onBeforeUpdate(){}}i.settings={strict:!1,exceptions:!0},i.create=function(e,t,n,s={}){s=Object.assign({},i.settings,s);const u={[e]:class extends i{constructor(e){return super(t,e),new Proxy(this,new c(t,s))}}}[e];return u.checkErrors=function(e,r){const n={};return Object.keys(t).forEach((s=>{const c=e[s];let i=o(t[s],s,c);i.length&&(r&&(i=i.filter((e=>!function(e){return[].concat([],e)}(r).includes(e.code)))),i.length&&(n[s]=i))})),!!Object.keys(n).length&&n},u.hydrate=function(e){return r(e)?e.map((e=>new u(e))):new u(e)},Object.assign(u.prototype,n),u};export default i;

@@ -6,3 +6,3 @@

* https://github.com/jaysalvat/smart-model
* @version 0.2.0 built 2021-02-19 10:32:47
* @version 0.2.1 built 2021-02-19 12:37:35
* @license ISC

@@ -110,3 +110,3 @@ * @author Jay Salvat http://jaysalvat.com

const updated = !isEqual(value, oldValue);
let entry = schema[property];
const entry = schema[property];

@@ -117,12 +117,10 @@ function trigger(method) {

if (this.settings.strict && !entry) {
if (!entry) {
if (!this.settings.strict) {
target[property] = value;
}
return true
} else {
entry = {};
}
if (entry.transform) {
value = entry.transform(value);
}
trigger(target.onBeforeSet);

@@ -134,2 +132,6 @@

if (entry.transform) {
value = entry.transform(value);
}
if (this.settings.exceptions) {

@@ -257,41 +259,35 @@ const errors = checkErrors(entry, property, value);

SuperModel.checkErrors = function (payload, required) {
return Model.checkErrors(schema, payload, required)
};
SuperModel.checkErrors = function (payload, filters) {
const invalidations = {};
Object.assign(SuperModel.prototype, prototype);
Object.keys(schema).forEach((property) => {
const value = payload[property];
const entry = schema[property];
let errors = checkErrors(entry, property, value);
return SuperModel
};
if (errors.length) {
if (filters) {
errors = errors.filter((error) => !toArray(filters).includes(error.code));
}
Model.checkErrors = function (schema, payload, filters) {
const invalidations = {};
if (errors.length) {
invalidations[property] = errors;
}
}
});
Object.keys(schema).forEach((property) => {
const value = payload[property];
const entry = schema[property];
let errors = checkErrors(entry, property, value);
return Object.keys(invalidations).length ? invalidations : false
};
if (errors.length) {
if (filters) {
errors = errors.filter((error) => !toArray(filters).includes(error.code));
}
if (errors.length) {
invalidations[property] = errors;
}
SuperModel.hydrate = function (payload) {
if (isArray(payload)) {
return payload.map((item) => new SuperModel(item))
}
return
});
return new SuperModel(payload)
};
return Object.keys(invalidations).length ? invalidations : false
};
Object.assign(SuperModel.prototype, prototype);
Model.hydrate = function (ModelToHydrate, payload) {
if (isArray(payload)) {
return payload.map((item) => new ModelToHydrate(item))
}
return new ModelToHydrate(payload)
return SuperModel
};

@@ -298,0 +294,0 @@

@@ -1,2 +0,2 @@

/*! smartModel v0.2.0 */
!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""===e||null==e}function r(e){return"function"==typeof e}function n(e){return Array.isArray(e)}function o(e){return e?JSON.parse(JSON.stringify(e)):e}function s(e,r,o){const s=[];if(e.required&&t(o))return s.push({message:`Invalid value 'required' on property '${r}'`,code:"required"}),s;if(void 0===o)return s;if(e.type){let c;const i=n(e.type)?e.type:[e.type];c=!(e.required||!t(o))||i.some((e=>typeof o==typeof e()||o instanceof e)),c||s.push({message:`Invalid type '${typeof o}' on property '${r}'`,code:"type"})}return e.rule&&Object.keys(e.rule).forEach((t=>{(0,e.rule[t])(o)&&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 i{constructor(e={},t={}){this.schema=e,this.settings=t}set(e,t,r){const n=this.schema,i=o(e[t]),u=(f=r,a=i,!(JSON.stringify(f)===JSON.stringify(a)));var f,a;let p=n[t];function d(o){return Reflect.apply(o,e,[t,r,i,n])}if(this.settings.strict&&!p)return!0;if(p={},p.transform&&(r=p.transform(r)),d(e.onBeforeSet),u&&d(e.onBeforeUpdate),this.settings.exceptions){const e=s(p,t,r);e.length&&e.forEach((e=>{throw new c({message:e.message,property:t,code:e.code})}))}return e[t]=r,d(e.onSet),u&&d(e.onUpdate),!0}get(e,t){const n=this.schema,o=n[t];let s=e[t];if(!o)return e[t];function c(t,r){return Reflect.apply(t,e,r)}return c(e.onBeforeGet,[t,s,n]),r(o)&&(s=c(o,[e])),r(o.format)&&(s=c(o.format,[s,e])),c(e.onGet,[t,s,n]),s}deleteProperty(e,t){const r=o(e[t]),n=this.schema;function s(t,r){return Reflect.apply(t,e,r)}if(n[t].required)throw new c({message:`Invalid delete on required propery ${t}`,property:t,code:"delete"});return Reflect.deleteProperty(e,t),s(e.onDelete,[t,r,n]),s(e.onUpdate,[t,undefined,n]),!0}}class u{constructor(e={},t={}){Object.keys(e).forEach((t=>{e[t].default&&(this[t]=e[t].default)})),this.feed(t)}feed(e){Object.keys(e).forEach((t=>{this[t]=e[t]}))}onSet(){}onGet(){}onDelete(){}onUpdate(){}onBeforeSet(){}onBeforeGet(){}onBeforeUpdate(){}}u.settings={strict:!1,exceptions:!0},u.create=function(e,t,r,n={}){n=Object.assign({},u.settings,n);const o={[e]:class extends u{constructor(e){return super(t,e),new Proxy(this,new i(t,n))}}}[e];return o.checkErrors=function(e,r){return u.checkErrors(t,e,r)},Object.assign(o.prototype,r),o},u.checkErrors=function(e,t,r){const n={};return Object.keys(e).forEach((o=>{const c=t[o];let i=s(e[o],o,c);i.length&&(r&&(i=i.filter((e=>!function(e){return[].concat([],e)}(r).includes(e.code)))),i.length&&(n[o]=i))})),!!Object.keys(n).length&&n},u.hydrate=function(e,t){return n(t)?t.map((t=>new e(t))):new e(t)},e.default=u,Object.defineProperty(e,"__esModule",{value:!0})}));
/*! smartModel v0.2.1 */
!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""===e||null==e}function n(e){return"function"==typeof e}function r(e){return Array.isArray(e)}function o(e){return e?JSON.parse(JSON.stringify(e)):e}function s(e,n,o){const s=[];if(e.required&&t(o))return s.push({message:`Invalid value 'required' on property '${n}'`,code:"required"}),s;if(void 0===o)return s;if(e.type){let c;const i=r(e.type)?e.type:[e.type];c=!(e.required||!t(o))||i.some((e=>typeof o==typeof e()||o instanceof e)),c||s.push({message:`Invalid type '${typeof o}' on property '${n}'`,code:"type"})}return e.rule&&Object.keys(e.rule).forEach((t=>{(0,e.rule[t])(o)&&s.push({message:`Invalid value '${t}' on property '${n}'`,code:t})})),s}class c extends Error{constructor(e){super(e.message),Object.assign(this,e)}}class i{constructor(e={},t={}){this.schema=e,this.settings=t}set(e,t,n){const r=this.schema,i=o(e[t]),f=(u=n,a=i,!(JSON.stringify(u)===JSON.stringify(a)));var u,a;const p=r[t];function d(o){return Reflect.apply(o,e,[t,n,i,r])}if(!p)return this.settings.strict||(e[t]=n),!0;if(d(e.onBeforeSet),f&&d(e.onBeforeUpdate),p.transform&&(n=p.transform(n)),this.settings.exceptions){const e=s(p,t,n);e.length&&e.forEach((e=>{throw new c({message:e.message,property:t,code:e.code})}))}return e[t]=n,d(e.onSet),f&&d(e.onUpdate),!0}get(e,t){const r=this.schema,o=r[t];let s=e[t];if(!o)return e[t];function c(t,n){return Reflect.apply(t,e,n)}return c(e.onBeforeGet,[t,s,r]),n(o)&&(s=c(o,[e])),n(o.format)&&(s=c(o.format,[s,e])),c(e.onGet,[t,s,r]),s}deleteProperty(e,t){const n=o(e[t]),r=this.schema;function s(t,n){return Reflect.apply(t,e,n)}if(r[t].required)throw new c({message:`Invalid delete on required propery ${t}`,property:t,code:"delete"});return Reflect.deleteProperty(e,t),s(e.onDelete,[t,n,r]),s(e.onUpdate,[t,undefined,r]),!0}}class f{constructor(e={},t={}){Object.keys(e).forEach((t=>{e[t].default&&(this[t]=e[t].default)})),this.feed(t)}feed(e){Object.keys(e).forEach((t=>{this[t]=e[t]}))}onSet(){}onGet(){}onDelete(){}onUpdate(){}onBeforeSet(){}onBeforeGet(){}onBeforeUpdate(){}}f.settings={strict:!1,exceptions:!0},f.create=function(e,t,n,o={}){o=Object.assign({},f.settings,o);const c={[e]:class extends f{constructor(e){return super(t,e),new Proxy(this,new i(t,o))}}}[e];return c.checkErrors=function(e,n){const r={};return Object.keys(t).forEach((o=>{const c=e[o];let i=s(t[o],o,c);i.length&&(n&&(i=i.filter((e=>!function(e){return[].concat([],e)}(n).includes(e.code)))),i.length&&(r[o]=i))})),!!Object.keys(r).length&&r},c.hydrate=function(e){return r(e)?e.map((e=>new c(e))):new c(e)},Object.assign(c.prototype,n),c},e.default=f,Object.defineProperty(e,"__esModule",{value:!0})}));
{
"name": "@jaysalvat/smart-model",
"version": "0.2.0",
"version": "0.2.1",
"description": "Javascript object model",

@@ -5,0 +5,0 @@ "main": "./build/smart-model.umd.js",

@@ -47,43 +47,37 @@

SuperModel.checkErrors = function (payload, required) {
return Model.checkErrors(schema, payload, required)
}
SuperModel.checkErrors = function (payload, filters) {
const invalidations = {}
Object.assign(SuperModel.prototype, prototype)
Object.keys(schema).forEach((property) => {
const value = payload[property]
const entry = schema[property]
let errors = checkErrors(entry, property, value)
return SuperModel
}
if (errors.length) {
if (filters) {
errors = errors.filter((error) => !toArray(filters).includes(error.code))
}
Model.checkErrors = function (schema, payload, filters) {
const invalidations = {}
if (errors.length) {
invalidations[property] = errors
}
}
})
Object.keys(schema).forEach((property) => {
const value = payload[property]
const entry = schema[property]
let errors = checkErrors(entry, property, value)
return Object.keys(invalidations).length ? invalidations : false
}
if (errors.length) {
if (filters) {
errors = errors.filter((error) => !toArray(filters).includes(error.code))
}
if (errors.length) {
invalidations[property] = errors
}
SuperModel.hydrate = function (payload) {
if (isArray(payload)) {
return payload.map((item) => new SuperModel(item))
}
return
})
return new SuperModel(payload)
}
return Object.keys(invalidations).length ? invalidations : false
}
Object.assign(SuperModel.prototype, prototype)
Model.hydrate = function (ModelToHydrate, payload) {
if (isArray(payload)) {
return payload.map((item) => new ModelToHydrate(item))
}
return new ModelToHydrate(payload)
return SuperModel
}
export default Model

@@ -14,3 +14,3 @@ import { clone, isFn, isEqual, checkErrors } from './utils.js'

const updated = !isEqual(value, oldValue)
let entry = schema[property]
const entry = schema[property]

@@ -21,12 +21,10 @@ function trigger(method) {

if (this.settings.strict && !entry) {
if (!entry) {
if (!this.settings.strict) {
target[property] = value
}
return true
} else {
entry = {}
}
if (entry.transform) {
value = entry.transform(value)
}
trigger(target.onBeforeSet)

@@ -38,2 +36,6 @@

if (entry.transform) {
value = entry.transform(value)
}
if (this.settings.exceptions) {

@@ -40,0 +42,0 @@ const errors = checkErrors(entry, property, value)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc