https://github.com/rodrigosaint/kaizen-validation
A simple and flexible javascript validation that allows you to test both on backend and frontend.
https://github.com/rodrigosaint/kaizen-validation
backend extensible flexible frontend javascript no-dependencies simple validation
Last synced: about 1 year ago
JSON representation
A simple and flexible javascript validation that allows you to test both on backend and frontend.
- Host: GitHub
- URL: https://github.com/rodrigosaint/kaizen-validation
- Owner: RodrigoSaint
- Created: 2018-01-31T11:40:20.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-05-29T16:04:59.000Z (about 8 years ago)
- Last Synced: 2025-01-24T19:17:55.991Z (over 1 year ago)
- Topics: backend, extensible, flexible, frontend, javascript, no-dependencies, simple, validation
- Language: JavaScript
- Size: 41 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Kaizen validation 
Kaizen validation is a javascript package designed to be simple and flexible. It relies on simple javascript functions so you can use it on frontend or backend and with any kind of framework.
To create a validation using it you only need to import it and invoke the validate method. This method receive two arguments, the validation constraint and the model to be validated. The constraint is a object that should have the attributes that you want to validate on the model and their value should be a array of functions that receive a value and return a string if has a error or undefined if is valid. If all validations pass undefined will be returned.
For example:
```javascript
const validationDefinition = {name: [ name => name == undefined? "The name is required": undefined ]};
const modelToValidate = {name: undefined};
const result = validate(validationDefinition, modelToValidate);
//the result will return a object with the attribute name containing a array with "The name is required" message
```
If you want to create a validation that can depends on another argument you may do it by [currying](https://en.wikipedia.org/wiki/Currying).
For example:
```javascript
export const maxLength = length =>
(property => property !== undefined && property.length > length? lengthError : undefined);
```
This maxLength function depends on the length argument, when it is invoked it will return a new funcion that can be used on the validation.
This package also have some built-in validations, but I encourage you to create your own if they not attend. At current time they are:
- **isRequired** - It will validate if the property is undefined.
- **minLength -** It will validate property's minimum length. It should receive a number as parameter.
- **maxLength -** It will validate property's maximum length. It should receive a number as parameter.
- **greatherThan** - It will validate if the property is bigger than its parameter. It should receive a number as parameter.
- **smallerThan** - It will validate if the property is smaller than its parameter. It should receive a number as parameter.
- **containsAtArray**- It will validate if a value is at a array. It should receive a array as parameter.
You may create PRs with new generic validations.