Skip to content

Commit

Permalink
Fixed a bug in validatePresenceOf that incorrectly stated a 0 value w…
Browse files Browse the repository at this point in the history
…as empty.
  • Loading branch information
ms committed Oct 31, 2012
1 parent ddfdcbc commit 06c5a4b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion observe/validations/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ can.each([ can.Observe, can.Model ], function(clss){
*/
validatePresenceOf: function(attrNames, options) {
validate.call(this, attrNames, options, function(value) {
if(typeof value == 'undefined' || value == "" || value === null){
if(typeof value == 'undefined' || value === "" || value === null){
return this.constructor.validationMessages.presence;
}
});
Expand Down
41 changes: 41 additions & 0 deletions observe/validations/validations_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,47 @@ test("validatesPresenceOf", function(){
ok(!errors, "no errors "+typeof errors);
})

test("validatesPresenceOf with numbers and a 0 value", function() {
can.Observe.extend("Person", { attributes: {age: "number"}});

Person.validatePresenceOf("age");

var person = new Person();
var errors = person.errors();

ok(errors)
ok(errors.age)
equals(errors.age[0], "can't be empty", "A new Person with no age generates errors.");

//test for null
person = new Person({age: null});
errors = person.errors();

ok(errors)
ok(errors.age)
equals(errors.age[0], "can't be empty" , "A new Person with null age generates errors.");

//test for ""
person = new Person({age: ""});
errors = person.errors();

ok(errors)
ok(errors.age)
equals(errors.age[0], "can't be empty" , "A new Person with an empty string age generates errors.");

//Affirmative test
person = new Person({age: 12});
errors = person.errors();

ok(!errors, "A new Person with a valid >0 age doesn't generate errors.");

//Affirmative test with 0
person = new Person({age: 0});
errors = person.errors();

ok(!errors, "A new Person with a valid 0 age doesn't generate errors");
});

test("validatesRangeOf", function(){
Person.validateRangeOf("thing", 2, 5);

Expand Down

0 comments on commit 06c5a4b

Please sign in to comment.