Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

add additionalProperties option #42

Closed
wants to merge 1 commit into from

2 participants

@okv
okv commented

Default value for object additionalProperties attribute

@Swaagie
Owner

Cherry picked with 0368e59 and a few additional fixes.
Thanks

@Swaagie Swaagie closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 6, 2013
  1. @okv
This page is out of date. Refresh to see the latest.
Showing with 38 additions and 0 deletions.
  1. +1 −0  README.md
  2. +4 −0 lib/revalidator.js
  3. +33 −0 test/validator-test.js
View
1  README.md
@@ -74,6 +74,7 @@ This will return with a value indicating if the `obj` conforms to the `schema`.
* __validateFormatsStrict__: When `validateFormats` is _true_ treat unrecognized formats as validation errors (_default false_)
* __validateFormatExtensions__: When `validateFormats` is _true_ also validate formats defined in `validate.formatExtensions` (_default true_)
* __cast__: Enforce casting of some types (for integers/numbers are only supported) when it's possible, e.g. `"42" => 42`, but `"forty2" => "forty2"` for the `integer` type.
+* __additionalProperties__: Default value for object `additionalProperties` attribute.
### Schema
For a property an `value` is that which is given as input for validation where as an `expected value` is the value of the below fields
View
4 lib/revalidator.js
@@ -193,6 +193,10 @@
}
}
+ //if additionalProperties is not defined set default value
+ if (schema.additionalProperties === undefined) {
+ schema.additionalProperties = options.additionalProperties;
+ }
// see 5.4
if (undefined !== schema.additionalProperties) {
var i, l;
View
33 test/validator-test.js
@@ -191,6 +191,39 @@ vows.describe('revalidator', {
},
"return an object with `valid` set to false": assertInvalid
}
+ },
+ "with option <additionalProperties>:false": {
+ topic: {
+ properties: {
+ town: { type: 'string' }
+ }
+ },
+ "when the object conforms": {
+ topic: function (schema) {
+ return revalidator.validate({ town: "luna" }, schema, {additionalProperties: false});
+ },
+ "return an object with `valid` set to true": assertValid
+ },
+ "when the object does not conform": {
+ topic: function (schema) {
+ return revalidator.validate({ town: "luna", area: 'park' }, schema, {additionalProperties: false});
+ },
+ "return an object with `valid` set to false": assertInvalid
+ },
+ "but overridden to true at schema": {
+ topic: {
+ properties: {
+ town: { type: 'string' }
+ },
+ additionalProperties: true
+ },
+ "when the object does not conform": {
+ topic: function (schema) {
+ return revalidator.validate({ town: "luna", area: 'park' }, schema, {additionalProperties: false});
+ },
+ "return an object with `valid` set to true": assertValid
+ }
+ }
}
}
}).addBatch({
Something went wrong with that request. Please try again.