LearnBoost/mongoose — Issue #596: strange behavior of validation
Author: RiadhKHEDHIRI
Created: 2011-11-06 01:09:51+00:00
State: open

Hello,

Given this (from separated files)

```javascript
exports.validatePresenceOf = function(value) {
if (value === null) return false;
return value;
};

var AccountSchema = new Schema({
balance: {type: Number, validate: [helpers.validatePresenceOf, 'nooooooooooooooooo']}
});

exports.Customer = mongoose.model('Customer', new Schema({
name	: String
, email	: String
, accounts: [AccountSchema]
})
);
```

* when I try to add some accounts to a customer, the validation routine is called many times, actually the number of the customer accounts. Is that OK?
* when I try to add a correct account, let's say, with balance = 123.456, that's fine, it passes.
* with a null, the validation doesn't pass and that's fine too.
* BUT when I try to add an account with alphabetic value as balance (which is a Number), the routine ''validatePresenceOf" will have the last valid balance value as argument and not the actual value being entered so it passes and the account record is saved with a null balance value in the database. Also the routine is called 2 x (number of customer accounts).

Is that really strange or i'm missing a thing?