Skip to content

Commit

Permalink
Merge branch 'release/0.1.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
diversario committed Jul 13, 2013
2 parents 7b4559f + 083de91 commit b567675
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
12 changes: 10 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,16 @@ module.exports = function propertyValidation(data, schema) {
/*
Regular expression
*/
if (rules.regexp && !rules.regexp.test(propertyValue)) return false

if (rules.regexp) {
if (Array.isArray(rules.regexp)) {
if (!rules.regexp.every(function(regexp) {return regexp.test(propertyValue)})) {
return false
}
} else if (!rules.regexp.test(propertyValue)) {
return false
}
}

return true
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-schema",
"version": "0.1.1",
"version": "0.1.2",
"description": "Simple object validation framework",
"keywords": [
"validation",
Expand Down
40 changes: 40 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,46 @@ describe('Simple-schema', function () {
return [1].indexOf(err.rule.error.code) !== -1
}))
})

it('supports array of regexps', function () {
var errors

var schema = {
'prop1': {
'required': true,
'type': 'string',
'regexp': [/don't/, /fail/],
'error': {'code': 1, 'message': 'one'}
}
}

errors = validate({
'prop1': 'don\'t fail'
}, schema)
assert.equal(errors.length, 0)
})

it('if any regexp in array fails - fail validation', function () {
var errors

var schema = {
'prop1': {
'required': true,
'type': 'string',
'regexp': [/please/, /fail/],
'error': {'code': 1, 'message': 'one'}
}
}

errors = validate({
'prop1': 'don\'t fail'
}, schema)
assert.equal(errors.length, 1)

assert(errors.every(function (err) {
return [1].indexOf(err.rule.error.code) !== -1
}))
})
})


Expand Down

0 comments on commit b567675

Please sign in to comment.