Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Aditya Tiwari
@aditya01933
so what's the solution for such kind of problem ?
Piotr Solnica
@solnic
just use two schemas
Aditya Tiwari
@aditya01933
Alright Thanks that would work for me
Fran Worley
@fran-worley

@aditya01933 unless your array is actually accessed via a key i.e.

{ 
  line_items: [
    {line_item_id: 10, quantity: 5},
    {line_item_id: 15, quantity: 3}
  ], 
  account_id: 5, 
  stripe_customer_id: '1103'
}

Then you could keep it all in one schema:

PurchaseSchema = Dry::Validation.Schema do 
  required(:line_items).each do 
    schema do
      required(:line_item_id).filled
      required(:quantity).filled
    end
  end
  required(:account_id).filled
  required(:stripe_customer_id).filled
end
Aditya Tiwari
@aditya01933
Perfect!
Fran Worley
@fran-worley
If there is a chance that 'line_items' could be blank or not an array, you might want to validate that first
Aditya Tiwari
@aditya01933
no line item should be filled.
Fran Worley
@fran-worley
Easy peasy then :smile_cat:
Aditya Tiwari
@aditya01933
Yeah thanks Fran :)
PurchaseSchema = Dry::Validation(ApplicationSchema).Schema do 
  required(:line_items).each do 
    schema do
      required(:line_item_id).filled
      required(:quantity).filled
    end
  end
  required(:account_id).filled
  required(:stripe_customer_id).filled
end

If I am passing ApplicationSchema then it is generating error :

NoMethodError: undefined method `Validation' for Dry:Module

Piotr Solnica
@solnic
@aditya01933 Dry::Validation.Schema(ApplicationSchema)
Aditya Tiwari
@aditya01933
yeah fixed it out.. it was my bad :(
Thanks @solnic
:)
Piotr Solnica
@solnic
@fran-worley I’m gonna be a :skull: - :star2: soon ;)
John Backus
@backus
@solnic looks like I'll have time to wrap up dry-types / dry-struct things tomorrow :)
Piotr Solnica
@solnic
@backus oh that would be amazing
it’s possible I’ll wrap up dry-v too…
we’d have an EPIC release then
John Backus
@backus
I don't know if I'll be able to finish it all but I'll have a solid 6-8 hours
and I can prioritize properly to get the necessary functionality ready and then dive into refactoring
to make it most likely we have something releaseable
I just don't want the schemas in dry-t to have so much duplication :'(
Piotr Solnica
@solnic
this is the tricky part
I optimized for performance hence duplication but who knows, maybe it can be cleaner & faster somehow :)
John Backus
@backus
Yeah I'll try to be conscious of that
The other aspect I'm unsure about is whether constraints should ideally be represented in dry logic as much as possible
or if we would rather do all of this key checking, default value filling, and coercion directly like it currently does
Piotr Solnica
@solnic
dry-logic is now few times faster so we could consider that
and there are opportunities for more optimization there
John Backus
@backus
Cool
I also imagine that, for dry-v and dry-t, the more that can be expressed using dry logic the better (barring big perf hits)
Piotr Solnica
@solnic
yeah I mean that was my intention when I extracted dry-logic (it was part of dry-validation originally)
John Backus
@backus
Right
Piotr Solnica
@solnic

ok folks I’m done with logic/validation stuff…I’d appreciate if you could add this:

gem 'dry-validation', github: 'dry-rb/dry-validation', branch: 'master'
gem 'dry-types', github: 'dry-rb/dry-types', branch: 'master'
gem 'dry-struct', github: 'dry-rb/dry-struct', branch: 'master'
gem ‘dry-logic’, github: 'dry-rb/dry-logic', branch: 'master'

to your Gemfile and run your tests…

Nikita Shilnikov
@flash-gordon
@solnic dry-rb/dry-validation#238 <- I'll update this today
Piotr Solnica
@solnic
@flash-gordon cool, you could collaborate with @cored as he’s working on optional dry-struct support which has the identical problem as it needs feature detection too
@fran-worley hey! could you tell me if latest stuff from master works fine with Reform and friends?
Rafael George
@cored
@flash-gordon hey
@solnic according to that issue the dependency then should be remove from the Gemfile? I mean by optional you mean that the user will install the dependency by itself ?
s/itself/himself
Nikita Shilnikov
@flash-gordon
@cored hey, that's what @solnic told me: https://github.com/dry-rb/dry-validation/pull/238#issuecomment-244924977 :) I'm just gonna do a monkey-patch with opt-in to_either method
so this case is trivial ^
Rafael George
@cored
so you will try to require at the same method level ?
Nikita Shilnikov
@flash-gordon
@cored require 'dry/monads/either'; require dry-v/either-ext at the end of result.rb I think, with LoadError being caught
Rafael George
@cored
I see
Nikita Shilnikov
@flash-gordon
@cored dry-struct part can be done with a runtime check like defined? ::Dry::Struct && input < ::Dry::Struct
Rafael George
@cored
I see
so since some specs have a definition for an struct, I guess the check also will be there