Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/boo1ean/app-validation
Promise based validation engine
https://github.com/boo1ean/app-validation
Last synced: 30 days ago
JSON representation
Promise based validation engine
- Host: GitHub
- URL: https://github.com/boo1ean/app-validation
- Owner: boo1ean
- Created: 2014-10-28T13:36:09.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2014-11-24T20:53:06.000Z (almost 10 years ago)
- Last Synced: 2024-10-04T06:05:38.458Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 121 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## app-validation
Part of app-helpers project.
Promise-based validation engine.
## Installation
```
npm install app-validation
```## Usage
```javascript
var createValidator = require('app-validation');// Create user validation function
var validateUser = createValidator({
email: ['required', 'email', 'only gmail'],
password: ['required', 'longer than 5'],
type: ['required', 'available user types']
});// Sample user object
var user = {
email: '[email protected]',
password: 'test',
type: 'admin'
};// Validate user object and return promise
validateUser(user)// If validation passes successfully, resolves promise with given data object
.then(function (user) {})
// If validation fails -> rejection with validation errors object
.catch(function (errors) {
// Fits well with app-controller
});
```## Define custom rules
Rule is a simple function that takes 3 aprams: `value` to validate,
`field` name of currently validating object property and `context` - whole validating data object.```javascript
v.extendRules({
'valid name': function (value, field, context) {
// ...
}
});var validate = v({
name: ['valid name'],
level: ['positive number']
});validate({
name: 'John',
level: 10
});// Will emit 'valid name' rule with params ('John', 'name', { name: 'John', level: 10 })
```To emit failure rule function must return error message otherwise any falsy value or nothing.
```javascript
var v = require('app-validation');v.extendRules({
'only gmail': function (value) {
if (value.indexOf('@gmail') === -1) {
return 'Not gmail actually';
}
}
});// Rule could be async
v.extendRules({
'user exists': function (email) {
// Assume users.findByEmail returns promise
return users.findByEmail(email).then(function (user) {
if (!user) {
return 'User does not exist';
}
});
}
});
```## LICENSE
MIT