Skip to content

Commit

Permalink
[docsite] add advanced/processor-steps section
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Oct 8, 2019
1 parent 20e38f4 commit f7d7204
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions docsite/source/advanced.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ sections:
- key-maps
- rule-ast
- custom-types
- processor-steps
---

- [Predicate Logic](/gems/dry-schema/advanced/predicate-logic)
Expand Down
37 changes: 37 additions & 0 deletions docsite/source/advanced/processor-steps.html.md
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={}>
```

0 comments on commit f7d7204

Please sign in to comment.