import { FieldValidator } from 'final-form'; import { Nullable } from './types'; export const required = (msg: string) => (value?: any) => (value ? undefined : msg); export const isEmail = (msg: string) => (value?: Nullable) => { if (!value) { return undefined; } return value.match(/^[a-zA-Z0-9.!#$%&’'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/) ? undefined : msg; }; export const minLength = (msg: string, length: number) => (values?: Nullable) => values && values.length >= length ? undefined : msg; export const compose = (...validators: FieldValidator[]): FieldValidator => { return (value, allValues) => { for (const validator of validators) { const result = validator(value, allValues); if (result) { return result; } } }; };