Skip to content

Info exension omits optional parameters with no predicates #347

Open
@taylorthurlow

Description

@taylorthurlow

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

  • Affects my production application: No
  • Ruby version: 2.5 to 2.7
  • OS: macOS Catalina

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions