import { ValidationError } from "../errors"; import { validate as uuidlibValidate, version as uuidlibVersion } from 'uuid' export function validatePisCode(code: unknown): boolean { if (typeof code == "number") { code = code.toString(); } if (typeof code !== "string") { throw new ValidationError("Invalid input: code must be a four digit number or string") } const codeRegex = /^\d{4}$/; if (!codeRegex.test(code)) { throw new ValidationError("Invalid input: code must be a four-character string consisting of only numerals"); } return true; } export function validateTiploc(tiploc: unknown): boolean { if (typeof tiploc !== "string") { throw new ValidationError("Invalid input: TIPLOC must be a string"); } const tiplocRegex = /[a-zA-Z0-9]{4,7}/; if (!tiplocRegex.test(tiploc)) { throw new ValidationError("Invalid input: TIPLOC must be between four and seven characters"); } return true; } export function validateCrs(crs: unknown): boolean { if (typeof crs !== "string") { throw new ValidationError("Invalid input: CRS/3ALPHA must be a string") } const crsRegex = /^[a-zA-Z]{3}$/; if (!crsRegex.test(crs)) { throw new ValidationError("Invalid input: CRS/3ALPHA must be exactly three letters") } 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"); } if (!uuidlibValidate(uuid)) { throw new ValidationError("Invalid input: The UUID/api_key is the expexted value") } return true; } export function validateStation(station: unknown): boolean { if (typeof station !== "string") { throw new ValidationError("Invalid input: The station name should be a string"); } if (!/^[A-Za-z0-9\s&'()-]+$/.test(station)) { throw new ValidationError("Invalid input: Station name should include letters, spaces, ', '/', '-', '&' only"); } return true; } export function validateReasonCode(code: unknown): boolean { if (typeof code === "number") { // Ensure it's a 3-digit number (100-999) if (!Number.isInteger(code) || code < 100 || code > 999) { throw new ValidationError("Invalid input: Reason code must be a three-digit number (100-999)."); } return true; } if (typeof code === "string") { // Ensure it consists of exactly 3 numeric characters if (!/^\d{3}$/.test(code)) { throw new ValidationError("Invalid input: Reason code must be a string of three digits."); } return true; } // 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."); } 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; }