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. Unique features
  2. When to use?
  3. Quick start

Introduction

dry-schema is a validation library for data structures. It ships with a set of many built-in predicates and powerful DSL to define even complex data validations with very concise syntax.

Main focus of this library is on:

^INFO
dry-schema is used as the schema engine in dry-validation which is the recommended solution for business domain validations
^

Unique features

There are a few features of dry-schema that make it unique:

When to use?

Always and everywhere. This is a general-purpose data validation library that can be used for many things and it's multiple times faster than ActiveRecord/ActiveModel::Validations and strong-parameters.

Possible use-cases include validation of:

Quick start

require 'dry/schema'

UserSchema = Dry::Schema.Params do
  required(:name).filled(:string)
  required(:email).filled(:string)

  required(:age).maybe(:integer)

  required(:address).hash do
    required(:street).filled(:string)
    required(:city).filled(:string)
    required(:zipcode).filled(:string)
  end
end

UserSchema.(
  name: 'Jane',
  email: 'jane@doe.org',
  address: { street: 'Street 1', city: 'NYC', zipcode: '1234' }
).inspect

# #<Dry::Schema::Result{:name=>"Jane", :email=>"jane@doe.org", :address=>{:street=>"Street 1", :city=>"NYC", :zipcode=>"1234"}} errors={:age=>["age is missing"]}>