Skip to content

Commit

Permalink
allow passing primitives as object key values
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeoneWeird committed Feb 7, 2016
1 parent 4e7837b commit 1be2e16
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ function makeValidator(validatorName, validatorFunc) {
if((validatorFunc.hasChildValidators == 'array' && Array.isArray(arg)) || (typeof arg === validatorFunc.hasChildValidators)) {

if(Object.keys(arg).every(function(k) {
return arg[k].hasOwnProperty('$validators');
let t = arg[k];
let _v = t ? validators[t.name] : null;
if(_v) {
let v = _v();
t = v;
arg[k] = v;
return true;
}
return t.hasOwnProperty('$validators');
})) {
childValidators = arg;
continue;
Expand Down
24 changes: 24 additions & 0 deletions test-src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,4 +496,28 @@ describe("Validator", function() {

});

describe("Primitives", function() {

it("should allow passing primitives as object key values", function() {

let schema = s.Object({
bool: Boolean,
string: String,
num: Number
});

schema.validate({
bool: true,
string: "hello world",
num: 5
});

assert.equal(schema.$validators.bool.$name, 'FastBoolean');
assert.equal(schema.$validators.string.$name, 'FastString');
assert.equal(schema.$validators.num.$name, 'FastNumber');

});

});

});

0 comments on commit 1be2e16

Please sign in to comment.