Add support for intersection types in schema definitions #496
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.
Add support for intersection types in schema definitions
Fixes #494
Problem
Dry-schema was failing when using intersection types (created with & operator) in schema definitions, throwing
NoMethodError: undefined method 'visit_intersection'
.Root Cause
The
PredicateInferrer::Compiler
andPrimitiveInferrer::Compiler
classes didn't have methods to handleintersection type AST nodes, and the DSL wasn't processing intersection types like it does for sum types.
Chances are that parts of this change should move upstream to
Dry::Types
.Solution
• Added
visit_intersection
methods to both compiler classes to handle intersection type inference• Extended
extract_type_spec
to process intersection types with AND logic (similar to how sum types useOR
logic)• Intersection types now properly validate that ALL constituent types must pass
Changes
•
lib/dry/schema/predicate_inferrer.rb
: Addvisit_intersection
method•
lib/dry/schema/primitive_inferrer.rb
: Addvisit_intersection
method•
lib/dry/schema/macros/dsl.rb
: Handle intersection types inextract_type_spec
•
spec/integration/schema/intersection_types_spec.rb
: Comprehensive test coverage•
CHANGELOG.md
: Document the fixTesting
• Validates the exact example from the issue works correctly
• Tests intersection with sum types (complex algebraic data types)
• Tests DSL integration with predicate rules
• Tests proper validation logic (AND semantics)
Example Usage