flash-gordon on v1.3.3
flash-gordon on master
Bump version to 1.3.3 (compare)
flash-gordon on master
Update CHANGELOG (compare)
flash-gordon on master
Halt with mutable backtrace Ex… Merge pull request #116 from jo… (compare)
JSON
or Form
. Consider this:Schema = Dry::Validation.Schema do
configure { config.type_specs = true }
required(:first_name, :string).filled(:str?)
end
OtherSchema = Dry::Validation.Schema(Schema) do
required(:last_name, :string).filled(:str?)
end
input = { first_name: 'John', last_name: 'Smith' }
Schema.(input).success? => true
Schema.(input).output => {:first_name=>"John", :last_name=>"Smith"}
OtherSchema.(input).success? => true
OtherSchema.(input).output => {:first_name=>"John", :last_name=>"Smith"}
Schema = Dry::Validation.JSON do
configure { config.type_specs = true }
required(:first_name, :string).filled(:str?)
end
OtherSchema = Dry::Validation.Schema(Schema) do
required(:last_name, :string).filled(:str?)
end
input = { 'first_name' => 'Joe', 'last_name' => 'Smith' }
Schema.(input).success? => true
Schema.(input).output => {:first_name=>"Joe"}
OtherSchema.(input).success? => false
OtherSchema.(input).errors => {:first_name=>["is missing"]}
OtherSchema
to recognize first_name
. Inheritance works as I would expect with the normal schema. It's not a problem with the JSON
macro either, as this would yield the same issues:Schema = Dry::Validation.Schema do
configure do
config.type_specs = true
config.input_processor = :json
config.hash_type = :symbolized
end
required(:first_name, :string).filled(:str?)
end
OtherSchema = Dry::Validation.Schema(Schema) do
required(:last_name, :string).filled(:str?)
end
OtherSchema = Dry::Validation.Schema(Schema) do
configure do
config.type_specs = true
config.input_processor = :json
config.hash_type = :symbolized
end
required(:last_name, :string).filled(:str?)
end
One more thing to add, it isn't just the input_preprocessor
but rather a combination of that and type_specs
set to true
. Looking at OtherSchema
I see that:
OtherSchema.config[:type_map] => {:last_name=>#<Dry::Types::Definition primitive=String options={} meta={}>}
Not that first_name
is not in the type_map
. I haven't done a deep dive into the code yet, but if anyone familiar with the finer points can give me any pointers (or just flat out tell me if this is a bug and, if so, I'll work to patch it) that would be really handy.
Looks like the bug has actually been patched: https://github.com/dry-rb/dry-validation/pull/359/commits/71872da3985c54b6930ed39c42d004f837e9812f
This fixes the behavior. Is there something wrong with this PR?
constructor_type
. Can someone point me in the direction of how to make all attributes omittable, without explicitly having to set .meta(omittable: true)
? I used constructor_type :schema
to get around it before..
class User < Dry::Struct
transform_types do |type|
type.meta(omittable: true)
end
attribute :name, Types::Strict::String
attribute :age, Types::Strict::Integer.default(18)
end
class User < Dry::Struct
transform_types do |type|
type.meta(omittable: true)
end
attribute :foo, Types::Hash.default(Hash.new)
end
User.new.foo # => {:meta=>{:omittable=>true}}
Dry::Struct::Error: [User.new] :name is missing in Hash input
User.new.foo # => {:meta=>{:omittable=>true}}
I'm trying to get User.new.foo # => {}
(which is what constructor_type :schema
did before)
class User < Dry::Struct
transform_types do |type|
type.optional.deafult({})
end
attribute :foo, Types::Hash
end
class User < Dry::Struct
transform_types do |type|
if type.default?
type
else
type.meta(omittable: true)
end
end
attribute :foo, Types::Hash.default({})
attribute :bar, Types::String
end
User.new
=> #<User foo={} bar=nil>
transform_types
many times