Add validation and tests for NLC and STANOX

This commit is contained in:
Fred Boniface
2025-03-13 20:25:14 +00:00
parent 0aca569c31
commit 9e53756551
2 changed files with 100 additions and 2 deletions

View File

@@ -38,6 +38,42 @@ export function validateCrs(crs: unknown): boolean {
return true;
}
export function validateNlc(nlc: unknown): boolean {
if (typeof nlc === "string") {
if (!/^\d{6}$/.test(nlc)) {
throw new ValidationError("Invalid input: NLC must be a 6 digit number (or a string representation)");
}
return true;
}
if (typeof nlc === "number") {
if (!Number.isInteger(nlc) || nlc < 100000 || nlc > 999999) {
throw new ValidationError("Invalid input: NLC must be a 6 digit number (or a string representation)");
}
return true;
}
// If not a string or number, throw ValidationError
throw new ValidationError("Invalid input: NLC must be a 6 digit number (or string representation");
}
export function validateStanox(stanox: unknown): boolean {
if (typeof stanox === "string") {
if (!/^\d{5}$/.test(stanox)) {
throw new ValidationError("Invalid input: STANOX must be a 5 digit number (or a string representation)");
}
return true;
}
if (typeof stanox === "number") {
if (!Number.isInteger(stanox) || stanox < 10000 || stanox > 99999) {
throw new ValidationError("Invalid input: STANOX must be a 5 digit number (or a string representation");
}
return true;
}
// If not a string or number, throw ValidationError
throw new ValidationError("Invalid input: STANOX must be a 5 digit numer (or a string representation)");
}
export function validateUuid(uuid: unknown): boolean {
if (typeof uuid !== "string") {
throw new ValidationError("Invalid input: The UUID/api_key should be a string");
@@ -78,4 +114,5 @@ export function validateHeadcode(headcode: unknown): boolean {
throw new ValidationError("Invalid input: Headcode must be in the format dLdd where d=digit and L=letter");
}
return true;
}
}