UNPKG

@tmlmobilidade/types

Version:

This package provides shared Zod validation schemas and their corresponding TypeScript types for use across projects. All types are automatically inferred from the schemas to ensure full type safety and reduce maintenance overhead.

97 lines (96 loc) 4.38 kB
/* * */ import { validateGtfsBinary, validateGtfsTernary } from './common.js'; /** * Validates and transforms a value into a GTFS Location Type. * It accepts numeric or string representations of location types. * @param value The value to validate and transform. * @returns A GTFS Location Type value (0-4). * @throws Error if the value is not a valid GTFS Location type representation. */ export function validateGtfsLocationType(value) { // Return 0 if the value is not provided or is null/undefined if (value === undefined || value === null || value === '') { return 0; } // Validate the route type value if (typeof value === 'number') { if (value >= 0 && value <= 4) return value; } else if (typeof value === 'string') { const numValue = parseInt(value, 10); if (!isNaN(numValue) && numValue >= 0 && numValue <= 4) return numValue; } // If the value does not match any known route type, throw an error throw new Error(`Invalid GTFS Location Type value: "${value}". It must be a number between 0 and 4.`); } /** * Validates and transforms a raw GTFS stop into the GTFS_Stop format. * This function checks the types of fields, converts boolean strings to boolean values, * and ensures that required fields are present. * @param rawData The raw trip data to validate and transform. * @returns A validated GTFS_Stop object. */ export function validateGtfsStop(rawData) { // Ensure required fields are present if (!rawData.stop_code) throw new Error('Missing required field "stop_code" on GTFS Stop.'); if (!rawData.stop_id) throw new Error('Missing required field "stop_id" on GTFS Stop.'); if (!rawData.stop_lat) throw new Error('Missing required field "stop_lat" on GTFS Stop.'); if (!rawData.stop_lon) throw new Error('Missing required field "stop_lon" on GTFS Stop.'); if (!rawData.stop_name) throw new Error('Missing required field "stop_name" on GTFS Stop.'); // Transform the raw data into the output format return { level_id: rawData.level_id, location_type: validateGtfsLocationType(rawData.location_type), parent_station: rawData.parent_station, platform_code: rawData.platform_code, stop_code: rawData.stop_code, stop_desc: rawData.stop_desc, stop_id: rawData.stop_id, stop_lat: Number(rawData.stop_lat), stop_lon: Number(rawData.stop_lon), stop_name: rawData.stop_name, stop_timezone: rawData.stop_timezone, stop_url: rawData.stop_url, wheelchair_boarding: validateGtfsTernary(rawData.wheelchair_boarding), zone_id: rawData.zone_id, }; } /** * Validates and transforms raw GTFS-TML stop data into a structured GTFS_Stop_Extended object. * This function checks the types of fields, converts boolean strings to boolean values, * and ensures that required fields are present, including the pattern_id. * @param rawData he raw stop data to validate and transform. * @returns A validated GTFS_Stop_Extended object. */ export function validateGtfsStopExtended(rawData) { // Validate the standard GTFS fields const stop = validateGtfsStop(rawData); // Validate the Extended GTFS fields // if (!rawData.municipality_id) throw new Error('Missing required field "municipality_id" on GTFS Stop.'); // Transform the raw data into the output format return { ...stop, has_bench: validateGtfsBinary(rawData.has_bench), has_network_map: validateGtfsBinary(rawData.has_network_map), has_pip_real_time: validateGtfsBinary(rawData.has_pip_real_time), has_schedules: validateGtfsBinary(rawData.has_schedules), has_shelter: validateGtfsBinary(rawData.has_shelter), has_stop_sign: validateGtfsBinary(rawData.has_stop_sign), has_tariffs_information: validateGtfsBinary(rawData.has_tariffs_information), municipality_id: rawData.municipality_id, parish_id: rawData.parish_id, public_visible: validateGtfsBinary(rawData.public_visible), region_id: rawData.region_id, shelter_code: rawData.shelter_code, shelter_maintainer: rawData.shelter_maintainer, stop_short_name: rawData.stop_short_name, tts_stop_name: rawData.tts_stop_name, }; }