Skip to content

Conversation

baweaver
Copy link

Fixes #481

Problem

Array size predicates (min_size?, max_size?) were incorrectly generating minLength/maxLength constraints on array items instead of minItems/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 fix

Before/After

# Before: Incorrect JSON schema
required(:users).value(:array?, min_size?: 5).each(:str?)
# => {type: "array", items: {type: "string", minLength: 5}}

# After: Correct JSON schema  
required(:users).value(:array?, min_size?: 5).each(:str?)
# => {type: "array", minItems: 5, items: {type: "string"}}

- Use minItems/maxItems for array size constraints instead of minLength/maxLength
- Add context awareness to distinguish array-level vs item-level predicates
- Preserve existing behavior for string size constraints
- Add comprehensive test coverage

Fixes dry-rb#481
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JSON schema array length definition (min|maxItems) issue
1 participant