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;
|
||||
}
|
||||
@@ -1,14 +1,35 @@
|
||||
import type { ApiPisObject } from '@owlboard/api-schema-types';
|
||||
import { ApiPisObject } from '@owlboard/api-schema-types';
|
||||
import type { BaseClient, ApiResult } from '../lib/base.js';
|
||||
import { IsValidCrs, IsValidTiploc, IsValidPis } from 'src/lib/validation.js';
|
||||
import { ValidationError } from 'src/lib/errors.js';
|
||||
|
||||
export class PisModule {
|
||||
constructor(private client: BaseClient) {}
|
||||
|
||||
async getByStartEndCrs(startCrs: string, endCrs: string): Promise<ApiResult<ApiPisObject.PisObjects>> {
|
||||
const path = `/pis/route/${encodeURIComponent(startCrs.toLowerCase())}/${encodeURIComponent(endCrs.toLowerCase())}`;
|
||||
async getByStartEndCrs(startCrs: string, endCrs: string): Promise<ApiResult<ApiPisObject.PisObjects[]>> {
|
||||
if (!IsValidCrs(startCrs)) {
|
||||
throw new ValidationError("startCrs", "Invalid CRS Format")
|
||||
}
|
||||
if (!IsValidCrs(endCrs)) {
|
||||
throw new ValidationError("endCrs", "Invalid CRS Format")
|
||||
}
|
||||
|
||||
return this.client.request<ApiPisObject.PisObjects>(path, {
|
||||
const path = `/pis/route/${encodeURIComponent(startCrs.toUpperCase())}/${encodeURIComponent(endCrs.toUpperCase())}`;
|
||||
|
||||
return this.client.request<ApiPisObject.PisObjects[]>(path, {
|
||||
method: 'GET',
|
||||
});
|
||||
}
|
||||
|
||||
async getByCode(code: string): Promise<ApiResult<ApiPisObject.PisObjects[]>> {
|
||||
if (!IsValidPis(code)) {
|
||||
throw new ValidationError("code", "Invalid PIS Code Format")
|
||||
}
|
||||
|
||||
const path = `/pis/code/${encodeURIComponent(code)}`;
|
||||
|
||||
return this.client.request<ApiPisObject.PisObjects[]>(path, {
|
||||
method: 'GET',
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user