Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/elcuervo/dry-types-json-schema
https://github.com/elcuervo/dry-types-json-schema
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/elcuervo/dry-types-json-schema
- Owner: elcuervo
- Created: 2024-03-22T12:34:19.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-05-31T19:41:48.000Z (7 months ago)
- Last Synced: 2024-05-31T20:59:30.413Z (7 months ago)
- Language: Ruby
- Size: 103 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Dry::Types::JSONSchema
![](https://images.unsplash.com/photo-1642952469120-eed4b65104be?q=80&w=600&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D)
## Example
```ruby
Dry::Types.load_extensions(:json_schema)AnnotatedString = Dry::Types["string"].meta(format: :email, title: "Notes")
AnnotatedString.json_schema
#=> {:type=>:string, :title=>"Notes", :format=>:email}module Types
include Dry.Types()
endclass StructTest < Dry::Struct
schema schema.meta(title: "Title", description: "description")VariableList = Types::Array
.of(Types::String | Types::Hash)
.constrained(min_size: 1)
.meta(description: "Allow an array of strings or multiple hashes")attribute :data, Types::String | Types::Hash
attribute :string, Types::String.constrained(min_size: 1, max_size: 255)
attribute :list, VariableList
endStructTest.json_schema
# =>
# {:type=>:object,
# :properties=>
# {:data=>{:anyOf=>[{:type=>:string}, {:type=>:object}]},
# :string=>{:type=>:string, :minLength=>1, :maxLength=>255},
# :list=>
# {:type=>:array,
# :items=>{:anyOf=>[{:type=>:string}, {:type=>:object}]},
# :description=>"Allow an array of strings or multiple hashes",
# :minItems=>1}},
# :required=>[:data, :string, :list],
# :title=>"Title",
# :description=>"description"}
```