UNPKG

apphouse

Version:

Component library for React that uses observable state management and theme-able components.

37 lines (35 loc) 864 B
/** * Helper function to validate a field. * @param value the value of the field * @param required whether the field is required or not * @param validation the validation function for the field * @returns boolean if true, the field is valid, otherwise it is not */ export function validateField<T>( /** * The value of the field. */ value: any, /** * Whether the field is required or not. */ required: boolean, /** * A function to validate the value of the field. */ validation?: (value?: T) => boolean ) { // if it is required, let's see if it has a value if (required) { if (value === undefined || value === '' || value === null) { return false; } } // if it has validation, let's see if it pass validation if (validation) { if (!validation(value)) { return false; } } return true; }