Skip to content

Commit

Permalink
Add ability to check for a particular value
Browse files Browse the repository at this point in the history
  • Loading branch information
chilts committed Jul 3, 2014
1 parent 5446f42 commit 6bf1cff
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -14,12 +14,14 @@ var schema = {
url : sound().isString().isUrl(), // optional
isAdmin : sound().isString().required().toBoolean().isBoolean(),
dob : sound().isDate(), // accepts 'yyyy-mm-dd'
agree : sound().isString().toBoolean().is(true), // make sure they tick T&C's
};
var params = {
username : 'chilts',
password : 'abcdefgh',
email : 'me@example.com',
agree : 'on', // will convert to true
};
var out = sound.validate(params, schema);
Expand Down
17 changes: 17 additions & 0 deletions sound.js
Expand Up @@ -97,6 +97,15 @@ Constraint.prototype.isDate = function(msg) {
return this;
};

Constraint.prototype.is = function(value, msg) {
this.rules.push({
type : 'is',
value : value,
msg : msg
});
return this;
};

Constraint.prototype.lt = function(value, msg) {
this.rules.push({
type : 'lt',
Expand Down Expand Up @@ -358,6 +367,14 @@ var validateParam = function(name, value, constraint) {
};
}
}
else if ( r.type === 'is' ) {
if ( value !== r.value ) {
return {
ok : false,
err : r.msg || name + ' should be ' + r.value,
};
}
}
else if ( r.type === 'gt' ) {
if ( value <= r.value ) {
return {
Expand Down
8 changes: 8 additions & 0 deletions test/basic.js
Expand Up @@ -83,6 +83,10 @@ var tests = [
isAdmin : sound().isBoolean().name('Is Admin').required(),
isHuman : sound().isBoolean().name('Is Human').required(),
date : sound().isDate().name('Date').required(),
agree : sound().is(true).name('Agree'),
hasHair : sound().is(false).name('Have Hair?'),
age : sound().is(21).name('Is 21?'),
yes : sound().is('yes'),
},
params : {
username : 'andy',
Expand All @@ -92,6 +96,10 @@ var tests = [
isAdmin : false,
isHuman : true,
date : new Date(),
agree : true,
hasHair : false,
age : 21,
yes : 'yes',
},
test : function(t, err, res) {
t.ok(_.isObject(err), "is an object");
Expand Down

0 comments on commit 6bf1cff

Please sign in to comment.