/**
 * These validations are the be used with the universial <Input /> component.
 */ import { Validation , ValidationFn } from "./types" ; export async function checkValidations ( validations = [ ] , value ) { const errors = [ ] ; for ( let i = 0 , l = validations . length ; i < l ; i ++ ) { const valid = await validations [ i ] . isValid ( value ) ; if ( ! valid ) { errors . push ( validations [ i ] . ifNotMessage ) ; } } return errors ; } export const validations = { required : ( customMessage ? ) => { return { isValid : ( v ) => ! ! v . length , ifNotMessage : customMessage || "Field required." } ; } , email : ( customMessage ? ) => { return { isValid : ( v ) => { const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ ; return re . test ( v ) ; } , ifNotMessage : customMessage || "A valid email address is required." } ; } , postal : ( customMessage ? ) => { return { isValid : ( v ) => /(^\d{5}$)|(^\d{5}-\d{4}$)/ . test ( v ) , ifNotMessage : customMessage || "A valid zip code is required." } ; } } ;