You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I call the schema with a bogus value for date, I get the expected errors:
MySchema.call(date: "00",number: 42,string: "Hi!")#=> #<Dry::Schema::Result errors={:date=>["must be a date time"]}>
If I call the schema generated from the struct, though, I get an extra error on a field that is really valid:
# With an invalid value for :dateSchemaFromStruct.call(data: {date: "00",number: 42,string: "Hi!"})#=> #<Dry::Schema::Result errors={:data=>{:date=>["must be a date time"], :number=>["must be a decimal"]}}># Has errors on both "date" and "number", but not "string"# With an invalid value for :numberSchemaFromStruct.call(data: {date: "09/07/2023",number: "",string: "Hi!"}).errors#=> {:data=>{:date=>["must be a date time"], :number=>["must be a decimal"]}}# Has errors on both "date" and "number", but not "string"# With an invalid value for :stringSchemaFromStruct.call(data: {date: "09/07/2023",number: 42,string: 42}).errors#=> {:data=>{:date=>["must be a date time"], :number=>["must be a decimal"], :string=>["must be a string"]}}# Has errors on the invalid string, but also still "date" and "number"
I suspect, but haven't really dug in, that it is the coercions that raise an exception which gets rescued are the ones that show up in errors when they're not supposed to. Eg, to_decimal on dry/types/coercions/json.rb.
Expected behavior
I'd expect a schema generated from a struct to have the same errors as an identical schema, and not have errors on fields that are valid.
My environment
Affects my production application: YES, but not urgently
Ruby version: 3.2.2
OS: Linux
Full test case
require"dry-types"require"dry-struct"require"dry-schema"Dry::Schema.load_extensions(:struct)moduleTypesincludeDry.Types()endMySchema=Dry::Schema.Paramsdorequired(:date).filled(Types::JSON::DateTime)required(:number).filled(Types::JSON::Decimal)required(:string).filled(Types::String)endclassMyStruct < Dry::Structattribute:date,Types::JSON::DateTimeattribute:number,Types::JSON::Decimalattribute:string,Types::StringendSchemaFromStruct=Dry::Schema.Paramsdorequired(:data).value(MyStruct)endgood_data={date: "09/07/2023",number: 42,string: "Hi!"}bad_data={date: "00",number: 42,string: "Hi!"}puts"With Good Data"pstruct: MyStruct.new(good_data)pschema: MySchema.call(good_data)pschema_from_struct: SchemaFromStruct.call(data: good_data)puts"With Bad Data"beginMyStruct.new(bad_data)rescue=>expstruct: exendpschema: MySchema.call(bad_data)pschema_from_struct: SchemaFromStruct.call(data: bad_data)
__END__
With Good Data
{:struct=>#<MyStruct date=#<DateTime: 2023-07-09T00:00:00+00:00 ((2460135j,0s,0n),+0s,2299161j)> number=0.42e2 string="Hi!">}
{:schema=>#<Dry::Schema::Result{:date=>#<DateTime: 2023-07-09T00:00:00+00:00 ((2460135j,0s,0n),+0s,2299161j)>, :number=>0.42e2, :string=>"Hi!"} errors={} path=[]>}
{:schema_from_struct=>#<Dry::Schema::Result{:data=>{:date=>#<DateTime: 2023-07-09T00:00:00+00:00 ((2460135j,0s,0n),+0s,2299161j)>, :number=>0.42e2, :string=>"Hi!"}} errors={} path=[]>}
With Bad Data
{:struct=>#<Dry::Struct::Error: [MyStruct.new] "00" (String) has invalid type for :date violates constraints (invalid date failed)>}
{:schema=>#<Dry::Schema::Result{:date=>"00", :number=>0.42e2, :string=>"Hi!"} errors={:date=>["must be a date time"]} path=[]>}
{:schema_from_struct=>#<Dry::Schema::Result{:data=>{:date=>"00", :number=>42, :string=>"Hi!"}} errors={:data=>{:date=>["must be a date time"], :number=>["must be a decimal"]}} path=[]>}
The text was updated successfully, but these errors were encountered:
Describe the bug
I'm getting some fields included in errors that would normally be fine, in a very specific circumstance:
Might be related to #424
To Reproduce
I've included a full example at the bottom. The short version:
If I call the schema with a bogus value for
date
, I get the expected errors:If I call the schema generated from the struct, though, I get an extra error on a field that is really valid:
I suspect, but haven't really dug in, that it is the coercions that raise an exception which gets rescued are the ones that show up in errors when they're not supposed to. Eg,
to_decimal
on dry/types/coercions/json.rb.Expected behavior
I'd expect a schema generated from a struct to have the same errors as an identical schema, and not have errors on fields that are valid.
My environment
Full test case
The text was updated successfully, but these errors were encountered: