These are chat archives for dry-rb/chat

17th
Jul 2018
Roland Laurès
@ShamoX
Jul 17 2018 13:15
Hi all, I have a somehow complexe validation to pass (complexe to oppose to simple ^^). But the custom validation blocks return a global failure message based on true/false where I would like to show a complete description of all the elements not passing the validation.
Here is the code I use :
validate(windows_consistency: %i[architecture interconnections]) do |architecture, interconnections|
          windows = interconnections.select do |interconn|
            interconn[:kind] == 'window'
          end
          failing_windows = []
          windows.each do |window|
            windows_materials = Materials.inca_ids category: 'fenetre'
            room_eid = window[:room_ids].first == '-1' ? window[:room_ids].last : window[:room_ids].first
            room_struct = architecture.find { |r| r[:external_id] == room_eid }
            unless room_struct[:materials].any? do |material|
                material[:compounds].any? { |compound| windows_materials.include? compound }
              end then
              failing_windows <<= window
            end
          end
          failing_windows.empty?
        end
In resumé : windows are inconsistent (windows_consistency rules) when one or several hasn't been described in the architecture field correctly (meaning they have no compounds corresponding to it).
I'd like to say that room_eid misses the material of the interconnection (with its index).
Can I pass arguments to the error message for :windows_consistency or have a way to generate as many windows_consistency messages with "local" as there is items in the failing_windows array ?
Roland Laurès
@ShamoX
Jul 17 2018 13:24
I suppose defining this through a rule statement would allow that, but it seems a little complicated...
Roland Laurès
@ShamoX
Jul 17 2018 13:32
(And in fact, here it is not complexe enough, because I should validate there is one Material different by interconnection declared for a given room...)
Roland Laurès
@ShamoX
Jul 17 2018 14:50
Maybe I should separate this validation and do it in an other module.
I may have an idea to simplify all this and get the mechanism already in place in dry-validation to know which window is failing and why...
Roland Laurès
@ShamoX
Jul 17 2018 15:08
No it's not working, because I would have to insert the schema after a first validation pass... :'( Or insert the messages of my second validation into the first one messages...
(thinking out loud, sorry)
Grant Shangreaux
@gcentauri
Jul 17 2018 15:24
@ShamoX , yeah this looks maybe too complex to handle the way you want. it looks to me like you might want to have multiple validation passes like you were saying. putting this all in one validation block means it is only breaking one rule if there is a failure. it seems like your instinct to break it into modules and then add all the errors together at the end might be the way to go. i would have to see the rest of the structure you are trying to validate, but it looks to me like you need a schema just for the windows array
i have been using a builder pattern to do validations in chunks, and one errors array to collect them all if there are any generated in the individual steps
Roland Laurès
@ShamoX
Jul 17 2018 15:26
yeah, but how to make a schema that depends on a distinct data set (the rooms described in the architecture array) ?
In fact this might not be a problem, maybe I should flatten all the compounds and reach to a valid id...
(for a given room)
but how do you associate several error table from different validating schema ? I need to rescue for the error of one and then call an other ?
or something like that ?
So multiple steps cascading if some dependencies are met
because here there is no reason to call the windows_consistency schema if the architecture and interconnections schemas didn't pass.
But I can call it even if something else failed.
Roland Laurès
@ShamoX
Jul 17 2018 15:49
In fact, I'm afraid I can't express this code as predicates...
And anyway it's incomplete because if there is several windows in the room, I cannot match one window to one of the room's compounds. I miss some informations here. The data structures has to be updated to include a link between those.
Thanks for the help anyway.
Grant Shangreaux
@gcentauri
Jul 17 2018 17:38
haha, sounds like a tough problem. sorry i was away from the chat. hope you can get some things figured out!
Jonah
@jonahx
Jul 17 2018 17:58

I am getting a Dry::Struct::RepeatedAttributeError in a rails application, triggered by code that loads classes I need loaded for introspection:

Dir[File.expand_path("../domain/authentication/**/*.rb", __dir__)].each do |f|
  require f
end

I believe the problem is caused by this fact:

If you have a Rails project, it has a Magic Loader that automagically loads the file foo.rb if the interpreter sees a Foo reference and has not seen a definition for Foo yet. This is a miracle and a pain in the butt, because a subsequent require 'foo' line might not have the same path, and you get the double-require issue.

Can anyone who's dealt with this isssue offer a solution?