Bugs/2520 zod strict with all of #2536
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: Correct handling of
allOfwith strict mode in Zod schemasSummary
This MR fixes an issue where Zod schema generation from OpenAPI specifications with
allOfcontaining multiple object schemas produced incorrect validation code when strict mode was enabled. Previously,.strict()was applied to each object separately and then combined using.and(), which caused validation issues. Now, all object properties are merged into a single object, and strict validation is applied only once to the merged schema.Fixes #2520
Problem
When generating Zod schemas from OpenAPI specifications with
allOfcontaining multiple object schemas and strict mode enabled, the generator produced invalid Zod code:This caused validation issues because:
.and()method creates an intersection that doesn't properly validate unknown keys in strict modeExample OpenAPI Schema
Solution
The fix detects when all parts of an
allOfschema are objects and strict mode is enabled. In such cases, it merges all object properties into a single object and applies.strict()(orstrictObjectfor Zod v4) only once to the final merged schema.Implementation Details
Core Logic Changes
The fix modifies the
parseZodValidationSchemaDefinitionfunction inpackages/zod/src/index.ts:allOfschema are objects when strict mode is enabledmergedPropertiesobject.strict()once to the merged object (or usesstrictObjectfor Zod v4)Design Decisions
Why merge instead of keeping
.and()?.and()method creates intersections that don't properly handle unknown keys in strict modeWhy conditional merging?
.and()for non-object schemas or when strict mode is disabledWhy support both Zod v3 and v4?
zod.object().strict()patternzod.strictObject()as a single methodChanges Made
1. Core Implementation (
packages/zod/src/index.ts)parseZodValidationSchemaDefinitionto detect and handle object-onlyallOfschemas2. Unit Tests (
packages/zod/src/zod.test.ts)Added comprehensive test cases:
handles allOf with strict mode for objects (issue #2520)- tests Zod v3 behaviorhandles allOf with strict mode for objects in Zod v4- tests Zod v4 behaviorBoth tests verify:
.and()is used in the output3. Integration Test
tests/specifications/all-of-strict.yamltests/configs/default.config.ts:Related Issues
Fixes #2520