@markdoc/markdoc 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +75 -0
- package/dist/index.d.ts +120 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7961 -0
- package/dist/index.js.map +7 -0
- package/dist/index.mjs +7959 -0
- package/dist/index.mjs.map +7 -0
- package/dist/react.js +40 -0
- package/dist/react.js.map +7 -0
- package/dist/src/ast/base.d.ts +6 -0
- package/dist/src/ast/base.d.ts.map +1 -0
- package/dist/src/ast/function.d.ts +9 -0
- package/dist/src/ast/function.d.ts.map +1 -0
- package/dist/src/ast/index.d.ts +26 -0
- package/dist/src/ast/index.d.ts.map +1 -0
- package/dist/src/ast/node.d.ts +21 -0
- package/dist/src/ast/node.d.ts.map +1 -0
- package/dist/src/ast/tag.d.ts +9 -0
- package/dist/src/ast/tag.d.ts.map +1 -0
- package/dist/src/ast/variable.d.ts +8 -0
- package/dist/src/ast/variable.d.ts.map +1 -0
- package/dist/src/functions/index.d.ts +11 -0
- package/dist/src/functions/index.d.ts.map +1 -0
- package/dist/src/parser.d.ts +4 -0
- package/dist/src/parser.d.ts.map +1 -0
- package/dist/src/renderers/html.d.ts +3 -0
- package/dist/src/renderers/html.d.ts.map +1 -0
- package/dist/src/renderers/index.d.ts +10 -0
- package/dist/src/renderers/index.d.ts.map +1 -0
- package/dist/src/renderers/react/react.d.ts +11 -0
- package/dist/src/renderers/react/react.d.ts.map +1 -0
- package/dist/src/renderers/react/shared.d.ts +5 -0
- package/dist/src/renderers/react/shared.d.ts.map +1 -0
- package/dist/src/renderers/react/static.d.ts +3 -0
- package/dist/src/renderers/react/static.d.ts.map +1 -0
- package/dist/src/schema-types/class.d.ts +6 -0
- package/dist/src/schema-types/class.d.ts.map +1 -0
- package/dist/src/schema.d.ts +27 -0
- package/dist/src/schema.d.ts.map +1 -0
- package/dist/src/tags/conditional.d.ts +5 -0
- package/dist/src/tags/conditional.d.ts.map +1 -0
- package/dist/src/tags/index.d.ts +44 -0
- package/dist/src/tags/index.d.ts.map +1 -0
- package/dist/src/tags/partial.d.ts +3 -0
- package/dist/src/tags/partial.d.ts.map +1 -0
- package/dist/src/tags/table.d.ts +3 -0
- package/dist/src/tags/table.d.ts.map +1 -0
- package/dist/src/tokenizer/index.d.ts +10 -0
- package/dist/src/tokenizer/index.d.ts.map +1 -0
- package/dist/src/tokenizer/plugins/annotations.d.ts +3 -0
- package/dist/src/tokenizer/plugins/annotations.d.ts.map +1 -0
- package/dist/src/tokenizer/plugins/frontmatter.d.ts +3 -0
- package/dist/src/tokenizer/plugins/frontmatter.d.ts.map +1 -0
- package/dist/src/transformer.d.ts +12 -0
- package/dist/src/transformer.d.ts.map +1 -0
- package/dist/src/transforms/index.d.ts +4 -0
- package/dist/src/transforms/index.d.ts.map +1 -0
- package/dist/src/transforms/table.d.ts +3 -0
- package/dist/src/transforms/table.d.ts.map +1 -0
- package/dist/src/types.d.ts +88 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/utils.d.ts +6 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/validator.d.ts +6 -0
- package/dist/src/validator.d.ts.map +1 -0
- package/global.d.ts +11 -0
- package/index.ts +138 -0
- package/package.json +63 -0
- package/spec/support/matchers.d.ts +6 -0
- package/src/ast/base.ts +41 -0
- package/src/ast/function.ts +22 -0
- package/src/ast/index.ts +28 -0
- package/src/ast/node.test.ts +307 -0
- package/src/ast/node.ts +71 -0
- package/src/ast/tag.ts +19 -0
- package/src/ast/variable.test.ts +35 -0
- package/src/ast/variable.ts +17 -0
- package/src/functions/index.test.ts +34 -0
- package/src/functions/index.ts +45 -0
- package/src/grammar/tag.d.ts +23 -0
- package/src/grammar/tag.js +1836 -0
- package/src/grammar/tag.pegjs +170 -0
- package/src/grammar/tag.test.ts +364 -0
- package/src/parser.test.ts +597 -0
- package/src/parser.ts +158 -0
- package/src/renderers/html.test.ts +62 -0
- package/src/renderers/html.ts +46 -0
- package/src/renderers/index.ts +5 -0
- package/src/renderers/react/react.test.ts +204 -0
- package/src/renderers/react/react.ts +51 -0
- package/src/renderers/react/shared.ts +16 -0
- package/src/renderers/react/static.ts +52 -0
- package/src/schema-types/class.ts +23 -0
- package/src/schema.ts +233 -0
- package/src/tags/conditional.ts +45 -0
- package/src/tags/index.ts +10 -0
- package/src/tags/partial.ts +33 -0
- package/src/tags/table.ts +5 -0
- package/src/tokenizer/index.ts +21 -0
- package/src/tokenizer/plugins/annotations.test.ts +337 -0
- package/src/tokenizer/plugins/annotations.ts +133 -0
- package/src/tokenizer/plugins/frontmatter.test.ts +86 -0
- package/src/tokenizer/plugins/frontmatter.ts +39 -0
- package/src/transformer.ts +72 -0
- package/src/transforms/index.ts +2 -0
- package/src/transforms/table.ts +51 -0
- package/src/types.ts +142 -0
- package/src/utils.test.ts +173 -0
- package/src/utils.ts +116 -0
- package/src/validator.test.ts +298 -0
- package/src/validator.ts +233 -0
package/src/utils.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { parse, SyntaxError } from './grammar/tag';
|
|
2
|
+
import Variable from './ast/variable';
|
|
3
|
+
import Function from './ast/function';
|
|
4
|
+
|
|
5
|
+
import type Token from 'markdown-it/lib/token';
|
|
6
|
+
|
|
7
|
+
enum STATES {
|
|
8
|
+
normal,
|
|
9
|
+
string,
|
|
10
|
+
escape,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const OPEN = '{%';
|
|
14
|
+
export const CLOSE = '%}';
|
|
15
|
+
|
|
16
|
+
export function findTagEnd(content: string, start = 0) {
|
|
17
|
+
let state = STATES.normal;
|
|
18
|
+
for (let pos = start; pos < content.length; pos++) {
|
|
19
|
+
const char = content[pos];
|
|
20
|
+
|
|
21
|
+
switch (state) {
|
|
22
|
+
case STATES.string:
|
|
23
|
+
switch (char) {
|
|
24
|
+
case '"':
|
|
25
|
+
state = STATES.normal;
|
|
26
|
+
break;
|
|
27
|
+
case '\\':
|
|
28
|
+
state = STATES.escape;
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
break;
|
|
32
|
+
case STATES.escape:
|
|
33
|
+
state = STATES.string;
|
|
34
|
+
break;
|
|
35
|
+
case STATES.normal:
|
|
36
|
+
if (char === '"') state = STATES.string;
|
|
37
|
+
else if (content.startsWith(CLOSE, pos)) return pos;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function parseTag(content: string, line: number, contentStart: number) {
|
|
43
|
+
try {
|
|
44
|
+
return parse(content, { Variable, Function });
|
|
45
|
+
} catch (error) {
|
|
46
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
47
|
+
const {
|
|
48
|
+
message,
|
|
49
|
+
location: { start, end },
|
|
50
|
+
} = error as SyntaxError;
|
|
51
|
+
const location = {
|
|
52
|
+
start: { line, character: start.offset + contentStart },
|
|
53
|
+
end: { line: line + 1, character: end.offset + contentStart },
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
return { type: 'error', meta: { error: { message, location } } };
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function parseTags(content: string, firstLine = 0): Token[] {
|
|
61
|
+
let line = firstLine + 1;
|
|
62
|
+
const output = [];
|
|
63
|
+
let start = 0;
|
|
64
|
+
|
|
65
|
+
for (let pos = 0; pos < content.length; pos++) {
|
|
66
|
+
if (content[pos] === '\n') {
|
|
67
|
+
line++;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!content.startsWith(OPEN, pos)) continue;
|
|
72
|
+
|
|
73
|
+
const end = findTagEnd(content, pos) || 0;
|
|
74
|
+
const text = content.slice(pos, end + CLOSE.length);
|
|
75
|
+
const inner = content.slice(pos + OPEN.length, end);
|
|
76
|
+
const lineStart = content.lastIndexOf('\n', pos);
|
|
77
|
+
const lineEnd = content.indexOf('\n', end);
|
|
78
|
+
const lineContent = content.slice(lineStart, lineEnd);
|
|
79
|
+
const tag = parseTag(inner.trim(), line, pos - lineStart);
|
|
80
|
+
|
|
81
|
+
// Throw away excess whitespace introduced by block-level tags
|
|
82
|
+
const precedingTextEnd = lineContent.trim() === text ? lineStart : pos;
|
|
83
|
+
const precedingText = content.slice(start, precedingTextEnd);
|
|
84
|
+
|
|
85
|
+
output.push({
|
|
86
|
+
type: 'text',
|
|
87
|
+
start,
|
|
88
|
+
end: pos - 1,
|
|
89
|
+
content: precedingText,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
output.push({
|
|
93
|
+
map: [line, line + 1],
|
|
94
|
+
position: {
|
|
95
|
+
start: pos - lineStart,
|
|
96
|
+
end: pos - lineStart + text.length,
|
|
97
|
+
},
|
|
98
|
+
start: pos,
|
|
99
|
+
end: pos + text.length - 1,
|
|
100
|
+
info: text,
|
|
101
|
+
...tag,
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
start = end + CLOSE.length;
|
|
105
|
+
pos = start - 1;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
output.push({
|
|
109
|
+
type: 'text',
|
|
110
|
+
start,
|
|
111
|
+
end: content.length - 1,
|
|
112
|
+
content: content.slice(start),
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
return output;
|
|
116
|
+
}
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import Markdoc from '../index';
|
|
2
|
+
import type { ValidationError } from './types';
|
|
3
|
+
|
|
4
|
+
function validate(string, config) {
|
|
5
|
+
return Markdoc.validate(Markdoc.parse(string), config);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
describe('validate', function () {
|
|
9
|
+
describe('function validation', function () {
|
|
10
|
+
it('ensures that function exists', function () {
|
|
11
|
+
const example = '{% foo bar=baz() /%}';
|
|
12
|
+
const output = validate(example, {
|
|
13
|
+
validation: { validateFunctions: true },
|
|
14
|
+
tags: {
|
|
15
|
+
foo: {
|
|
16
|
+
attributes: {
|
|
17
|
+
bar: { type: String },
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
expect(output).toDeepEqualSubset([
|
|
24
|
+
{
|
|
25
|
+
error: {
|
|
26
|
+
id: 'function-undefined',
|
|
27
|
+
message: "Undefined function 'baz'",
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
]);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe('return type checking', function () {
|
|
34
|
+
const example = '{% foo bar=baz() /%}';
|
|
35
|
+
const schema = {
|
|
36
|
+
validation: { validateFunctions: true },
|
|
37
|
+
functions: {
|
|
38
|
+
baz: {
|
|
39
|
+
returns: String,
|
|
40
|
+
},
|
|
41
|
+
number: {
|
|
42
|
+
returns: Number,
|
|
43
|
+
},
|
|
44
|
+
nested: {
|
|
45
|
+
returns: String,
|
|
46
|
+
parameters: {
|
|
47
|
+
0: { type: String },
|
|
48
|
+
1: { type: Number },
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
withUnion: {
|
|
52
|
+
returns: [String, Number],
|
|
53
|
+
parameters: {},
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
tags: {
|
|
57
|
+
foo: {
|
|
58
|
+
attributes: {
|
|
59
|
+
bar: { type: String },
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
'union-tag-1': {
|
|
63
|
+
attributes: {
|
|
64
|
+
foo: { type: String },
|
|
65
|
+
bar: { type: Number },
|
|
66
|
+
baz: { type: Boolean },
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
it('correctly handles union types', function () {
|
|
73
|
+
const example = '{% union-tag-1 foo=withUnion() bar=withUnion() /%}';
|
|
74
|
+
expect(validate(example, schema)).toEqual([]);
|
|
75
|
+
|
|
76
|
+
const example2 =
|
|
77
|
+
'{% union-tag-1 foo=withUnion() bar=withUnion() baz=withUnion() /%}';
|
|
78
|
+
expect(validate(example2, schema)).toDeepEqualSubset([
|
|
79
|
+
{
|
|
80
|
+
error: {
|
|
81
|
+
id: 'attribute-type-invalid',
|
|
82
|
+
message: "Attribute 'baz' must be type 'Boolean'",
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
]);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('correctly handles return types for nested function calls', function () {
|
|
89
|
+
const example = '{% foo bar=nested(baz(), number()) /%}';
|
|
90
|
+
expect(validate(example, schema)).toEqual([]);
|
|
91
|
+
|
|
92
|
+
const example2 = '{% foo bar=nested(number(), baz()) /%}';
|
|
93
|
+
expect(validate(example2, schema)).toDeepEqualSubset([
|
|
94
|
+
{
|
|
95
|
+
error: {
|
|
96
|
+
id: 'parameter-type-invalid',
|
|
97
|
+
message: "Parameter '0' of 'nested' must be type 'String'",
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
error: {
|
|
102
|
+
id: 'parameter-type-invalid',
|
|
103
|
+
message: "Parameter '1' of 'nested' must be type 'Number'",
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
]);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('accepts a correct return type', function () {
|
|
110
|
+
expect(validate(example, schema)).toEqual([]);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('correctly handles no return type', function () {
|
|
114
|
+
const modifiedSchema = {
|
|
115
|
+
...schema,
|
|
116
|
+
functions: { baz: {} },
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
expect(validate(example, modifiedSchema)).toEqual([]);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('identifies an incorrect return type', function () {
|
|
123
|
+
const modifiedSchema = {
|
|
124
|
+
...schema,
|
|
125
|
+
functions: { baz: { returns: Number } },
|
|
126
|
+
};
|
|
127
|
+
expect(validate(example, modifiedSchema)).toDeepEqualSubset([
|
|
128
|
+
{ error: { id: 'attribute-type-invalid' } },
|
|
129
|
+
]);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe('checks parameters', function () {
|
|
134
|
+
const schema = {
|
|
135
|
+
validation: { validateFunctions: true },
|
|
136
|
+
functions: {
|
|
137
|
+
baz: {
|
|
138
|
+
returns: String,
|
|
139
|
+
parameters: {},
|
|
140
|
+
},
|
|
141
|
+
qux: {
|
|
142
|
+
returns: String,
|
|
143
|
+
parameters: {
|
|
144
|
+
test: { type: String },
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
noTyping: {},
|
|
148
|
+
requiredParam: {
|
|
149
|
+
returns: String,
|
|
150
|
+
parameters: {
|
|
151
|
+
test: { type: String },
|
|
152
|
+
req: { type: String, required: true },
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
tags: {
|
|
157
|
+
foo: {
|
|
158
|
+
attributes: {
|
|
159
|
+
bar: { type: String },
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
it('with a missing optional parameter', function () {
|
|
166
|
+
expect(validate('{% foo bar=qux() /%}', schema)).toEqual([]);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('with a missing required parameter', function () {
|
|
170
|
+
expect(
|
|
171
|
+
validate('{% foo bar=requiredParam() /%}', schema)
|
|
172
|
+
).toDeepEqualSubset([
|
|
173
|
+
{
|
|
174
|
+
error: {
|
|
175
|
+
id: 'parameter-missing-required',
|
|
176
|
+
message: "Missing required parameter 'req'",
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
]);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
describe('accepts defined parameters', function () {
|
|
183
|
+
it('with keyed parameter', function () {
|
|
184
|
+
expect(
|
|
185
|
+
validate('{% foo bar=qux(test="example") /%}', schema)
|
|
186
|
+
).toEqual([]);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('ignores parameters when there is no typing', function () {
|
|
191
|
+
expect(validate('{% foo bar=noTyping(foo=1) /%}', schema)).toEqual([]);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
describe('rejects undeclared parameters', function () {
|
|
195
|
+
it('with a keyed parameter', function () {
|
|
196
|
+
const output = validate('{% foo bar=baz(foo=1) /%}', schema);
|
|
197
|
+
expect(output).toDeepEqualSubset([
|
|
198
|
+
{
|
|
199
|
+
type: 'tag',
|
|
200
|
+
error: {
|
|
201
|
+
id: 'parameter-undefined',
|
|
202
|
+
message: "Invalid parameter 'foo'",
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
]);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('with a positional parameter', function () {
|
|
209
|
+
expect(validate('{% foo bar=baz(1) /%}', schema)).toDeepEqualSubset([
|
|
210
|
+
{
|
|
211
|
+
type: 'tag',
|
|
212
|
+
error: {
|
|
213
|
+
id: 'parameter-undefined',
|
|
214
|
+
message: "Invalid parameter '0'",
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
]);
|
|
218
|
+
|
|
219
|
+
expect(
|
|
220
|
+
validate('{% foo bar=baz(1, test=2) /%}', schema)
|
|
221
|
+
).toDeepEqualSubset([
|
|
222
|
+
{
|
|
223
|
+
type: 'tag',
|
|
224
|
+
error: {
|
|
225
|
+
id: 'parameter-undefined',
|
|
226
|
+
message: "Invalid parameter '0'",
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
]);
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
describe('custom type registration example', () => {
|
|
236
|
+
class Link {
|
|
237
|
+
validate(value) {
|
|
238
|
+
if (value.startsWith('http')) {
|
|
239
|
+
return [];
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const error: ValidationError = {
|
|
243
|
+
id: 'attribute-type-invalid',
|
|
244
|
+
level: 'error',
|
|
245
|
+
message: "Attribute 'href' must be type 'Link'",
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
return [error];
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
it('should return error on failure', () => {
|
|
253
|
+
const example = '{% link href="/relative-link" /%}';
|
|
254
|
+
const output = validate(example, {
|
|
255
|
+
tags: {
|
|
256
|
+
link: {
|
|
257
|
+
render: 'a',
|
|
258
|
+
attributes: {
|
|
259
|
+
href: {
|
|
260
|
+
type: Link,
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
},
|
|
264
|
+
},
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
expect(output).toDeepEqualSubset([
|
|
268
|
+
{
|
|
269
|
+
type: 'tag',
|
|
270
|
+
error: {
|
|
271
|
+
id: 'attribute-type-invalid',
|
|
272
|
+
level: 'error',
|
|
273
|
+
message: "Attribute 'href' must be type 'Link'",
|
|
274
|
+
},
|
|
275
|
+
},
|
|
276
|
+
]);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it('should return no errors when valid', () => {
|
|
280
|
+
const example = '{% link href="http://google.com" /%}';
|
|
281
|
+
const output = validate(example, {
|
|
282
|
+
tags: {
|
|
283
|
+
link: {
|
|
284
|
+
render: 'a',
|
|
285
|
+
selfClosing: true,
|
|
286
|
+
attributes: {
|
|
287
|
+
href: {
|
|
288
|
+
type: Link,
|
|
289
|
+
},
|
|
290
|
+
},
|
|
291
|
+
},
|
|
292
|
+
},
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
expect(output).toEqual([]);
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
});
|
package/src/validator.ts
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { globalAttributes } from './transformer';
|
|
2
|
+
import Ast from './ast/index';
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
Node,
|
|
6
|
+
Function,
|
|
7
|
+
Config,
|
|
8
|
+
SchemaAttribute,
|
|
9
|
+
ValidationError,
|
|
10
|
+
Value,
|
|
11
|
+
} from './types';
|
|
12
|
+
|
|
13
|
+
const TypeMappings = {
|
|
14
|
+
String: String,
|
|
15
|
+
Number: Number,
|
|
16
|
+
Array: Array,
|
|
17
|
+
Object: Object,
|
|
18
|
+
Boolean: Boolean,
|
|
19
|
+
} as const;
|
|
20
|
+
|
|
21
|
+
type TypeParam = NonNullable<SchemaAttribute['type']>;
|
|
22
|
+
|
|
23
|
+
export function validateType(
|
|
24
|
+
type: TypeParam,
|
|
25
|
+
value: Value,
|
|
26
|
+
config: Config
|
|
27
|
+
): boolean | ValidationError[] {
|
|
28
|
+
if (!type) return true;
|
|
29
|
+
|
|
30
|
+
if (Ast.isFunction(value) && config.validation?.validateFunctions) {
|
|
31
|
+
const schema = config.functions?.[value.name];
|
|
32
|
+
return !schema?.returns
|
|
33
|
+
? true
|
|
34
|
+
: Array.isArray(schema.returns)
|
|
35
|
+
? schema.returns.find((t) => t === type) !== undefined
|
|
36
|
+
: schema.returns === type;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (Ast.isAst(value)) return true;
|
|
40
|
+
|
|
41
|
+
if (Array.isArray(type))
|
|
42
|
+
return type.some((t) => validateType(t, value, config));
|
|
43
|
+
|
|
44
|
+
if (typeof type === 'string') type = TypeMappings[type];
|
|
45
|
+
|
|
46
|
+
if (typeof type === 'function') {
|
|
47
|
+
const instance: any = new type();
|
|
48
|
+
if (instance.validate) {
|
|
49
|
+
return instance.validate(value, config);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return value != null && value.constructor === type;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function typeToString(type: TypeParam): string {
|
|
57
|
+
if (typeof type === 'string') return type;
|
|
58
|
+
|
|
59
|
+
if (Array.isArray(type)) return type.map(typeToString).join(' | ');
|
|
60
|
+
|
|
61
|
+
return type.name;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function validateFunction(fn: Function, config: Config): ValidationError[] {
|
|
65
|
+
const schema = config.functions?.[fn.name];
|
|
66
|
+
const errors: ValidationError[] = [];
|
|
67
|
+
|
|
68
|
+
if (!schema)
|
|
69
|
+
return [
|
|
70
|
+
{
|
|
71
|
+
id: 'function-undefined',
|
|
72
|
+
level: 'critical',
|
|
73
|
+
message: `Undefined function '${fn.name}'`,
|
|
74
|
+
},
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
if (schema.validate) errors.push(...schema.validate(fn, config));
|
|
78
|
+
|
|
79
|
+
if (schema.parameters) {
|
|
80
|
+
for (const [key, value] of Object.entries(fn.parameters)) {
|
|
81
|
+
const param = schema.parameters?.[key];
|
|
82
|
+
|
|
83
|
+
if (!param) {
|
|
84
|
+
errors.push({
|
|
85
|
+
id: 'parameter-undefined',
|
|
86
|
+
level: 'error',
|
|
87
|
+
message: `Invalid parameter '${key}'`,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (Ast.isAst(value) && !Ast.isFunction(value)) continue;
|
|
94
|
+
|
|
95
|
+
if (param.type) {
|
|
96
|
+
const valid = validateType(param.type, value, config);
|
|
97
|
+
if (valid === false) {
|
|
98
|
+
errors.push({
|
|
99
|
+
id: 'parameter-type-invalid',
|
|
100
|
+
level: 'error',
|
|
101
|
+
message: `Parameter '${key}' of '${
|
|
102
|
+
fn.name
|
|
103
|
+
}' must be type '${typeToString(param.type)}'`,
|
|
104
|
+
});
|
|
105
|
+
} else if (Array.isArray(valid)) {
|
|
106
|
+
errors.push(...valid);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
for (const [key, { required }] of Object.entries(schema.parameters ?? {}))
|
|
113
|
+
if (required && fn.parameters[key] === undefined)
|
|
114
|
+
errors.push({
|
|
115
|
+
id: 'parameter-missing-required',
|
|
116
|
+
level: 'error',
|
|
117
|
+
message: `Missing required parameter '${key}'`,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
return errors;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export default function validate(node: Node, config: Config) {
|
|
124
|
+
const schema = node.findSchema(config);
|
|
125
|
+
const errors: ValidationError[] = [...(node.errors || [])];
|
|
126
|
+
|
|
127
|
+
if (!schema) {
|
|
128
|
+
errors.push({
|
|
129
|
+
id: node.tag ? 'tag-undefined' : 'node-undefined',
|
|
130
|
+
level: 'critical',
|
|
131
|
+
message: node.tag
|
|
132
|
+
? `Undefined tag '${node.tag}'`
|
|
133
|
+
: `Undefined node '${node.type}'`,
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
return errors;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (schema.selfClosing && node.children.length > 0)
|
|
140
|
+
errors.push({
|
|
141
|
+
id: 'tag-selfclosing-has-children',
|
|
142
|
+
level: 'critical',
|
|
143
|
+
message: `Tag '${node.tag}' should be self-closing`,
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const attributes = {
|
|
147
|
+
...globalAttributes,
|
|
148
|
+
...schema.attributes,
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
if (schema.validate) errors.push(...schema.validate(node, config));
|
|
152
|
+
|
|
153
|
+
for (let [key, value] of Object.entries(node.attributes)) {
|
|
154
|
+
const attrib = attributes[key];
|
|
155
|
+
|
|
156
|
+
if (!attrib) {
|
|
157
|
+
errors.push({
|
|
158
|
+
id: 'attribute-undefined',
|
|
159
|
+
level: 'error',
|
|
160
|
+
message: `Invalid attribute '${key}'`,
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
let { type, matches, errorLevel } = attrib;
|
|
167
|
+
|
|
168
|
+
if (Ast.isAst(value)) {
|
|
169
|
+
if (Ast.isFunction(value) && config.validation?.validateFunctions)
|
|
170
|
+
errors.push(...validateFunction(value, config));
|
|
171
|
+
else continue;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
value = value as string;
|
|
175
|
+
if (key === 'id' && value.match(/^[0-9]/))
|
|
176
|
+
errors.push({
|
|
177
|
+
id: 'attribute-value-invalid',
|
|
178
|
+
level: 'error',
|
|
179
|
+
message: 'The ID attribute must not start with a number',
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
if (type) {
|
|
183
|
+
const valid = validateType(type, value, config);
|
|
184
|
+
if (valid === false) {
|
|
185
|
+
errors.push({
|
|
186
|
+
id: 'attribute-type-invalid',
|
|
187
|
+
level: errorLevel || 'error',
|
|
188
|
+
message: `Attribute '${key}' must be type '${typeToString(type)}'`,
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
if (Array.isArray(valid)) {
|
|
192
|
+
errors.push(...valid);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (typeof matches === 'function') matches = matches(config);
|
|
197
|
+
|
|
198
|
+
if (Array.isArray(matches) && !matches.includes(value))
|
|
199
|
+
errors.push({
|
|
200
|
+
id: 'attribute-value-invalid',
|
|
201
|
+
level: errorLevel || 'error',
|
|
202
|
+
message: `Attribute '${key}' must match one of ${JSON.stringify(
|
|
203
|
+
matches
|
|
204
|
+
)}`,
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
if (matches instanceof RegExp && !matches.test(value))
|
|
208
|
+
errors.push({
|
|
209
|
+
id: 'attribute-value-invalid',
|
|
210
|
+
level: errorLevel || 'error',
|
|
211
|
+
message: `Attribute '${key}' must match ${matches}`,
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
for (const [key, { required }] of Object.entries(attributes))
|
|
216
|
+
if (required && node.attributes[key] === undefined)
|
|
217
|
+
errors.push({
|
|
218
|
+
id: 'attribute-missing-required',
|
|
219
|
+
level: 'error',
|
|
220
|
+
message: `Missing required attribute '${key}'`,
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
for (const { type } of node.children) {
|
|
224
|
+
if (schema.children && type !== 'error' && !schema.children.includes(type))
|
|
225
|
+
errors.push({
|
|
226
|
+
id: 'child-invalid',
|
|
227
|
+
level: 'warning',
|
|
228
|
+
message: `Can't nest '${type}' in '${node.tag || node.type}'`,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return errors;
|
|
233
|
+
}
|