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"]
# }