Comparing version 0.3.0 to 0.3.1
@@ -19,3 +19,4 @@ // Load modules | ||
skipFunctions: false, | ||
saveConversions: false | ||
saveConversions: false, | ||
skipConversions: false | ||
}; | ||
@@ -84,3 +85,3 @@ | ||
var converter = T && T().convert || null; | ||
if (typeof converter === 'function') { | ||
if (!self.settings.skipConversions && typeof converter === 'function') { | ||
try { | ||
@@ -87,0 +88,0 @@ value = converter(value); |
@@ -33,3 +33,2 @@ // Load modules | ||
internals.ObjectType.prototype.__name = 'Object'; | ||
internals.ObjectType.prototype.__defaultValids = [null]; | ||
@@ -86,3 +85,3 @@ | ||
if (itemConfig.type === 'Object') { | ||
if (itemConfig.type === 'Object' && value) { | ||
traverse(value, itemConfig._config, errors, topKeyPath); | ||
@@ -89,0 +88,0 @@ } |
{ | ||
"name": "joi", | ||
"description": "Object schema validation", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"author": "Van Nguyen <the.gol.effect@gmail.com>", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
@@ -430,2 +430,11 @@ <a href="https://github.com/spumko"><img src="https://raw.github.com/spumko/spumko/master/images/from.png" align="right" /></a> | ||
#### Skip Conversions | ||
By default Joi tries to parse and convert object's values into correct type. You might want to disable this behaviour e.g. when you are validating program's internal objects instead of user input. | ||
To force Joi to not convert object values, use the `skipConversions` option: | ||
Joi.settings.skipConversions = true; | ||
### Type-Specific | ||
@@ -432,0 +441,0 @@ |
@@ -19,3 +19,3 @@ // Load modules | ||
var it = Lab.test; | ||
var T = Joi.Types; | ||
var T = Joi.types; | ||
@@ -25,6 +25,6 @@ | ||
var config = { | ||
var config1 = { | ||
a: Joi.types.Number().min(0).max(3), | ||
b: Joi.types.String().valid('a', 'b', 'c'), | ||
c: Joi.types.String().email() | ||
c: Joi.types.String().email().optional() | ||
}; | ||
@@ -45,3 +45,3 @@ | ||
i: Joi.types.String(), | ||
j: Joi.types.Object().optional() | ||
j: Joi.types.Object() | ||
}; | ||
@@ -66,3 +66,3 @@ | ||
}; | ||
var err = Joi.validate(obj, config); | ||
var err = Joi.validate(obj, config1); | ||
@@ -75,3 +75,3 @@ expect(err).to.not.exist; | ||
var err = Joi.validate(null, config); | ||
var err = Joi.validate(null, config1); | ||
@@ -131,6 +131,6 @@ expect(err).to.exist; | ||
Joi.types.Object({ | ||
mode: T.String().valid('required', 'optional', 'try').optional().nullOk() | ||
}).optional().nullOk(), | ||
T.String().optional().nullOk(), | ||
T.Boolean().optional().nullOk() | ||
mode: T.String().valid('required', 'optional', 'try').nullOk() | ||
}).nullOk(), | ||
T.String().nullOk(), | ||
T.Boolean().nullOk() | ||
] | ||
@@ -142,3 +142,3 @@ }; | ||
expect(Joi.validate({ something: undefined }, config)).to.be.null; | ||
expect(Joi.validate({ auth: { something: undefined }}, config)).to.be.null; | ||
expect(Joi.validate({ auth: { something: undefined } }, config)).to.be.null; | ||
@@ -152,4 +152,4 @@ done(); | ||
auth: Joi.types.Object({ | ||
mode: T.String().valid('required', 'optional', 'try').optional().nullOk() | ||
}).optional().nullOk() | ||
mode: T.String().valid('required', 'optional', 'try').nullOk() | ||
}).nullOk() | ||
}; | ||
@@ -166,9 +166,9 @@ | ||
var config = { | ||
handler: [T.Object(), T.Function(), T.String().valid('notFound').optional()], | ||
handler: [T.Object(), T.Function(), T.String().valid('notFound')], | ||
payload: T.String().valid('stream', 'raw', 'parse').nullOk(), | ||
response: T.Object({ | ||
schema: T.Object().nullOk().optional(), | ||
sample: T.Number().optional(), | ||
failAction: T.String().optional().valid('error', 'log', 'ignore') | ||
}).optional().nullOk().allow(true).allow(false), | ||
schema: T.Object().nullOk(), | ||
sample: T.Number(), | ||
failAction: T.String().valid('error', 'log', 'ignore') | ||
}).nullOk().allow(true).allow(false), | ||
auth: [ | ||
@@ -183,5 +183,5 @@ T.Object({ | ||
payload: T.String().nullOk() | ||
}).optional().nullOk(), | ||
T.Boolean().allow(false).optional().nullOk(), | ||
T.String().optional().nullOk() | ||
}).nullOk(), | ||
T.Boolean().allow(false).nullOk(), | ||
T.String().nullOk() | ||
] | ||
@@ -195,2 +195,22 @@ }; | ||
it('validates required key with multiple options', function (done) { | ||
var config = { | ||
module: [ | ||
T.Object({ | ||
compile: T.Function().required(), | ||
execute: T.Function() | ||
}).required(), | ||
T.String().required() | ||
] | ||
}; | ||
expect(Joi.validate({}, config)).to.not.be.null; | ||
expect(Joi.validate({ module: 'test' }, config)).to.be.null; | ||
expect(Joi.validate({ module: {} }, config)).to.not.be.null; | ||
expect(Joi.validate({ module: { compile: function () { } } }, config)).to.be.null; | ||
done(); | ||
}); | ||
it('should not require optional numbers', function (done) { | ||
@@ -209,2 +229,15 @@ | ||
it('should not require optional objects', function (done) { | ||
var config = { | ||
position: T.Number(), | ||
suggestion: T.Object() | ||
}; | ||
expect(Joi.validate({ suggestion: {} }, config)).to.be.null; | ||
expect(Joi.validate({ position: 1 }, config)).to.be.null; | ||
done(); | ||
}); | ||
it('should validate object successfully when config has an array of types', function (done) { | ||
@@ -241,3 +274,3 @@ | ||
}; | ||
var err = Joi.validate(obj, config); | ||
var err = Joi.validate(obj, config1); | ||
@@ -248,18 +281,31 @@ expect(err).to.exist; | ||
it('should fail validation when the wrong types are supplied', function (done) { | ||
var obj = { | ||
a: 'a', | ||
b: 'a', | ||
c: 'joe@example.com' | ||
}; | ||
var err = Joi.validate(obj, config); | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
it('should fail validation when missing a required parameter', function (done) { | ||
it('should fail validation when the wrong types are supplied', function (done) { | ||
var obj = { | ||
a: 'a', | ||
b: 'a', | ||
c: 'joe@example.com' | ||
}; | ||
var err = Joi.validate(obj, config1); | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
it('should fail validation when the wrong types are supplied', function (done) { | ||
var obj = { | ||
a: 'a', | ||
b: 'a', | ||
c: 'joe@example.com' | ||
}; | ||
var err = Joi.validate(obj, config1); | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
it('should fail validation when missing a required parameter', function (done) { | ||
var obj = { | ||
c: 10 | ||
@@ -276,3 +322,3 @@ }; | ||
var obj = { | ||
a: { } | ||
a: {} | ||
}; | ||
@@ -314,3 +360,3 @@ var err = Joi.validate(obj, { a: Joi.types.Object({ b: Joi.types.String().required() }) }); | ||
var schema = { username: Joi.types.String() }; | ||
var input = { username: 'test', func: function() { } }; | ||
var input = { username: 'test', func: function () { } }; | ||
var err = Joi.validate(input, schema); | ||
@@ -327,3 +373,3 @@ | ||
var schema = { username: Joi.types.String() }; | ||
var input = { username: 'test', func: function() { } }; | ||
var input = { username: 'test', func: function () { } }; | ||
var err = Joi.validate(input, schema); | ||
@@ -372,2 +418,16 @@ | ||
it('should not convert values when skipConversions is set', function (done) { | ||
Joi.settings.skipConversions = true; | ||
var schema = { | ||
arr: Joi.types.Array().includes(Joi.types.String()) | ||
}; | ||
var input = { arr: 'foo' }; | ||
var err = Joi.validate(input, schema); | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
it('should fail validation when a child object has an invalid string value but object traversal isn\'t complete', function (done) { | ||
@@ -395,11 +455,11 @@ | ||
path: T.String().required(), | ||
handler: [T.Object().optional(), T.Function().optional(), T.String().valid('notFound').optional()], | ||
handler: [T.Object(), T.Function(), T.String().valid('notFound')], | ||
config: T.Object({ | ||
handler: [T.Object().optional(), T.Function().optional(), T.String().valid('notFound').optional()], | ||
payload: T.String().valid(['stream', 'raw', 'parse']).optional(), | ||
handler: [T.Object(), T.Function(), T.String().valid('notFound')], | ||
payload: T.String().valid(['stream', 'raw', 'parse']), | ||
response: T.Object({ | ||
schema: T.Object().nullOk().optional(), | ||
sample: T.Number().optional(), | ||
failAction: T.String().optional().valid(['error', 'log', 'ignore']) | ||
}).optional() | ||
schema: T.Object().nullOk(), | ||
sample: T.Number(), | ||
failAction: T.String().valid(['error', 'log', 'ignore']) | ||
}) | ||
}) | ||
@@ -406,0 +466,0 @@ }; |
@@ -56,3 +56,3 @@ // Load modules | ||
var t = O({ | ||
num: Joi.Types.Number() | ||
num: Joi.types.Number() | ||
}); | ||
@@ -69,5 +69,5 @@ | ||
var t = O({ | ||
num: Joi.Types.Number(), | ||
obj: Joi.Types.Object({ | ||
item: Joi.Types.String() | ||
num: Joi.types.Number(), | ||
obj: O({ | ||
item: Joi.types.String() | ||
}) | ||
@@ -77,3 +77,3 @@ }); | ||
verifyBehavior(t, [ | ||
[{ num: 1 }, false], | ||
[{ num: 1 }, true], | ||
[{ num: [1,2,3] }, false], | ||
@@ -88,6 +88,6 @@ [{ num: 1, obj: { item: 'something' }}, true], | ||
var t = O({ | ||
obj: Joi.Types.Object({ | ||
obj: Joi.Types.Object({ | ||
obj: Joi.Types.Object({ | ||
item: Joi.Types.Boolean() | ||
obj: O({ | ||
obj: O({ | ||
obj: O({ | ||
item: Joi.types.Boolean() | ||
}) | ||
@@ -100,4 +100,4 @@ }) | ||
[{ num: 1 }, false], | ||
[{ obj: {} }, false], | ||
[{ obj: { obj: { }}}, false], | ||
[{ obj: {} }, true], | ||
[{ obj: { obj: { }}}, true], | ||
[{ obj: { obj: { obj: { } }}}, true], | ||
@@ -108,4 +108,28 @@ [{ obj: { obj: { obj: { item: true } }}}, true], | ||
}); | ||
it('should traverse an object several levels with required levels', function (done) { | ||
var t = O({ | ||
obj: O({ | ||
obj: O({ | ||
obj: O({ | ||
item: Joi.types.Boolean() | ||
}) | ||
}).required() | ||
}) | ||
}); | ||
verifyBehavior(t, [ | ||
[null, false], | ||
[undefined, true], | ||
[{}, true], | ||
[{ obj: {} }, false], | ||
[{ obj: { obj: {} } }, true], | ||
[{ obj: { obj: { obj: {} } } }, true], | ||
[{ obj: { obj: { obj: { item: true } } } }, true], | ||
[{ obj: { obj: { obj: { item: 10 } } } }, false] | ||
], done); | ||
}); | ||
}); | ||
}); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
285878
3142
534