validation
Use the validation
helper to validate object
import React from 'react';
import { validation as validationHelper } from '@myunisoft/design-system';
import _isEmpty from 'lodash/isEmpty';
const { validate, pipe, required, min } = validationHelper;
const schema = {
name: pipe(required()),
firstname: pipe(required()),
age: pipe(required(), min(18))
}
export const onSubmit = (formData) => {
const errors = validate(formData, schema)
if (!_isEmpty(errors)) {
return false;
}
await save(formData);
}
Validators
required: (message?: string)
date: (message?: string)
after: (beforeKey: string, message?: string)
max: (limit: number, message?: string)
min: (limit: number, message?: string)
positive: (message?: string)
Custom validator
You can create your own validator
const between = (min, max, message) => (obj, key) => {
if (
_get(obj, key) < min || _get(obj, key) > max
) {
return (
message || `Value should be between ${min} and ${max} `
);
}
};
const rules = {
age: pipe(required(), between(18, 62))
}