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


Custom Type Builders

It is idiomatic to construct new types based on existing.

source_type = Dry::Types['integer']
constructor_type = source_type.constructor(Kernel.method(:Integer))
constrained_type = constructor_type.constrained(gteq: 18)

This API can be extended with Dry::Types.define_builder

Dry::Types.define_builder(:or) { |type, value| type.fallback(value) }

source_type = Dry::Types['integer']
type = source_type.or(0)
type.(10) # => 10
type.(:invalid) # => 0