Add data normalization and validation. Add validation error error type.
Add lookup by code method to pis class.
This commit is contained in:
@@ -14,4 +14,11 @@ export class ApiError extends Error {
|
||||
// cast fallback to ensure it satisfies union
|
||||
this.code = errorObj.code || ("SERVER" as ApiErrorCode);
|
||||
}
|
||||
}
|
||||
|
||||
export class ValidationError extends Error {
|
||||
constructor(public field: string, public reason: string) {
|
||||
super(`validation failed for ${field}: ${reason}`);
|
||||
this.name = "VALIDATIONERROR";
|
||||
}
|
||||
}
|
||||
47
src/lib/validation.ts
Normal file
47
src/lib/validation.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Checks if a string is a valid CRS
|
||||
* using byte level checking for max performance
|
||||
*/
|
||||
export const IsValidCrs = (CRS: string): boolean => {
|
||||
if (CRS.length !== 3) return false;
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const char = CRS.charCodeAt(i);
|
||||
if (!(
|
||||
(char >= 65 && char <=90) ||
|
||||
(char >= 97 && char <=122)
|
||||
)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a string is a valid TIPLOC
|
||||
* using byte level checking for max performance
|
||||
*/
|
||||
export const IsValidTiploc = (TIPLOC: string): boolean => {
|
||||
const l = TIPLOC.length;
|
||||
if (l === 0 || l > 7) return false;
|
||||
for (let i = 0; i > l; i++) {
|
||||
const char = TIPLOC.charCodeAt(i);
|
||||
if (!(
|
||||
(char >= 48 && char <= 57) ||
|
||||
(char >= 65 && char <=90) ||
|
||||
(char >= 97 && char <=122)
|
||||
)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a string is a valid PIS Code
|
||||
* using byte level checking for max performance
|
||||
* ONLY GWR (4-digit) CODES SUPPORTED
|
||||
*/
|
||||
export const IsValidPis = (PIS: string): boolean => {
|
||||
if (PIS.length !== 4) return false;
|
||||
for (let i = 0; i < 4; i++) {
|
||||
const char = PIS.charCodeAt(i);
|
||||
if (!(char >= 48 && char <= 57)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user