Skip to content

Commit

Permalink
report fix #74 to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainpolletvillard committed Jun 19, 2018
1 parent 37010d2 commit c4e791c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/object-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ Model[OBJECT] = function ObjectModel(def){
if(!is(model, this)){
return new model(obj);
}

if(!is(Object, obj) && obj !== undefined){
var err = {};
err[EXPECTED] = OBJECT;
err[RECEIVED] = obj;
model[ERROR_STACK].push(err);
}

obj = defaultTo(model[DEFAULT], obj);
merge(this, obj, true);
var proxy = getProxy(model, this, model[DEFINITION]);
Expand Down
24 changes: 22 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,26 @@ function testSuite(Model){
assert.throws(function(){ Model.Object() }, /Error.*Model definition is required/, "ObjectModel without definition throws")
});

QUnit.test("edge cases of constructors", function (assert) {
assert.ok(Model.Object({}) instanceof Model.Object, "ObjectModel can receive empty object as argument");

const M = Model.Object({})
assert.strictEqual(M.test(undefined), true, "undefined is valid for empty objectmodels, due to null-safe object traversal")
assert.strictEqual(M.test(null), false, "null is invalid for empty objectmodels")
assert.strictEqual(M.test(1), false, "number is invalid for empty objectmodels")
assert.strictEqual(M.test(new Number(1)), true, "Numbers through constructor are valid for empty objectmodels")
assert.strictEqual(M.test("string"), false, "string is invalid for empty objectmodels")
assert.strictEqual(M.test(function(){}), true, "function is valid for empty objectmodels")

const O = Model.Object({ x: [ Number ]})
assert.strictEqual(O.test(undefined), true, "undefined is valid for optional objectmodels, due to null-safe object traversal")
assert.strictEqual(O.test(null), false, "null is invalid for optional objectmodels")
assert.strictEqual(O.test(1), false, "number is invalid for optional objectmodels")
assert.strictEqual(O.test(new Number(1)), true, "Numbers through constructor are valid for optional objectmodels")
assert.strictEqual(O.test("string"), false, "string is invalid for optional objectmodels")
assert.strictEqual(O.test(function(){}), true, "function is valid for optional objectmodels")
});

QUnit.test("Optional and multiple parameters", function(assert){
var Person = Model({
name: [String],
Expand Down Expand Up @@ -369,8 +389,8 @@ function testSuite(Model){
})(function(options){
return options.list.reduce(function(a, b){
switch(options.op){
case "sum": return a + b; break;
case "product": return a * b; break;
case "sum": return a + b;
case "product": return a * b;
}
}, options.op === "product" ? 1 : 0);
});
Expand Down

0 comments on commit c4e791c

Please # to comment.