https://github.com/danielweinmann/validate-model
Validate model objects with validator.js
https://github.com/danielweinmann/validate-model
Last synced: about 1 year ago
JSON representation
Validate model objects with validator.js
- Host: GitHub
- URL: https://github.com/danielweinmann/validate-model
- Owner: danielweinmann
- License: mit
- Created: 2016-02-22T15:40:02.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2018-03-30T09:12:02.000Z (about 8 years ago)
- Last Synced: 2024-10-11T18:09:19.450Z (over 1 year ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 10
- Watchers: 2
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# validate-model
Validate model objects with [validator.js](https://github.com/chriso/validator.js). Read validator.js documentation to see available validators.
## Installation
```npm install validate-model --save```
## Usage
```js
var ValidateModel = require('validate-model');
var validate = ValidateModel.validate;
var validateAll = ValidateModel.validateAll;
var UserValidators = {
name: {
title: 'Name',
validate: [{
validator: 'isLength',
arguments: [1, 255],
}]
},
email: {
title: 'Email',
validate: [{
validator: 'isLength',
arguments: [20, 255],
message: '{TITLE} is too short'
},
{
validator: 'isEmail',
message: '{TITLE} must be valid'
}]
},
password: {
title: 'Password',
validate: [{
validator: 'isLength',
arguments: [8, 255],
message: '{TITLE} is too short'
}]
}
};
var user = {
name: 'Foo',
email: 'invalid@email',
password: 'short'
};
var nameValidation = validate(UserValidators.name, user.name);
// { valid: true, messages: [] }
var emailValidation = validate(UserValidators.email, user.email);
// { valid: false, messages: ['Email is too short', 'Email must be valid'] }
var passwordValidation = validate(UserValidators.password, user.password);
// { valid: false, messages: ['Password is too short'] }
var userValidation = validateAll(UserValidators, user)
// { valid: false, messages: {email: ['Email is too short', 'Email must be valid'], password: ['Password is too short']}}
```
## Inspiration
This package is inspired by the way [FaridSafi/react-native-gifted-form](https://github.com/FaridSafi/react-native-gifted-form) implements form validation.
## Contributing
Please create issues and send pull requests!
## License
[MIT](LICENSE)