Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve phone & cellphone validation #45

Open
github-actions bot opened this issue Jan 31, 2024 · 0 comments
Open

improve phone & cellphone validation #45

github-actions bot opened this issue Jan 31, 2024 · 0 comments
Labels
components Components pkgs scope enhancement New feature or request help wanted Extra attention is needed todo

Comments

@github-actions
Copy link

// TODO: improve phone & cellphone validation

	// field not empty, validate
	switch (input.type) {
		case eFormType.PHONE:
			// TODO: improve phone & cellphone validation
			return Array.isArray(value) && value[1].toString().length >= 7;
		case eFormType.CELLPHONE:
			return Array.isArray(value) && value[1].toString().length === 10;
		case eFormType.NEW_PASSWORD:
			return Array.isArray(value) && value[0] === value[1];
		case eFormType.EMAIL:
			return typeof value === "string" && isEmail(value);
		default:
			// no validation required, assume true
			return true;
	}
}

/**
 * Check if FormInput value is valid
 *
 * Array.every is truthy for empty arrays
 */
export const isValidFormInputValue = (input: FormInput, ignoreRequired = false): boolean => {
	const { values, multiple, type, required, min, max } = input;

	// When required, false if empty array
	if ((!values || !values.length) && required && !ignoreRequired) return false;

	if (multiple) {
		// Bypass if not required
		// The UI should ensure the limits are not surpased
		if (required && !ignoreRequired) {
			if (values.length < min) return false;
			if (values.length > max) return false;
		}
	} else {
		if (type === eFormType.NUMBER) {
			// Number in range
			return values.every((number) => {
				number = Number(number);

				return number >= min && number <= max;
			});
		} else if (!type || type === eFormType.TEXT) {
			// String length in range
			return values.every((string) => {
				const length = String(string).length;

				return !string || (length >= min && length <= max);
			});
		}
	}

	// The actual values are valid
	const valid = values.every((value) => isValidValue(value, input));
	// All values have content
	const notEmpty = values.every((v) => notEmptyValue(v, input.defaults));

	// if empty but not required then value is truthy
	return valid || (!notEmpty && (!required || ignoreRequired));
};

/** suffixes used on values */
@github-actions github-actions bot added the todo label Jan 31, 2024
@vis97c vis97c added enhancement New feature or request help wanted Extra attention is needed components Components pkgs scope labels May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
components Components pkgs scope enhancement New feature or request help wanted Extra attention is needed todo
Projects
None yet
Development

No branches or pull requests

1 participant