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.
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
To use a custom error message instead of that provided by [validator.js][3], 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
```js
```js
isInt:{
isInt:{
...
@@ -291,7 +296,7 @@ isInt: {
...
@@ -291,7 +296,7 @@ isInt: {
}
}
```
```
or if arguments need to also be passed add an`args`property:
or if arguments need to also be passed add an`args`property:
```js
```js
isIn:{
isIn:{
...
@@ -300,19 +305,52 @@ isIn: {
...
@@ -300,19 +305,52 @@ isIn: {
}
}
```
```
When using custom validator functions the error message will be whatever message the thrown`Error`object holds.
When using custom validator functions the error message will be whatever message the thrown`Error`object holds.
See [the validator.js project][3] for more details on the built in validation methods.
See [the validator.js project][3] 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.
**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.
### Per-attribute validators and `allowNull`
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`):
### Validators and `allowNull`
```js
constUser=sequelize.define('user',{
username:{
type:Sequelize.STRING,
allowNull:true,
validate:{
len:[5,10]
}
}
});
```
If a particular field of a model is set to allow null (with `allowNull: true`) and that value has been set to `null` , its validators do not run.
You also can conditionally allow `null` values, with a custom validator, since it won't be skipped:
This means you can, for instance, have a string field which validates its length to be at least 5 characters, but which also allows `null`.
```js
constUser=sequelize.define('user',{
age:Sequelize.INTEGER,
name:{
type:Sequelize.STRING,
allowNull:true,
validate:{
customValidator:function(value){
if(value===null&&this.age!==10){
thrownewError("name can't be null unless age is 10");
}
})
}
}
});
```
You can customize `allowNull` error message by setting `notNull` validator, like this
You can customize `allowNull` error message by setting the `notNull` validator:
```js
```js
constUser=sequelize.define('user',{
constUser=sequelize.define('user',{
...
@@ -328,7 +366,7 @@ const User = sequelize.define('user', {
...
@@ -328,7 +366,7 @@ const User = sequelize.define('user', {
});
});
```
```
### Model validations
### Model-wide validations
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.
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.
...
@@ -374,6 +412,8 @@ In this simple case an object fails validation if either latitude or longitude i
...
@@ -374,6 +412,8 @@ In this simple case an object fails validation if either latitude or longitude i
}
}
```
```
Such validation could have also been done with a custom validator defined on a single attribute (such as the `latitude` attribute, by checking `(value === null) !== (this.longitude === null)`), but the model-wide validation approach is cleaner.
## Configuration
## Configuration
You can also influence the way Sequelize handles your column names:
You can also influence the way Sequelize handles your column names:
@@ -26,6 +26,10 @@ With v5 Sequelize now use `sequelize-pool` which is a modernized fork of `generi
...
@@ -26,6 +26,10 @@ With v5 Sequelize now use `sequelize-pool` which is a modernized fork of `generi
### Model
### Model
**Validators**
Custom validators defined per attribute (as opposed to the custom validators defined in the model's options) now run when the attribute's value is `null` and `allowNull` is `true` (while previously they didn't run and the validation succeeded immediately). To avoid problems when upgrading, please check all your custom validators defined per attribute, where `allowNull` is `true`, and make sure all these validators behave correctly when the value is `null`. See [#9143](https://github.com/sequelize/sequelize/issues/9143).
**Attributes**
**Attributes**
`Model.attributes` now removed, use `Model.rawAttributes`. [#5320](https://github.com/sequelize/sequelize/issues/5320)
`Model.attributes` now removed, use `Model.rawAttributes`. [#5320](https://github.com/sequelize/sequelize/issues/5320)
* @param {string} [attributes.column.onDelete] What should happen when the referenced key is deleted. One of CASCADE, RESTRICT, SET DEFAULT, SET NULL or NO ACTION
* @param {string} [attributes.column.onDelete] What should happen when the referenced key is deleted. One of CASCADE, RESTRICT, SET DEFAULT, SET NULL or NO ACTION
* @param {Function} [attributes.column.get] Provide a custom getter for this column. Use `this.getDataValue(String)` to manipulate the underlying values.
* @param {Function} [attributes.column.get] Provide a custom getter for this column. Use `this.getDataValue(String)` to manipulate the underlying values.
* @param {Function} [attributes.column.set] Provide a custom setter for this column. Use `this.setDataValue(String, Value)` to manipulate the underlying values.
* @param {Function} [attributes.column.set] Provide a custom setter for this column. Use `this.setDataValue(String, Value)` to manipulate the underlying values.
* @param {Object} [attributes.column.validate] An object of validations to execute for this column every time the model is saved. Can be either the name of a validation provided by validator.js, a validation function provided by extending validator.js (see the `DAOValidator` property for more details), or a custom validation function. Custom validation functions are called with the value of the field, and can possibly take a second callback argument, to signal that they are asynchronous. If the validator is sync, it should throw in the case of a failed validation, it it is async, the callback should be called with the error text.
* @param {Object} [attributes.column.validate] An object of validations to execute for this column every time the model is saved. Can be either the name of a validation provided by validator.js, a validation function provided by extending validator.js (see the `DAOValidator` property for more details), or a custom validation function. Custom validation functions are called with the value of the field and the instance itself as the `this` binding, and can possibly take a second callback argument, to signal that they are asynchronous. If the validator is sync, it should throw in the case of a failed validation; if it is async, the callback should be called with the error text.
* @param {Object} options These options are merged with the default define options provided to the Sequelize constructor
* @param {Object} options These options are merged with the default define options provided to the Sequelize constructor
* @param {Object} options.sequelize Define the sequelize instance to attach to the new Model. Throw error if none is provided.
* @param {Object} options.sequelize Define the sequelize instance to attach to the new Model. Throw error if none is provided.
* @param {string} [options.modelName] Set name of the model. By default its same as Class name.
* @param {string} [options.modelName] Set name of the model. By default its same as Class name.