Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/titarenko/wrong
Another JS object validator with focus on simplicity.
https://github.com/titarenko/wrong
Last synced: about 1 month ago
JSON representation
Another JS object validator with focus on simplicity.
- Host: GitHub
- URL: https://github.com/titarenko/wrong
- Owner: titarenko
- Created: 2014-07-17T11:53:25.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-10-14T09:11:21.000Z (about 10 years ago)
- Last Synced: 2024-11-30T15:57:14.645Z (about 1 month ago)
- Language: JavaScript
- Size: 180 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# wrong
Another JS object validation module with focus on simplicity.
# API
## buildValidator(rules)
Builds function which promises validation errors for given object.
```js
var validate = buildValidator({
name: ['required'],
cash: ['required', 'positive number']
});var user = {
name: '',
cash: -10
};validate(user).then(function (errors) {
console.log('User is not valid!', errors);
});
```## rules
Contains hash of rules: key is name, value is validation function.
Validation function has following signature: `function (value, fieldName, wholeObject)`. It must promise error or return nothing if value is valid. Note: context (`this`) of validate function will be applied to each field validator, so you can use it to pass additional information, like current user.
## arrayValidator(itemValidator)
Constructs array value validator by taking item validator as parameter.
## existanceValidator(finder, [message])
Constructs existance validator (value must be an ID of existing object).
# License
BSD