zucchetti-sa5/themes/onepirate/modules/form/validation.js
Arnaud Fauconnet 328a760eb8 added theme
2022-10-28 11:41:58 +02:00

30 lines
816 B
JavaScript

/**
* This is a simplified logic.
* Consider using `import isEmail from 'validator/lib/isEmail'` from
* https://github.com/validatorjs/validator.js/blob/7376945b4ce028b65955ae57b8fccbbf3fe58467/src/lib/isEmail.js
* for a more robust version.
*/
function isEmail(string) {
const re =
/^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})$/i;
return re.test(string);
}
export function email(value) {
return value && !isEmail(value.trim()) ? 'Invalid email' : null;
}
function isDirty(value) {
return value || value === 0;
}
export function required(requiredFields, values) {
return requiredFields.reduce(
(fields, field) => ({
...fields,
...(isDirty(values[field]) ? undefined : { [field]: 'Required' }),
}),
{},
);
}