dry-schema

v1.14
  1. Introduction
  2. Basics
    1. Built-in predicates
    2. Macros
    3. Type specs
    4. Working with schemas
  3. Optional keys and values
  4. Nested data
  5. Reusing schemas
  6. Params
  7. JSON
  8. Error messages
  9. Advanced
    1. Composing schemas
    2. Custom predicates
    3. Custom types
    4. Filtering
    5. Key maps
    6. Predicate logic
    7. Processor steps
    8. Rule AST
    9. Unexpected keys
  10. Extensions
    1. Hints
    2. Info
    3. JSON Schema
    4. Monads

TOC


Processor steps

^WARNING
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:

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={}>