@tmlmobilidade/types
Version:
123 lines (122 loc) • 5.33 kB
JavaScript
/* * */
import { validateGtfsBinary, validateGtfsPickupDropoffType } from './common.js';
/**
* Validates and transforms a value into a GTFS Route Type.
* It accepts numeric or string representations of route types.
* @param value The value to validate and transform.
* @returns A GTFS Route Type value (0-12).
* @throws Error if the value is not a valid GTFS Route type representation.
*/
export function validateGtfsRouteType(value) {
// Validate the route type value
if (typeof value === 'number') {
if (value >= 0 && value <= 12)
return value;
}
else if (typeof value === 'string') {
const numValue = parseInt(value, 10);
if (!isNaN(numValue) && numValue >= 0 && numValue <= 12)
return numValue;
}
// If the value does not match any known route type, throw an error
throw new Error(`Invalid GTFS Route Type value: "${value}". It must be a number between 0 and 12.`);
}
/**
* Validates and transforms raw GTFS trip data into a structured GTFS_Route 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_Route object.
*/
export function validateGtfsRoute(rawData) {
// Ensure required fields are present
if (!rawData.agency_id)
throw new Error('Missing required field "agency_id" on GTFS Route.');
if (!rawData.route_color)
throw new Error('Missing required field "route_color" on GTFS Route.');
if (!rawData.route_id)
throw new Error('Missing required field "route_id" on GTFS Route.');
if (!rawData.route_long_name)
throw new Error('Missing required field "route_long_name" on GTFS Route.');
if (!rawData.route_short_name)
throw new Error('Missing required field "route_short_name" on GTFS Route.');
if (!rawData.route_text_color)
throw new Error('Missing required field "route_text_color" on GTFS Route.');
if (!rawData.route_type)
throw new Error('Missing required field "route_type" on GTFS Route.');
// Transform the raw data into the output format
return {
agency_id: rawData.agency_id,
continuous_drop_off: validateGtfsPickupDropoffType(rawData.continuous_drop_off),
continuous_pickup: validateGtfsPickupDropoffType(rawData.continuous_pickup),
route_color: rawData.route_color,
route_desc: rawData.route_desc,
route_id: rawData.route_id,
route_long_name: rawData.route_long_name,
route_short_name: rawData.route_short_name,
route_sort_order: Number(rawData.route_sort_order),
route_text_color: rawData.route_text_color,
route_type: validateGtfsRouteType(rawData.route_type),
route_url: rawData.route_url,
};
}
/**
* Validates and transforms a value into a GTFS Path Type.
* It accepts numeric or string representations of path types.
* @param value The value to validate and transform.
* @returns A GTFS Path Type value (1, 2, or 3).
* @throws Error if the value is not a valid GTFS Path Type representation.
*/
export function validateGTFSPathType(value) {
// If the value is not provided, default to 1 (Base path)
if (value === undefined || value === null)
return 1;
// Handle numeric and string representations of GTFS Path Type values
if (typeof value === 'number') {
if (value === 1)
return 1;
if (value === 2)
return 2;
if (value === 3)
return 3;
}
if (typeof value === 'string') {
if (value === '1')
return 1;
if (value === '2')
return 2;
if (value === '3')
return 3;
}
// If the value does not match any known GTFS Path Type representation, throw an error
throw new Error(`Invalid GTFS Path Type value: "${value}". It must be 1, 2 or 3.`);
}
/**
* Validates and transforms raw GTFS-TML route data into a structured GTFS_Route_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 route data to validate and transform.
* @returns A validated GTFS_Route_Extended object.
*/
export function validateGtfsRouteExtended(rawData) {
// Validate the standard GTFS fields
const route = validateGtfsRoute(rawData);
// Validate the Extended GTFS fields
if (!rawData.line_id)
throw new Error('Missing required field "line_id" on GTFS Route.');
if (!rawData.line_long_name)
throw new Error('Missing required field "line_long_name" on GTFS Route.');
if (!rawData.line_short_name)
throw new Error('Missing required field "line_short_name" on GTFS Route.');
// Transform the raw data into the output format
return {
...route,
circular: validateGtfsBinary(rawData.circular),
line_id: rawData.line_id,
line_long_name: rawData.line_long_name,
line_short_name: rawData.line_short_name,
path_type: validateGTFSPathType(rawData.path_type),
route_remarks: rawData.route_remarks,
school: validateGtfsBinary(rawData.school),
};
}