Add headcode validation and test
All checks were successful
Run Jest tests / run-tests (push) Successful in 4m4s
All checks were successful
Run Jest tests / run-tests (push) Successful in 4m4s
This commit is contained in:
parent
196cc8783b
commit
827014ea49
@ -3,7 +3,8 @@ import { validateCrs,
|
|||||||
validatePisCode,
|
validatePisCode,
|
||||||
validateReasonCode,
|
validateReasonCode,
|
||||||
validateTiploc,
|
validateTiploc,
|
||||||
validateUuid } from "./inputValidation";
|
validateUuid,
|
||||||
|
validateHeadcode } from "./inputValidation";
|
||||||
import { ValidationError } from "../errors";
|
import { ValidationError } from "../errors";
|
||||||
|
|
||||||
describe("PIS Validation Tests", () => {
|
describe("PIS Validation Tests", () => {
|
||||||
@ -108,3 +109,35 @@ describe("Reason Code Validation Tests", () => {
|
|||||||
expect(() => validateReasonCode({})).toThrow(ValidationError);
|
expect(() => validateReasonCode({})).toThrow(ValidationError);
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("Headcode Validation Tests", () => {
|
||||||
|
test("Headcodes of the valid format should return true", () => {
|
||||||
|
expect(validateHeadcode("1A23")).toBe(true);
|
||||||
|
expect(validateHeadcode("5t09")).toBe(true);
|
||||||
|
expect(validateHeadcode("0z70")).toBe(true);
|
||||||
|
expect(validateHeadcode("4A10")).toBe(true);
|
||||||
|
expect(validateHeadcode("0m00")).toBe(true);
|
||||||
|
expect(validateHeadcode("9I94")).toBe(true);
|
||||||
|
expect(validateHeadcode("5u41")).toBe(true);
|
||||||
|
expect(validateHeadcode("5U34")).toBe(true);
|
||||||
|
expect(validateHeadcode("1F34")).toBe(true);
|
||||||
|
})
|
||||||
|
|
||||||
|
test("Headcodes of an invalid format should throw ValidationError", () => {
|
||||||
|
expect(() => validateHeadcode("A212")).toThrow(ValidationError);
|
||||||
|
expect(() => validateHeadcode("a433")).toThrow(ValidationError);
|
||||||
|
expect(() => validateHeadcode(" 1F43 ")).toThrow(ValidationError);
|
||||||
|
expect(() => validateHeadcode("f43a")).toThrow(ValidationError);
|
||||||
|
expect(() => validateHeadcode("&ate")).toThrow(ValidationError);
|
||||||
|
expect(() => validateHeadcode("")).toThrow(ValidationError);
|
||||||
|
})
|
||||||
|
|
||||||
|
test("Headcode inputs that are not strings should throw ValidationError", () => {
|
||||||
|
expect(() => validateHeadcode(4322)).toThrow(ValidationError);
|
||||||
|
expect(() => validateHeadcode([])).toThrow(ValidationError);
|
||||||
|
expect(() => validateHeadcode({})).toThrow(ValidationError);
|
||||||
|
expect(() => validateHeadcode(undefined)).toThrow(ValidationError);
|
||||||
|
expect(() => validateHeadcode(null)).toThrow(ValidationError);
|
||||||
|
expect(() => validateHeadcode(true)).toThrow(ValidationError);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
@ -67,4 +67,15 @@ export function validateReasonCode(code: unknown): boolean {
|
|||||||
|
|
||||||
// If it's neither a number nor a string, throw an error
|
// If it's neither a number nor a string, throw an error
|
||||||
throw new ValidationError("Invalid input: Reason code must be a number or a string of three digits.");
|
throw new ValidationError("Invalid input: Reason code must be a number or a string of three digits.");
|
||||||
|
}
|
||||||
|
|
||||||
|
export function validateHeadcode(headcode: unknown): boolean {
|
||||||
|
if (typeof headcode !== "string") {
|
||||||
|
throw new ValidationError("Invalid input: Headcode must be a string");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!/^\d[A-Za-z]\d{2}$/.test(headcode)) {
|
||||||
|
throw new ValidationError("Invalid input: Headcode must be in the format dLdd where d=digit and L=letter");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user