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

  1. Learn more

Basics

Here's a basic example where we validate the following things:

This can be easily expressed through the DSL:

require 'dry-schema'

schema = Dry::Schema.Params do
  required(:email).filled(:string)
  required(:age).filled(:integer, gt?: 18)
end

schema.call(email: 'jane@doe.org', age: 19)
# #<Dry::Schema::Result{:email=>"jane@doe.org", :age=>19} errors={}>

schema.call("email" => "", "age" => "19")
# #<Dry::Schema::Result{:email=>"", :age=>19} errors={:email=>["must be filled"]}>

When you apply this schema to an input, 3 things happen:

  1. Input keys are coerced to symbols using schema's key map
  2. Input values are coerced based on type specs
  3. Input keys and values are validated using defined schema rules

Learn more