These are chat archives for dry-rb/chat

4th
Aug 2017
Valentin Trinqué
@ValentinTrinque
Aug 04 2017 12:56

Hi everyone. I have trouble making dry-validation work:

class UserSchema < Dry::Validation::Schema
  define! do
    required(:name).filled

    required(:age).maybe(:int?)

    required(:address).schema do
      required(:street).filled
      required(:city).filled
      required(:zipcode).filled
    end
  end
end

# => UserSchema < Dry::Validation::Schema

UserSchema.call({})

# => NoMethodError: undefined method `call' for UserSchema:Class from dry-configurable-0.7.0/lib/dry/configurable.rb:177:in `method_missing'

Did you have encounter this issue ?

It also doesn't recognize the call to configure { ... }
Gustavo Caso
@GustavoCaso
Aug 04 2017 13:14
which versions are you using ?
looks like since you are inheriting you have to instantiate it
UserSchema.new.call({})
Gustavo Caso
@GustavoCaso
Aug 04 2017 13:19
#<Dry::Validation::Result output={} errors={:name=>["is missing"], :age=>["is missing"], :address=>["is missing"]}>
Usually when using this way is to create Base Schema Class
Then you would inherit from this class
Dry::Validation.Schema(UserSchema) do
  # define your rules
end
Usually if you want to create a simple schema
UserSchema = Dry::Validation.Schema do
  required(:name).filled

    required(:age).maybe(:int?)

    required(:address).schema do
      required(:street).filled
      required(:city).filled
      required(:zipcode).filled
    end
end
Valentin Trinqué
@ValentinTrinque
Aug 04 2017 13:25
I am using the 0.11 version. You helped me, I'll make further test then. I'll come back to you ASAP
Valentin Trinqué
@ValentinTrinque
Aug 04 2017 13:59

@GustavoCaso Thanks, I've got some results but face another problem.
I have:

module Crawler
  module Api
    module V1
      Adapter = Dry::Validation.Schema do
        configure do
          config.input_processor = :sanitizer
          config.messages = :i18n
          config.type_specs = true
        end

        required(:locale, Crawler::Api::V1::Types::Locale).filled(:str?)
      end

      Event = Dry::Validation.Schema(Adapter) do
        required(:page).schema(Crawler::Api::V1::Schemas::Page)
      end
    end
  end
end

I get :

[19] pry(main)> Crawler::Api::V1::Adapter.call({})
=> {
    :locale => "fr"
}
[20] pry(main)> Crawler::Api::V1::Event.call({}).messages
=> {
    :locale => [
        [0] "est manquant"
    ],
      :page => [
        [0] "est manquant"
    ]
}

I expected Crawler::Api::V1::Event.call({}).messages to give me the default locale just like Crawler::Api::V1::Adapter.call({}) did (My Locale custom type is handling this). But it doesn't.

Also, is it possible to add default to the rules without recreating a type ?
Gustavo Caso
@GustavoCaso
Aug 04 2017 14:07
What does Crawler::Api::V1::Types::Locale returns
Valentin Trinqué
@ValentinTrinque
Aug 04 2017 14:14
module Crawler
  module Api
    module V1
      module Types
        include Dry::Types.module

        Locale = Types::String
          .constructor do |str|
            break if str.blank?
            str = str.squish.downcase
            is_supported = I18n.available_locales.map(&:to_s).include?(str)
            is_supported ? str : I18n.default_locale.to_s
          end
          .default(I18n.default_locale.to_s)
      end
    end
  end
end
Valentin Trinqué
@ValentinTrinque
Aug 04 2017 15:16
@GustavoCaso ^
Gustavo Caso
@GustavoCaso
Aug 04 2017 16:34
Sorry @ValentinTrinque I'm not at home
Let get there I will try help you
I do not have my laptop to try the code
Gustavo Caso
@GustavoCaso
Aug 04 2017 16:39
Valentin Trinqué
@ValentinTrinque
Aug 04 2017 16:46
@GustavoCaso Ok thanks. Let's keep in touch
Gustavo Caso
@GustavoCaso
Aug 04 2017 16:57
Great @ValentinTrinque