Composite validation API for JS data models. Based on this project idea. This library
npm install composite-validation
For some data model...
const dataModel = {
name: 'Leonardo',
age: 45
};
...describe validation map that folows the structure of the original object.
import { ValidationMap, Conditions, required, equals } from 'composite-validation';
const map = ValidationMap({
name: Conditions(v => required(v)),
age: Conditions([
v => required(v),
v => equals(v, 21)
])
});
Call function.
const result = map(dataModel);
Function applies validation map to data model, check all validity conditions and returns object with validity states for each field.
{
"name": {
"value": "Leonardo",
"isRequired": true
},
"age": {
"error": "Value should be defined",
"isRequired": true
}
}