These are chat archives for dry-rb/chat

8th
Apr 2016
Artem Pyankov
@iBublik
Apr 08 2016 10:58
@solnic two more questions about dry-rb.
  1. Is there a way to provide default configuration for my schemas? For example i want to use I18n for messages in all of them. I've tried this:
This message was deleted
Artem Pyankov
@iBublik
Apr 08 2016 11:05
I18nSchema = Dry::Validation.Schema do
  configure { config.messages = :i18n }
end

SomeSchema = Dry::Validation.Schema(I18nSchema) {}
This works fine, but when I try to make I18nSchema with method Dry::Validation.Form, it raises exception - so I can't create Form schema with only configuration setup, it requires at least one key to be present.
  1. How can I use custom coercion for my schema?
Piotr Solnica
@solnic
Apr 08 2016 11:06
@iBublik this should work, please report an issue then
at least that’s how I’ve been setting this up
I’ve got a “Document how to set up base schemas for sharing config” in my todo list :/
oh wait
you want to define a class, rather than using block setup, this is a shortcut
this is confusing though, I should improve that prior 1.0
but it SHOULD work, because that’s how I was using it
just do class I18Schema < Dry::Validation::Schema; configure { |config| config.messages = :18n }; end
then SomeSchema = Dry::Validation.Schema(I18nSchema) { .. }
Form should work too
Artem Pyankov
@iBublik
Apr 08 2016 11:08
thanks, I'll try it out
Piotr Solnica
@solnic
Apr 08 2016 11:10
ie here’s a core abstract schema class that I use in an app:
require "dry-validation"

module AppNameSpace
  module Validation
    class Form < Dry::Validation::Schema::Form
      EMAIL_REGEX = /\A[^@]+@([^@\.]+\.)+[^@\.]+\z/.freeze

      configure do |config|
        config.messages = :i18n
      end

      def email?(input)
        EMAIL_REGEX.match(input).size > 0
      end
    end

    def self.Form(&block)
      Dry::Validation.Schema(Form, &block)
    end
  end
end
@iBublik ^^
Artem Pyankov
@iBublik
Apr 08 2016 11:13
@solnic yep, that works fine
Piotr Solnica
@solnic
Apr 08 2016 11:13
great
Artem Pyankov
@iBublik
Apr 08 2016 14:01
@solnic is there something wrong with my code?
Dry::Validation.Form { key(:foo).required(Dry::Types['form.int']) }
=> undefined method `to_ast'  for #<Dry::Types::Constructor:0x007f99c57ad148>
Piotr Solnica
@solnic
Apr 08 2016 14:04
@iBublik yes, only constrained types are supported; and there’s no reason to use form.int
instead, just do required(:int?)
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 08 2016 16:09
Hey folks, a question to custom predicates: Is it right that defintion order and calling order of arguments are reversed? see this example:
module Auction::Contract
  class Create < Reform::Form
    #...
    #v1 = 1; v2= 2; v3 = 3; v4 = 4

    validation :default do
      rule( test_rule: [ :v1, :v2, :v3, :v4 ] ) do
        |v1, v2, v3, v4|
        ( v4.test?( v3, v2, v1 ) )
      end

      configure do
        config.messages  = :i18n

        def test?( v1, v2, v3, v4 )
          Rails.logger.debug 'v1 ' + v1.to_s
          Rails.logger.debug 'v2 ' + v2.to_s
          Rails.logger.debug 'v3 ' + v3.to_s
          Rails.logger.debug 'v4 ' + v4.to_s
          true
        end
      end
    end
  end
end

v1 1
v2 2
v3 3
v4 4
Piotr Solnica
@solnic
Apr 08 2016 16:12
yeah foo.bar?(baz) => bar?(baz, foo)
the dsl is an abstract language
predicates are stateless functions that are composed
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 08 2016 16:16
ok
John Backus
@backus
Apr 08 2016 20:04
@solnic I followed up on dry-rb/dry-types#72
Piotr Solnica
@solnic
Apr 08 2016 20:06
@backus thank you. I'll give it some thought and get back to you tomorrow.
John Backus
@backus
Apr 08 2016 20:08
Cool :) just wanted to make sure I got a decision out of you before you left
Piotr Solnica
@solnic
Apr 08 2016 20:11
@backus I'm gonna be around during next week too. I discovered a couple of extra things I want to do in rom before I go
John Backus
@backus
Apr 08 2016 20:12
Oh heh cool