These are chat archives for dry-rb/chat

30th
Oct 2018
Artemiy Solopov
@art-solopov
Oct 30 2018 10:16
@solnic I'm working on my web app (you can see the half-baked code at https://gitlab.com/art-solopov/billy-bones-2/tree/refactoring/)
I want to use Dry-Validation to validate stuff and to use Dry-Struct as intermediate form objects (mostly because it's easier to integrate with Rails helpers than hash-like validation results).
Currently I have to duplicate quite a lot of data (attribute names and types).
For example, here's app/schemas/bill_basic_schema.rb:
BillBasicSchema = Dry::Validation::Schema(build: false) do
  required(:period_month) { filled? & int? & included_in?(1..12) }
  required(:period_year) { filled? & int? & gt?(2010) }
  required(:cost) { filled? & float? & gt?(0) }
  optional(:paid_at) { filled? > date? }
  optional(:state) { filled? > included_in?(Bill.state.values) }
  optional(:payment_method_id) { filled? > int? }
  optional(:tags) { array? }
end
And here's the app/forms/bill_form.rb:
class BillForm < ApplicationForm
  attribute :period_month, Types::Params::Integer.default { Time.zone.today.month }
  attribute :period_year, Types::Params::Integer.default { Time.zone.today.year }
  attribute :cost, Types::Params::Float.default(0.0)
  attribute :paid_at, Types::Params::Date.optional.default(nil)
  attribute :state, Types::Coercible::String.default('init')
  attribute :payment_method_id, Types::Params::Integer.optional.default(nil)
  attribute :tags, Types::Coercible::Array.of(Types::Coercible::String).default([])

  SCHEMA = Dry::Validation.Params(BillBasicSchema)

  attr_accessor :errors

  def self.from_model(model)
    period = model.period || Time.zone.today
    attrs = model.values.slice(:cost, :state, :paid_at,
                               :payment_method_id, :tags)
              .merge(period_month: period.month, period_year: period.year)
    new(attrs)
  end

  def self.new_attrs(overrides = {})
    period = Time.zone.today
    overrides.reverse_merge(state: :init,
                            period_month: period.month,
                            period_year: period.year)
  end
end
choallin
@choallin
Oct 30 2018 12:46
Hey guys! I have a strange problem with dry-validations... . I have a validate block in my scheme and, if I have an error in it I get the message that it fails.
But when I want to access the errors, I get the error TypeError (no implicit conversion of Hash into String)
I tried to access the errors with the errors and the messages methods but I get the same error everytime...
Kacper Pucek
@KacperPucek
Oct 30 2018 14:38

Is it possible to use validate in combination with each? Let me show the example:

Dry::Validation.Params do
  required(:tickets).each do
    validate(allowed_quantity: %i(selling_strategy quantity)) do |selling_strategy, quantity|
      # Some logic for checking if quantity can be used
    end

    required(:price).filled(:int?)
    required(:quantity).filled(:int?)
    required(:selling_strategy).filled(:str?)
  end
end

While required checks are nicely nested under tickets, allowed_quantity always bubbles to the top. It would look like this:

{:tickets => { 0 => {:price => ["is missing"]}, :allowed_quantity => ["some msg]}

Any ideas how to nest this custom validation block as well? Cheers!

Kacper Pucek
@KacperPucek
Oct 30 2018 15:33
For anyone interested: dry-rb/dry-validation#305