Implement input validation and supporting tests with automation of the tests via actions.

This commit is contained in:
Fred Boniface
2025-03-12 21:47:03 +00:00
parent d20d981190
commit c54e517700
6 changed files with 4032 additions and 6 deletions

View File

@@ -1,6 +1,13 @@
import { ValidationError } from "../errors";
import { validate as uuidlibValidate, version as uuidlibVersion } from 'uuid'
export function validatePisCode(code: string): boolean {
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");
@@ -8,18 +15,56 @@ export function validatePisCode(code: string): boolean {
return true;
}
export function validateTiploc(tiploc: string): boolean {
const tiplocRegex = /[a-zA-z0-9]{4,7}/;
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: string): boolean {
const crsRegex = /[a-zA-z]{3}/;
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 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 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.");
}