Monads
The monads extension makes Dry::Validation::Result objects compatible with dry-monads.
To enable the extension:
Dry::Validation.load_extensions(:monads)
After loading the extension, you can leverage monad API:
params do
required(:name).filled(:string)
end
end
my_contract = MyContract.new
my_contract.(name: "")
.to_monad
.fmap { puts "passed: " }
.or { puts "failed: " }
Predicates as macros
This extension makes Dry Logic predicates which are concerned with data values (in opposition with types) available as macros for validation rules.
Besides enabling the extension, you have to call import_predicates_as_macros before being able to use them:
Dry::Validation.load_extensions(:predicates_as_macros)
import_predicates_as_macros
end
schema do
required(:age).filled(:integer)
end
rule(:age).validate(gteq?: 18)
end
AgeContract.new.(age: 17).errors.first.text
# => 'must be greater than or equal to 18'
Hints
The hints extension is implemented in Dry Schema. This extension enables hints in Contracts.
To enable the extension:
Dry::Validation.load_extensions(:hints)