Skip to content

Commit

Permalink
nested objects beyond first level not validating
Browse files Browse the repository at this point in the history
  • Loading branch information
wavded committed Mar 22, 2011
1 parent 4d7765f commit c9daac4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 6 additions & 4 deletions lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ function Validator(fieldname, label) {

stack.forEach(function(validate) {
validate.getValue = function(name) {
var match = name.match(/^([^\[]*)(?:\[(.*)\])*$/);
var value = formData[match[1] || name];
if(match.length > 2) { // if complex property (e.g. user[info][password])
for(var mi = 2, mval; mval = match[mi]; mi++) value = value[mval];
var match = name.match(/[^\[\]]*/g).filter(function(m){ return !!m; })
var value = formData[match[0] || name];
if(match.length > 1) { // if complex property (e.g. user[info][password])
for(var mi = 1, mval; mval = match[mi]; mi++){
value = value[mval];
}
}
return value;
};
Expand Down
8 changes: 5 additions & 3 deletions test/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,13 @@ module.exports = {
},

"validation : complex properties": function() {
var request = { body: { field: { inner: "value" }}};
var request = { body: { field: { inner: "value", even: { more: { inner: "value" }}}}};
form(
validate("field[inner]").required().equals("value"),
validate("field[inner]").required().equals("fail")
validate("field[inner]").required().equals("fail"),
validate("field[even][more][inner]").required().equals("value"),
validate("field[even][more][inner]").required().equals("fail")
)(request, {});
assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors.length, 2);
}
};

0 comments on commit c9daac4

Please sign in to comment.