Validations are automatically run on , update
and save
. You can also call validate()
to manually validate an instance.
You can define your custom validators or use several built-in validators, implemented by , as shown below.
Note that where multiple arguments need to be passed to the built-in validation functions, the arguments to be passed must be in an array. But if a single array argument is to be passed, for instance an array of acceptable strings for isIn
, this will be interpreted as multiple string arguments instead of one array argument. To work around this pass a single-length array of arguments, such as [['one', 'two']]
as shown above.
To use a custom error message instead of that provided by validator.js, use an object instead of the plain value or array of arguments, for example a validator which needs no argument can be given a custom message with
isInt: {
msg: "Must be an integer number of pennies"
}
or if arguments need to also be passed add an args
property:
See for more details on the built in validation methods.
Hint: You can also define a custom function for the logging part. Just pass a function. The first parameter will be the string that is logged.
If a particular field of a model is set to not allow null (with allowNull: false
) and that value has been set to null
, all validators will be skipped and a ValidationError
will be thrown.
On the other hand, if it is set to allow null (with allowNull: true
) and that value has been set to null
, only the built-in validators will be skipped, while the custom validators will still run.
This means you can, for instance, have a string field which validates its length to be between 5 and 10 characters, but which also allows null
(since the length validator will be skipped automatically when the value is null
):
class User extends Model {}
User.init({
username: {
validate: {
len: [5, 10]
}
}
}, { sequelize });
You can customize allowNull
error message by setting the notNull
validator:
class User extends Model {}
User.init({
name: {
type: Sequelize.STRING,
allowNull: false,
notNull: {
msg: 'Please enter your name'
}
}
}
}, { sequelize });
Validations can also be defined to check the model after the field-specific validators. Using this you could, for example, ensure either neither of latitude
and longitude
are set or both, and fail if one but not the other is set.
Model validator methods are called with the model object's context and are deemed to fail if they throw an error, otherwise pass. This is just the same as with custom field-specific validators.
Any error messages collected are put in the validation result object alongside the field validation errors, with keys named after the failed validation method's key in the validate
option object. Even though there can only be one error message for each model validation method at any one time, it is presented as a single string error in an array, to maximize consistency with the field errors.
An example:
{
'latitude': ['Invalid number: latitude'],
'bothCoordsOrNone': ['Require either both latitude and longitude or neither']
}
Such validation could have also been done with a custom validator defined on a single attribute (such as the latitude
attribute, by checking ), but the model-wide validation approach is cleaner.