Processor steps
Warning
This feature is experimental. It should become stable in version 2.0.0.
Schemas process the input using 4 steps:
key_coercer
- Prepare input hash using a key mapfilter_schema
- Apply pre-coercion filtering rules (optional step, used only whenfilter
was used)value_coercer
- Apply value coercions based on type specificationsrule_applier
- Apply rules
It is possible to add before
or after
callbacks to theses steps if you wish to customize processing. Let's say you want to remove all keys with nil
values before coercion is applied:
schema = Dry::Schema.Params do
required(:name).value(:string)
optional(:age).value(:integer)
before(:value_coercer) do |result|
result.to_h.compact
end
end
Now when the schema is applied, it'll remove all keys with nil
values before coercions and rules are applied:
schema.(name: "Jane", age: nil)
# => #<Dry::Schema::Result{:name=>"jane"} errors={}>