new Validatable()
This class provides methods that add validation cababilities to models.
Each of the validations runs when the `obj.isValid()` method is called.
All of the methods have an options object parameter that has a
`message` property. When there is only a single error message, this property is just a string;
for example: `Post.validatesPresenceOf('title', { message: 'can not be blank' });`
In more complicated cases it can be a set of messages, for each possible error condition; for example:
`User.validatesLengthOf('password', { min: 6, max: 20, message: {min: 'too short', max: 'too long'}});`
- Source:
Members
(static) validate
Validate using custom validation function.
Example:
```javascript
User.validate('name', customValidator, {message: 'Bad name'});
function customValidator(err) {
if (this.name === 'bad') err();
});
var user = new User({name: 'Peter'});
user.isValid(); // true
user.name = 'bad';
user.isValid(); // false
```
Properties:
Name | Type | Description |
---|---|---|
message |
String | Optional error message if property is not valid. Default error message: " is invalid". |
allowNull |
Boolean | Whether null values are allowed. |
- Source:
(static) validateAsync
Validate using custom asynchronous validation function.
Example:
```js
User.validateAsync('name', customValidator, {message: 'Bad name'});
function customValidator(err, done) {
process.nextTick(function () {
if (this.name === 'bad') err();
done();
});
});
var user = new User({name: 'Peter'});
user.isValid(); // false (because async validation setup)
user.isValid(function (isValid) {
isValid; // true
})
user.name = 'bad';
user.isValid(); // false
user.isValid(function (isValid) {
isValid; // false
})
```
Properties:
Name | Type | Description |
---|---|---|
message |
String | Optional error message if property is not valid. Default error message: " is invalid". |
allowNull |
Boolean | Whether null values are allowed. |
- Source:
(static) validatesAbsenceOf
Validate absence of one or more specified properties.
A model should not include a property to be considered valid; fails when validated field is not blank.
For example, validate absence of reserved
```
Post.validatesAbsenceOf('reserved', { unless: 'special' });
```
Properties:
Name | Type | Description |
---|---|---|
message |
String | Error message to use instead of default. |
if |
String | Validate only if `if` exists. |
unless |
String | Validate only if `unless` exists. |
- Source:
(static) validatesDateOf
Validate if a value for a property is a Date.
Example
```
User.validatesDateOf('today', {message: 'today is not a date!'});
```
Properties:
Name | Type | Description |
---|---|---|
message |
String | Error message to use instead of default. |
- Source:
(static) validatesExclusionOf
Validate exclusion in a set.
Require a property value not be in the specified array.
Example: `Company.validatesExclusionOf('domain', {in: ['www', 'admin']});`
Properties:
Name | Type | Description |
---|---|---|
in |
Array | Property must not match any of the values in the array to be valid. |
message |
String | Optional error message if property is not valid. Default error message: "is reserved". |
allowNull |
Boolean | Whether null values are allowed. |
- Source:
(static) validatesFormatOf
Validate format.
Require a model to include a property that matches the given format.
Example: `User.validatesFormatOf('name', {with: /\w+/});`
Properties:
Name | Type | Description |
---|---|---|
with |
RegExp | Regular expression to validate format. |
message |
String | Optional error message if property is not valid. Default error message: " is invalid". |
allowNull |
Boolean | Whether null values are allowed. |
- Source:
(static) validatesInclusionOf
Validate inclusion in set.
Require a value for property to be in the specified array.
Example:
```
User.validatesInclusionOf('gender', {in: ['male', 'female']});
User.validatesInclusionOf('role', {
in: ['admin', 'moderator', 'user'], message: 'is not allowed'
});
```
Properties:
Name | Type | Description |
---|---|---|
in |
Array | Property must match one of the values in the array to be valid. |
message |
String | Optional error message if property is not valid. Default error message: "is not included in the list". |
allowNull |
Boolean | Whether null values are allowed. |
- Source:
(static) validatesLengthOf
Validate length.
Require a property length to be within a specified range.
There are three kinds of validations: min, max, is.
Default error messages:
- min: too short
- max: too long
- is: length is wrong
Example: length validations
```
User.validatesLengthOf('password', {min: 7});
User.validatesLengthOf('email', {max: 100});
User.validatesLengthOf('state', {is: 2});
User.validatesLengthOf('nick', {min: 3, max: 15});
```
Example: length validations with custom error messages
```
User.validatesLengthOf('password', {min: 7, message: {min: 'too weak'}});
User.validatesLengthOf('state', {is: 2, message: {is: 'is not valid state name'}});
```
Properties:
Name | Type | Description |
---|---|---|
is |
Number | Value that property must equal to validate. |
min |
Number | Value that property must be less than to be valid. |
max |
Number | Value that property must be less than to be valid. |
message |
Object | Optional object with string properties for custom error message for each validation: is, min, or max. |
- Source:
(static) validatesNumericalityOf
Validate numericality.
Requires a value for property to be either an integer or number.
Example
```
User.validatesNumericalityOf('age', { message: { number: 'is not a number' }});
User.validatesNumericalityOf('age', {int: true, message: { int: 'is not an integer' }});
```
Properties:
Name | Type | Description |
---|---|---|
int |
Boolean | If true, then property must be an integer to be valid. |
allowBlank |
Boolean | Allow property to be blank. |
allowNull |
Boolean | Allow property to be null. |
message |
Object | Optional object with string properties for 'int' for integer validation. Default error messages: - number: is not a number - int: is not an integer |
- Source:
(static) validatesPresenceOf
Validate presence of one or more specified properties.
Requires a model to include a property to be considered valid; fails when validated field is blank.
For example, validate presence of title
```
Post.validatesPresenceOf('title');
```
Validate that model has first, last, and age properties:
```
User.validatesPresenceOf('first', 'last', 'age');
```
Example with custom message
```
Post.validatesPresenceOf('title', {message: 'Cannot be blank'});
```
Properties:
Name | Type | Description |
---|---|---|
message |
String | Error message to use instead of default. |
if |
String | Validate only if `if` exists. |
unless |
String | Validate only if `unless` exists. |
- Source:
(static) validatesUniquenessOf
Validate uniqueness of the value for a property in the collection of models.
Not available for all connectors. Currently supported with these connectors:
- In Memory
- Oracle
- MongoDB
```
// The login must be unique across all User instances.
User.validatesUniquenessOf('login');
// Assuming SiteUser.belongsTo(Site)
// The login must be unique within each Site.
SiteUser.validateUniquenessOf('login', { scopedTo: ['siteId'] });
```
Properties:
Name | Type | Description |
---|---|---|
with |
RegExp | Regular expression to validate format. |
scopedTo |
Array.<String> | List of properties defining the scope. |
message |
String | Optional error message if property is not valid. Default error message: "is not unique". |
allowNull |
Boolean | Whether null values are allowed. |
ignoreCase |
String | Make the validation case insensitive. |
if |
String | Validate only if `if` exists. |
unless |
String | Validate only if `unless` exists. |
- Source: