-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfile-field.mjs
More file actions
43 lines (38 loc) · 1.29 KB
/
Copy pathfile-field.mjs
File metadata and controls
43 lines (38 loc) · 1.29 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
/**
* Asserts that the output contains a form with a file-typed field.
*
* Optional config:
* - sensitive: boolean — asserts the file field sets `sensitive: true`
*
* Note: `accept` and `multiple` are renderer-level concerns and are NOT part
* of the MDMA spec, so they are not asserted here.
*/
export default function (output, { config } = {}) {
const blockRegex = /```mdma\n([\s\S]*?)```/g;
const blocks = [...output.matchAll(blockRegex)].map((m) => m[1]);
const formBlocks = blocks.filter((b) => /^type:\s*form/m.test(b));
if (formBlocks.length === 0) {
return { pass: false, score: 0, reason: 'No form block found in output' };
}
const fileBlock = formBlocks.find((b) => /type:\s*file\b/.test(b));
if (!fileBlock) {
return {
pass: false,
score: 0,
reason: 'No form field with `type: file` found',
};
}
const reasons = ['Form contains a file field'];
if (config?.sensitive === true) {
const sensitivePattern = /type:\s*file[\s\S]{0,200}sensitive:\s*true/;
if (!sensitivePattern.test(fileBlock)) {
return {
pass: false,
score: 0,
reason: 'File field expected sensitive: true but not found',
};
}
reasons.push('sensitive: true');
}
return { pass: true, score: 1, reason: reasons.join('; ') };
}