db-vendo-client/lib/validate-profile.js

93 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

2017-12-12 03:21:12 +01:00
const types = {
locale: 'string',
timezone: 'string',
2019-10-31 18:48:11 +01:00
request: 'function',
2017-12-12 03:21:12 +01:00
transformReq: 'function',
transformReqBody: 'function',
2019-10-31 20:08:56 +01:00
formatStationBoardReq: 'function',
formatLocationsReq: 'function',
formatStopReq: 'function',
formatTripReq: 'function',
formatRefreshJourneyReq: 'function',
2024-12-21 15:26:49 +00:00
formatJourneysReq: 'function',
2017-12-12 03:21:12 +01:00
transformJourneysQuery: 'function',
products: 'array',
2017-12-12 03:28:54 +01:00
2017-12-12 03:21:12 +01:00
parseDateTime: 'function',
parseDeparture: 'function',
2018-06-26 15:49:50 +02:00
parseArrival: 'function',
2017-12-28 16:56:27 +01:00
parseJourneyLeg: 'function',
2017-12-12 03:21:12 +01:00
parseJourney: 'function',
parseLine: 'function',
parseStationName: 'function',
parseLocation: 'function',
parsePolyline: 'function',
2017-12-12 03:21:12 +01:00
parseOperator: 'function',
2024-12-07 16:16:31 +00:00
parseRemarks: 'function',
2017-12-12 03:21:12 +01:00
parseStopover: 'function',
formatAddress: 'function',
formatCoord: 'function',
formatDate: 'function',
formatLocationFilter: 'function',
formatProductsFilter: 'function',
2017-12-12 03:21:12 +01:00
formatPoi: 'function',
formatStation: 'function',
formatTime: 'function',
formatLocation: 'function',
formatRectangle: 'function',
};
2017-12-12 03:21:12 +01:00
const validateProfile = (profile) => {
for (let key of Object.keys(types)) {
const type = types[key];
if (type === 'array') {
if (!Array.isArray(profile[key])) {
throw new TypeError(`profile.${key} must be an array.`);
}
} else if (type !== typeof profile[key]) {
throw new TypeError(`profile.${key} must be a ${type}.`);
2017-12-12 03:21:12 +01:00
}
2017-12-12 03:28:54 +01:00
if (type === 'object' && profile[key] === null) {
throw new TypeError(`profile.${key} must not be null.`);
2017-12-12 03:28:54 +01:00
}
2017-12-12 03:21:12 +01:00
}
if (!Array.isArray(profile.products)) {
throw new TypeError('profile.products must be an array.');
}
if (profile.products.length === 0) {
throw new Error('profile.products is empty.');
}
for (let product of profile.products) {
2018-03-16 17:06:32 +01:00
if ('string' !== typeof product.id) {
throw new TypeError('profile.products[].id must be a string.');
}
if ('boolean' !== typeof product.default) {
throw new TypeError('profile.products[].default must be a boolean.');
}
if (!Array.isArray(product.bitmasks)) {
throw new TypeError(product.id + '.bitmasks must be an array.');
}
for (let bitmask of product.bitmasks) {
if ('number' !== typeof bitmask) {
throw new TypeError(product.id + '.bitmasks[] must be a number.');
}
}
}
if ('trip' in profile && 'boolean' !== typeof profile.trip) {
throw new Error('profile.trip must be a boolean.');
}
if ('journeyLeg' in profile) {
throw new Error('profile.journeyLeg has been removed. Use profile.trip.');
}
};
2017-12-12 03:21:12 +01:00
2022-05-07 16:17:37 +02:00
export {
validateProfile,
};