-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidateSchedule.js
More file actions
196 lines (161 loc) · 6.25 KB
/
Copy pathvalidateSchedule.js
File metadata and controls
196 lines (161 loc) · 6.25 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const TOP_LEVEL_ARRAYS = ["sessions", "speakers", "session_types", "rooms", "tags"]
const OPTIONAL_SESSION_STRINGS = ["uri", "qa", "slide", "live", "record", "language", "co_write"]
function isObject(value) {
return value != null && typeof value === "object" && !Array.isArray(value)
}
function isNonEmptyString(value) {
return typeof value === "string" && value.length > 0
}
function validateString(value, path, errors, { nonEmpty = false } = {}) {
if (typeof value !== "string" || (nonEmpty && value.length === 0)) {
errors.push(`${path}: expected ${nonEmpty ? "non-empty " : ""}string`)
}
}
function validateOptionalString(value, path, errors, { nonEmpty = false } = {}) {
if (value != null) {
validateString(value, path, errors, { nonEmpty })
}
}
function validateDateTime(value, path, errors) {
validateString(value, path, errors)
if (typeof value === "string" && Number.isNaN(Date.parse(value))) {
errors.push(`${path}: expected parseable date-time string`)
}
}
function validateStringList(value, path, errors) {
if (!Array.isArray(value)) {
errors.push(`${path}: expected array`)
return
}
value.forEach((item, index) => validateString(item, `${path}[${index}]`, errors))
}
function validateLocalized(value, path, fields, errors) {
if (!isObject(value)) {
errors.push(`${path}: expected object`)
return
}
for (const field of fields) {
validateString(value[field], `${path}.${field}`, errors)
}
}
function validateCollectionIds(items, collection, errors) {
const ids = new Set()
items.forEach((item, index) => {
const path = `$.${collection}[${index}].id`
if (!isObject(item)) {
return
}
if (!isNonEmptyString(item.id)) {
return
}
if (ids.has(item.id)) {
errors.push(`${path}: duplicate id "${item.id}"`)
}
ids.add(item.id)
})
return ids
}
function validateNamedEntity(item, path, errors) {
if (!isObject(item)) {
errors.push(`${path}: expected object`)
return
}
validateString(item.id, `${path}.id`, errors, { nonEmpty: true })
validateLocalized(item.zh, `${path}.zh`, ["name"], errors)
validateLocalized(item.en, `${path}.en`, ["name"], errors)
}
function validateSpeaker(speaker, path, errors) {
if (!isObject(speaker)) {
errors.push(`${path}: expected object`)
return
}
validateString(speaker.id, `${path}.id`, errors, { nonEmpty: true })
validateString(speaker.avatar, `${path}.avatar`, errors)
validateLocalized(speaker.zh, `${path}.zh`, ["name", "bio"], errors)
validateLocalized(speaker.en, `${path}.en`, ["name", "bio"], errors)
}
function validateSession(session, path, errors) {
if (!isObject(session)) {
errors.push(`${path}: expected object`)
return
}
validateString(session.id, `${path}.id`, errors, { nonEmpty: true })
validateString(session.room, `${path}.room`, errors, { nonEmpty: true })
validateOptionalString(session.type, `${path}.type`, errors, { nonEmpty: true })
validateDateTime(session.start, `${path}.start`, errors)
validateDateTime(session.end, `${path}.end`, errors)
validateLocalized(session.zh, `${path}.zh`, ["title", "description"], errors)
validateLocalized(session.en, `${path}.en`, ["title", "description"], errors)
validateStringList(session.speakers, `${path}.speakers`, errors)
validateStringList(session.tags, `${path}.tags`, errors)
if (session.broadcast != null) {
validateStringList(session.broadcast, `${path}.broadcast`, errors)
}
for (const field of OPTIONAL_SESSION_STRINGS) {
if (session[field] != null) {
validateString(session[field], `${path}.${field}`, errors)
}
}
}
function checkReference(id, ids, path, target, diagnostics) {
if (typeof id !== "string" || id.length === 0) {
return
}
if (!ids.has(id)) {
diagnostics.push(`${path}: unknown ${target} id "${id}"`)
}
}
function validateReferences(schedule, ids, errors, warnings) {
schedule.sessions.forEach((session, sessionIndex) => {
if (!isObject(session)) {
return
}
const path = `$.sessions[${sessionIndex}]`
checkReference(session.room, ids.rooms, `${path}.room`, "room", errors)
checkReference(session.type, ids.session_types, `${path}.type`, "session_type", warnings)
if (Array.isArray(session.speakers)) {
session.speakers.forEach((speakerId, index) =>
checkReference(speakerId, ids.speakers, `${path}.speakers[${index}]`, "speaker", warnings)
)
}
if (Array.isArray(session.tags)) {
session.tags.forEach((tagId, index) => checkReference(tagId, ids.tags, `${path}.tags[${index}]`, "tag", warnings))
}
})
}
export function validateSchedule(schedule, { strict = false } = {}) {
const errors = []
const warnings = []
if (!isObject(schedule)) {
return { valid: false, errors: ["$: expected object"], warnings }
}
for (const collection of TOP_LEVEL_ARRAYS) {
if (!Array.isArray(schedule[collection])) {
errors.push(`$.${collection}: expected array`)
}
}
const collections = Object.fromEntries(
TOP_LEVEL_ARRAYS.map((collection) => [collection, Array.isArray(schedule[collection]) ? schedule[collection] : []])
)
collections.sessions.forEach((session, index) => validateSession(session, `$.sessions[${index}]`, errors))
collections.speakers.forEach((speaker, index) => validateSpeaker(speaker, `$.speakers[${index}]`, errors))
for (const collection of ["session_types", "rooms", "tags"]) {
collections[collection].forEach((item, index) => validateNamedEntity(item, `$.${collection}[${index}]`, errors))
}
const ids = {
sessions: validateCollectionIds(collections.sessions, "sessions", errors),
speakers: validateCollectionIds(collections.speakers, "speakers", errors),
session_types: validateCollectionIds(collections.session_types, "session_types", errors),
rooms: validateCollectionIds(collections.rooms, "rooms", errors),
tags: validateCollectionIds(collections.tags, "tags", errors),
}
validateReferences(collections, ids, errors, warnings)
return { valid: errors.length === 0 && (!strict || warnings.length === 0), errors, warnings }
}
export function assertValidSchedule(schedule, options) {
const result = validateSchedule(schedule, options)
if (!result.valid) {
throw new Error([...result.errors, ...result.warnings].join("\n"))
}
return schedule
}