/
journey.js
34 lines (26 loc) · 1.14 KB
/
journey.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict'
const is = require('@sindresorhus/is')
const a = require('assert')
const isCurrencyCode = require('is-currency-code')
const isField = (obj, f) => {
return !is.null(obj[f]) && !is.undefined(obj[f])
}
const validateJourney = (val, journey, name = 'journey') => {
val.item(val, journey, name)
a.strictEqual(journey.type, 'journey', name + '.type must be `journey`')
val.ref(val, journey.id, name + '.id')
a.ok(Array.isArray(journey.legs), name + '.legs must be an array')
a.ok(journey.legs.length > 0, name + '.legs can\'t be empty')
for (let i = 0; i < journey.legs.length; i++) {
val.journeyLeg(val, journey.legs[i], name + `.legs[${i}]`)
}
// todo: check if sorted correctly
if (isField(journey, 'price')) {
const p = journey.price
a.ok(is.object(p) && !is.array(p), name + '.price must be an object')
a.strictEqual(typeof p.amount, 'number', name + '.price.amount must be a number')
a.strictEqual(typeof p.currency, 'string', name + '.price.currency must be a string')
a.ok(isCurrencyCode(p.currency), name + '.price.currency must be a valid ISO 4217 currency code')
}
}
module.exports = validateJourney