Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
Merge 7fc33dc into e913a41
Browse files Browse the repository at this point in the history
  • Loading branch information
flogiston committed Sep 19, 2018
2 parents e913a41 + 7fc33dc commit 2f926fb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
21 changes: 21 additions & 0 deletions specs/validate-spec.js
Expand Up @@ -192,6 +192,27 @@ describe("validate", function() {
]);
});

it("does not allow undeclared attributes in exclusive mode", function() {
var attributes = {name: "Nicklas", age: 23, year: 2018}
, globalOptions = {exclusive: true}
, constraints = {name: true};
expect(validate.runValidations(attributes, constraints, globalOptions)).toHaveItems([
{
attribute: "age",
value: 23,
globalOptions: globalOptions,
attributes: attributes,
error: "is not permitted (exclusive mode)"
}, {
attribute: "year",
value: 2018,
globalOptions: globalOptions,
attributes: attributes,
error: "is not permitted (exclusive mode)"
}
]);
});

it("allows the options for an attribute to be a function", function() {
var options = {pass: {option1: "value1"}}
, attrs = {name: "Nicklas"}
Expand Down
24 changes: 24 additions & 0 deletions validate.js
Expand Up @@ -80,6 +80,7 @@
runValidations: function(attributes, constraints, options) {
var results = []
, attr
, constraint
, validatorName
, value
, validators
Expand All @@ -91,6 +92,29 @@
attributes = v.collectFormValues(attributes);
}

// In 'exclusive' mode, make sure that there's a constraint for each and
// every attribute. Nested attribute names are matched as well.
if (options.exclusive === true) {
for (attr in attributes) {
value = v.getDeepObjectValue(attributes, attr);
var declared = false;
for (constraint in constraints) {
if (constraint === attr || constraint.indexOf(attr + ".") === 0) {
declared = true;
}
}
if (!declared) {
results.push({
attribute: attr,
value: value,
globalOptions: options,
attributes: attributes,
error: options.exclusiveErrorMessage || "is not permitted (exclusive mode)",
});
}
}
}

// Loops through each constraints, finds the correct validator and run it.
for (attr in constraints) {
value = v.getDeepObjectValue(attributes, attr);
Expand Down

0 comments on commit 2f926fb

Please sign in to comment.