dry-types

v1.8 v1.7
  1. Introduction
  2. Getting Started
  3. Built-in Types
  4. Type Attributes
  5. Default Values
  6. Fallbacks
  7. Constraints
  8. Hash Schemas
  9. Array With Member
  10. Enum
  11. Map
  12. Combining Types
    1. Intersection
    2. Sum
  13. Custom Types
  14. Custom Type Builders
  15. Extensions
    1. Maybe
    2. Monads

TOC


Intersection

Intersection types are specified using the & operator. It combines two
compatible types into a single one with properties from each.

One example is a Hash that allows any keys, but requires one of them to be named id:

Id = Types::Hash.schema(id: Types::Integer)
HashWithId = Id & Types::Hash

Id[{id: 1}]                             # => {:id=>1}
Id[{id: 1, message: 'foo'}]             # => {:id=>1}
Id[{message: 'foo'}]                    # => Dry::Types::MissingKeyError: :id is missing in Hash input

HashWithId[{ message: 'hello' }]        # => Dry::Types::MissingKeyError: :id is missing in Hash input
HashWithId[{ id: 1, message: 'hello' }] # => {:id=>1, :message=>"hello"}