Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Sean Winner
@swinner2
@timriley that makes sense and I agree. I guess it’s more of a convenience for the other developers. I wanted a generic schema to validate input for trailblazer operations that would whitelist and coerce, specifically ”true” => true. For now we’ll go with just whitelisting. Some people freak out when seeing types in ruby :scream_cat:
Markus Unterwaditzer
@untitaker
Hi, is there any way to have a Dry::Struct with private fields?
Piotr Solnica
@solnic
@untitaker just do private :attr_name
Markus Unterwaditzer
@untitaker
ah thanks. I only tried private; attribute :foo which wasn't working
Piotr Solnica
@solnic
@untitaker private is a method, it accepts name of a method that you want to mark as private
Markus Unterwaditzer
@untitaker
yes, but in plain classes I can write a line that just says private and all methods below that line are then private, that's why I tried it out
Tim Canty
@timmcanty
Hi, I'm trying to do something specific with dry-validations, but am having a tough time. I have a field that needs to: 1. be optional 2. be an array, if it is present 3. have an each predicate run on the contents. It seems like chaining maybe(:array?).each(predicate) does not throw errors when the value is not an array.
Eugene Khutko
@FoboCasteR
Hi!
What is the Dry::Types::Definition#try method for?
Gustavo Caso
@GustavoCaso
I checks if the value provided is the valid
If so returns a Success object with the value
If is not valid it will return Failureobject with the value and the error message
Gustavo Caso
@GustavoCaso
@FoboCasteR did it help?
Eugene Khutko
@FoboCasteR
Thanks. I already use it this way. But I not found any docs or code examples.
Eugene Khutko
@FoboCasteR
Also, it seems to me inconvenient that Result from Dry::Types instead of Dry::Monads
Piotr Solnica
@solnic
@FoboCasteR we could consider changing it to use Dry::Monads
Gustavo Caso
@GustavoCaso
I should not be a hard change to make
Eugene Khutko
@FoboCasteR
@GustavoCaso Maybe. But this change will make dry-monads a hard dependency.
Gustavo Caso
@GustavoCaso
Yes definitely it will make a dependency of dry-types
Pablo Herrero
@pabloh
Any Idea why this works:
class SubForm < Form
  define! do
    required(:foo).filled(:int?)
    optional(:bar).maybe(:int?)
  end
end

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

    optional(:bar).maybe do
      schema(SubForm.new) # Using new
    end
  end
end
But this doesn't?:
class SubForm < Form
  define! do
    required(:foo).filled(:int?)
    optional(:bar).maybe(:int?)
  end
end

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

    optional(:bar).maybe do
      schema(SubForm) # Using class directly
    end
  end
end
Gustavo Caso
@GustavoCaso
:clap: :clap:
Jeff Dickey
@jdickey
:tada:
Vasily Kolesnikov
@v-kolesnikov
:fire: :+1:
Chris Richards
@cmrichards
Is Factory Girl a good technique to create dB objects use during testing with a new app that will use dry-validation heavily when creating records from form data? Or would it be better to create servicey-type objects and only use them to setup initial DB data in tests?
Piotr Solnica
@solnic
@cmrichards whatever works, we use rom-factory but it's not as mature as FactoryBot (reminder: they renamed the gem)
Chris Richards
@cmrichards
ok. I thought you'd use the same service objects you create in your app to make the objects required in your tests. e.g. CreateCompany.({...}).
Chris Richards
@cmrichards
My concern with using something like factory-bot is that it's a third place you need to maintain the idea of what a valid object is: tests, normal-code, and factory-bot.
Chris Richards
@cmrichards
The functional style of dry-validation::Form is great. The way simple_form and rails renders forms using the form-object/AM-interface is also great. I want some way of living in both worlds, but im not sure if it is possible
@solnic how do you render forms? Do you just pass in a hash to the form renderer rather than any kind of form object?
Chris Richards
@cmrichards
So it seems like this can't be placed in a Service superclass: include Dry::Matcher.for(:call, with: Dry::Matcher::EitherMatcher)
Piotr Solnica
@solnic
@cmrichards we use an in-house gem called formalist
Chris Richards
@cmrichards
Strange. I have to turn rails' params into a hash before the string will be accepted as an integer:
(byebug) params[:company_branch]
<ActionController::Parameters {"company_id"=>"9", "name"=>"", "address"=>"", "post_code"=>"", "phone"=>""} permitted: true>
(byebug)  CreateCompanyBranch.new.call(params[:company_branch])
Failure({:company_id=>["must be an integer"], :name=>["must be filled"]})
(byebug)  CreateCompanyBranch.new.call(params[:company_branch].to_h)
Failure({:name=>["must be filled"]})
Scott Eckenrode
@selectport
@cmrichards - assuming you're on rails 5 now - params now returns a ActionController::Parameters object which is not hash-like itself
roma
@milushov

Hey guys, mb somebody already asked, but I did not find; so can u help me find information about:

How to add custom variable to the error message?

en:
  errors:
    minimum_amount?: "amount must be greteat or equal to %{minimum}, yours is %{value}"

I have the value, but what is the best way to pass minimum variable to the err message?

Pablo Herrero
@pabloh

Sorry, an idea why this works:

class SubForm < Form
  define! do
    required(:foo).filled(:int?)
    optional(:bar).maybe(:int?)
  end
end

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

    optional(:bar).maybe do
      schema(SubForm.new) # Using new
    end
  end
end

but this doesn't:

class SubForm < Form
  define! do
    required(:foo).filled(:int?)
    optional(:bar).maybe(:int?)
  end
end

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

    optional(:bar).maybe do
      schema(SubForm) # Using class directly
    end
  end
end

?

Aaron Barthel
@abrthel
@pabloh a couple of questions, what's Form and what are you trying to do with these schemas, are you just trying to be able to reuse them or are you trying to create some form of base schema that other schemas can build off of?
Aaron Barthel
@abrthel
I'm just going to take a leap of faith and assume you're most likely just looking for reusable validation schemas. Hopefully that's the case :smile:
SubForm = Dry::Validation.Form do
  required(:foo).filled(:int?)
  optional(:bar).maybe(:int?)
end

MainForm = Dry::Validation.Form do
  required(:qux).filled(:int?)
  optional(:bar).maybe do
    schema(SubForm)
  end
end
Chris Richards
@cmrichards
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
@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
so it does!
--thumbs-up--
Tim Riley
@timriley
:information_desk_person:
Pablo Herrero
@pabloh
@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