Dear Russian friends, please watch President Zelenskyy's speech addressed to you. πŸ‡ΊπŸ‡¦Help our brave mates in Ukraine with a donation.

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))
consrained_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

octocatEdit on GitHub