Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
ojab
@ojab
my bad, thanks
Gustavo Caso
@GustavoCaso
No problem
happy to help
ojab
@ojab
hmm, actually it returns 'true' instead of true
Gustavo Caso
@GustavoCaso
hmm actually it is
Gustavo Caso
@GustavoCaso
checking code
ojab
@ojab
also looks like 'bool' doesn't propertly handle .constructor:
bool = Dry::Types['bool'].constructor { |input| input }
p bool.valid?(true)
# => undefined method `primitive' for #<Dry::Types::Sum:0x0000000001fd1500> (NoMethodError)
# Did you mean?  primitive?
Gustavo Caso
@GustavoCaso
Ok so for Enum is the expected behaviour based on the specs
spec following your example:
context 'with hash value' do
    subject(:enum) { Dry::Types['string'].enum('true' => true, 'false' => false) }

    it 'allows defining an enum from a specific type' do
      expect(enum['true']).to eql('true')
      expect(enum['false']).to eql('false')
      expect(enum[true]).to eql('true')
      expect(enum[false]).to eql('false')
    end
  end
The type Dry::Types['string'] is for making sure the keys are from that specific type
ojab
@ojab
So enum returning Boolean is not possible by design or Dry::Types['bool'] in theory should work?
Gustavo Caso
@GustavoCaso
That would require the keys being bool
let me check
Dry::Types['bool'].enum(true => 'true')['true']
# => true
Dry::Types['bool'].enum(true => 'true')[true]
# => true
ojab
@ojab
looks like it actually mapping values to keys, not vise-a-versa, so it's impossible to create mappings to boolean values :/
looks like the proper way to map values to Booleans is constructor, but we can't check .valid? in that case
Gustavo Caso
@GustavoCaso
What are you trying to achieve could you paste the code?
ojab
@ojab
I'm trying to spec some code that does the same as Coercions::Params for bool with different BOOLEAN_MAP and the issue is specs:
    subject(:valid?) do
      begin
        coerced = described_class[input]
        described_class.valid?(coerced)
      rescue Dry::Types::ConstraintError
        false
      end
    end
and valid? fails with undefined method 'primitive'
I can compare it with expected value of course, but it's not so intuitive IMO
and also what if returned value is expected, but invalid itself because I wrote a shitty test?
Nikita Shilnikov
@flash-gordon
it's a legitimate case to have a constructor on top of sum, so looks like a bug to me
Gustavo Caso
@GustavoCaso
I just checking that
looks like works for call method
Nikita Shilnikov
@flash-gordon
Gustavo Caso
@GustavoCaso
Dry::Types['bool'].constructor { |input| input }.(true)
=> true
Dry::Types['bool'].constructor { |input| input }.(false)
=> false
Dry::Types['bool'].constructor { |input| input }.('hello')
=> "hello"
Dry::Types['bool'].constructor { |input| input }['hello']
Nikita Shilnikov
@flash-gordon
@ojab pls report an issue
we'll take a look
Gustavo Caso
@GustavoCaso
@flash-gordon bulls eye
ojab
@ojab
done dry-rb/dry-types#256
Gustavo Caso
@GustavoCaso
No need to call super because Definition valid? method is an alias of
def primitive?(value)
  value.is_a?(primitive)
end
Nikita Shilnikov
@flash-gordon
it's from superclass but it's not relevant really for the constructor type
ojab
@ojab
Is there any way to change type name without unfreezing it, btw? specs saying that I'm testing #<Dry::Types::Constructor:0x00000000031bef38> are not really descriptive.
Nikita Shilnikov
@flash-gordon
nope, introspection to be improved, eventually
Roland Laurès
@ShamoX
@GustavoCaso Hi again, it seems that my problem is coming from the fact I validate my arrays like this :
  required(:architecture).filled(:array?).each do
    schema(RequestRoom.validation_schema)
  end
  required(:interconnections).each do
    schema(Interconnection.validation_schema)
  end
(this is inside the Form do end block
the first one (:architecture) will fail only if the architecture field is not filled.
If the field exist and it is an array, it is enough to validate (it seems)
It seems that in that case, RequestToom.validation_schema validation is not called on the data.
I try to make a consistent example that show the bug (maybe it is a behavior :D)
Roland Laurès
@ShamoX
nested_schema = Dry::Validation.Schema do
  required(:name).filled(:str?)
end

global_schema = Dry::Validation.Schema do
  required(:working_array).each { schema(nested_schema) }
  required(:notworking_array).filled.each { schema(nested_schema) }
end

invalid_input = {
  working_array: [{ not_name: 'test' }],
  notworking_array: [{ not_name: 'test' }]
}

result = global_schema.call(invalid_input)

puts "notworking_array in error? #{result.errors.include?(:notworking_array)}"
puts "working_array in error? #{result.errors.include?(:working_array)}"
I probably miss use .filled or .value predicates.
Roland Laurès
@ShamoX
But in my understanding it should be an 'and' logic. Because I also want the fields not to be nil.
Ok, I read back the doc : http://dry-rb.org/gems/dry-validation/nested-data/ and it was not clear to me that each would also validate not nil field.
global_schema.call(working_array: nil, notworking_array: nil)
=> #<Dry::Validation::Result output={:working_array=>nil, :notworking_array=>nil} errors={:working_array=>["must be an array"], :notworking_array=>["must be filled"]}>
global_schema.call(working_array: nil, notworking_array: nil).errors
=> {:working_array=>["must be an array"], :notworking_array=>["must be filled"]}