Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • 14:03

    dry-bot on master

    [devtools] config sync (compare)

  • 10:01
    Travis dry-rb/dry-view (master) errored (636)
  • 09:58
    Travis dry-rb/dry-view (master) errored (635)
  • 09:56

    dry-bot on master

    [devtools] config sync (compare)

  • 09:56

    dry-bot on master

    [devtools] config sync (compare)

  • 09:56

    dry-bot on master

    [devtools] config sync (compare)

  • 09:56

    dry-bot on master

    [devtools] config sync (compare)

  • 09:56

    dry-bot on master

    [devtools] config sync (compare)

  • 09:56

    dry-bot on master

    [devtools] config sync (compare)

  • 09:56

    dry-bot on master

    [devtools] config sync (compare)

  • 09:56

    dry-bot on master

    [devtools] config sync (compare)

  • 09:56

    dry-bot on master

    [devtools] config sync (compare)

  • 09:56

    dry-bot on master

    [devtools] config sync (compare)

  • 09:56

    dry-bot on master

    [devtools] config sync (compare)

  • 09:56

    dry-bot on master

    [devtools] config sync (compare)

  • 09:54

    dry-bot on master

    [devtools] config sync (compare)

  • 09:54

    dry-bot on master

    [devtools] config sync (compare)

  • 09:54

    dry-bot on master

    [devtools] config sync (compare)

  • 09:54

    dry-bot on master

    [devtools] config sync (compare)

  • 09:54

    dry-bot on master

    [devtools] config sync (compare)

Jonah
@jonahx
Also separate question for today: I have an Dry::Struct with an attribute with a Class type constraint. When using rspec, I want to pass in a double for the object. But an rspec double does not have type class. Have you ever run into this? What was your workaround?
Tim Riley
@timriley
@jonahx What you have in that code example above seems fine. There’s not any kind of first-class support for something like that.
Jonah
@jonahx
@timriley thanks
Tim Riley
@timriley
@jonahx Alternatively, you could just use the type definition directly whenever you need it?
Webservices = Types::Strict::Array(Types::Instance(Webservice))
services = Types::Webservices[some_array]
Jonah
@jonahx
@timriley yeah, in this case i felt there was benefit in explictily naming the concept. Also, the real object has a from_string ctor so there’s a bit more reason to have it
Tim Riley
@timriley
Right, that makes sense then.
Jonah
@jonahx
any advice on the rspec question?
Tim Riley
@timriley
Mind repeating it?
Jonah
@jonahx
Also separate question for today: I have an Dry::Struct with an attribute with a Class type constraint. When using rspec, I want to pass in a double for the object. But an rspec double does not have type class. Have you ever run into this? What was your workaround?
Tim Riley
@timriley
I’d just construct an object of the actual class :)
Jonah
@jonahx
ha, you’re not as lazy as me. thanks :)
Tim Riley
@timriley
Code should inform tests :)
(and if it makes testing too hard, well, that’s a code problem)
Jonah
@jonahx
@timriley, well, the test is perfectly easy. it’s just that rspec double’s aren’t designed with type checking in mind, which is fair enough given how atypcal it is in ruby. imo dry::struct types bring a certain sanity to ruby that should be there, but the fact remains it’s still an addon to the language, so to speak.
Tim Riley
@timriley
Yeah. I’d only use doubles where duck-typing matters more than the actual strict class check
Jonah
@jonahx
@timriley which is indeed the case here. i sort of added the class constraint just as a way of enforcing the interface, but now that you mention it i think that’s where my mistake was — the contract really is just an interface and not a particular class. and removing the type constraint which i’ve what i did to fix the issue was probably the correct move anyway and not just a hack as i’ve been viewing it
Tim Riley
@timriley
Cool :)
Alexander Chernik
@achernik
Hello!
I have a question regarding dry-validation usage. Is it possible to "filter out" invalid parts instead of flat out rejecting entire hash?
I could use result.errors to filter result.output manually, but maybe there is another way?
Jonah
@jonahx
How can I get an attribute that is both optional and default? I want to be able to leave it out, and when I do, it should have a default value.
Nikita Shilnikov
@flash-gordon
@jonahx I think it's what dry-types/dry-struct does in master, check out the changelogs
Tiago Moraes
@tiagoefmoraes

Hello,
when defining an enum without 0 as a valid value:

subject(:enum) { Dry::Types['integer'].enum(4, 5, 6) }

I expected that it would be invalid

expect(enum.try(0)).to be_failure

but it is actually valid because of inverted_mapping. I found this to be really confusing.
Should't this inverted behavior only happen when the mapping is defined explicitly?

subject(:enum) { Dry::Types['integer'].enum(4 => 0) }

At least when the values are all integers?

Nikita Shilnikov
@flash-gordon
it always worked like that for integers but I think this behavior can be made configurable
well, try didn't work in prev version actually but fixed this :)
[] worked
I would say it's better not to have mapping by default
but this is a breaking change
Nikita Shilnikov
@flash-gordon
@tiagoefmoraes I'll do what you suggested before the release, thanks for bringing it
Jonah
@jonahx

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
Nikita Shilnikov
@flash-gordon
@jonahx most likely the file containing Input getting loaded twice
Jonah
@jonahx
@flash-gordon yep, rails was autoloading and there was a require statement somewhere. when i removed the require the error vanished.
Tiago Moraes
@tiagoefmoraes
@flash-gordon thanks!
Ryan Bigg
@radar
in ur repos, submitting PRs: dry-rb/dry-core#25
Jonah
@jonahx

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)
Jean-Denis Koeck
@jeandenis-k
Hi @jonahx ! Seems to me you would have to use, in the body of your class, either constructor_type :strict_with_defaults or constructor_type :schema
Jonah
@jonahx
@jeandenis-k thanks, i’ll try it
Jean-Denis Koeck
@jeandenis-k
:+1:
Jonah
@jonahx
Separate question. Consider:
class Test < Dry::Struct
  attribute :cls, Types::Any.default(String)
end

p Test.new.cls == String # return true
Yet in my actual code I’m doing the same thing with a Sequel class:
    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={}>>
Is there any magic the dry::struct does on class values for defaults that would explain this?
Jonah
@jonahx

Interesting…
The problem is fixed if I do

attribute :resource_class, ::Types::Any.default { Resource }

I wonder why those are different?

Jean-Denis Koeck
@jeandenis-k
Interesting, tried to find why in the source code of dry-types but I can't make head or tails of it :sweat_smile:
Andrea Tore
@nakedmoon
Hi guys, quick question: is there any way to have a dry-struct attribute like a Constructor Types::Hash with a default value ?
Andrea Tore
@nakedmoon
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
Andrea Tore
@nakedmoon
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>
Nikolai Budko
@nibygro

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'