Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Piotr Solnica
@solnic
we don’t want to do it all over again during message compilation
Fran Worley
@fran-worley
Yeah adding more overhead especially for valid runs
I've also got these coming through:
dry-validation-0.9.5/lib/dry/validation/schema/value.rb:42: warning: method redefined; discarding old schema
dry-types-0.8.1/lib/dry/types/options.rb:18: warning: method redefined; discarding old meta
dry-types-0.8.1/lib/dry/types/struct/class_interface.rb:65: warning: instance variable @schema not initialized
dry-types-0.8.1/lib/dry/types/struct/class_interface.rb:59: warning: instance variable @constructor_type not
But the messages one is the worst as it comes through for every single message. I know I could just turn warnings of but :monkey_face:
Piotr Solnica
@solnic
I thought we fixed those warnings in dry-struct
Fran Worley
@fran-worley
Possibly thats coming through form old dry-types
Piotr Solnica
@solnic
oh right
Piotr Solnica
@solnic
@fran-worley just added support for arbitrary validation blocks: dry-rb/dry-validation@8922ac0
@dsounded ^^ this will make things simpler for you, but I still need to figure out a way for defining that error message should be attached to something else than the last attribute name in the list of rule deps
ie right now doing validate(foo?: [:bar, :baz, :foo]) { … } will attach error messages to :foo key (the last dependency)
Fran Worley
@fran-worley
@solnic what's the difference between these and high level rules?
My end goal is to be able to essentially validate X schema if Y schema passes
Piotr Solnica
@solnic
@fran-worley this allows arbitrary code
and it’s executed in the context of the schema object
so self in this block is a schema
Fran Worley
@fran-worley
So in theory you could make another validate block a dependency ?
Piotr Solnica
@solnic
yes, I always wanted to support that
but first I need to figure out how to identify them and how to enforce error key so that it doesn’t default to the last dep name
Fran Worley
@fran-worley
validate :simple_checks do
  required(:email).filled(:str?)
  required(:name).filled(:str?)
  required(:birthday).filled(:date?)
end

# only run this group if our simple_checks pass
validate advanced_checks: :simple_checks do
  # not sure how you could append to an existing key. 
  # In theory we don't need to check that the key exists any-more as we've already done that.
  value(:email) { valid_email?  & unique_email? }
  value(:birthday) { gt?(some_date) }
end
Piotr Solnica
@solnic
what’s the reason for grouping here?
Fran Worley
@fran-worley
I split my validations up into existence/ type/ format checks vs more complex database reliant checks like existence or permissions. That way if the simple ones fail, I don't bother going any further
However I might have simple checks on all fields in my form which could have dozens of fields so declaring each one as a dependency would be a PITA
Piotr Solnica
@solnic
but that makes entire group of rules depend on other groups of rules, so ie failing basic check for :name will prevent :email which is completely unrelated to :name
Fran Worley
@fran-worley
I want to start by validating that email, name and birthday exist and are the correct data type. If that group passes then I want to validate some other things which could involve hitting the database etc. If it doesn't then I'll just return the errors back to the user for them to try again.
I usually only split database checks, or safe guards against HTML form hacking (i.e. checking the my user has the right to link to that record) so hints on these rules are a bit irrelevant.
Kiril Dokh
@dsounded
@solnic I do appreciate your job
Fran Worley
@fran-worley
@dsounded he's a bit of a :star2: really. I'm not sure how you find the time @solnic between a job, family and sleep!
Aditya Tiwari
@aditya01933
i want to create schema mix of array and normal param. something like this
PurchaseSchema = Dry::Validation.Schema do 
  each do 
    schema do
      required(:line_item_id).filled
      required(:quantity).filled
    end
  end
  required(:account_id).filled
  required(:stripe_customer_id).filled
end
Piotr Solnica
@solnic
that’s not possible, each is for arrays, you can’t have a schema for a hash and an array at the same time
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