-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjourney-leg.js
76 lines (60 loc) · 2.6 KB
/
journey-leg.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict'
const is = require('@sindresorhus/is')
const a = require('assert')
const anyOf = require('./lib/any-of')
const isField = (obj, f) => {
return !is.null(obj[f]) && !is.undefined(obj[f])
}
const validateJourneyLeg = (val, leg, name = 'journeyLeg') => {
a.ok(is.object(leg) && !is.array(leg), name + ' must be an object')
anyOf([
'station', 'stop', 'location'
], val, leg.origin, name + '.origin')
anyOf([
'station', 'stop', 'location'
], val, leg.destination, name + '.destination')
val.date(val, leg.departure, name + '.departure')
val.date(val, leg.arrival, name + '.arrival')
if (isField(leg, 'departurePlatform')) {
a.strictEqual(typeof leg.departurePlatform, 'string', name + '.departurePlatform must be a string')
a.ok(leg.departurePlatform.length > 0, name + '.departurePlatform can\'t be empty')
}
if (isField(leg, 'arrivalPlatform')) {
a.strictEqual(typeof leg.arrivalPlatform, 'string', name + '.arrivalPlatform must be a string')
a.ok(leg.arrivalPlatform.length > 0, name + '.arrivalPlatform can\'t be empty')
}
if (isField(leg, 'departureDelay')) {
a.strictEqual(typeof leg.departureDelay, 'number', name + '.departureDelay must be a number')
a.ok(leg.departureDelay >= 0, name + '.departureDelay must be >= 0')
}
if (isField(leg, 'arrivalDelay')) {
a.strictEqual(typeof leg.arrivalDelay, 'number', name + '.arrivalDelay must be a number')
a.ok(leg.arrivalDelay >= 0, name + '.arrivalDelay must be >= 0')
}
if (isField(leg, 'stopovers')) {
a.ok(Array.isArray(leg.stopovers), name + '.stopovers must be an array')
for (let i = 0; i < leg.stopovers.length; i++) {
val.stopover(val, leg.stopovers[i], `${name}.stopovers[${i}]`)
}
}
if (isField(leg, 'schedule')) {
anyOf(['schedule', 'ref'], val, leg.schedule, name + '.schedule')
}
if (isField(leg, 'mode')) {
val.mode(val, leg.mode, name + '.mode')
}
if (!is.undefined(leg.subMode)) {
a.fail(name + '.subMode is reserved and should not be used for now')
}
if (isField(leg, 'public')) {
a.strictEqual(typeof leg.public, 'boolean', name + '.public must be a boolean')
}
anyOf(['operator', 'ref'], val, leg.operator, name + '.operator')
if (isField(leg, 'price')) {
a.ok(is.object(leg.price), name + '.price must be an object')
a.strictEqual(typeof leg.price.amount, 'number', name + '.price.amount must be a number')
a.strictEqual(typeof leg.price.currency, 'string', name + '.price.currency must be a string')
a.ok(leg.price.currency.length > 0, name + '.price.currency can\'t be empty')
}
}
module.exports = validateJourneyLeg