UNPKG

@tmlmobilidade/types

Version:
103 lines (102 loc) 4.69 kB
/* * */ import { validateGtfsBinary, validateGtfsHasField, 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, district_id: rawData.district_id, district_name: rawData.district_name, has_bench: validateGtfsHasField(rawData.has_bench), has_network_map: validateGtfsHasField(rawData.has_network_map), has_pip_real_time: validateGtfsHasField(rawData.has_pip_real_time), has_schedules: validateGtfsHasField(rawData.has_schedules), has_shelter: validateGtfsBinary(rawData.has_shelter), has_stop_sign: validateGtfsHasField(rawData.has_stop_sign), has_tariffs_information: validateGtfsHasField(rawData.has_tariffs_information), locality_id: rawData.locality_id, locality_name: rawData.locality_name, municipality_id: rawData.municipality_id, municipality_name: rawData.municipality_name, parish_id: rawData.parish_id, parish_name: rawData.parish_name, 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, }; }