Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/tests/unit/schema-validation/object-super.test.ts
6451 views
1
/*
2
* object-super.test.ts
3
*
4
* Copyright (C) 2022 Posit Software, PBC
5
*
6
*/
7
8
import {
9
assertYamlValidationFails,
10
expectValidationError,
11
schemaFromString,
12
yamlValidationUnitTest,
13
} from "./utils.ts";
14
import {
15
readAndValidateYamlFromMappedString,
16
ValidationError,
17
} from "../../../src/core/lib/yaml-schema/validated-yaml.ts";
18
import { asMappedString } from "../../../src/core/lib/mapped-text.ts";
19
import { refSchema } from "../../../src/core/lib/yaml-schema/common.ts";
20
import { assert } from "testing/asserts";
21
22
yamlValidationUnitTest("object-super-closed", async () => {
23
const _schema1 = schemaFromString(`
24
id: object-super-closed-1
25
object:
26
closed: true
27
properties:
28
foo: string
29
required: all
30
`);
31
const schema2 = schemaFromString(`
32
id: object-super-closed-2
33
object:
34
super:
35
resolveRef: object-super-closed-1
36
closed: true
37
properties:
38
bar: string
39
required: all
40
`);
41
42
const obj1 = `
43
bar: yeah
44
`;
45
const obj2 = `
46
foo: yeah
47
`;
48
const obj3 = `
49
bar: yeah
50
foo: yeah
51
`;
52
53
await assertYamlValidationFails(async () => {
54
const { yamlValidationErrors } = await readAndValidateYamlFromMappedString(
55
asMappedString(obj1),
56
schema2,
57
);
58
throw new ValidationError("This should reject", yamlValidationErrors);
59
}, (e) => {
60
return expectValidationError(e)
61
.toHaveLength(1)
62
.forSchemaPathToEndWith("required");
63
});
64
await assertYamlValidationFails(async () => {
65
const { yamlValidationErrors } = await readAndValidateYamlFromMappedString(
66
asMappedString(obj2),
67
schema2,
68
);
69
throw new ValidationError("This should reject", yamlValidationErrors);
70
}, (e) => {
71
return expectValidationError(e)
72
.toHaveLength(1)
73
.forSchemaPathToEndWith("required");
74
});
75
76
const { yamlValidationErrors } = await readAndValidateYamlFromMappedString(
77
asMappedString(obj3),
78
schema2,
79
);
80
assert(yamlValidationErrors.length === 0);
81
});
82
83
yamlValidationUnitTest("object-super", async () => {
84
const _schema1 = schemaFromString(`
85
id: test-schema-1
86
object:
87
properties:
88
foo: string
89
`);
90
const schema2 = schemaFromString(`
91
id: test-schema-2
92
object:
93
super:
94
resolveRef: test-schema-1
95
properties:
96
bar: number
97
`);
98
99
const obj = `
100
foo: 3
101
bar: "bar"
102
`;
103
104
await assertYamlValidationFails(async () => {
105
const { yamlValidationErrors } = await readAndValidateYamlFromMappedString(
106
asMappedString(obj),
107
schema2,
108
);
109
throw new ValidationError("This should reject", yamlValidationErrors);
110
}, (e) => {
111
return expectValidationError(e)
112
.forSchemaPathToEndWith("type", 0)
113
.forSchemaPathToEndWith("type", 1)
114
.toHaveLength(2);
115
});
116
});
117
118
yamlValidationUnitTest("object-super-2", async () => {
119
const obj = `
120
website:
121
title: "Sippin' Yack and Talkin' Smack"
122
twitter-card:
123
image: [profile.jpg, 2, 3, false]
124
`;
125
try {
126
await readAndValidateYamlFromMappedString(
127
asMappedString(obj),
128
refSchema("project-config", ""),
129
);
130
} catch (e: any) {
131
return expectValidationError(e)
132
.forSchemaPathToEndWith("type")
133
.toHaveLength(1);
134
}
135
});
136
137