How can i do a validation on a non required field only if it's not an empty string ?
My model :
var Customer = db.define('Customer', {
name : { type: "text", unique: true, required: true },
adress : { type: "text" },
zipcode : { type: "text" },
city : { type: "text" },
country : { type: "text" },
siret : { type: "text" },
phone : { type: "text" },
fax : { type: "text" },
email : { type: "text" },
website : { type: "text" },
active : { type: 'integer', required: true },
createdAt : { type: 'date', required: true, time: true }
}, {
hooks: {
beforeValidation: function () {
this.createdAt = new Date();
this.active = 1;
}
},
methods: {
fullCity: function () {
return this.zipcode + ' ' + this.city;
}
},
validations: {
name: [
orm.enforce.notEmptyString("Not empty string"),
orm.enforce.unique("Already exists")
],
siret: orm.enforce.ranges.length(15, 15, "Must contain 15 characters"),
email: orm.enforce.patterns.email("Not valid email")
}
});
The validation occurs even if it is an empty string (fields siret and email). I know that is work fine if the value is null or undefined, but i want do to de the same with empty string.
Any ideas are welcome.
Thanks.