These are chat archives for dry-rb/chat

4th
Jul 2016
Peter Leitzen
@splattael
Jul 04 2016 08:44
:point_up: 1. Juli 2016 10:57 Oops, sorry for that :-( I'll take a look at it soonish!
Peter Leitzen
@splattael
Jul 04 2016 08:53
@solnic caching the cache (sic!) lazily would work.
diff --git a/lib/dry/validation/messages/abstract.rb b/lib/dry/validation/messages/abstract.rb
index f083b1c..9e05222 100644
--- a/lib/dry/validation/messages/abstract.rb
+++ b/lib/dry/validation/messages/abstract.rb
@@ -98,7 +98,7 @@ module Dry
         end

         def cache
-          self.class.cache[self]
+          @cache ||= self.class.cache[self]
         end
       end
     end
@solnic dry-rb/dry-validation#203
Piotr Solnica
@solnic
Jul 04 2016 09:08
@splattael no worries man! and thanks for looking into this further :)
pls just merge it in once travis is green
oh and we gotta remove —full-index from travis bundling, rubygems.org finally sees new dry-logic
Peter Leitzen
@splattael
Jul 04 2016 09:40
@solnic merged!
Vladimir Dralo
@vladra
Jul 04 2016 14:58

Trying dry component and got a question. Is it ok that all classes I register are accessible globally?

# accesible
UserRepository

# accesible also
Container['repo.users']

If I don't instantiate container, UserRepository is not accesible. It is expected behaviour or am I doing something wrong?

Andy Holland
@AMHOL
Jul 04 2016 15:31
@vladra that's just because dry-component handles loading for you too, I haven't looked at the source code in a while but I assume it does it lazily
The idea is that you don't access things globally tho, as to depend on an interface rather than anything concrete
Maciej Mensfeld
@mensfeld
Jul 04 2016 16:20
@solnic is there a way to somehow validate hash keys? I would like to describe something like htis:
{ 1 => rand, 2 => rand } # etc
and create sme rules with dry-validations that will ensure that my keys are integer based and that inside there are float numbers as well
Scott Eckenrode
@selectport
Jul 04 2016 17:59
Is this a usage-error on my part or unsupported or bug for reusing schema’s in dry-validations 0.8 ?

AgeSchema = Dry::Validation.Schema do
    required(:age, :int) { filled? & int?  & gt?(40) & lt?(50) }
end

PersonValidator = Dry::Validation.Schema do
  configure { config.type_specs = true }
  required(:name, :string).filled(:str?, min_size?: 5)
  required(:age, :int) { filled? & int? & lt?(50) & gt?(40) }
  optional(:state, :string).maybe(:str?, max_size?: 2)
end

PeopleValidator = Dry::Validation.Schema do
  configure { config.type_specs = true }
  required(:name, :string).filled(:str?, min_size?: 5)
  required(:age).schema(AgeSchema)
  optional(:state, :string).maybe(:str?, max_size?: 2)
end

h = {name: 'Scott', age: 45, state: 'SC'}

puts '... testing as PersonValidator'
person_results = PersonValidator.(h)

puts person_results.errors.inspect

puts '... testing as PeopleValidator'
people_results = PeopleValidator.(h)

puts people_results.errors.inspect
results : $ bundle exec ruby test_composite.rb ... testing as PersonValidator [] ... testing as PeopleValidator [#<Dry::Validation::Error name=:age result=#<Dry::Logic::Result::Named success?=false input=45 rule=#<Dry::Logic::Rule::Key predicate=#<Dry::Logic::Predicate::Curried id=:hash? args=[45]> options={:evaluator=>#<Dry::Logic::Evaluator::Key path=[:age]>, :name=>:age}>>>] $
Piotr Solnica
@solnic
Jul 04 2016 18:40
@selectport schema(OtherSchema) is for nested structures, and :age is a flat value in the input hash so it won’t work like that
Scott Eckenrode
@selectport
Jul 04 2016 18:41
@solnic thanks - is there any way to accomplish what i’m trying to do here - ie. reuse AgeSchema in validators where it’s common ?
Piotr Solnica
@solnic
Jul 04 2016 18:43
@selectport you can inherit rules from another schema, ie Dry::Validation.Schema(AgeSchema)
Wilson Silva
@wilsonsilva
Jul 04 2016 18:51
There is a dead link on http://dry-rb.org/gems/dry-monads/ pointing to the old dry-result_matcher
Scott Eckenrode
@selectport
Jul 04 2016 18:54
@solnic - just scoured all doc and specs and must be missing it … sorry …. how do i inherit rules from another schema ?
Piotr Solnica
@solnic
Jul 04 2016 18:55
@selectport PeopleSchema = Dry::Validation.Schema(AgeSchema) do .. end
Scott Eckenrode
@selectport
Jul 04 2016 18:59
@solnic … worked ! of course - thanks. any way to specify more than 1 schema ? (tried (a,b) and ([a,b]) - no joy)
Nikita Shilnikov
@flash-gordon
Jul 04 2016 19:01
@wilsonsilva fixed, thanks
Wilson Silva
@wilsonsilva
Jul 04 2016 19:06
:thumbsup: @flash-gordon
Piotr Solnica
@solnic
Jul 04 2016 19:09
@selectport not atm but you can do Schema(rules: s1.class.rules + s2.class.rules)
Scott Eckenrode
@selectport
Jul 04 2016 19:13
@solnic - Thanks for your work and the answers - very much appreciated. Would be great to support the nested semantics for ‘flat’ rules … use case i have : large domain with around 30 fields that are ‘common’ around commands, forms, etc. and would be great to specify rules like we do with types and then just ref them whereever …. just a thought …. thank you !