These are chat archives for dry-rb/chat

4th
Jul 2018
Nikolay Markov
@mainameiz
Jul 04 2018 09:51

Hello! Could you help me with validation of nested form params?
Seems like type conversions does not work with nested forms + when method.
E.g.

BookSchema = Dry::Validation.Params do
  required(:publication_date).filled(:time?)
end

AudiobookSchema = Dry::Validation.Params do
  required(:started_at).filled(:time?)
end

MessageSchema = Dry::Validation.Params do
  required(:book).maybe

  required(:type).filled(:str?)
    .when(eql?: 'book') do
      required(:book).schema(BookSchema)
    end
    .when(eql?: 'audio') do
      required(:book).schema(AudiobookSchema)
    end
end

puts BookSchema.call(publication_date: Time.now.to_s).inspect # => ok
puts MessageSchema.call(type: 'book', book: { publication_date: Time.now.to_s }).inspect # => error
# => errors={:book=>{:publication_date=>["must be a time"]}}>
puts MessageSchema.call(type: 'audio', book: { started_at: Time.now.to_s }).inspect # => error
# => errors={:book=>{:started_at=>["must be a time"]}}>

Is it possible to do this kind of validation?

Chris Richards
@cmrichards
Jul 04 2018 13:06
How did I not know about 'when'??
ok, so 'when' is awesome
Nikolay Markov
@mainameiz
Jul 04 2018 13:10
don't remember :) read about it somewhere in the internet
Chris Richards
@cmrichards
Jul 04 2018 13:11
Just reading about it in the docs here http://dry-rb.org/gems/dry-validation/basics/macros/#when . I'm already not sure how I lived without it.
Nikolay Markov
@mainameiz
Jul 04 2018 13:14
Seems like there should be a predicate that checks value against some schema
Piotr Solnica
@solnic
Jul 04 2018 13:14
pls use rule or validate instead, when is gonna be removed in 1.0
Chris Richards
@cmrichards
Jul 04 2018 13:24
@solnic Why is it being removed? It looks like a really good way to trigger a different set of validations based on certain conditions
Ok i see
Rules are more capable
Piotr Solnica
@solnic
Jul 04 2018 13:29
when is actually translated to rule
Nikolay Markov
@mainameiz
Jul 04 2018 13:29
:thumbsup: I will try
Piotr Solnica
@solnic
Jul 04 2018 13:30
it's being removed because of the general refactoring for 1.0, where dry-validation will focus on high-level validations with simplified and improved syntax, and dry-schema will be used solely for basic type-checks & coercion
Nikolay Markov
@mainameiz
Jul 04 2018 13:39

Ok, I tried

BookSchema = Dry::Validation.Params do
  required(:publication_date).filled(:time?)
end

AudiobookSchema = Dry::Validation.Params do
  required(:started_at).filled(:time?)
end

MessageSchema = Dry::Validation.Schema do
  configure do
    def self.messages
      super.merge(en: { errors: { book: 'invalid format' }})
    end
  end

  required(:book).filled(:hash?)
  required(:type).filled(:str?)

  validate(book: %i(type book)) do |type, book|
    case type
    when 'book'
      BookSchema.call(book).success?
    when 'audio'
      AudiobookSchema.call(book).success?
    end
  end
end

puts BookSchema.call(publication_date: Time.now.to_s).inspect # => ok (with conversion)
# => output={:publication_date=>2018-07-04 16:37:28 +0300} errors={}>
puts MessageSchema.call(type: 'book', book: { publication_date: Time.now.to_s }).inspect # => ok (date is not converted)
# => output={:type=>"book", :book=>{:publication_date=>"2018-07-04 16:37:28 +0300"}} errors={}>
puts MessageSchema.call(type: 'audio', book: { started_at: Time.now.to_s }).inspect # => ok (date is not converted)
# => output={:type=>"audio", :book=>{:started_at=>"2018-07-04 16:37:28 +0300"}} errors={}>

There is no errors anymore, but serialized attributes is not converted anyway