refactor(schema): use one ordered guidance set (#17413)

This commit is contained in:
Vincent Koc
2026-12-06 14:31:34 +08:00
committed by GitHub
parent 69e00599fe
commit aa158d4180
+10 -15
View File
@@ -345,19 +345,12 @@ const typeListForm = (out: Schema): Schema => {
/** One projection: guidance sentences accumulate while the schema is built. */
class DialectProjection {
readonly notes: string[] = [];
readonly #seenNotes = new Set<string>();
note(sentence: string): void {
if (this.#seenNotes.has(sentence)) return;
this.#seenNotes.add(sentence);
this.notes.push(sentence);
}
readonly notes = new Set<string>();
project(value: unknown, path: string): unknown {
if (!isRecord(value)) return value;
const source = mergeAllOf(value, (pattern) =>
this.note(`${label(path)} must also match the pattern ${pattern}.`),
this.notes.add(`${label(path)} must also match the pattern ${pattern}.`),
);
const out: Schema = {};
if (typeof source.type === "string" || isStringList(source.type))
@@ -366,10 +359,12 @@ class DialectProjection {
out.description = truncatedDescription(source.description);
this.objectKeywords(source, out, path);
this.arrayKeywords(source, out, path);
scalarBounds(source, out, (rule) => this.note(`${label(path)} ${rule}.`));
scalarBounds(source, out, (rule) =>
this.notes.add(`${label(path)} ${rule}.`),
);
literalKeywords(source, out);
if (isRecord(source.not) && isStringList(source.not.enum))
this.note(
this.notes.add(
`${label(path)} must not be ${source.not.enum.map((item) => JSON.stringify(item)).join(" or ")}.`,
);
this.unionKeywords(source, out, path);
@@ -380,7 +375,7 @@ class DialectProjection {
* rules precede those of its properties. */
private objectKeywords(source: Schema, out: Schema, path: string): void {
for (const sentence of dependencyGuidance(source))
this.note(path === "" ? sentence : `In ${label(path)}: ${sentence}`);
this.notes.add(path === "" ? sentence : `In ${label(path)}: ${sentence}`);
if (isRecord(source.properties)) {
// `fromEntries` defines each name as an own property, so a property
// named `__proto__` stays a property rather than setting the prototype.
@@ -433,7 +428,7 @@ class DialectProjection {
} else if (isRecord(source.items))
out.items = this.project(source.items, `${path}[]`);
if (source.uniqueItems === true)
this.note(`${label(path)} entries must be distinct.`);
this.notes.add(`${label(path)} entries must be distinct.`);
}
/** One element schema accepting every member; any-value members stay open. */
@@ -505,7 +500,7 @@ class DialectProjection {
path: string,
parentType: unknown,
): unknown[] {
const branchNotes: string[][] = [];
const branchNotes: ReadonlySet<string>[] = [];
const projectedBranches = distinct(
branches.flatMap((branch) => {
const typed =
@@ -531,7 +526,7 @@ class DialectProjection {
}),
);
for (const note of branchNotes[0] ?? [])
if (branchNotes.every((notes) => notes.includes(note))) this.note(note);
if (branchNotes.every((notes) => notes.has(note))) this.notes.add(note);
return projectedBranches;
}
}