dry-schema icon indicating copy to clipboard operation
dry-schema copied to clipboard

Empty arrays of struct type error as invalid value

Open joelvh opened this issue 5 years ago • 6 comments

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

joelvh avatar Oct 16 '20 06:10 joelvh

Something's wrong here

dry-schema> params { required(:list).array(Dry.Struct(name: 'string')) }.type_schema.(list: [])
=> {:list=>{}}

flash-gordon avatar Oct 16 '20 10:10 flash-gordon

@flash-gordon I can reproduce it, make sure you have expected gem versions loaded:

irb(main):011:0> 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"]}>

solnic avatar Oct 16 '20 10:10 solnic

@solnic I meant something's wrong with type schema, it converts an empty array to a hash

flash-gordon avatar Oct 16 '20 10:10 flash-gordon

@flash-gordon ah sorry, yeah so the inferred type must be wrong then

solnic avatar Oct 16 '20 10:10 solnic

Hey guys, thanks for jumping on this so quickly. I forgot to mention that I also enabled the struct extension.

BTW the docs are only at 1.0 on the dry-struct website, and the struct extension is not listed on the dry-schema website either. Should there be a new version published to the site for each minor release?

Otherwise, let me know how else I can help.

joelvh avatar Oct 16 '20 18:10 joelvh

BTW the docs are only at 1.0 on the dry-struct website, and the struct extension is not listed on the dry-schema website either. Should there be a new version published to the site for each minor release?

Yes, but we don't have this automated so some gems are missing some docs :( I'll deal with this eventually.

BTW just like in case of #321 - it looks like predicate inferrer isn't there yet when it comes to inferring from struct's ASTs

solnic avatar Nov 11 '20 12:11 solnic