Skip to content

Commit d330ccb

Browse files
committed
expose pruneErrors?:boolean in validation, improve narrowing heuristics, restore test
1 parent e271b80 commit d330ccb

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

src/core/lib/yaml-schema/validated-yaml.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const isObject = (value: unknown) => {
4040
export async function readAndValidateYamlFromMappedString(
4141
mappedYaml: MappedString,
4242
schema: Schema,
43+
pruneErrors = true,
4344
): Promise<{
4445
yaml: { [key: string]: unknown };
4546
yamlValidationErrors: LocalizedError[];
@@ -57,7 +58,11 @@ export async function readAndValidateYamlFromMappedString(
5758

5859
const yaml = annotation.result;
5960
if (validateYaml) {
60-
const valResult = await validator.validateParse(mappedYaml, annotation);
61+
const valResult = await validator.validateParse(
62+
mappedYaml,
63+
annotation,
64+
pruneErrors,
65+
);
6166
return {
6267
yaml: yaml as { [key: string]: unknown },
6368
yamlValidationErrors: valResult.errors,

src/core/lib/yaml-validation/validator.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ class ValidationContext {
156156
// suggesting invalid property names is bad if there are other errors to report
157157
return 10;
158158
}
159+
if (t[0] === "required") {
160+
return 0; // we slightly prefer reporting "required" fields.
161+
}
159162
if (t[0] === "type") {
160163
if (t[1] === "null") {
161164
return 10; // suggesting a null value is bad.
@@ -634,8 +637,9 @@ export function validate(
634637
value: AnnotatedParse,
635638
schema: Schema,
636639
source: MappedString,
640+
pruneErrors = true,
637641
): LocalizedError[] {
638642
const context = new ValidationContext();
639643

640-
return context.validate(schema, source, value);
644+
return context.validate(schema, source, value, pruneErrors);
641645
}

src/core/lib/yaml-validation/yaml-schema.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,14 @@ export class YAMLSchema {
6262
async validateParse(
6363
src: MappedString,
6464
annotation: AnnotatedParse,
65+
pruneErrors = true,
6566
) {
66-
const validationErrors = validate(annotation, this.schema, src);
67+
const validationErrors = validate(
68+
annotation,
69+
this.schema,
70+
src,
71+
pruneErrors,
72+
);
6773

6874
if (validationErrors.length) {
6975
const localizedErrors = this.transformErrors(

tests/unit/schema-validation/error-narrowing.test.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ baz: 3
5151
.forSchemaPathToEndWith("required"));
5252
});
5353

54-
/*
5554
// deno-lint-ignore require-await
5655
yamlValidationUnitTest("schema-narrowing", async () => {
5756
const _s1 = schemaFromString(`
@@ -130,13 +129,29 @@ arrayOf:
130129
- part: "-----"
131130
`;
132131

133-
assertYamlValidationFails(async () => {
132+
await assertYamlValidationFails(async () => {
134133
await readAndThrow(ymlStr, s3);
135134
}, (e: ValidationError) => {
136-
console.log(e);
137135
return expectValidationError(e)
138136
.toHaveLength(1)
139137
.forSchemaPathToEndWith("required");
140138
});
141139
});
142-
*/
140+
141+
yamlValidationUnitTest("error-narrowing", async () => {
142+
const schema = schemaFromString(`
143+
id: error-narrowing-schema-test
144+
anyOf:
145+
- string
146+
- record:
147+
foo: string
148+
bar: string`);
149+
150+
await assertYamlValidationFails(async () => {
151+
await readAndThrow("foo: bar", schema);
152+
}, (e: ValidationError) => {
153+
return expectValidationError(e)
154+
.toHaveLength(1)
155+
.forSchemaPathToEndWith("required");
156+
});
157+
});

0 commit comments

Comments
 (0)