Skip to content

Instantly share code, notes, and snippets.

@mijms
Created Jul 22, 2021
Embed
What would you like to do?
import * as Validations from 'validation Module'
validationUpdate = ()=>{
// select validation function by key
this.validations = map(this.props.validates, (d, key) => (get(Validations, key, false)(d))).filter(Boolean)
}
validation = (value) => {
// try to validate one by one
const valid = this.validations.reduce((res, fun) => (res || fun(value, {parentValues})), undefined)
return valid;
}
// validates data is become something like
// {
validates: {
required: true,
maxLength: 10
}
}
// max field validation sample inside validations module
export const maxLength = (max) => (value) => {
if (value && value.length > max) {
return `Input cannot be more than ${max} character`;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment