Fix JSON schema generation for array size predicates #498
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.
Fixes #481
Problem
Array size predicates (
min_size?
,max_size?
) were incorrectly generatingminLength
/maxLength
constraints on array items instead ofminItems
/maxItems
on the array itself, violating JSON Schema specification.Root Cause
The JSON schema compiler applied all predicates to array items when the target type was an array, without distinguishing between array-level constraints (size of array) and item-level constraints (properties of individual items).
Solution
• Enhanced predicate processing to detect array size predicates (
min_size?
,max_size?
)• Added context awareness using the member flag to distinguish array-level vs item-level predicates
• Array size predicates now correctly map to minItems/maxItems when not in item context
• Item-level predicates continue to map to minLength/maxLength as expected
Changes
•
lib/dry/schema/extensions/json_schema/schema_compiler.rb
: Enhanced predicate processing logic•
spec/integration/extensions/json_schema/array_size_predicates_spec.rb
: Comprehensive test coverage•
CHANGELOG.md
: Document the fixBefore/After