2018-11-30 04:04:56 -06:00
|
|
|
|
import { ValidationRule, ValidationEvents } from 'app/types';
|
|
|
|
|
import { EventsWithValidation } from 'app/core/components/Form/Input';
|
2018-11-30 03:16:04 -06:00
|
|
|
|
|
|
|
|
|
export const validate = (value: string, validationRules: ValidationRule[]) => {
|
|
|
|
|
const errors = validationRules.reduce((acc, currRule) => {
|
|
|
|
|
if (!currRule.rule(value)) {
|
|
|
|
|
return acc.concat(currRule.errorMessage);
|
|
|
|
|
}
|
|
|
|
|
return acc;
|
|
|
|
|
}, []);
|
|
|
|
|
return errors.length > 0 ? errors : null;
|
|
|
|
|
};
|
2018-11-30 04:04:56 -06:00
|
|
|
|
|
|
|
|
|
export const hasValidationEvent = (event: EventsWithValidation, validationEvents: ValidationEvents) => {
|
|
|
|
|
return validationEvents && validationEvents[event];
|
|
|
|
|
};
|