These are chat archives for dry-rb/chat

9th
Jan 2018
Chris Richards
@cmrichards
Jan 09 2018 11:12
Might be nice if my Dry::Matcher::EitherMatcher when used as service-objects would throw an exception if a success and failure block aren't used - not sure though.
Tim Riley
@timriley
Jan 09 2018 11:13
@cmrichards dry-matched already raises an exception if the result block isn’t exhaustive (ie. If it doesn’t provide handlers for all cases)
Dry-matcher I mean
Is that what you were asking?
Chris Richards
@cmrichards
Jan 09 2018 11:15
so it does!
--thumbs-up--
Tim Riley
@timriley
Jan 09 2018 11:15
:information_desk_person:
Pablo Herrero
@pabloh
Jan 09 2018 21:51
@abrthel , sorry I couldn't answer you yesterday, Form is just Dry::Validation::Schema::Form.
And what I was trying to do is define base forms so you can latter extend them by doing:
class AppForm < Dry::Validation::Form
  define! do
    # define common rules, if any
  end
end

MyForm = Dry::Validation.Schema(AppForm) do
  # define your rules
end
I was surprised you could do Dry::Validation.Schema(AppForm) but required(:foo).schema(AppForm) would not work.
Since apparently the schema method can take a class as parameter: https://github.com/dry-rb/dry-validation/blob/master/lib/dry/validation/schema/rule.rb#L31
Aaron Barthel
@abrthel
Jan 09 2018 22:05
@pabloh, you're running into a problem of definitions vs instanced objects. When you use Dry::Validation.Form you're actually creating an instance of a schema but when you subclass Dry::Validation::Schema::Form all you have is the subclass but it isn't an instantiated instance... if that makes sense. Methods like required(:qux).schema() need schema instances to work which is why you needed Subform.new
Can you describe the situation where you need to define a base schema that also references another base schema?
An example of what I mean by an instance problem
SubForm = Dry::Validation.Form do
  required(:foo).filled(:int?)
  optional(:bar).maybe(:int?)
end

class MainForm < Dry::Validation::Schema::Form
  define! do
    required(:qux).filled(:int?)

    optional(:bar).maybe do
      schema(SubForm)
    end
  end
end

schema = Dry::Validation.Form(MainForm) do
end

puts schema.call({qux: 1, bar: {foo: 2, bar: 'f'}}).inspect
Chris Richards
@cmrichards
Jan 09 2018 22:37
Does anyone know how to grab the Dry::Validation.Form from a Reform::Form class?
Aaron Barthel
@abrthel
Jan 09 2018 23:15
@cmrichards Probably a question to as over at https://gitter.im/trailblazer/chat
Chris Richards
@cmrichards
Jan 09 2018 23:16
it's ok now, I solved this a different way
thanks