dry-rb / dry-rb/dry-schema

Empty arrays of struct type error as invalid value

Open
#320 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug help wanted
Dominant language
Ruby
Stars
492
Forks
123
PR merge metrics
No merged PRs in 30d

Description

When an array of type struct is defined for a schema and an empty array is specified, the empty array is reported as the wrong type. This is not the case for a basic type.

# incorrect w/ struct
Class.new(Dry::Validation::Contract) { params { required(:list).array(Dry.Struct(name: 'string')) } }.new.call(list: [])
=> #<Dry::Validation::Result{:list=>{}} errors={:list=>["must be an array"]}>

# correct w/o struct
Class.new(Dry::Validation::Contract) { params { required(:list).array(:string) } }.new.call(list: [])
=> #<Dry::Validation::Result{:list=>[]} errors={}>

I'm not quite sure why dry-schema thinks it's not an array being passed in.

  • dry-schema 1.5.5
  • dry-validation 1.5.6
  • dry-struct 1.3.0
  • dry-types 1.4.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the struct-array example from the issue and compare it with the basic-type array example. Trace the schema entry point created by required(:list).array(Dry.Struct(...)) to find why [] is reported as invalid, then verify that an empty struct array succeeds without errors while invalid element values remain rejected.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend-api-design
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.