timriley on rich-component-dirs-config
Add extra attributes to compone… Add unit tests for Component.lo… (compare)
timriley on rich-component-dirs-config
Add unit tests for ComponentDir (compare)
timriley on rich-component-dirs-config
Tidy AutoRegistrar Move regist… (compare)
timriley on rich-component-dirs-config
Tidy AutoRegistrar Move regist… (compare)
timriley on rich-component-dirs-config
Add spec (and adjust approach) … (compare)
timriley on rich-component-dirs-config
Initialize components directly … Remove unneeded requiring of co… (compare)
timriley on rich-component-dirs-config
Initialize components directly … (compare)
timriley on rich-component-dirs-config
Remove stale comment Scan file for magic comment opt… Do not load components with aut… (compare)
timriley on rich-component-dirs-config
Use dry-configurable master (compare)
timriley on cherry-pick-concurrent-requires
[]
worked
Any idea why I could be getting the error:
Attribute :authenticator_name has already been defined (Dry::Struct::RepeatedAttributeError)
when that is not the case?
My class looks like this:
class Input < ::Dry::Struct
attribute :authenticator_name, Types::NonEmptyString
attribute :service_id, Types::Coercible::String
attribute :account, Types::NonEmptyString
attribute :username, Types::NonEmptyString
attribute :password, Types::NonEmptyString
end
How can I mix required and optional/default attributes? That is, the following works fine:
class Test < Dry::Struct
attribute :optional, Types::Any.default('default value')
end
p Test.new.optional #=> prints 'default value'
However
class Test2 < Dry::Struct
attribute :required, Types::Any
attribute :optional, Types::Any.default('default value')
end
p Test2.new(required: 'my required val')
# Raises:
# [Test2.new] :optional is missing in Hash input (Dry::Struct::Error)
attribute :resource_class, ::Types::Any.default(Resource)
but when I later access resource_class
it returns not the class, but it seems an instance of the class:#<Resource @values=#<#<Class:0x00000003e7bf00> primitive=Object options={} meta={}>>
IntType = Types.Constructor(Types::Hash){|value| Types::Hash(:strict_with_defaults, int: Types::Form::Int)[int: value]}
class MyClass < Dry::Struct
constructor_type :strict_with_defaults
attribute int_attribute_one, Types::IntType.default(1)
attribute int_attribute_two, Types::IntType.default(2)
end
2.3.3 :181 > MyClass.new()
TypeError: class or module required
using the block notation:
attribute int_attribute_one, Types::IntType.default{1}
attribute int_attribute_two, Types::IntType.default{2}
2.3.3 :188 > MyClass.new()
=> #<MyClass int_attribute_one={:int=>1} int_attribute_two={:int=>2}>
2.3.3 :189 > MyClass.new(int_attribute_one: 1)
=> #<MyClass int_attribute_one={:int=>1} int_attribute_two=2>
Hi guys! I have a problem with dry-validations (0.11)
require 'dry-validation'
UserSchema = Dry::Validation.Schema do
rule(value_filled: [:value1, :value2, :value3]) do |value1, value2, value3|
value1.eql?('1') | value2.eql?('1') | value3.eql?('1')
end
end
UserSchema.(
value1: '0',
value2: '0',
value3: '0'
).inspect
# => "#<Dry::Validation::Result output={:value1=>\"0\", :value2=>\"0\", :value3=>\"0\"} errors={}>"
Why there are no errors for the schema? y rule is one of the fields must be filled with '1'
=> "#<Dry::Validation::Result output={:value1=>\"0\", :value2=>\"0\", :value3=>\"0\"} errors={:some_filled=>[\"must be equal to 1 or must be equal to 1\"]}>"
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}}