This commit is contained in:
Ian Ker-Seymer 2021-07-22 18:31:37 -04:00
parent 53baaa050d
commit e27a268902
No known key found for this signature in database
GPG Key ID: 10C142546A8A6F6B
3 changed files with 41 additions and 1 deletions

View File

@ -109,4 +109,4 @@ result.error?([:tags, 1])
### Learn more
- [Customizing messages](docs::error-messages)
- [Validation hints](docs::extensions/hints)
- [Validation hints](docs::extensions/hints)

View File

@ -6,6 +6,7 @@ sections:
- hints
- info
- monads
- json_schema
---
`dry-schema` can be extended with extension. Those extensions are loaded with `Dry::Schema.load_extensions`.
@ -15,3 +16,4 @@ Available extensions:
- [Hints](docs::extensions/hints)
- [Info](docs::extensions/info)
- [Monads](docs::extensions/monads)
- [JSON Schema](docs::extensions/json_schema)

View File

@ -0,0 +1,38 @@
---
title: JSON Schema
layout: gem-single
name: dry-schema
---
The `:json_schema` extension allows you to generate a valid [JSON Schema](https://json-schema.org/) from a `Dry::Schema`. JSON Schema is a widely used standard, so this unlocks many possibilities.
```ruby
Dry::Schema.load_extensions(:json_schema)
UserSchema = Dry::Schema.JSON do
required(:email).filled(:string, min_size?: 8)
optional(:age).filled(:integer)
end
UserSchema.json_schema
# {
# "type": "object",
# "properties": {
# "email": {
# "type": "string",
# "minLength": 8
# },
# "age": {
# "type": "integer"
# },
# "required": ["email"]
# }
```
### Learn more
- [Official JSON Schema docs](https://json-schema.org/)
- [Auto-generate forms with React + JSON Schema](https://github.com/rjsf-team/react-jsonschema-form))
- [Integrate with other languages more easily](https://json-schema.org/implementations.html)