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. Examples

JSON

To validate JSON data structures, you can use JSON schemas. The difference between Params and JSON is coercion logic. Refer to dry-types documentation for more information about supported JSON coercions.

Examples

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

errors = schema.call('email' => '', 'age' => 18).errors.to_h

puts errors.inspect
# {
#   :email => ["must be filled"],
#   :age => ["must be greater than 18"]
# }

Notice that JSON schemas are suitable for checking hash objects exclusively. There's an outstanding issue about making it work with any JSON-compatible input.