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

JSON Schema

The :json_schema extension allows you to generate a valid JSON Schema from a Dry::Schema. JSON Schema is a widely used standard, so this unlocks many possibilities.

Dry::Schema.load_extensions(:json_schema)

UserSchema = Dry::Schema.JSON do
  required(:email).filled(:str?, min_size?: 8)
  optional(:favorite_color).filled(:str?, included_in?: %w[red green blue pink])
  optional(:age).filled(:int?)
end

UserSchema.json_schema 
# {
#   "type": "object",
#   "properties": {
#     "email": {
#       "type": "string",
#       "minLength": 8
#     },
#     "favorite_color": {
#       "type": "string",
# .     "enum": ["red", "green", "blue", "pink"]
#     },
#     "age": {
#       "type": "integer"
#     },
#   },
#   "required": ["email"] 
# }

Learn more