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 validateSurname(
surname: string,
{
minLength?: number,
maxLength?: number,
errorMsg?: string[]
}
): { isValid: boolean, errorMsg: string | null };
surname
(string) - The surname to be validated.minLength
(number) [optional] - The minimum length of the surname. Default is 1.maxLength
(number) [optional] - The maximum length of the surname. Default is 25.errorMsg
(string[]) [optional] - An array of error messages to customize the response. If not provided, the function will use 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',
]
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' }