Surname Validation Function Documentation

The validateSurname function is used to validate surnames. It returns an object with two properties: 'isValid' (boolean) and 'errorMsg' (string). The 'isValid' property will be true if the surname meets the specified criteria, and 'errorMsg' will contain the error message if the surname is invalid, or it will be null if the surname is valid.

Function Signature

function validateSurname(
	surname: string,
	{
		minLength?: number,
		maxLength?: number,
		errorMsg?: string[]
	}
): { isValid: boolean, errorMsg: string | null };

Parameters

Default Error Messages

[
	'Invalid value passed',
	'Surname cannot contain numbers',
	'Surname cannot contain special characters',
	'This surname is not valid',
	'Surname too big, try again',
	'Unknown error',
]

Examples

const result1 = validateSurname("Jackson", {
    minLength: 3,
    maxLength: 25,
});
console.log(result1);
// Output: { isValid: true, errorMsg: null }

const customErrorMsg = [null, "Custom error 2"];
const result2 = validateSurname("J@ckson", {
    minLength: 3,
    maxLength: 25,
    errorMsg: customErrorMsg,
});
console.log(result2);
// Output: { isValid: false, errorMsg: 'Surname cannot contain special characters' }