dry-types

v1.8 v1.7
  1. Introduction
  2. Getting Started
  3. Built-in Types
  4. Type Attributes
  5. Default Values
  6. Fallbacks
  7. Sum
  8. Constraints
  9. Hash Schemas
  10. Array With Member
  11. Enum
  12. Map
  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