From 3d5cf6b1228ebe62369a0410ce84320edcd5137e Mon Sep 17 00:00:00 2001 From: sylvainpolletvillard Date: Tue, 17 Jan 2017 21:32:33 +0100 Subject: [PATCH] release 2.6.0 --- dist/object-model.js | 52 +++++++++++++++++++++++----------------- dist/object-model.min.js | 4 ++-- dist/object-model.umd.js | 52 +++++++++++++++++++++++----------------- index.html | 14 +++++------ package.json | 3 ++- 5 files changed, 71 insertions(+), 54 deletions(-) diff --git a/dist/object-model.js b/dist/object-model.js index 6224df8..ddf5199 100644 --- a/dist/object-model.js +++ b/dist/object-model.js @@ -1,4 +1,4 @@ -// ObjectModel v2.5.3 - http://objectmodel.js.org +// ObjectModel v2.6.0 - http://objectmodel.js.org ;(function(global){ // string constants var @@ -9,7 +9,7 @@ CONVENTION_CONSTANT = "conventionForConstant", CONVENTION_PRIVATE = "conventionForPrivate", DEFINITION = "definition", ASSERTIONS = "assertions", -ON_FAIL = "_onFail", +DESCRIPTION = "_description", VALIDATE = "validate", VALIDATOR = "_validator", TEST = "test", @@ -152,7 +152,7 @@ function Model(def){ return obj; }; - initModel(model, def, Model); + initModel(model, arguments, Model); return model; } @@ -228,10 +228,7 @@ ModelProto[EXTEND] = function(){ ModelProto[ASSERT] = function(assertion, description){ description = description || toString(assertion); - var onFail = isFunction(description) ? description : function (assertionResult, value) { - return 'assertion "' + description + '" returned ' + toString(assertionResult) + ' for value ' + toString(value); - }; - define(assertion, ON_FAIL, onFail); + define(assertion, DESCRIPTION, description); this[ASSERTIONS] = this[ASSERTIONS].concat(assertion); return this; }; @@ -255,7 +252,7 @@ Model[CONVENTION_PRIVATE] = function(key){ return key[0] === "_" }; // private methods define(ModelProto, VALIDATOR, function(obj, path, callStack, errorStack){ checkDefinition(obj, this[DEFINITION], path, callStack, errorStack); - checkAssertions(obj, this, errorStack); + checkAssertions(obj, this, path, errorStack); }); // throw all errors collected @@ -280,9 +277,10 @@ define(ModelProto, UNSTACK, function(errorCollector){ errorCollector.call(this, errors); }) -function initModel(model, def, constructor){ +function initModel(model, args, constructor){ + if(args.length === 0) throw new Error("Model definition is required"); setConstructor(model, constructor); - model[DEFINITION] = def; + model[DEFINITION] = args[0]; model[ASSERTIONS] = model[ASSERTIONS].slice(); // clone from Model.prototype define(model, ERROR_STACK, []); } @@ -354,10 +352,16 @@ function checkDefinitionPart(obj, def, path, callStack){ || obj[CONSTRUCTOR] === def; } -function checkAssertions(obj, model, errorStack){ +function checkAssertions(obj, model, path, errorStack){ for(var i=0, l=model[ASSERTIONS].length; i= 0){ - value = checkDefinition(value, model[DEFINITION], ARRAY+'['+key+']', [], model[ERROR_STACK], true); + value = checkDefinition(value, model[DEFINITION], path, [], model[ERROR_STACK], true); } var testArray = array.slice(); testArray[key] = value; - checkAssertions(testArray, model, model[ERROR_STACK]); + checkAssertions(testArray, model, path, model[ERROR_STACK]); model[UNSTACK](); array[key] = value; } @@ -619,7 +627,7 @@ Model[FUNCTION] = function FunctionModel(){ def[ARGS].forEach(function (argDef, i) { args[i] = checkDefinition(args[i], argDef, ARGS + '[' + i + ']', [], model[ERROR_STACK], true); }); - checkAssertions(args, model, model[ERROR_STACK]); + checkAssertions(args, model, ARGS, model[ERROR_STACK]); //TODO: improve path parameter if(!model[ERROR_STACK].length){ returnValue = fn.apply(this, args); @@ -638,7 +646,7 @@ Model[FUNCTION] = function FunctionModel(){ var def = {}; def[ARGS] = cloneArray(arguments); - initModel(model, def, Model[FUNCTION]); + initModel(model, [def], Model[FUNCTION]); return model; }; diff --git a/dist/object-model.min.js b/dist/object-model.min.js index 1b02b35..f0db38c 100644 --- a/dist/object-model.min.js +++ b/dist/object-model.min.js @@ -1,2 +1,2 @@ -// ObjectModel v2.5.3 - http://objectmodel.js.org -!function(a){function b(a,b){return b instanceof a}function c(a){return"function"==typeof a}function d(a){return"object"==typeof a}function e(a){return a&&d(a)&&Object.getPrototypeOf(a)===Object.prototype}function f(a){return{}.toString.call(a).match(/\s([a-zA-Z]+)/)[1]}function g(a){return Array.prototype.slice.call(a)}function h(a,b){return void 0===b?a:b}function i(a,b,c,d){for(var f in b||{})if(d||b.hasOwnProperty(f))if(c&&e(b[f])){var g={};i(g,a[f],c),i(g,b[f],c),a[f]=g}else a[f]=b[f]}function j(a,b,c,d){aa(a,b,{value:c,enumerable:d,writable:!0,configurable:!0})}function k(a,b){Object.setPrototypeOf(a,b[S]),j(a,T,b)}function l(a,b){a[S]=Object.create(b),a[S][T]=a}function m(a,e){if(e=e||[],e.length>15||e.indexOf(a)>=0)return"...";if(null==a)return String(a);if("string"==typeof a)return'"'+a+'"';if(b(n,a))return a.toString(e);if(e=[a].concat(e),c(a))return a.name||a.toString(e);if(b(Array,a))return"["+a.map(function(a){return m(a,e)}).join(", ")+"]";if(a.toString!==Object.prototype.toString)return a.toString();if(a&&d(a)){var f=new Array(e.length).join("\t"),g=Object.keys(a);return"{"+g.map(function(b){return"\n"+f+"\t"+b+": "+m(a[b],e)}).join(",")+(g.length?"\n"+f:"")+"}"}return String(a)}function n(a){if(e(a))return n[y](a);var b=function(a){return a=h(b[U],a),b[G](a),a};return o(b,a,n),b}function o(a,b,c){k(a,c),a[D]=b,a[E]=a[E].slice(),j(a,P,[])}function p(a){if(e(a))Object.keys(a).forEach(function(b){a[b]=p(a[b])});else{if(!b(Array,a))return[a];if(1===a.length)return a.concat(void 0,null)}return a}function q(a,c,d,f,g,h){var i=f.indexOf(c);if(i!==-1&&f.slice(i+1).indexOf(c)!==-1)return a;if(h&&(a=t(a,c)),b(n,c))c[H](a,d,f.concat(c),g);else if(e(c))Object.keys(c).forEach(function(b){var e=null!=a?a[b]:void 0;q(e,c[b],d?d+"."+b:b,f,g)});else{for(var j=p(c),k=0,l=j.length;k1&&console.warn("Ambiguous model for value "+m(a)+", could be "+e.join(" or ")),a)}function u(a,c,d,f){if(!e(d))return t(c,d);var g=b(Object,c)?c:{},h=Object.create(Object.getPrototypeOf(g));for(var i in g)!g.hasOwnProperty(i)||i in d||(h[i]=g[i]);return Object.keys(d).forEach(function(b){var e=f?f+"."+b:b,i=n[B](b);aa(h,b,{get:function(){return u(a,g[b],d[b],e)},set:function(f){if(i&&void 0!==g[b]){var h={};h[O]="cannot redefine constant "+b,a[P].push(h)}var j=u(a,f,d[b],e);q(j,d[b],e,[],a[P]);var k=g[b];g[b]=j,s(c,a,a[P]),a[P].length&&(g[b]=k,a[R]())},enumerable:!n[C](b)})}),h}function v(a,b,c,d){aa(a,c,{enumerable:!0,get:function(){return b[c]},set:function(a){x(b,c,a,d)}})}function w(a,b,c,d){return function(){var e=a.slice();if(Array[S][b].apply(e,arguments),c[G](e),!_)for(var f in e)!e.hasOwnProperty(f)||f in d||v(d,a,f,c);for(var g=Array[S][b].apply(a,arguments),h=0,i=a.length;h=0&&(c=q(c,d[D],z+"["+b+"]",[],d[P],!0));var e=a.slice();e[b]=c,s(e,d,d[P]),d[R](),a[b]=c}var y="Object",z="Array",A="Function",B="conventionForConstant",C="conventionForPrivate",D="definition",E="assertions",F="_onFail",G="validate",H="_validator",I="test",J="extend",K="assert",L="expected",M="received",N="path",O="message",P="errorStack",Q="errorCollector",R="unstack",S="prototype",T="constructor",U="default",V="defaultTo",W="defaults",X="return",Y="arguments",Z=["pop","push","reverse","shift","sort","splice","unshift"],$=/\n.*object-model(.|\n)*object-model.*/,_="function"==typeof Proxy,aa=Object.defineProperty;"name"in Function.prototype&&"name"in function(){}||aa(Function.prototype,"name",{get:function(){var a=Function.prototype.toString.call(this).match(/\s*function\s+([^\(\s]*)\s*/);return a&&a[1]}}),!Object.setPrototypeOf&&b(Array,{__proto__:[]})&&(Object.setPrototypeOf=function(a,b){return a.__proto__=b,a}),l(n,Function[S]);var ba=n[S];ba.toString=function(a){return p(this[D]).map(function(b){return m(b,a)}).join(" or ")},ba[E]=[],ba[G]=function(a,b){this[H](a,null,[],this[P]),this[R](b)},ba[I]=function(a){var b,c=this[Q];return this[Q]=function(){b=!0},this(a),this[Q]=c,!b},ba[J]=function(){var a,e,f=g(this[E]),h=g(arguments);b(n[y],this)?(a={},e={},i(a,this[D]),i(e,this[S],!1,!0),h.forEach(function(f){b(n,f)&&i(a,f[D],!0),c(f)&&i(e,f[S],!0,!0),d(f)&&i(a,f,!0,!0)})):a=h.reduce(function(a,b){return a.concat(p(b))},p(this[D])).filter(function(a,b,c){return c.indexOf(a)===b}),h.forEach(function(a){b(n,a)&&(f=f.concat(a[E]))});var j=new this[T](a);return l(j,this[S]),i(j[S],e),j[E]=f,j[Q]=this[Q],j},ba[K]=function(a,b){b=b||m(a);var d=c(b)?b:function(a,c){return'assertion "'+b+'" returned '+m(a)+" for value "+m(c)};return j(a,F,d),this[E]=this[E].concat(a),this},ba[V]=function(a){return this[U]=a,this},ba[Q]=function(a){var b=new TypeError(a.map(function(a){return a[O]}).join("\n"));throw b.stack&&(b.stack=b.stack.replace($,"")),b},n[B]=function(a){return a.toUpperCase()===a},n[C]=function(a){return"_"===a[0]},j(ba,H,function(a,b,c,d){q(a,this[D],b,c,d),s(a,this,d)}),j(ba,R,function(a){if(this[P].length){a||(a=this[Q]);var c=this[P].map(function(a){if(!a[O]){var c=b(Array,a[L])?a[L]:[a[L]];a[O]="expecting "+(a[N]?a[N]+" to be ":"")+c.map(function(a){return m(a)}).join(" or ")+", got "+(null!=a[M]?f(a[M])+" ":"")+m(a[M])}return a});this[P]=[],a.call(this,c)}}),n[y]=function(a){var c=function(a){if(b(c,a))return a;if(!b(c,this))return new c(a);a=h(c[U],a),i(this,a,!0);var d=u(c,this,c[D]);return c[G](d),d};return l(c,Object[S]),o(c,a,n[y]),c},l(n[y],ba);var ca=n[y][S];ca[W]=function(a){return i(this[S],a),this},ca.toString=function(a){return m(this[D],a)},j(ca,H,function(a,b,c,e){if(d(a))q(a,this[D],b,c,e);else{var f={};f[L]=this,f[M]=a,f[N]=b,e.push(f)}s(a,this,e)}),n[z]=function(a){var b=function(a){a=h(b[U],a);var c;if(b[G](a),_)c=new Proxy(a,{get:function(a,c){return c===T?b:Z.indexOf(c)>=0?w(a,c,b):a[c]},set:function(a,c,d){x(a,c,d,b)},getPrototypeOf:function(){return b[S]}});else{c=Object.create(Array[S]);for(var d in a)a.hasOwnProperty(d)&&v(c,a,d,b);aa(c,"length",{get:function(){return a.length}}),aa(c,"toJSON",{value:function(){return a}}),Z.forEach(function(d){j(c,d,w(a,d,b,c))}),k(c,b)}return c};return l(b,Array[S]),o(b,a,n[z]),b},l(n[z],n[S]);var da=n[z][S];da.toString=function(a){return z+" of "+m(this[D],a)},j(da,H,function(a,c,d,e){if(b(Array,a))for(var f=0,g=a.length;fc[Y].length){var f={};f[L]=m(b)+" to be called with "+c[Y].length+" "+Y,f[M]=e.length,a[P].push(f)}return c[Y].forEach(function(b,c){e[c]=q(e[c],b,Y+"["+c+"]",[],a[P],!0)}),s(e,a,a[P]),a[P].length||(d=b.apply(this,e),X in c&&(d=q(d,c[X],X+" value",[],a[P],!0))),a[R](),d};return k(d,a),d};l(a,Function[S]);var b={};return b[Y]=g(arguments),o(a,b,n[A]),a},l(n[A],n[S]);var ea=n[A][S];ea.toString=function(a){var b=A+"("+this[D][Y].map(function(b){return m(b,a)}).join(",")+")";return X in this[D]&&(b+=" => "+m(this[D][X])),b},ea[X]=function(a){return this[D][X]=a,this},ea[W]=function(){return this[D][W]=g(arguments),this},j(ea,H,function(a,b,d,e){if(!c(a)){var f={};f[L]=A,f[M]=a,f[N]=b,e.push(f)}}),a.Model=n}(this); \ No newline at end of file +// ObjectModel v2.6.0 - http://objectmodel.js.org +!function(a){function b(a,b){return b instanceof a}function c(a){return"function"==typeof a}function d(a){return"object"==typeof a}function e(a){return a&&d(a)&&Object.getPrototypeOf(a)===Object.prototype}function f(a){return{}.toString.call(a).match(/\s([a-zA-Z]+)/)[1]}function g(a){return Array.prototype.slice.call(a)}function h(a,b){return void 0===b?a:b}function i(a,b,c,d){for(var f in b||{})if(d||b.hasOwnProperty(f))if(c&&e(b[f])){var g={};i(g,a[f],c),i(g,b[f],c),a[f]=g}else a[f]=b[f]}function j(a,b,c,d){aa(a,b,{value:c,enumerable:d,writable:!0,configurable:!0})}function k(a,b){Object.setPrototypeOf(a,b[S]),j(a,T,b)}function l(a,b){a[S]=Object.create(b),a[S][T]=a}function m(a,e){if(e=e||[],e.length>15||e.indexOf(a)>=0)return"...";if(null==a)return String(a);if("string"==typeof a)return'"'+a+'"';if(b(n,a))return a.toString(e);if(e=[a].concat(e),c(a))return a.name||a.toString(e);if(b(Array,a))return"["+a.map(function(a){return m(a,e)}).join(", ")+"]";if(a.toString!==Object.prototype.toString)return a.toString();if(a&&d(a)){var f=new Array(e.length).join("\t"),g=Object.keys(a);return"{"+g.map(function(b){return"\n"+f+"\t"+b+": "+m(a[b],e)}).join(",")+(g.length?"\n"+f:"")+"}"}return String(a)}function n(a){if(e(a))return n[y](a);var b=function(a){return a=h(b[U],a),b[G](a),a};return o(b,arguments,n),b}function o(a,b,c){if(0===b.length)throw new Error("Model definition is required");k(a,c),a[D]=b[0],a[E]=a[E].slice(),j(a,P,[])}function p(a){if(e(a))Object.keys(a).forEach(function(b){a[b]=p(a[b])});else{if(!b(Array,a))return[a];if(1===a.length)return a.concat(void 0,null)}return a}function q(a,c,d,f,g,h){var i=f.indexOf(c);if(i!==-1&&f.slice(i+1).indexOf(c)!==-1)return a;if(h&&(a=t(a,c)),b(n,c))c[H](a,d,f.concat(c),g);else if(e(c))Object.keys(c).forEach(function(b){var e=null!=a?a[b]:void 0;q(e,c[b],d?d+"."+b:b,f,g)});else{for(var j=p(c),k=0,l=j.length;k1&&console.warn("Ambiguous model for value "+m(a)+", could be "+e.join(" or ")),a)}function u(a,c,d,f){if(!e(d))return t(c,d);var g=b(Object,c)?c:{},h=Object.create(Object.getPrototypeOf(g));for(var i in g)!g.hasOwnProperty(i)||i in d||(h[i]=g[i]);return Object.keys(d).forEach(function(b){var e=f?f+"."+b:b,i=n[B](b);aa(h,b,{get:function(){return u(a,g[b],d[b],e)},set:function(f){if(i&&void 0!==g[b]){var h={};h[O]="cannot redefine constant "+b,a[P].push(h)}var j=u(a,f,d[b],e);q(j,d[b],e,[],a[P]);var k=g[b];g[b]=j,s(c,a,e,a[P]),a[P].length&&(g[b]=k,a[R]())},enumerable:!n[C](b)})}),h}function v(a,b,c,d){aa(a,c,{enumerable:!0,get:function(){return b[c]},set:function(a){x(b,c,a,d)}})}function w(a,b,c,d){return function(){var e=a.slice();if(Array[S][b].apply(e,arguments),c[G](e),!_)for(var f in e)!e.hasOwnProperty(f)||f in d||v(d,a,f,c);for(var g=Array[S][b].apply(a,arguments),h=0,i=a.length;h=0&&(c=q(c,d[D],e,[],d[P],!0));var f=a.slice();f[b]=c,s(f,d,e,d[P]),d[R](),a[b]=c}var y="Object",z="Array",A="Function",B="conventionForConstant",C="conventionForPrivate",D="definition",E="assertions",F="_description",G="validate",H="_validator",I="test",J="extend",K="assert",L="expected",M="received",N="path",O="message",P="errorStack",Q="errorCollector",R="unstack",S="prototype",T="constructor",U="default",V="defaultTo",W="defaults",X="return",Y="arguments",Z=["pop","push","reverse","shift","sort","splice","unshift"],$=/\n.*object-model(.|\n)*object-model.*/,_="function"==typeof Proxy,aa=Object.defineProperty;"name"in Function.prototype&&"name"in function(){}||aa(Function.prototype,"name",{get:function(){var a=Function.prototype.toString.call(this).match(/\s*function\s+([^\(\s]*)\s*/);return a&&a[1]}}),!Object.setPrototypeOf&&b(Array,{__proto__:[]})&&(Object.setPrototypeOf=function(a,b){return a.__proto__=b,a}),l(n,Function[S]);var ba=n[S];ba.toString=function(a){return p(this[D]).map(function(b){return m(b,a)}).join(" or ")},ba[E]=[],ba[G]=function(a,b){this[H](a,null,[],this[P]),this[R](b)},ba[I]=function(a){var b,c=this[Q];return this[Q]=function(){b=!0},this(a),this[Q]=c,!b},ba[J]=function(){var a,e,f=g(this[E]),h=g(arguments);b(n[y],this)?(a={},e={},i(a,this[D]),i(e,this[S],!1,!0),h.forEach(function(f){b(n,f)&&i(a,f[D],!0),c(f)&&i(e,f[S],!0,!0),d(f)&&i(a,f,!0,!0)})):a=h.reduce(function(a,b){return a.concat(p(b))},p(this[D])).filter(function(a,b,c){return c.indexOf(a)===b}),h.forEach(function(a){b(n,a)&&(f=f.concat(a[E]))});var j=new this[T](a);return l(j,this[S]),i(j[S],e),j[E]=f,j[Q]=this[Q],j},ba[K]=function(a,b){return b=b||m(a),j(a,F,b),this[E]=this[E].concat(a),this},ba[V]=function(a){return this[U]=a,this},ba[Q]=function(a){var b=new TypeError(a.map(function(a){return a[O]}).join("\n"));throw b.stack&&(b.stack=b.stack.replace($,"")),b},n[B]=function(a){return a.toUpperCase()===a},n[C]=function(a){return"_"===a[0]},j(ba,H,function(a,b,c,d){q(a,this[D],b,c,d),s(a,this,b,d)}),j(ba,R,function(a){if(this[P].length){a||(a=this[Q]);var c=this[P].map(function(a){if(!a[O]){var c=b(Array,a[L])?a[L]:[a[L]];a[O]="expecting "+(a[N]?a[N]+" to be ":"")+c.map(function(a){return m(a)}).join(" or ")+", got "+(null!=a[M]?f(a[M])+" ":"")+m(a[M])}return a});this[P]=[],a.call(this,c)}}),n[y]=function(a){var c=function(a){if(b(c,a))return a;if(!b(c,this))return new c(a);a=h(c[U],a),i(this,a,!0);var d=u(c,this,c[D]);return c[G](d),d};return l(c,Object[S]),o(c,arguments,n[y]),c},l(n[y],ba);var ca=n[y][S];ca[W]=function(a){return i(this[S],a),this},ca.toString=function(a){return m(this[D],a)},j(ca,H,function(a,b,c,e){if(d(a))q(a,this[D],b,c,e);else{var f={};f[L]=this,f[M]=a,f[N]=b,e.push(f)}s(a,this,b,e)}),n[z]=function(a){var b=function(a){a=h(b[U],a);var c;if(b[G](a),_)c=new Proxy(a,{get:function(a,c){return c===T?b:Z.indexOf(c)>=0?w(a,c,b):a[c]},set:function(a,c,d){x(a,c,d,b)},getPrototypeOf:function(){return b[S]}});else{c=Object.create(Array[S]);for(var d in a)a.hasOwnProperty(d)&&v(c,a,d,b);aa(c,"length",{get:function(){return a.length}}),aa(c,"toJSON",{value:function(){return a}}),Z.forEach(function(d){j(c,d,w(a,d,b,c))}),k(c,b)}return c};return l(b,Array[S]),o(b,arguments,n[z]),b},l(n[z],n[S]);var da=n[z][S];da.toString=function(a){return z+" of "+m(this[D],a)},j(da,H,function(a,c,d,e){if(b(Array,a))for(var f=0,g=a.length;fc[Y].length){var f={};f[L]=m(b)+" to be called with "+c[Y].length+" "+Y,f[M]=e.length,a[P].push(f)}return c[Y].forEach(function(b,c){e[c]=q(e[c],b,Y+"["+c+"]",[],a[P],!0)}),s(e,a,Y,a[P]),a[P].length||(d=b.apply(this,e),X in c&&(d=q(d,c[X],X+" value",[],a[P],!0))),a[R](),d};return k(d,a),d};l(a,Function[S]);var b={};return b[Y]=g(arguments),o(a,[b],n[A]),a},l(n[A],n[S]);var ea=n[A][S];ea.toString=function(a){var b=A+"("+this[D][Y].map(function(b){return m(b,a)}).join(",")+")";return X in this[D]&&(b+=" => "+m(this[D][X])),b},ea[X]=function(a){return this[D][X]=a,this},ea[W]=function(){return this[D][W]=g(arguments),this},j(ea,H,function(a,b,d,e){if(!c(a)){var f={};f[L]=A,f[M]=a,f[N]=b,e.push(f)}}),a.Model=n}(this); \ No newline at end of file diff --git a/dist/object-model.umd.js b/dist/object-model.umd.js index 15c27d2..9bf2bf1 100644 --- a/dist/object-model.umd.js +++ b/dist/object-model.umd.js @@ -1,4 +1,4 @@ -// ObjectModel v2.5.3 - http://objectmodel.js.org +// ObjectModel v2.6.0 - http://objectmodel.js.org ;(function (globals, factory) { if (typeof define === 'function' && define.amd) define(factory); // AMD else if (typeof exports === 'object') module.exports = factory(); // Node @@ -13,7 +13,7 @@ CONVENTION_CONSTANT = "conventionForConstant", CONVENTION_PRIVATE = "conventionForPrivate", DEFINITION = "definition", ASSERTIONS = "assertions", -ON_FAIL = "_onFail", +DESCRIPTION = "_description", VALIDATE = "validate", VALIDATOR = "_validator", TEST = "test", @@ -156,7 +156,7 @@ function Model(def){ return obj; }; - initModel(model, def, Model); + initModel(model, arguments, Model); return model; } @@ -232,10 +232,7 @@ ModelProto[EXTEND] = function(){ ModelProto[ASSERT] = function(assertion, description){ description = description || toString(assertion); - var onFail = isFunction(description) ? description : function (assertionResult, value) { - return 'assertion "' + description + '" returned ' + toString(assertionResult) + ' for value ' + toString(value); - }; - define(assertion, ON_FAIL, onFail); + define(assertion, DESCRIPTION, description); this[ASSERTIONS] = this[ASSERTIONS].concat(assertion); return this; }; @@ -259,7 +256,7 @@ Model[CONVENTION_PRIVATE] = function(key){ return key[0] === "_" }; // private methods define(ModelProto, VALIDATOR, function(obj, path, callStack, errorStack){ checkDefinition(obj, this[DEFINITION], path, callStack, errorStack); - checkAssertions(obj, this, errorStack); + checkAssertions(obj, this, path, errorStack); }); // throw all errors collected @@ -284,9 +281,10 @@ define(ModelProto, UNSTACK, function(errorCollector){ errorCollector.call(this, errors); }) -function initModel(model, def, constructor){ +function initModel(model, args, constructor){ + if(args.length === 0) throw new Error("Model definition is required"); setConstructor(model, constructor); - model[DEFINITION] = def; + model[DEFINITION] = args[0]; model[ASSERTIONS] = model[ASSERTIONS].slice(); // clone from Model.prototype define(model, ERROR_STACK, []); } @@ -358,10 +356,16 @@ function checkDefinitionPart(obj, def, path, callStack){ || obj[CONSTRUCTOR] === def; } -function checkAssertions(obj, model, errorStack){ +function checkAssertions(obj, model, path, errorStack){ for(var i=0, l=model[ASSERTIONS].length; i= 0){ - value = checkDefinition(value, model[DEFINITION], ARRAY+'['+key+']', [], model[ERROR_STACK], true); + value = checkDefinition(value, model[DEFINITION], path, [], model[ERROR_STACK], true); } var testArray = array.slice(); testArray[key] = value; - checkAssertions(testArray, model, model[ERROR_STACK]); + checkAssertions(testArray, model, path, model[ERROR_STACK]); model[UNSTACK](); array[key] = value; } @@ -623,7 +631,7 @@ Model[FUNCTION] = function FunctionModel(){ def[ARGS].forEach(function (argDef, i) { args[i] = checkDefinition(args[i], argDef, ARGS + '[' + i + ']', [], model[ERROR_STACK], true); }); - checkAssertions(args, model, model[ERROR_STACK]); + checkAssertions(args, model, ARGS, model[ERROR_STACK]); //TODO: improve path parameter if(!model[ERROR_STACK].length){ returnValue = fn.apply(this, args); @@ -642,7 +650,7 @@ Model[FUNCTION] = function FunctionModel(){ var def = {}; def[ARGS] = cloneArray(arguments); - initModel(model, def, Model[FUNCTION]); + initModel(model, [def], Model[FUNCTION]); return model; }; diff --git a/index.html b/index.html index 1884718..11b71c3 100644 --- a/index.html +++ b/index.html @@ -119,17 +119,17 @@

What's inside the box ?

  • Constants and private properties based on name conventions
  • Explicit error messages
  • Custom error collectors
  • -
  • all in 7.9 kB minified, 3.3 kB gzipped
  • +
  • all in 8 kB minified, 3.4 kB gzipped
  • Download

    -

    Current version: v2.5.4

    +

    Current version: v2.6.0

    Changelog and previous releases

    Checkout the Github Releases

    @@ -752,9 +752,9 @@

    Custom error collectors

    Error collectors are callback functions called with an array of all the errors collected during the last model inspection. Every error is an object with these properties:

    • message: a message describing the error
    • -
    • [expected]: the expected type definition (when suitable)
    • -
    • [received]: the received value, to compare to the expected (when suitable)
    • -
    • [path]: the path where the error occurred in an object model definition (when suitable)
    • +
    • [expected]: the expected type definition or assertion/li> +
    • [received]: the received value, to compare to the expected
    • +
    • [path]: the path where the error occurred in an object model definition
    diff --git a/package.json b/package.json index 53849df..e508ba6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "objectmodel", - "version": "2.5.4", + "version": "2.6.0", "description": "Strong Dynamically Typed Object Modeling for JavaScript", "author": "Sylvain Pollet-Villard", "license": "MIT", @@ -25,6 +25,7 @@ "typed", "types", "typing", + "type-checking", "dynamic", "strong", "model",