Skip to content

Commit

Permalink
release 2.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainpolletvillard committed Jan 17, 2017
1 parent 80e574d commit 3d5cf6b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 54 deletions.
52 changes: 30 additions & 22 deletions dist/object-model.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -152,7 +152,7 @@ function Model(def){
return obj;
};

initModel(model, def, Model);
initModel(model, arguments, Model);
return model;
}

Expand Down Expand Up @@ -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;
};
Expand All @@ -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
Expand All @@ -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, []);
}
Expand Down Expand Up @@ -354,18 +352,27 @@ 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<l ; i++){
var assert = model[ASSERTIONS][i],
assertionResult;
assertionResult,
description = assert[DESCRIPTION],
onFail = isFunction(description) ? description : function (assertionResult, value) {
return 'assertion "' + description
+ '" returned ' + toString(assertionResult)
+ ' for value ' + toString(value);
};
try {
assertionResult = assert.call(model, obj);
} catch(err){
assertionResult = err;
}
if(assertionResult !== true){
var err = {};
err[MESSAGE] = assert[ON_FAIL].call(model, assertionResult, obj)
err[MESSAGE] = onFail.call(model, assertionResult, obj)
err[EXPECTED] = assert;
err[RECEIVED] = obj;
err[PATH] = path;
errorStack.push(err);
}
}
Expand Down Expand Up @@ -414,7 +421,7 @@ Model[OBJECT] = function ObjectModel(def){
};

setConstructorProto(model, Object[PROTO]);
initModel(model, def, Model[OBJECT]);
initModel(model, arguments, Model[OBJECT]);
return model;
};

Expand All @@ -441,7 +448,7 @@ define(ObjectModelProto, VALIDATOR, function(obj, path, callStack, errorStack){
} else {
checkDefinition(obj, this[DEFINITION], path, callStack, errorStack);
}
checkAssertions(obj, this, errorStack);
checkAssertions(obj, this, path, errorStack);
});

function getProxy(model, obj, defNode, path) {
Expand Down Expand Up @@ -475,7 +482,7 @@ function getProxy(model, obj, defNode, path) {
checkDefinition(newProxy, defNode[key], newPath, [], model[ERROR_STACK]);
var oldValue = wrapper[key];
wrapper[key] = newProxy;
checkAssertions(obj, model, model[ERROR_STACK]);
checkAssertions(obj, model, newPath, model[ERROR_STACK]);
if(model[ERROR_STACK].length){
wrapper[key] = oldValue;
model[UNSTACK]();
Expand Down Expand Up @@ -529,7 +536,7 @@ Model[ARRAY] = function ArrayModel(def){
};

setConstructorProto(model, Array[PROTO]);
initModel(model, def, Model[ARRAY]);
initModel(model, arguments, Model[ARRAY]);
return model;
};

Expand All @@ -553,7 +560,7 @@ define(ArrayModelProto, VALIDATOR, function(arr, path, callStack, errorStack){
arr[i] = checkDefinition(arr[i], this[DEFINITION], (path||ARRAY)+'['+i+']', callStack, errorStack, true);
}
}
checkAssertions(arr, this, errorStack);
checkAssertions(arr, this, path, errorStack);
});

function proxifyArrayKey(proxy, array, key, model){
Expand Down Expand Up @@ -591,12 +598,13 @@ function proxifyArrayMethod(array, method, model, proxy){
}

function setArrayKey(array, key, value, model){
var path = ARRAY+'['+key+']';
if(parseInt(key) === +key && key >= 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;
}
Expand All @@ -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);
Expand All @@ -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;
};

Expand Down
Loading

0 comments on commit 3d5cf6b

Please # to comment.