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

Info exension omits optional parameters with no predicates #347

Open taylorthurlow opened 3 years ago

taylorthurlow commented 3 years ago

Describe the bug

Using the info extension, I expected to be able to fetch a concise list of provided schema keys and whether or not they are required by using #info. This works great, but I'm finding that defining an optional schema key which has no predicates attached is completely omitted from this result. (See reproduction script below)

I'm not sure if this is an intentional thing or not, because I do see that, in my provided example, optional(:age) is sort of a "nothing burger". As far as the schema is concerned it doesn't end up really meaning anything. In my use case, a user is using dry-schema to build a definition for object attributes. optional(:age) has meaning to them/me because it signals the intent to create a getter/setter for :age, even if the value will just pass through the schema validator.

To Reproduce

require 'dry/schema'

Dry::Schema.load_extensions(:info)

Dry::Schema.Params {
  required(:email).filled(:string)
  optional(:age)
}.info # => {:keys=>{:email=>{:required=>true, :type=>"string"}}}

puts Dry::Schema.Params {
  required(:email).filled(:string)
  optional(:age).filled(:integer)
}.info # => {:keys=>{:email=>{:required=>true, :type=>"string"}, :age=>{:required=>false, :type=>"integer"}}}

Expected behavior

require 'dry/schema'

Dry::Schema.load_extensions(:info)

Dry::Schema.Params {
  required(:email).filled(:string)
  optional(:age)
}.info # => {:keys=>{:email=>{:required=>true, :type=>"string"}, :age=>{:required=>false}}}

puts Dry::Schema.Params {
  required(:email).filled(:string)
  optional(:age).filled(:integer)
}.info # => {:keys=>{:email=>{:required=>true, :type=>"string"}, :age=>{:required=>false, :type=>"integer"}}}

My environment