joi
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -0,0 +0,0 @@ // Load modules |
@@ -0,0 +0,0 @@ // Load modules |
@@ -0,0 +0,0 @@ // Load modules |
@@ -0,0 +0,0 @@ // Load modules |
@@ -0,0 +0,0 @@ // Load modules |
@@ -0,0 +0,0 @@ // Load modules |
@@ -8,2 +8,4 @@ // Load modules | ||
var ArrayType = require('./array'); | ||
var ObjectType = require('./object'); | ||
var FunctionType = require('./function'); | ||
@@ -25,2 +27,4 @@ | ||
this.Array = ArrayType; | ||
this.Object = ObjectType; | ||
this.Function = FunctionType; | ||
@@ -68,4 +72,2 @@ return this; | ||
return result; | ||
}; | ||
}; |
@@ -0,0 +0,0 @@ // Load modules |
@@ -6,2 +6,3 @@ // Load modules | ||
var Utils = require('../utils'); | ||
var Validator = require('../'); | ||
@@ -14,12 +15,15 @@ | ||
module.exports = internals.createType = function () { | ||
module.exports = internals.createType = function (config) { | ||
return new internals.ObjectType(); | ||
return new internals.ObjectType(config); | ||
}; | ||
module.exports.ObjectType = internals.ObjectType = function () { | ||
module.exports.ObjectType = internals.ObjectType = function (config) { | ||
internals.ObjectType.super_.call(this); | ||
Utils.mixin(this, BaseType); | ||
this._config = config || {}; | ||
return this; | ||
@@ -32,2 +36,3 @@ }; | ||
internals.ObjectType.prototype.__name = 'Object'; | ||
internals.ObjectType.prototype.__defaultValids = [null]; | ||
@@ -43,5 +48,22 @@ | ||
var self = this; | ||
return function (value) { | ||
return (value === null || typeof value === 'object'); | ||
if (typeof value !== 'object') { | ||
return false; | ||
} | ||
var valid = false; | ||
self._traverse(value, self._config, function (isValid) { | ||
if (!isValid) { | ||
return false; | ||
} | ||
valid = isValid; | ||
}); | ||
return valid; | ||
}; | ||
@@ -57,1 +79,29 @@ }; | ||
internals.ObjectType.prototype._traverse = function (obj, config, callback) { | ||
var self = this; | ||
var traversing = false; | ||
Object.keys(config).forEach(function (key) { | ||
var itemConfig = config[key]; | ||
var value = obj ? obj[key] : null; | ||
if (itemConfig.type === 'Object') { | ||
traversing = true; | ||
self._traverse(value, itemConfig._config, callback); | ||
} | ||
else if (typeof itemConfig.validate === 'function' && | ||
itemConfig.validate(value) === false) { | ||
traversing = true; | ||
return callback(false); | ||
} | ||
}); | ||
if (!traversing) { | ||
callback(true); | ||
} | ||
}; |
@@ -0,0 +0,0 @@ // Load modules |
@@ -0,0 +0,0 @@ // Load modules |
{ | ||
"name": "joi", | ||
"description": "Object schema validation", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"author": "Van Nguyen <the.gol.effect@gmail.com>", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
@@ -0,0 +0,0 @@ <a href="https://github.com/walmartlabs/blammo"><img src="https://raw.github.com/walmartlabs/blammo/master/images/from.png" align="right" /></a> |
@@ -0,0 +0,0 @@ // Load modules |
@@ -0,0 +0,0 @@ // Load modules |
@@ -0,0 +0,0 @@ // Load modules |
@@ -0,0 +0,0 @@ // Load modules |
@@ -0,0 +0,0 @@ // Load modules |
@@ -0,0 +0,0 @@ // Load modules |
@@ -0,0 +0,0 @@ // Load modules |
@@ -0,0 +0,0 @@ // Load modules |
@@ -44,2 +44,3 @@ // Load modules | ||
verifyBehavior(t, [ | ||
[{ }, true], | ||
[{ hi: true }, true], | ||
@@ -49,4 +50,55 @@ ['', false] | ||
}); | ||
it('should traverse an object and validate all properties in the top level', function (done) { | ||
var t = O({ | ||
num: Joi.Types.Number() | ||
}); | ||
verifyBehavior(t, [ | ||
[{ num: 1 }, true], | ||
[{ num: [1,2,3] }, false] | ||
], done); | ||
}); | ||
it('should traverse an object and child objects and validate all properties', function (done) { | ||
var t = O({ | ||
num: Joi.Types.Number(), | ||
obj: Joi.Types.Object({ | ||
item: Joi.Types.String() | ||
}) | ||
}); | ||
verifyBehavior(t, [ | ||
[{ num: 1 }, false], | ||
[{ num: [1,2,3] }, false], | ||
[{ num: 1, obj: { item: 'something' }}, true], | ||
[{ num: 1, obj: { item: 123 }}, false] | ||
], done); | ||
}); | ||
it('should traverse an object several levels', function (done) { | ||
var t = O({ | ||
obj: Joi.Types.Object({ | ||
obj: Joi.Types.Object({ | ||
obj: Joi.Types.Object({ | ||
item: Joi.Types.Boolean() | ||
}) | ||
}) | ||
}) | ||
}); | ||
verifyBehavior(t, [ | ||
[{ num: 1 }, false], | ||
[{ obj: {} }, false], | ||
[{ obj: { obj: { }}}, false], | ||
[{ obj: { obj: { obj: { } }}}, true], | ||
[{ obj: { obj: { obj: { item: true } }}}, true], | ||
[{ obj: { obj: { obj: { item: 10 } }}}, false] | ||
], done); | ||
}); | ||
}); | ||
}); | ||
@@ -0,0 +0,0 @@ // Load modules |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
268176
32
2752
14