Skip to content

Replace validationChain for form validation #595

Open
@tomlynchRNA

Description

Several critical issues:

Type confusion:


			if (validations.lengthMin > 0 && (item == null || item.length < validations.lengthMin)) {
				error = `${fieldDescription} is too short`;
			}

Will allow a single element array with a very long string as its only element to pass, when its supposed to check the length of a string. Either add stringLengthMin or you have to put it in combination with an ofType, but that requires a developer to know the nuances of validation chain.

Uncaught errors because of using e.g. regex match on non-string types, .startsWith/endsWith on non string types:

			if (
				validations.regexMatchAll &&
				item != null &&
				validations.regexMatchAll instanceof RegExp &&
				(!Array.isArray(item) ||
					item.length <= 0 ||
					!item.every(x => validations.regexMatchAll.test(x)))
			) {
				error = `${fieldDescription} does not match regular expression ${validations.regexMatchAll.toString()}`;
			}
			if (
				validations.startsWith &&
				validations.startsWith.length > 0 &&
				(item == null || !item.startsWith(validations.startsWith))
			) {
				error = `${fieldDescription} does not start with ${validations.startsWith}`;
			}

Overcomplicted bloat:

snip
function getField(object: any, fieldName: string, allowSplit = false) {
	if (fieldName === PARENT_OBJECT_FIELD_NAME) {
		return object;
	} else if (allowSplit) {
		const keys = fieldName.split('.');
		let parentObject = object,
			value,
			lastIndex = keys.length - 1;

		for (let i = 0; i < keys.length; i++) {
			const key = keys[i];
			value = parentObject[key];
			parentObject = value;
			if (i < lastIndex && typeof value != 'object') {
				return undefined;
			}
		}
		return value;
	} else {
		return object[fieldName];
	}
}

replace with:

function getField(object, fieldName, allowSplit = false) {
	if (allowSplit) {
		return fieldName.split('.').reduce((o, key) => o?.[key], object);
	} else {
		return object[fieldName];
	}
}

alternatives:

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions