-
Notifications
You must be signed in to change notification settings - Fork 392
/
block.ts
219 lines (191 loc) · 6.15 KB
/
block.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
import {omit, isPlainObject} from 'lodash'
import humanizeList from 'humanize-list'
import {error, warning} from '../createValidationResult'
import {isJSONTypeOf} from '../utils/isJSONTypeOf'
const getTypeOf = (thing) => (Array.isArray(thing) ? 'array' : typeof thing)
const quote = (str) => `"${str}"`
const allowedKeys = [
'components',
'lists',
'marks',
'name',
'of',
'options',
'styles',
'title',
'type',
'validation',
]
const allowedMarkKeys = ['decorators', 'annotations']
const allowedStyleKeys = ['title', 'value', 'components']
const allowedDecoratorKeys = ['title', 'value', 'icon', 'components']
export default function validateBlockType(typeDef, visitorContext) {
const problems = []
let styles = typeDef.styles
let marks = typeDef.marks
let members = typeDef.of
const disallowedKeys = Object.keys(typeDef).filter(
(key) => !allowedKeys.includes(key) && !key.startsWith('_')
)
if (disallowedKeys.length > 0) {
problems.push(
error(
`Found unknown properties for block declaration: ${humanizeList(disallowedKeys.map(quote))}`
)
)
}
if (marks) {
marks = validateMarks(typeDef.marks, visitorContext, problems)
}
if (styles) {
styles = validateStyles(styles, visitorContext, problems)
}
if (members) {
members = validateMembers(members, visitorContext, problems)
}
return {
...omit(typeDef, disallowedKeys),
marks,
styles,
name: typeDef.name || typeDef.type,
of: members,
_problems: problems,
}
}
function validateMarks(marks, visitorContext, problems) {
let decorators = marks.decorators
let annotations = marks.annotations
if (!isPlainObject(marks)) {
problems.push(error(`"marks" declaration should be an object, got ${getTypeOf(marks)}`))
return problems
}
const disallowedMarkKeys = Object.keys(marks).filter(
(key) => !allowedMarkKeys.includes(key) && !key.startsWith('_')
)
if (disallowedMarkKeys.length > 0) {
problems.push(
error(
`Found unknown properties for block declaration: ${humanizeList(
disallowedMarkKeys.map(quote)
)}`
)
)
}
if (decorators && !Array.isArray(decorators)) {
problems.push(
error(`"marks.decorators" declaration should be an array, got ${getTypeOf(decorators)}`)
)
} else if (decorators) {
decorators = validateDecorators(decorators, visitorContext, problems)
}
if (annotations && !Array.isArray(annotations)) {
problems.push(
error(`"marks.annotations" declaration should be an array, got ${getTypeOf(annotations)}`)
)
} else if (annotations) {
annotations = validateAnnotations(annotations, visitorContext, problems)
}
return {...marks, decorators, annotations}
}
function validateStyles(styles, visitorContext, problems) {
if (!Array.isArray(styles)) {
problems.push(error(`"styles" declaration should be an array, got ${getTypeOf(styles)}`))
return problems
}
styles.forEach((style, index) => {
if (!isPlainObject(style)) {
problems.push(error(`Style must be an object, got ${getTypeOf(style)}`))
return
}
const name = style.value || `#${index}`
const disallowedKeys = Object.keys(style).filter(
(key) => !allowedStyleKeys.includes(key) && !key.startsWith('_')
)
if (disallowedKeys.length > 0) {
problems.push(
error(
`Found unknown properties for style ${name}: ${humanizeList(disallowedKeys.map(quote))}`
)
)
}
if (!style.value) {
problems.push(error(`Style #${index} is missing required "value" property`))
} else if (typeof style.value !== 'string') {
problems.push(
error(
`Style #${index} has an invalid "value" property, expected string, got ${getTypeOf(
style.value
)}`
)
)
} else if (!style.title) {
problems.push(warning(`Style ${name} is missing recommended "title" property`))
}
})
return styles
}
function validateDecorators(decorators, visitorContext, problems) {
decorators.forEach((decorator, index) => {
if (!isPlainObject(decorator)) {
problems.push(error(`Annotation must be an object, got ${getTypeOf(decorator)}`))
return
}
const name = decorator.value || `#${index}`
const disallowedKeys = Object.keys(decorator).filter(
(key) => !allowedDecoratorKeys.includes(key) && !key.startsWith('_')
)
if (disallowedKeys.length > 0) {
problems.push(
error(
`Found unknown properties for decorator ${name}: ${humanizeList(
disallowedKeys.map(quote)
)}`
)
)
}
if (!decorator.value) {
problems.push(error(`Decorator #${index} is missing required "value" property`))
} else if (typeof decorator.value !== 'string') {
problems.push(
error(
`Decorator #${index} has an invalid "value" property, expected string, got ${getTypeOf(
decorator.value
)}`
)
)
} else if (!decorator.title) {
problems.push(warning(`Decorator ${name} is missing recommended "title" property`))
}
})
return decorators
}
function validateAnnotations(annotations, visitorContext, problems) {
return annotations.map((annotation) => {
if (!isPlainObject(annotation)) {
return {
...annotation,
_problems: [error(`Annotation must be an object, got ${getTypeOf(annotation)}`)],
}
}
const {_problems} = visitorContext.visit(annotation, visitorContext)
const targetType = annotation.type && visitorContext.getType(annotation.type)
if (targetType && !isJSONTypeOf(targetType, 'object', visitorContext)) {
_problems.push(
error(
`Annotation cannot have type "${annotation.type}" - annotation types must inherit from object`
)
)
}
return {...annotation, _problems}
})
}
function validateMembers(members, visitorContext, problems) {
if (!Array.isArray(members)) {
problems.push(error(`"of" declaration should be an array, got ${getTypeOf(members)}`))
return undefined
}
return members.map((member) => {
const {_problems} = visitorContext.visit(member, visitorContext)
return {...member, _problems}
})
}