Remove validation methods, now covered in JSDoc.
This commit is contained in:
parent
e1889ff8eb
commit
d58a2f0938
|
@ -442,145 +442,6 @@ appearing in many groups, you could declare the models this way,
|
|||
user.groups.add(group, callback); // connect an existing group with the user
|
||||
user.groups.remove(group, callback); // remove the user from the group
|
||||
```
|
||||
|
||||
### Validations
|
||||
|
||||
#### Model.validatesFormatOf(property, options)
|
||||
|
||||
Require a model to include a property that matches the given format.
|
||||
|
||||
```js
|
||||
User.validatesFormat('name', {with: /\w+/});
|
||||
```
|
||||
|
||||
#### Model.validatesPresenceOf(properties...)
|
||||
|
||||
Require a model to include a property to be considered valid.
|
||||
|
||||
```js
|
||||
User.validatesPresenceOf('first', 'last', 'age');
|
||||
```
|
||||
|
||||
#### Model.validatesLengthOf(property, options)
|
||||
|
||||
Require a property length to be within a specified range.
|
||||
|
||||
```js
|
||||
User.validatesLengthOf('password', {min: 5, message: {min: 'Password is too short'}});
|
||||
```
|
||||
|
||||
#### Model.validatesInclusionOf(property, options)
|
||||
|
||||
Require a value for `property` to be in the specified array.
|
||||
|
||||
```js
|
||||
User.validatesInclusionOf('gender', {in: ['male', 'female']});
|
||||
```
|
||||
|
||||
#### Model.validatesExclusionOf(property, options)
|
||||
|
||||
Require a value for `property` to not exist in the specified array.
|
||||
|
||||
```js
|
||||
User.validatesExclusionOf('domain', {in: ['www', 'billing', 'admin']});
|
||||
```
|
||||
|
||||
#### Model.validatesNumericalityOf(property, options)
|
||||
|
||||
Require a value for `property` to be a specific type of `Number`.
|
||||
|
||||
```js
|
||||
User.validatesNumericalityOf('age', {int: true});
|
||||
```
|
||||
|
||||
#### Model.validatesUniquenessOf(property, options)
|
||||
|
||||
Ensure the value for `property` is unique in the collection of models.
|
||||
|
||||
```js
|
||||
User.validatesUniquenessOf('email', {message: 'email is not unique'});
|
||||
```
|
||||
|
||||
**Note:** not available for all [connectors](#connectors).
|
||||
|
||||
Currently supported in these connectors:
|
||||
|
||||
- [In Memory](#memory-connector)
|
||||
- [Oracle](http://github.com/strongloop/loopback-connector-oracle)
|
||||
- [MongoDB](http://github.com/strongloop/loopback-connector-mongodb)
|
||||
|
||||
#### myModel.isValid()
|
||||
|
||||
Validate the model instance.
|
||||
|
||||
```js
|
||||
user.isValid(function (valid) {
|
||||
if (!valid) {
|
||||
console.log(user.errors);
|
||||
// => hash of errors
|
||||
// => {
|
||||
// => username: [errmessage, errmessage, ...],
|
||||
// => email: ...
|
||||
// => }
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
#### loopback.ValidationError
|
||||
|
||||
`ValidationError` is raised when the application attempts to save an invalid
|
||||
model instance.
|
||||
|
||||
Example:
|
||||
|
||||
```js
|
||||
{
|
||||
"name": "ValidationError",
|
||||
"status": 422,
|
||||
"message": "The Model instance is not valid. \
|
||||
See `details` property of the error object for more info.",
|
||||
"statusCode": 422,
|
||||
"details": {
|
||||
"context": "user",
|
||||
"codes": {
|
||||
"password": [
|
||||
"presence"
|
||||
],
|
||||
"email": [
|
||||
"uniqueness"
|
||||
]
|
||||
},
|
||||
"messages": {
|
||||
"password": [
|
||||
"can't be blank"
|
||||
],
|
||||
"email": [
|
||||
"Email already exists"
|
||||
]
|
||||
}
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
You might run into situations where you need to raise a validation error
|
||||
yourself, for example in a "before" hook or a custom model method.
|
||||
|
||||
```js
|
||||
MyModel.prototype.preflight = function(changes, callback) {
|
||||
// Update properties, do not save to db
|
||||
for (var key in changes) {
|
||||
model[key] = changes[key];
|
||||
}
|
||||
|
||||
if (model.isValid()) {
|
||||
return callback(null, { success: true });
|
||||
}
|
||||
|
||||
// This line shows how to create a ValidationError
|
||||
err = new ValidationError(model);
|
||||
callback(err);
|
||||
}
|
||||
```
|
||||
|
||||
### Shared methods
|
||||
|
||||
|
|
Loading…
Reference in New Issue