Skip to content

Instantly share code, notes, and snippets.

@brandonsueur
Created February 17, 2021 04:52
Show Gist options
  • Save brandonsueur/036a169542915612ff561a1344d5c000 to your computer and use it in GitHub Desktop.
Save brandonsueur/036a169542915612ff561a1344d5c000 to your computer and use it in GitHub Desktop.
rules form
import isEmpty from 'lodash.isempty';
import validate from './validator';
import i18n from '../translations/i18n';
export const nullable = () => {
return (value) => [true, null, null != value && 0 < value.length];
};
export const required = () => {
return (value) => [null != value && 0 < value?.toString()?.length, i18n.t('Errors.validation.required'), true];
};
export const email = () => {
const regex = /^(([^<>()[\]\\.,;:\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 (value) => [regex.test(value), i18n.t('Errors.validation.email'), true];
};
export const minLength = (min) => {
return (value) => [value.length >= min, i18n.t('Errors.validation.minLength', { min }), true];
};
export const maxLength = (max) => {
return (value) => [value.length <= max, i18n.t('Errors.validation.maxLength', { max }), true];
};
export const length = (excepted) => {
return (value) => [value.length === excepted, i18n.t('Errors.validation.length', { excepted }), true];
};
export const oneOf = (items) => {
return (value) => [items.includes(value), i18n.t('Errors.validation.oneOf'), true];
};
export const numeric = () => {
return (value) => [/^-?\d+\.?\d*$/.test(value), i18n.t('Errors.validation.numeric'), true];
};
export const positive = () => {
return (value) => [0 <= value, i18n.t('Errors.validation.positive'), true];
};
export const phone = () => {
return (value) => {
// eslint-disable-next-line no-param-reassign
value = value?.replace(/ /g, '');
return [/^[+](\d{3})\)?(\d{3})(\d{5,6})$|^(\d{10})$/.test(value), i18n.t('Errors.validation.phone'), true];
};
};
export const same = (otherField, message) => {
return (value, data) => [value === data[otherField], message ?? i18n.t('Errors.validation.same'), true];
};
export const when = (cond, then, otherwise) => {
return (value, data) => {
if (cond(value, data)) {
return then(value, data);
}
return otherwise(value, data);
};
};
export const date = () => {
const regex = /^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
return (value) => [regex.test(value), i18n.t('Errors.validation.date'), true];
};
export const boolean = () => {
// eslint-disable-next-line eqeqeq
return (value) => [true == value || false == value, i18n.t('Errors.validation.boolean'), true];
};
export const arrayOf = (rules) => {
return (value) => {
if (!Array.isArray(value)) {
return [false, i18n.t('Errors.validation.arrayOf'), false];
}
let isValid = true;
const errors = {};
value.forEach((item, index) => {
const itemErrors = validate(item, rules);
if (!isEmpty(itemErrors)) {
errors[index] = itemErrors;
isValid = false;
}
});
return [isValid, errors, false];
};
};
export default {
nullable,
required,
email,
minLength,
maxLength,
same,
length,
oneOf,
numeric,
phone,
date,
when,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment