-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docsite] add advanced/processor-steps section
- Loading branch information
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
title: Processor steps | ||
layout: gem-single | ||
name: dry-schema | ||
--- | ||
|
||
^WARN | ||
This feature is experimental. It should become stable in version 2.0.0. | ||
^ | ||
|
||
Schemas process the input using 4 steps: | ||
|
||
1. `key_coercer` - Prepare input hash using a key map | ||
2. `filter_schema` - Apply pre-coercion filtering rules | ||
(optional step, used only when `filter` was used) | ||
3. `value_coercer` - Apply value coercions based on type specifications | ||
4. `rule_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: | ||
|
||
```ruby | ||
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: | ||
|
||
```ruby | ||
schema.(name: "Jane", age: nil) | ||
# => #<Dry::Schema::Result{:name=>"jane"} errors={}> | ||
``` |