Added ability to supply custom validation methods. Each custom validation method…
… recieves the field value to validate as its sole argument. It should throw an error to indicate a validation failure. For example:
<pre>
User = sequelize.define('User', {
name: {
type: Sequelize.STRING,
validate: {
len: {
args: 9,
msg: "Need atleast 9 chars"
},
customFn1: function(val) {
# if name doesn't start with 'joe' then fail
if (val.substr(0,3) !== "joe")
throw new Error("name should start with 'joe'")
},
customFn2: function(val) {
# if name doesn't end in 'bloggs' then fail
if (val.substr(-6) !== "bloggs")
throw new Error("name should end with 'bloggs'")
}
}
}
});
</pre>
You still invoke validation as you normally would, i.e.:
<pre>
# build user
var user = User.build({
name : "test"
});
# validate
errors = user.validate();
# At this point errors will contain:
#
# { name : [
# "Need atleast 9 chars"
# "name should start with 'joe'"
# "name should end with 'bloggs'"
# ] }
#
</pre>
Showing
with
258 additions
and
212 deletions
-
Please register or sign in to post a comment