-
Notifications
You must be signed in to change notification settings - Fork 26
/
display_information.ts
102 lines (94 loc) · 2.64 KB
/
display_information.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
import { type Summary, isSummary } from './summary.js'
import { type DataExample, isDataExample } from './data_example.js'
export interface DisplayInformation {
taskTitle: string
summary: Summary
dataFormatInformation?: string
// TODO merge dataExample
dataExampleText?: string
model?: string
// TODO no need for undefined
dataExample?: DataExample[]
// TODO no need for undefined
headers?: string[]
// Displays the image at this URL in the UI as an example when connecting data
dataExampleImage?: string
// URL to download a dataset for the task, is displayed in the UI when asking to connect data
sampleDatasetLink?: string
// Instructions to download, unzip, and connect the right file of the sample dataset
sampleDatasetInstructions?: string
}
export function isDisplayInformation (raw: unknown): raw is DisplayInformation {
if (typeof raw !== 'object' || raw === null) {
return false
}
const {
dataExample,
dataExampleImage,
dataExampleText,
dataFormatInformation,
sampleDatasetLink,
sampleDatasetInstructions,
headers,
model,
summary,
taskTitle,
}: Partial<Record<keyof DisplayInformation, unknown>> = raw
if (
typeof taskTitle !== 'string' ||
(dataExampleText !== undefined && typeof dataExampleText !== 'string') ||
(sampleDatasetLink !== undefined && typeof sampleDatasetLink !== 'string') ||
(dataFormatInformation !== undefined && typeof dataFormatInformation !== 'string') ||
(model !== undefined && typeof model !== 'string') ||
(dataExampleImage !== undefined && typeof dataExampleImage !== 'string') ||
(sampleDatasetInstructions !== undefined && typeof sampleDatasetInstructions !== 'string')
) {
return false
}
if (!isSummary(summary)) {
return false
}
if (sampleDatasetLink !== undefined) {
try {
new URL(sampleDatasetLink)
} catch {
return false
}
}
if (dataExampleImage !== undefined) {
try {
new URL(dataExampleImage)
} catch {
return false
}
}
if (
dataExample !== undefined && !(
Array.isArray(dataExample) &&
dataExample.every(isDataExample))
) {
return false
}
if (
headers !== undefined && !(
Array.isArray(headers) &&
headers.every((e) => typeof e === 'string'))
) {
return false
}
const repack = {
dataExample,
dataExampleImage,
dataExampleText,
dataFormatInformation,
sampleDatasetLink,
sampleDatasetInstructions,
headers,
model,
summary,
taskTitle,
}
const _correct: DisplayInformation = repack
const _total: Record<keyof DisplayInformation, unknown> = repack
return true
}