Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Nikita Shilnikov
@flash-gordon
@bilby91 Maybe types should behave as Default, so whether keys are mandatory or not depends on what constructor_type you use
Martín Fernández
@bilby91
@flash-gordon Is there any doc on how the constructors work ?
Nick Sutterer
@apotonick
my favorite question haha
Martín Fernández
@bilby91
jja
Nick Sutterer
@apotonick
i get it asked around 10x a week
Martín Fernández
@bilby91
:( sorry to be one of them!
Nikita Shilnikov
@flash-gordon
@bilby91 here dry-rb/dry-rb.org#104 :P
Martín Fernández
@bilby91
Crystal clear now :)
Nick Sutterer
@apotonick
crystal?
Martín Fernández
@bilby91
@apotonick like super clear what type of constructor_types are ? :)
Nick Sutterer
@apotonick
@bilby91 got it :P
Alfonso Perez
@alfonsoperez
Hi there!
I got introduced to these gems by a coworker and they are actually making me love my daily work with Ruby!
I have been trying to implement new functionality with Structs and Types, but I am getting a uninitialized constant Types::MyType (NameError), any ideas?
Tim Riley
@timriley
Can you show us how you defined MyType, @alfonsoperez?
Alfonso Perez
@alfonsoperez
Sure!, thanks!, I created a new file under /engines/mymodel/lib (this is a rails project), /engines/mymodel/lib/types.rb
module MyModel::Types
  include Types.module # this is in /app/types/app.rb 
  ...
  AnswerOutcome = Types::Strict::String.enum('expired', 'verified', 'unverifiable', 'flag')
end
and in /app/types/arr.rb ...
Dry::Types.register_class(Question)

module Types
  include Dry::Types.module
end
one hint I guess is that it says Types::AnswerOutcome, where it should say MyModel::Types::AnswerOutcome
Alfonso Perez
@alfonsoperez
(I am doing MyModel::Types::AnswerOutcome in another file and that's where it's failing)
Luca Guidi
@jodosha
@wafcio @solnic yay! :sparkles: :clap:
Steve
@dnd
I'm trying to validate an object that has an array of hashes where I want the sum of values from one of the hash keys to match a key from the main object. {total: 123, items: [{amount: 22}, {amount: 193}]} What complicates this is that I was thinking of defining the validation on items, but that means that if there are validation issues with some of the other content in the items hashes that the errors returned may sometimes be an array of error strings, other times an array of error hashes
Ramon Tayag
@ramontayag
How do I make initialization hash keys optional?
class Car < Dry::Struct
  attribute :brand_name, Types::String.optional
  attribute :model_number, Types::String.optional
end
Car.new(model_number: "81AC") # raises Dry::Struct::Error, ":brand_name is missing in Hash input"
Piotr Solnica
@solnic
@ramontayag constructor_type :schema
Ramon Tayag
@ramontayag
Thanks - not familiar with that macro. Will check it out.
Piotr Solnica
@solnic
@ramontayag structs can use different hash schema types from dry-types, it's strict by default so all keys are expected to be present in attributes hash that's passed to .new
Ramon Tayag
@ramontayag
Ok thanks for the explanation @solnic!
If anyone's interested, just putting this out there: https://github.com/imacchiato/dry-validation-matchers
Jeff Dickey
@jdickey
@ramontayag That's Useful :smiley:
Ramon Tayag
@ramontayag
@solnic I want to add something to the readme about constructor_type to the readme. As far as I can see, there are these types: :strict, :permissive, and :schema. I guess you meant that it is :strict by default, but what is the difference with the latter two?
Let me check dry-types first
Ramon Tayag
@ramontayag
There are certain schemas in the codebase of dry-struct that can't be found in http://dry-rb.org/gems/dry-types/hash-schemas/ that
Ramon Tayag
@ramontayag
For dry-struct, what is the recommended way of creating setters?
Ramon Tayag
@ramontayag
@jdickey cool :)
Piotr Solnica
@solnic
@ramontayag the recommend way is to not use setters :)
Piotr Solnica
@solnic
@ramontayag there’s a WIP PR which will add more docs about it dry-rb/dry-rb.org#104
Ramon Tayag
@ramontayag
@solnic ok cool I can discard my changes then! How would you go about replacing virtus with the dry gems? Would you recommend doing so?
Hannes Nevalainen
@kwando
dry-validation is freaking awesome! xD
Steve
@dnd
@timriley, @solnic I posted my question from the other day on the discussion board, but haven't gotten any traction on it. Any idea if there is a way to easily do this currently, or where I could start looking to make it possible? https://discuss.dry-rb.org/t/get-keys-instead-of-sentences-for-validation-errors/150/1
Piotr Solnica
@solnic
@dnd hey, I’ll be replying to posts later today. I’ve been busy with rom-sql new release lately
Steve
@dnd
cool, thanks
Lucas Hosseini
@beauby
Any specific reason why no built-in predicates on min/max length of strings in dry-validation?
Fran Worley
@fran-worley
@beauby there are built in predicates. min_size? And max_size? Work on anything that responds to size.
Lucas Hosseini
@beauby
Hmm some examples seem to imply that the _size? predicates work on string, but the doc says it is valid for arrays only
Fran Worley
@fran-worley
Take a look here to see all built in predicates and the code behind them: https://github.com/dry-rb/dry-logic/blob/master/lib/dry/logic/predicates.rb
Lucas Hosseini
@beauby
@fran-worley Right – yeah, that was my source of information, but as you can see, it explicitly talks only about arrays
I’ll issue a PR
Fran Worley
@fran-worley
Good spot on the docs and thanks in advance for the PR.
Piotr Solnica
@solnic
@kwando haha, is it? :D
@dnd re virtus, you need dry-struct + attr_writer