Comparing version 1.1.0 to 1.2.0
@@ -20,3 +20,5 @@ // Load modules | ||
saveConversions: false, | ||
skipConversions: false | ||
skipConversions: false, | ||
stripExtraKeys: false, | ||
allowExtraKeys: false | ||
}; | ||
@@ -28,2 +30,4 @@ | ||
skipConversions: 'boolean', | ||
stripExtraKeys: 'boolean', | ||
allowExtraKeys: 'boolean', | ||
languagePath: 'string' | ||
@@ -51,2 +55,4 @@ }; | ||
var skipFunctions = settings.skipFunctions; | ||
var stripExtraKeys = settings.stripExtraKeys; | ||
var allowExtraKeys = settings.allowExtraKeys; | ||
var errors = new Errors(object, settings); | ||
@@ -76,3 +82,8 @@ | ||
if ((!skipFunctions || unprocessedValueType !== 'function') && unprocessedValueType !== 'undefined') { | ||
errors.addLocalized('base.unknown', unprocessedKey, null, unprocessedKey); | ||
if (stripExtraKeys && allowExtraKeys) { | ||
delete object[unprocessedKey] | ||
} | ||
else if (!allowExtraKeys) { | ||
errors.addLocalized('base.unknown', unprocessedKey, null, unprocessedKey); | ||
} | ||
} | ||
@@ -79,0 +90,0 @@ }); |
@@ -73,3 +73,3 @@ // Load modules | ||
errors.addLocalized('number.min', key, { | ||
value: value | ||
value: n | ||
}, keyPath); | ||
@@ -99,3 +99,3 @@ } | ||
errors.addLocalized('number.max', key, { | ||
value: value | ||
value: n | ||
}, keyPath); | ||
@@ -102,0 +102,0 @@ } |
{ | ||
"name": "joi", | ||
"description": "Object schema validation", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"repository": "git://github.com/spumko/joi", | ||
@@ -6,0 +6,0 @@ "main": "index", |
@@ -455,2 +455,16 @@ <a href="https://github.com/spumko"><img src="https://raw.github.com/spumko/spumko/master/images/from.png" align="right" /></a> | ||
#### Allow Extra Keys & Strip Extra Keys | ||
By default Joi will throw an error on keys that are not specified in the configuration object. | ||
To force Joi to not throw errors when it encounters an unknown key, use the `allowExtraKeys` option: | ||
Joi.settings.allowExtraKeys = true; | ||
If you'd like Joi to remove the unknown keys from the object, enable both the `stripExtraKeys` option and the `allowExtraKeys` option: | ||
Joi.settings.allowExtraKeys = true; | ||
Joi.settings.stripExtraKeys = true; | ||
### Local | ||
@@ -457,0 +471,0 @@ |
@@ -522,2 +522,80 @@ // Load modules | ||
it('should fail validation with extra keys', function (done) { | ||
var obj = { | ||
a: 1, | ||
b: 'a', | ||
d: 'c' | ||
}; | ||
var err = Joi.validate(obj, config1); | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
it('should pass validation with extra keys and remove them when stripExtraKeys is set', function (done) { | ||
Joi.settings.stripExtraKeys = true; | ||
Joi.settings.allowExtraKeys = true; | ||
var obj = { | ||
a: 1, | ||
b: 'a', | ||
d: 'c' | ||
}; | ||
var err = Joi.validate(obj, config1); | ||
expect(err).to.be.null; | ||
expect(obj).to.deep.equal({a: 1, b: 'a'}); | ||
Joi.settings.stripExtraKeys = false; | ||
Joi.settings.allowExtraKeys = false; | ||
done(); | ||
}); | ||
it('should pass validation with extra keys when allowExtraKeys is set', function (done) { | ||
Joi.settings.allowExtraKeys = true; | ||
var obj = { | ||
a: 1, | ||
b: 'a', | ||
d: 'c' | ||
}; | ||
var err = Joi.validate(obj, config1); | ||
expect(err).to.be.null; | ||
expect(obj).to.deep.equal({a: 1, b: 'a', d: 'c'}); | ||
Joi.settings.allowExtraKeys = false; | ||
done(); | ||
}); | ||
it('should pass validation with extra keys and remove them when skipExtraKeys is set locally', function (done) { | ||
expect(Joi.settings.stripExtraKeys).to.equal(false); | ||
var localConfig = { | ||
a: Joi.types.Number().min(0).max(3), | ||
b: Joi.types.String().valid('a', 'b', 'c'), | ||
stripExtraKeys: true, | ||
allowExtraKeys: true | ||
}; | ||
var obj = { | ||
a: 1, | ||
b: 'a', | ||
d: 'c' | ||
}; | ||
var err = Joi.validate(obj, localConfig); | ||
expect(err).to.be.null; | ||
expect(obj).to.deep.equal({a: 1, b: 'a'}); | ||
expect(Joi.settings.stripExtraKeys).to.equal(false); | ||
done(); | ||
}); | ||
it('should work when the skipFunctions setting is enabled', function (done) { | ||
@@ -687,4 +765,2 @@ | ||
}; | ||
}); | ||
}); |
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
312519
3790
591