Skip to content

improve phone & cellphone validation #45

Open
@github-actions

Description

@github-actions

// 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 */

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions