Files
rea/src/contracts/jsonSchemaKeywords.ts
T
morluto d0fee4e58e fix(mcp): re-emit advertised patterns in a portable regex grammar (#10582)
* test(mcp): check advertised schemas against a portable dialect in every profile

* fix(mcp): re-emit advertised patterns in a portable regex grammar

* fix(mcp): validate advertised dialect by selected profile

* fix(mcp): preserve schema widening through references

* fix(mcp): refuse zero-padded repeat bounds and record host-specific path patterns

* fix(mcp): keep references valid when presentation widens their targets

* fix(mcp): relax references into negations and conditionals presentation may remove

* fix(mcp): keep astral literals and dynamic references widening in every mode

* fix(mcp): omit astral literals and relax untracked references when presentation widens

* fix(mcp): read reference pointers by schema position, not segment spelling

* fix(mcp): protect references into collapsing boolean unions and keep referencing branches out of the collapse

* fix(mcp): omit patterns whose classes name astral characters

* fix(mcp): decide union collapse and pointer protection from canonical branches

* docs(mcp): document non-u astral matching and pin BMP agreement across V8 modes

* test(mcp): expect the advertised base64 schema to omit the unbounded pad-bit probe

* fix(mcp): protect references into unevaluated keywords lost evaluation removes

* fix(mcp): emit stack-safe classes with non-ASCII members and protect draft-07 reference siblings

* fix(mcp): protect references into literal siblings of relaxed draft-07 references
2026-11-15 17:06:43 +08:00

96 lines
2.8 KiB
TypeScript

export const schemaMaps = new Set([
"$defs",
"definitions",
"properties",
"patternProperties",
"dependentSchemas",
"dependencies",
]);
export const schemaArrays = new Set(["allOf", "anyOf", "oneOf", "prefixItems"]);
export const schemaChildren = new Set([
"items",
"additionalItems",
"contains",
"additionalProperties",
"unevaluatedProperties",
"unevaluatedItems",
"propertyNames",
"contentSchema",
"not",
"if",
"then",
"else",
]);
/** Visit schema-valued positions without interpreting instance-valued keywords. */
export const schemaSubschemas = (
schema: Readonly<Record<string, unknown>>,
options: { readonly includeDefinitions: boolean },
): unknown[] =>
Object.entries(schema).flatMap(([key, value]) => {
if (
schemaMaps.has(key) &&
typeof value === "object" &&
value !== null &&
!Array.isArray(value)
)
return !options.includeDefinitions &&
(key === "$defs" || key === "definitions")
? []
: Object.values(value);
if (schemaArrays.has(key) && Array.isArray(value)) return value;
if (schemaChildren.has(key)) return Array.isArray(value) ? value : [value];
return [];
});
/** Instance values, unless explicitly reached by a schema reference. */
export const schemaLiteralValues = new Set([
"const",
"enum",
"default",
"examples",
]);
/** One segment of a JSON Pointer into a schema, classified by position. */
export type SchemaPointerStep =
| { readonly kind: "keyword"; readonly keyword: string }
| { readonly kind: "entry" }
| { readonly kind: "value"; readonly keyword: string };
/**
* Classify each segment of a JSON Pointer into a schema by its position, so a
* map entry or property named like a keyword is never read as that keyword.
* The walk ends at the first keyword that is not a subschema keyword (such as
* `default`); the segments after it address instance data and are omitted.
*/
export const schemaPointerSteps = (
path: readonly string[],
): SchemaPointerStep[] => {
const steps: SchemaPointerStep[] = [];
let position: "schema" | "entry" | "schemaOrIndex" = "schema";
for (const segment of path) {
if (
position === "entry" ||
(position === "schemaOrIndex" && /^(?:0|[1-9]\d*)$/u.test(segment))
) {
steps.push({ kind: "entry" });
position = "schema";
continue;
}
if (schemaMaps.has(segment) || schemaArrays.has(segment)) {
steps.push({ kind: "keyword", keyword: segment });
position = "entry";
continue;
}
if (schemaChildren.has(segment)) {
steps.push({ kind: "keyword", keyword: segment });
// `items` and its siblings may hold a draft-07 tuple array.
position = "schemaOrIndex";
continue;
}
steps.push({ kind: "value", keyword: segment });
break;
}
return steps;
};