forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdocsValidation.ts
287 lines (261 loc) · 9.23 KB
/
gdocsValidation.ts
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import {
OwidGdocPostContent,
OwidGdocPostInterface,
OwidGdocErrorMessage,
OwidGdocErrorMessageType,
OwidGdocType,
checkIsOwidGdocType,
traverseEnrichedBlock,
OwidGdocErrorMessageProperty,
OwidGdoc,
checkIsGdocPost,
checkIsDataInsight,
OwidGdocDataInsightInterface,
checkIsAuthor,
OwidGdocAuthorInterface,
} from "@ourworldindata/utils"
function validateTitle(gdoc: OwidGdoc, errors: OwidGdocErrorMessage[]) {
if (!gdoc.content.title) {
errors.push(getMissingContentPropertyError("title"))
}
}
function validatePublishedAt(gdoc: OwidGdoc, errors: OwidGdocErrorMessage[]) {
if (!gdoc.publishedAt) {
errors.push({
property: "publishedAt",
type: OwidGdocErrorMessageType.Warning,
message: `The publication date will be set to the current date on publishing.`,
})
}
}
function validateSlug(gdoc: OwidGdoc, errors: OwidGdocErrorMessage[]) {
if (!gdoc.slug.match(/^[a-z0-9-\/]+$/)) {
errors.push({
property: "slug",
type: OwidGdocErrorMessageType.Error,
message: `Slug must only contain lowercase letters, numbers and hyphens. Double underscores are interpreted as slashes.`,
})
}
}
function validateContentType(gdoc: OwidGdoc, errors: OwidGdocErrorMessage[]) {
if (!gdoc.content.type || !checkIsOwidGdocType(gdoc.content.type)) {
errors.push({
property: "content",
message: `Invalid or unset document type. Must be one-of: ${Object.values(
OwidGdocType
).join(", ")}`,
type: OwidGdocErrorMessageType.Error,
})
}
}
function validateBody(gdoc: OwidGdoc, errors: OwidGdocErrorMessage[]) {
if (!gdoc.content.body) {
errors.push(getMissingContentPropertyError("body"))
} else {
for (const block of gdoc.content.body) {
traverseEnrichedBlock(block, (block) => {
errors.push(
...block.parseErrors.map((parseError) => ({
message: parseError.message,
type: parseError.isWarning
? OwidGdocErrorMessageType.Warning
: OwidGdocErrorMessageType.Error,
property: "body" as const,
}))
)
})
}
}
}
function validateRefs(
gdoc: OwidGdocPostInterface,
errors: OwidGdocErrorMessage[]
) {
if (gdoc.content.refs) {
// Errors due to refs being unused / undefined / malformed
if (gdoc.content.refs.errors.length) {
errors.push(...gdoc.content.refs.errors)
}
// Errors due to the content of the refs having parse errors
if (gdoc.content.refs.definitions) {
Object.values(gdoc.content.refs.definitions).map((definition) => {
definition.content.map((block) => {
traverseEnrichedBlock(block, (node) => {
if (node.parseErrors.length) {
for (const parseError of node.parseErrors) {
errors.push({
message: `Parse error in "${definition.id}" ref content: ${parseError.message}`,
property: "refs",
type: parseError.isWarning
? OwidGdocErrorMessageType.Warning
: OwidGdocErrorMessageType.Error,
})
}
}
})
})
})
}
}
}
// Kind of arbitrary, see https://github.com/owid/owid-grapher/issues/2983
export const EXCERPT_MAX_LENGTH = 175
function validateExcerpt(
gdoc: OwidGdocPostInterface,
errors: OwidGdocErrorMessage[]
) {
if (!gdoc.content.excerpt) {
errors.push(getMissingContentPropertyError("excerpt"))
} else if (gdoc.content.excerpt.length > EXCERPT_MAX_LENGTH) {
errors.push({
property: "excerpt",
type: OwidGdocErrorMessageType.Warning,
message: `Long excerpts may not display well in our list of articles or on social media.`,
})
}
}
function validateBreadcrumbs(
gdoc: OwidGdocPostInterface,
errors: OwidGdocErrorMessage[]
) {
if (gdoc.breadcrumbs) {
for (const [i, breadcrumb] of gdoc.breadcrumbs.entries()) {
if (!breadcrumb.label) {
errors.push({
property: `breadcrumbs[${i}].label`,
type: OwidGdocErrorMessageType.Error,
message: `Breadcrumb must have a label`,
})
}
// Last item can be missing a href
if (!breadcrumb.href && i !== gdoc.breadcrumbs.length - 1) {
errors.push({
property: `breadcrumbs[${i}].href`,
type: OwidGdocErrorMessageType.Error,
message: `Breadcrumb must have a url`,
})
}
if (breadcrumb.href && !breadcrumb.href.startsWith("/")) {
errors.push({
property: `breadcrumbs[${i}].href`,
type: OwidGdocErrorMessageType.Error,
message: `Breadcrumb url must start with a /`,
})
}
}
}
}
function validateApprovedBy(
gdoc: OwidGdocDataInsightInterface,
errors: OwidGdocErrorMessage[]
) {
if (!gdoc.content["approved-by"]) {
errors.push({
property: "approved-by",
type: OwidGdocErrorMessageType.Error,
message: `This data insight hasn't been approved by anyone yet. Please have someone approve it and add "approved-by: their name" to the front-matter.`,
})
}
}
function validateGrapherUrl(
gdoc: OwidGdocDataInsightInterface,
errors: OwidGdocErrorMessage[]
) {
if (!gdoc.content["grapher-url"]) {
errors.push({
property: "grapher-url",
type: OwidGdocErrorMessageType.Warning,
message: `Missing "grapher-url". This isn't required, but if you're referencing a grapher, it's a good idea to add it so that we can link it to this data insight in the future. Include country selections, if applicable.`,
})
}
}
function validateAtomFields(
gdoc: OwidGdocPostInterface,
errors: OwidGdocErrorMessage[]
) {
const rssTitle = gdoc.content["atom-title"]
const rssExcerpt = gdoc.content["atom-excerpt"]
if (rssTitle && typeof rssTitle !== "string") {
errors.push({
property: "atom-title",
message: "atom-title must be a string",
type: OwidGdocErrorMessageType.Error,
})
}
if (rssExcerpt && typeof rssExcerpt !== "string") {
errors.push({
property: "atom-excerpt",
message: "atom-excerpt must be a string",
type: OwidGdocErrorMessageType.Error,
})
}
}
function validateSocials(
gdoc: OwidGdocAuthorInterface,
errors: OwidGdocErrorMessage[]
) {
const { socials } = gdoc.content
if (!socials?.parseErrors) return
errors.push(
...socials.parseErrors.map((parseError) => ({
message: parseError.message,
type: parseError.isWarning
? OwidGdocErrorMessageType.Warning
: OwidGdocErrorMessageType.Error,
property: "socials" as const,
}))
)
}
export const getErrors = (gdoc: OwidGdoc): OwidGdocErrorMessage[] => {
const errors: OwidGdocErrorMessage[] = []
// These errors come from attachments; see GdocBase.validate()
gdoc.errors?.forEach((error) => errors.push(error))
validateTitle(gdoc, errors)
validateSlug(gdoc, errors)
validateBody(gdoc, errors)
validatePublishedAt(gdoc, errors)
validateContentType(gdoc, errors)
if (checkIsGdocPost(gdoc)) {
validateRefs(gdoc, errors)
validateExcerpt(gdoc, errors)
validateBreadcrumbs(gdoc, errors)
validateAtomFields(gdoc, errors)
} else if (checkIsDataInsight(gdoc)) {
validateApprovedBy(gdoc, errors)
validateGrapherUrl(gdoc, errors)
} else if (checkIsAuthor(gdoc)) {
validateSocials(gdoc, errors)
}
return errors
}
export const getPropertyFirstErrorOfType = (
type: OwidGdocErrorMessageType,
property: OwidGdocErrorMessageProperty,
errors?: OwidGdocErrorMessage[]
) => errors?.find((error) => error.property === property && error.type === type)
export const getPropertyMostCriticalError = (
property: OwidGdocErrorMessageProperty,
errors: OwidGdocErrorMessage[] | undefined
): OwidGdocErrorMessage | undefined => {
return (
getPropertyFirstErrorOfType(
OwidGdocErrorMessageType.Error,
property,
errors
) ||
getPropertyFirstErrorOfType(
OwidGdocErrorMessageType.Warning,
property,
errors
)
)
}
const getMissingContentPropertyError = (
property: keyof OwidGdocPostContent
) => {
return {
property,
type: OwidGdocErrorMessageType.Error,
message: `Missing ${property}. Add "${property}: ..." at the top of the Google Doc.`,
}
}