Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/tests/unit/schema-validation/error-narrowing.test.ts
6451 views
1
/*
2
* error-narrowing.test.ts
3
*
4
* Unit tests regarding the YAML validation error narrowing heuristics
5
*
6
* Copyright (C) 2022 Posit Software, PBC
7
*
8
*/
9
10
import {
11
assertYamlValidationFails,
12
expectValidationError,
13
schemaFromString,
14
yamlValidationUnitTest,
15
} from "./utils.ts";
16
import {
17
readAndValidateYamlFromMappedString,
18
ValidationError,
19
} from "../../../src/core/lib/yaml-schema/validated-yaml.ts";
20
import { asMappedString } from "../../../src/core/lib/mapped-text.ts";
21
import { Schema } from "../../../src/core/lib/yaml-schema/types.ts";
22
23
const readAndThrow = async (str: string, schema: Schema): Promise<unknown> => {
24
const { yaml, yamlValidationErrors } =
25
await readAndValidateYamlFromMappedString(
26
asMappedString(str),
27
schema,
28
);
29
if (yamlValidationErrors.length) {
30
throw new ValidationError("this should throw", yamlValidationErrors);
31
}
32
return yaml;
33
};
34
35
// deno-lint-ignore require-await
36
yamlValidationUnitTest("schema-narrowing", async () => {
37
const schema = schemaFromString(`
38
id: schema-test-1
39
record:
40
baz: number
41
bar: string
42
`);
43
const ymlStr = `
44
baz: 3
45
`;
46
assertYamlValidationFails(async () => {
47
await readAndThrow(ymlStr, schema);
48
}, (e: ValidationError) =>
49
expectValidationError(e)
50
.toHaveLength(1)
51
.forSchemaPathToEndWith("required"));
52
});
53
54
yamlValidationUnitTest("schema-narrowing", async () => {
55
const _s1 = schemaFromString(`
56
id: navigation-item-test-1
57
anyOf:
58
- path
59
- object:
60
properties:
61
href:
62
string:
63
description: |
64
Link to file contained with the project or external URL
65
url:
66
hidden: true
67
string:
68
description: |
69
Alias for href
70
file:
71
hidden: true
72
string:
73
description: |
74
Alias for href
75
text:
76
string:
77
description: |
78
Text to display for navigation item (defaults to the
79
document title if not provided)
80
icon:
81
string:
82
description:
83
short: Name of bootstrap icon (e.g. \`github\`, \`twitter\`, \`share\`)
84
long: |
85
Name of bootstrap icon (e.g. \`github\`, \`twitter\`, \`share\`)
86
See <https://icons.getbootstrap.com/> for a list of available icons
87
aria-label:
88
string:
89
description: "Accessible label for the navigation item."
90
menu:
91
arrayOf:
92
schema:
93
ref: navigation-item-test-1
94
closed: true
95
`);
96
97
const _s2 = schemaFromString(`
98
id: chapter-item-test-1
99
anyOf:
100
- ref: navigation-item-test-1
101
- record:
102
part:
103
path:
104
description: "Part title or path to input file"
105
chapters:
106
arrayOf:
107
ref: navigation-item-test-1
108
description: "Path to chapter input file"
109
`);
110
111
const s3 = schemaFromString(`
112
id: chapter-list-test-1
113
arrayOf:
114
ref: chapter-item-test-1
115
`);
116
117
const ymlStr = `
118
- part: "Hardening"
119
chapters:
120
- hardening/hardening.qmd
121
- text: "Set Up SSL"
122
file: hardening/set_up_ssl.qmd
123
- hardening/browser_security.qmd
124
- hardening/r_session_security.qmd
125
- hardening/database.qmd
126
- hardening/other.qmd
127
- hardening/example_secure_configuration.qmd
128
- part: "-----"
129
`;
130
131
await assertYamlValidationFails(async () => {
132
await readAndThrow(ymlStr, s3);
133
}, (e: ValidationError) => {
134
return expectValidationError(e)
135
.toHaveLength(1)
136
.forSchemaPathToEndWith("required");
137
});
138
});
139
140
yamlValidationUnitTest("error-narrowing", async () => {
141
const schema = schemaFromString(`
142
id: error-narrowing-schema-test
143
anyOf:
144
- string
145
- record:
146
foo: string
147
bar: string`);
148
149
await assertYamlValidationFails(async () => {
150
await readAndThrow("foo: bar", schema);
151
}, (e: ValidationError) => {
152
return expectValidationError(e)
153
.toHaveLength(1)
154
.forSchemaPathToEndWith("required");
155
});
156
});
157
158