Sha256: 00a40a0305b7767cf0f64b7e64f8c9d371b9af6041f8fac816100da3941e093f

Contents?: true

Size: 1.35 KB

Versions: 4

Compression:

Stored size: 1.35 KB

Contents

# 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()
end

class 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
end

StructTest.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"}
```

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dry-types-json-schema-0.0.4 README.md
dry-types-json-schema-0.0.3 README.md
dry-types-json-schema-0.0.2 README.md
dry-types-json-schema-0.0.1 README.md