dry-types master

Fallbacks

Fallback value will be returned when invalid input is provided:

type = Dry::Types['integer'].fallback(100)

type.(99) # => 99
type.('99') # => 100
type.(:invalid) # => 100

Block syntax:

cnt = 0
type = Dry::Types['integer'].fallback { cnt += 1 }

type.(99) # => 99
type.('99') # => 1
type.(:invalid) # => 2

Fallbacks are different from default values because the latter are triggered on missing input rather than invalid. They can be combined:

schema = Dry::Types['hash'].schema(
  size: Dry::Types['integer'].fallback(50).default(100)
)
schema.({}) # => { size: 100 }
schema.({ size: 'invalid' }) # => { size: 50 }

octocatEdit on GitHub