From 9e5375655117712f6bb3a6636b5ffa766c937b5e Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 13 Mar 2025 20:25:14 +0000 Subject: [PATCH] Add validation and tests for NLC and STANOX --- src/inputValidation/inputValidation.test.ts | 63 ++++++++++++++++++++- src/inputValidation/inputValidation.ts | 39 ++++++++++++- 2 files changed, 100 insertions(+), 2 deletions(-) diff --git a/src/inputValidation/inputValidation.test.ts b/src/inputValidation/inputValidation.test.ts index 531fcae..3d334ae 100644 --- a/src/inputValidation/inputValidation.test.ts +++ b/src/inputValidation/inputValidation.test.ts @@ -4,7 +4,9 @@ import { validateCrs, validateReasonCode, validateTiploc, validateUuid, - validateHeadcode } from "./inputValidation"; + validateHeadcode, + validateNlc, + validateStanox} from "./inputValidation"; import { ValidationError } from "../errors"; describe("PIS Validation Tests", () => { @@ -59,6 +61,65 @@ describe("CRS Validation Tests", () => { }) }) +describe("NLC Validation Tests", () => { + test("NLC inputs that are valid should return true", () => { + expect(validateNlc(354300)).toBe(true); + expect(validateNlc("322962")).toBe(true); + expect(validateNlc(999999)).toBe(true); + expect(validateNlc("999999")).toBe(true); + expect(validateNlc(100000)).toBe(true); + expect(validateNlc("100000")).toBe(true); + }) + + test("NLC inputs that are out of range should throw ValidationError", () => { + expect(() => validateNlc(3543)).toThrow(ValidationError); + expect(() => validateNlc("3229")).toThrow(ValidationError); + expect(() => validateNlc(3543001)).toThrow(ValidationError); + expect(() => validateNlc("3543001")).toThrow(ValidationError); + expect(() => validateNlc(99999)).toThrow(ValidationError); + expect(() => validateNlc(1000001)).toThrow(ValidationError); + expect(() => validateNlc("99999")).toThrow(ValidationError); + expect(() => validateNlc("1000001")).toThrow(ValidationError); + }) + + test("NLC inputs that are not numeric should throw ValidationError", () => { + expect(() => validateNlc([])).toThrow(ValidationError); + expect(() => validateNlc({})).toThrow(ValidationError); + expect(() => validateNlc(null)).toThrow(ValidationError); + expect(() => validateNlc(undefined)).toThrow(ValidationError); + expect(() => validateNlc(false)).toThrow(ValidationError); + expect(() => validateNlc("3543ab")).toThrow(ValidationError); + }) +}) + +describe("STANOX Validation Tests", () => { + test("STANOX inputs of the correct format should return true", () => { + expect(validateStanox(11234)).toBe(true); + expect(validateStanox("11234")).toBe(true); + expect(validateStanox("99999")).toBe(true); + expect(validateStanox(10000)).toBe(true); + expect(validateStanox("10000")).toBe(true); + expect(validateStanox("01234")).toBe(true); + }) + + test("STANOX inputs that are out of range should throw Validation Error", () => { + expect(() => validateStanox(9999)).toThrow(ValidationError); + expect(() => validateStanox("9999")).toThrow(ValidationError); + expect(() => validateStanox(0o1234)).toThrow(ValidationError); + expect(() => validateStanox("100000")).toThrow(ValidationError); + expect(() => validateStanox(100000)).toThrow(ValidationError); + }) + + test("STANOX inputs that are not numeric should throw ValidationError", () => { + expect(() => validateStanox([])).toThrow(ValidationError); + expect(() => validateStanox({})).toThrow(ValidationError); + expect(() => validateStanox(null)).toThrow(ValidationError); + expect(() => validateStanox(undefined)).toThrow(ValidationError); + expect(() => validateStanox(false)).toThrow(ValidationError); + expect(() => validateStanox("3543ab")).toThrow(ValidationError); + }) +}) + describe("UUID Validation Tests", () => { test("UUID inputs that are valid v4-UUIDs should return true", () => { expect(validateUuid("5c5db50b-91c8-440c-80af-afc7bb375b4b")).toBe(true); diff --git a/src/inputValidation/inputValidation.ts b/src/inputValidation/inputValidation.ts index 2626503..120016b 100644 --- a/src/inputValidation/inputValidation.ts +++ b/src/inputValidation/inputValidation.ts @@ -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; -} \ No newline at end of file +} +