dry-rb / dry-schema

Coercion and validation for data structures
https://dry-rb.org/gems/dry-schema
MIT License
425 stars 110 forks source link

Loose generation fails for nested schema #407

Closed xronos-i-am closed 2 years ago

xronos-i-am commented 2 years ago

Describe the bug

In "loose" mode JSON schema should be generated. The following code throws Dry::Schema::JSONSchema::SchemaCompiler::UnknownConversionError instead

To Reproduce

require 'bundler/inline'

gemfile do
  gem "dry-schema"
end

require "dry-schema"

Dry::Schema.load_extensions(:json_schema)

schema = Dry::Schema.Params do
  required(:personal_info).filled(:hash) do
    required(:email).filled(:string, format?: /@/)
  end
end

schema.json_schema(loose: true)

Expected behavior

A valid JSON schema

My environment

solnic commented 2 years ago

What if you do:

schema = Dry::Schema.Params do
  required(:personal_info).hash do
    required(:email).filled(:string, format?: /@/)
  end
end

??

xronos-i-am commented 2 years ago
schema = Dry::Schema.Params do
  required(:personal_info).hash do
    required(:email).filled(:string, format?: /@/)
  end
end

Nothing changed, same error

solnic commented 2 years ago

What if you remove the format? part?

xronos-i-am commented 2 years ago

What if you remove the format? part?

It works

{:$schema=>"http://json-schema.org/draft-06/schema#",
 :type=>"object",
 :properties=>
  {:personal_info=>
    {:type=>"object",
     :properties=>{:email=>{:type=>"string", :minLength=>1}},
     :required=>["email"]}},
 :required=>["personal_info"]}
solnic commented 2 years ago

Yeah so it's just that there's no support for format? predicate yet.

xronos-i-am commented 2 years ago

not quite, the following works too:

schema = Dry::Schema.Params do
  required(:email).filled(:string, format?: /@/)
end
{:$schema=>"http://json-schema.org/draft-06/schema#",
 :type=>"object",
 :properties=>{:email=>{:type=>"string", :minLength=>1}},
 :required=>["email"]}
xronos-i-am commented 2 years ago

so, the problem is nesting

xronos-i-am commented 2 years ago

sorry, fixed in https://github.com/dry-rb/dry-schema/commit/35bf48e96f47158f54f19156952835f957ef6f7e