Repository navigation
Conversation
…in JSON schemas
The JSON Schema to Zod converter ignored `minProperties` and
`maxProperties`, and converted a schema with no `type` to `z.unknown()`.
The second turns `not: {required: [...]}` into "reject every value", so a
valid value is refused.
Apply both bounds to arguments objects and to object properties, counting
keys on the value as written. Convert a typeless schema that declares object
keywords so it checks the objects it matches and accepts every other value,
as JSON Schema does.
Rows 1, 2 and 6 of a2ui-project#3998. The top-level `oneOf`, `anyOf` and `not` gaps are
left for the approach the issue asks maintainers to choose.
Part of a2ui-project#3998
There was a problem hiding this comment.
Code Review
This pull request introduces support for enforcing minProperties and maxProperties constraints on JSON schemas, and properly handles typeless subschemas that declare object keywords within not schemas. The feedback suggests ensuring that minProperties and maxProperties are validated as non-negative integers to fully align with the JSON Schema specification.
| const min = typeof raw.minProperties === 'number' ? raw.minProperties : undefined; | ||
| const max = typeof raw.maxProperties === 'number' ? raw.maxProperties : undefined; |
There was a problem hiding this comment.
To align with the JSON Schema specification and ensure robust validation, minProperties and maxProperties should be validated as non-negative integers. This prevents negative values or floating-point numbers from causing unexpected validation behavior.
const min =
typeof raw.minProperties === 'number' &&
Number.isInteger(raw.minProperties) &&
raw.minProperties >= 0
? raw.minProperties
: undefined;
const max =
typeof raw.maxProperties === 'number' &&
Number.isInteger(raw.maxProperties) &&
raw.maxProperties >= 0
? raw.maxProperties
: undefined;There was a problem hiding this comment.
Applied in 41b4558, with a test that a negative or fractional bound is ignored. Thanks.
…n-negative integers
…-not # Conflicts: # typescript/core/web_core/CHANGELOG.md
|
Hi @nan-yu, this covers rows 1, 2 and 6 of #3998 ( |
…-not # Conflicts: # typescript/core/web_core/CHANGELOG.md # typescript/core/web_core/src/catalog/schema_loader.ts
|
From triage bot, analysis: For humansTL;DR Summary: Targeted issues: Why flagged for triage: Suggestion:
To adjust the analysis and apply its suggestion, run in your a2ui checkout: tools/internal/triage/triage_ui.sh https://github.com/a2ui-project/a2ui/pull/4057For softwareThis analysis is auto-created to help humans to triage this item. Please, do not treat it as a team decision. 🤖 Agent-generated |
…-not # Conflicts: # typescript/core/web_core/CHANGELOG.md
…-not # Conflicts: # typescript/core/web_core/CHANGELOG.md
…-not # Conflicts: # typescript/core/web_core/CHANGELOG.md
gspencergoog
left a comment
There was a problem hiding this comment.
This change adds minProperties and maxProperties validation to JSON Schema objects loaded by schema_loader.ts (for Catalog.fromJson properties and AgentFunction arguments) via withPropertyCountBounds, and adds convertTypelessToZod so schemas without type that declare object keywords validate objects while letting non-objects pass.
The core withPropertyCountBounds and convertTypelessToZod helpers handle zero bounds, invalid bounds, non-object pass-through, and ZodEffects key extraction cleanly, and the prior gemini-code-assist thread requesting non-negative integer bound checks is addressed at HEAD. However, allOf and $ref argument subschemas that declare only minProperties or maxProperties are dropped in collectArgSubSchemas, allOf-composed properties bypass withPropertyCountBounds, convertTypelessToZod and convertPropertyToZod drop additionalProperties: false and unevaluatedProperties: false when properties and patternProperties are absent, CHANGELOG.md misplaces and mislabels a validation-tightening entry, and key test invariants lack coverage.
🤖 Agent-generated
| ); | ||
| } | ||
| } | ||
| for (const piece of new Set(allSchemas)) { |
There was a problem hiding this comment.
allSchemas omits any allOf branch or $ref target that declares only minProperties or maxProperties because collectArgSubSchemas does not check those keywords. Function argument schemas composing property-count bounds via allOf or $ref silently accept payloads that violate those bounds.
--- a/typescript/core/web_core/src/catalog/schema_loader.ts
+++ b/typescript/core/web_core/src/catalog/schema_loader.ts
@@ -1185,7 +1185,9 @@ function collectArgSubSchemas(
requiredDisjunction ||
schema.patternProperties ||
schema.additionalProperties !== undefined ||
- schema.unevaluatedProperties !== undefined
+ schema.unevaluatedProperties !== undefined ||
+ schema.minProperties !== undefined ||
+ schema.maxProperties !== undefined
) {
result.schemas.push(schema);
}Details
Impact: Function argument schemas composing property-count bounds via allOf or $ref silently accept payloads that violate those bounds.
Evidence (Deduction): schema_loader.ts:1235 collects subschemas via collectArgSubSchemas; schema_loader.ts:1182-1191 only pushes schema when properties, required, requiredDisjunction, patternProperties, additionalProperties, or unevaluatedProperties is present; schema_loader.ts:1267 builds allSchemas from [rawSchema, ...collected.schemas], so schema_loader.ts:1287-1289 skips bounds-only branches.
For example, {allOf: [{type: 'object', properties: {a: {type: 'string'}}}, {minProperties: 1}]} or {$ref: '#/$defs/Bounded'} where Bounded is {type: 'object', maxProperties: 1} resolves cleanly in collectArgSubSchemas, fails the guard at schema_loader.ts:1182-1189, and never reaches withPropertyCountBounds at schema_loader.ts:1288. Adding schema.minProperties !== undefined || schema.maxProperties !== undefined to collectArgSubSchemas is safe for all existing loops over collected.schemas and allSchemas, because each loop guards on its own keyword.
🤖 Agent-generated
| } | ||
|
|
||
| /** Keywords that constrain an object even when the schema declares no `type`. */ | ||
| const TYPELESS_OBJECT_KEYWORDS = [ |
There was a problem hiding this comment.
TYPELESS_OBJECT_KEYWORDS omits additionalProperties and unevaluatedProperties, and convertPropertyToZod's object branch falls back to z.record(z.unknown()) whenever properties and patternProperties are absent. {not: {additionalProperties: false}} rejects every input, and {required: ['b'], additionalProperties: false} silently accepts disallowed keys.
--- a/typescript/core/web_core/src/catalog/schema_loader.ts
+++ b/typescript/core/web_core/src/catalog/schema_loader.ts
@@ -235,6 +235,8 @@ const TYPELESS_OBJECT_KEYWORDS = [
'required',
'minProperties',
'maxProperties',
+ 'additionalProperties',
+ 'unevaluatedProperties',
];
@@ -657,6 +659,8 @@ function convertPropertyToZod(
const hasPatterns =
typeof propSchema.patternProperties === 'object' && propSchema.patternProperties !== null;
+ const forbidExtra =
+ propSchema.additionalProperties === false || propSchema.unevaluatedProperties === false;
let obj: z.ZodTypeAny;
- if ((typeof props === 'object' && props !== null) || hasPatterns) {
+ if ((typeof props === 'object' && props !== null) || hasPatterns || forbidExtra) {
@@ -673,8 +677,6 @@ function convertPropertyToZod(
visitedPointers,
defCache,
),
);
- const forbidExtra =
- propSchema.additionalProperties === false || propSchema.unevaluatedProperties === false;
obj = closeObjectSchema(Details
Impact: {not: {additionalProperties: false}} rejects every input, and {required: ['b'], additionalProperties: false} silently accepts disallowed keys.
Evidence (Deduction): schema_loader.ts:232-238 omits additionalProperties and unevaluatedProperties from TYPELESS_OBJECT_KEYWORDS, so schema_loader.ts:259-260 returns z.unknown(); when paired with required or bounds without properties, schema_loader.ts:273-278 calls convertPropertyToZod({...keywords, type: 'object'}), which hits schema_loader.ts:685 (obj = z.record(z.unknown())).
When propSchema is {additionalProperties: false} inside not, convertTypelessToZod returns z.unknown(), which matches {a: 1} as well as {} and causes not to reject every object. Even when combined with a listed keyword such as {required: ['b'], additionalProperties: false}, stripping required leaves {additionalProperties: false, type: 'object'}, which bypasses closeObjectSchema because props and hasPatterns are both absent. Routing forbidExtra through closeObjectSchema with an empty props map produces z.object({}).strict(), rejecting non-empty objects while preserving non-object pass-through in convertTypelessToZod.
🤖 Agent-generated
| } | ||
| return finalizePropertyZod(obj, propSchema, rootDoc, visitedPointers, defCache); | ||
| return finalizePropertyZod( | ||
| withPropertyCountBounds(obj, propSchema), |
There was a problem hiding this comment.
withPropertyCountBounds is wired only into case 'object' and convertFunctionArgsJsonSchemaToZod, skipping convertAllOfPropertyToZod and collectComponentSubSchemas. Component or nested properties composed with allOf silently ignore minProperties and maxProperties declared on either the outer schema or any branch.
--- a/typescript/core/web_core/src/catalog/schema_loader.ts
+++ b/typescript/core/web_core/src/catalog/schema_loader.ts
@@ -481,13 +481,17 @@ function convertAllOfPropertyToZod(
s.kind === 'json' &&
(s.schema.additionalProperties === false || s.schema.unevaluatedProperties === false),
);
- const obj = closeObjectSchema(
+ const jsonPieces = [
+ propSchema,
+ ...subSchemas.flatMap(s =>
+ s.kind === 'json' && s.schema !== propSchema ? [s.schema] : [],
+ ),
+ ];
+ let obj = closeObjectSchema(
baseObj,
- [
- propSchema,
- ...subSchemas.flatMap(s =>
- s.kind === 'json' && s.schema !== propSchema ? [s.schema] : [],
- ),
- ],
+ jsonPieces,
forbidExtra,
rootDoc,
visitedPointers,
defCache,
);
+ for (const piece of new Set(jsonPieces)) {
+ obj = withPropertyCountBounds(obj, piece);
+ }
return finalizePropertyZod(obj, propSchema, rootDoc, visitedPointers, defCache);
}
}
@@ -505,7 +509,13 @@ function convertAllOfPropertyToZod(
for (let i = 1; i < zodBranches.length; i++) {
intersected = intersected.and(zodBranches[i]);
}
- return finalizePropertyZod(intersected, propSchema, rootDoc, visitedPointers, defCache);
+ return finalizePropertyZod(
+ withPropertyCountBounds(intersected, propSchema),
+ propSchema,
+ rootDoc,
+ visitedPointers,
+ defCache,
+ );
}Details
Impact: Component or nested properties composed with allOf silently ignore minProperties and maxProperties declared on either the outer schema or any branch.
Evidence (Deduction): schema_loader.ts:556-559 dispatches allOf properties to convertAllOfPropertyToZod before case 'object' (schema_loader.ts:688); schema_loader.ts:484-497 flattens subSchemas into z.object(shape) and calls finalizePropertyZod(obj, ...) without withPropertyCountBounds, and schema_loader.ts:508 also omits withPropertyCountBounds on propSchema.
Catalog.fromJson and AgentFunction always pass rootDoc. An allOf property like {allOf: [{$ref: '#/$defs/NonEmpty'}, {properties: {b: {type: 'string'}}}]} enters the flattening path at schema_loader.ts:438-498. That path drops minProperties and maxProperties on both #/$defs/NonEmpty and the outer schema. Also, collectComponentSubSchemas (schema_loader.ts:1010) only keeps fragments that declare properties, so it drops bounds-only allOf branches too.
🤖 Agent-generated
| @@ -1,5 +1,6 @@ | |||
| ## Unreleased | |||
|
|
|||
| - Function arguments and component properties defined in JSON Schema (by `Catalog.fromJson` or an `AgentFunction` `schema`) enforce `minProperties` and `maxProperties`, and a `not` subschema with no `type` that declares object keywords (`required`, `properties`, `patternProperties`, `minProperties`, `maxProperties`) checks the objects it matches instead of matching every value, so a `not` over it no longer rejects everything. Such a subschema accepts non-objects, as in JSON Schema. [#3998](https://github.com/a2ui-project/a2ui/issues/3998), [#4057](https://github.com/a2ui-project/a2ui/pull/4057) | |||
There was a problem hiding this comment.
This entry tightens validation rules without a **BREAKING CHANGE**: prefix or migration note and sits above existing **BREAKING CHANGE**: bullets. It also narrows typeless object validation to not subschemas, though all typeless schemas now enforce those keywords. Consumers upgrading may hit unexpected validation rejections on payloads that previously passed, and the entry conflicts with repository changelog ordering rules.
| - Function arguments and component properties defined in JSON Schema (by `Catalog.fromJson` or an `AgentFunction` `schema`) enforce `minProperties` and `maxProperties`, and a `not` subschema with no `type` that declares object keywords (`required`, `properties`, `patternProperties`, `minProperties`, `maxProperties`) checks the objects it matches instead of matching every value, so a `not` over it no longer rejects everything. Such a subschema accepts non-objects, as in JSON Schema. [#3998](https://github.com/a2ui-project/a2ui/issues/3998), [#4057](https://github.com/a2ui-project/a2ui/pull/4057) | |
| - **BREAKING CHANGE**: Function arguments and component properties defined in JSON Schema (by `Catalog.fromJson` or an `AgentFunction` `schema`) enforce `minProperties` and `maxProperties`, and any typeless schema that declares object keywords (`required`, `properties`, `patternProperties`, `minProperties`, `maxProperties`) validates object values while accepting non-objects, so a `not` over one no longer rejects every value. Recommended migration: ensure payloads satisfy declared property-count bounds and typeless object constraints. [#3998](https://github.com/a2ui-project/a2ui/issues/3998), [#4057](https://github.com/a2ui-project/a2ui/pull/4057) |
Details
Impact: Consumers upgrading may hit unexpected validation rejections on payloads that previously passed, and the entry conflicts with repository changelog ordering rules.
Evidence (Read): "- Function arguments and component properties defined in JSON Schema (by \Catalog.fromJson` or an `AgentFunction` `schema`) enforce `minProperties` and `maxProperties`, and a `not` subschema with no `type` that declares object keywords..." (typescript/core/web_core/CHANGELOG.md:3), placed above three BREAKING CHANGE: bullets (typescript/core/web_core/CHANGELOG.md:4-6); schema_loader.ts:696-702callsconvertTypelessToZodfor all typeless schemas, not onlynotsubschemas;.agents/skills/update-changelog/SKILL.md:60-63requiresBREAKING CHANGE:first for validation-rule changes (matchingCHANGELOG.md:18`).
🤖 Agent-generated
| assert.throws(() => validateWith(schema, {o: {a: 'y', b: 'x'}}), isValidationError); | ||
| }); | ||
|
|
||
| it('checks minProperties and maxProperties on an arguments object and on a property', () => { |
There was a problem hiding this comment.
The new tests never exercise minProperties or maxProperties alongside property defaults, so replacing z.preprocess with .superRefine in withPropertyCountBounds would leave the suite green despite violating the documented contract (schema_loader.ts:196-197). They also skip maxProperties: 0, standalone typeless object schemas, the other TYPELESS_OBJECT_KEYWORDS, and Catalog.fromJson component properties. Regressions in pre-default counting, zero-bound truthiness checks, or component property loading would ship undetected.
Details
Impact: Regressions in pre-default counting, zero-bound truthiness checks, or component property loading would ship undetected.
Evidence (Read): agent_functions.test.ts:1041-1080 tests only maxProperties: 1, minProperties: 1, malformed bounds [-1, 1.5], and not: {required: ['b']}, with no defaults on bounded properties, no zero upper bound, no Catalog.fromJson component property, and none of the other four keywords in TYPELESS_OBJECT_KEYWORDS (schema_loader.ts:232-238).
🤖 Agent-generated
…-not # Conflicts: # typescript/core/web_core/CHANGELOG.md
…ess schemas Mark the changelog entry breaking, name that a schema with no type enforces its object keywords everywhere, not only under not, and add a migration note. Add tests for maxProperties 0, bounds counted before defaults fill in, and standalone typeless object schemas.
|
Thanks @gspencergoog, this is a useful review. In 9c86dcc:
I have not taken the rest, which widens this PR beyond the rows I started from:
|
|
@Anoshor Thanks for updating the changelog and adding those tests. Leaving the Two small things before we can merge:
|
Description
Rows 1, 2 and 6 of #3998. These are the gaps in the table that are self-contained and don't depend on which of the two options in the issue is chosen; row 6 is the one the issue says could be split out and fixed sooner, because it refuses a valid value.
minPropertiesandmaxPropertiesweren't translated. They now apply to an arguments object (including constraints onallOfpieces) and to an object property. Keys are counted on the value as written, before the object schema fills in defaults or drops undeclared keys, with the samez.preprocesspatternfinalizePropertyZoduses fornot.typebecamez.unknown(), sonot: {required: ['b']}matched every value and rejected everything. A typeless schema that declares object keywords (properties,patternProperties,required,minProperties,maxProperties) is now converted so it checks the objects it matches and accepts every other value, as JSON Schema does. A typeless schema with none of those keywords is stillz.unknown().requiredis checked directly in the new helper: an object withrequiredbut nopropertiesis still converted to an opaque record by the existingobjectcase, which I left unchanged.Rows 3 to 5 (top-level
oneOf,anyOfandnot) are not touched; they depend on the choice between patching the converter and validating with a JSON Schema validator.Verification:
agent_functions.test.ts(PayloadValidator with AgentFunction) cover rows 1, 2 and 6, plus amaxPropertieson a nested property and the non-object cases for a typelessnot. Both fail onmainand pass here.yarn testintypescript/core/web_core: 1536 passed, 0 failed (includes the conformance suites).eslintreports no new warnings;prettier --checkis clean.Part of #3998
Pre-launch Checklist
One time:
For this PR: