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

96 lines
2.6 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',
formatNearbyReq: 'function',
formatTripReq: 'function',
formatRadarReq: 'function',
formatReachableFromReq: 'function',
formatRefreshJourneyReq: '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
parseMovement: 'function',
parseNearby: 'function',
parseOperator: 'function',
parseHint: 'function',
2018-06-07 18:46:24 +02:00
parseWarning: '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'
}
const validateProfile = (profile) => {
for (let key of Object.keys(types)) {
const type = types[key]
if (type === 'array') {
if (!Array.isArray(profile[key])) {
2020-02-04 18:47:25 +01:00
throw new TypeError(`profile.${key} must be an array.`)
}
} else if (type !== typeof profile[key]) {
2020-02-04 18:47:25 +01:00
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) {
2020-02-04 18:47:25 +01:00
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)) {
2020-02-04 18:47:25 +01:00
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) {
2020-02-04 18:47:25 +01:00
throw new TypeError('profile.products[].id must be a string.')
}
if ('boolean' !== typeof product.default) {
2020-02-04 18:47:25 +01:00
throw new TypeError('profile.products[].default must be a boolean.')
}
if (!Array.isArray(product.bitmasks)) {
2020-02-04 18:47:25 +01:00
throw new TypeError(product.id + '.bitmasks must be an array.')
}
for (let bitmask of product.bitmasks) {
if ('number' !== typeof bitmask) {
2020-02-04 18:47:25 +01:00
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,
}