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.

57 lines (56 loc) 2.5 kB
/* * */ import { validateGtfsBinary, validateGtfsTernary } from './common.js'; /** * Validates and transforms raw GTFS Trip data into a structured GTFS_Trip object. * 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_Trip object. */ export function validateGtfsTrip(rawData) { // Ensure required fields are present if (!rawData.route_id) throw new Error('Missing required field "route_id" on GTFS Trip.'); if (!rawData.service_id) throw new Error('Missing required field "service_id" on GTFS Trip.'); if (!rawData.trip_id) throw new Error('Missing required field "trip_id" on GTFS Trip.'); if (!rawData.direction_id) throw new Error('Missing required field "direction_id" on GTFS Trip.'); if (!rawData.trip_headsign) throw new Error('Missing required field "trip_headsign" on GTFS Trip.'); if (!rawData.shape_id) throw new Error('Missing required field "shape_id" on GTFS Trip.'); // Transform the raw data into the output format return { bikes_allowed: validateGtfsTernary(rawData.bikes_allowed), block_id: rawData.block_id, direction_id: validateGtfsBinary(rawData.direction_id), route_id: rawData.route_id, service_id: rawData.service_id, shape_id: rawData.shape_id, trip_headsign: rawData.trip_headsign, trip_id: rawData.trip_id, trip_short_name: rawData.trip_short_name, wheelchair_accessible: validateGtfsTernary(rawData.wheelchair_accessible), }; } /** * Validates and transforms raw GTFS-TML trip data into a structured GTFS_Trip_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 trip data to validate and transform. * @returns A validated GTFS_Trip_Extended object. */ export function validateGtfsTripExtended(rawData) { // Validate the standard GTFS fields const trip = validateGtfsTrip(rawData); // Validate the Extended GTFS fields if (!rawData.pattern_id) throw new Error('Missing required field "pattern_id" on GTFS Trip.'); // Transform the raw data into the output format return { ...trip, pattern_id: rawData.pattern_id, }; }