by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Daniel Gollahon
@dgollahon
@solnic: great, thanks. :)
Piotr Solnica
@solnic
@dgollahon wait, you want to inherit them how exactly?
@dgollahon ie if you have rules for a in two schemas, you’d like to AND them?
Daniel Gollahon
@dgollahon

Well, mainly looking to understand all the reuse options available because i only know what's in that docs page^

In this case i basically want the equivalence of inheritance and I was unsure if you could do something like that. a, b, and c just represent different keys I want to validate. If object A has the a and b attributes and object B has A's attributes plus a new one, c, I'd like to be able to reuse my a and b validations.

or something like a mixin if i have year, month, day attributes in multiple places i might like to mixin the date schema i've built.
for a date with year, month, and day though it's likely they'd be in a separate object and i could use the reuse mechanism from the docs
but i have a few equivalent cases where i have more than one object with common "flat" attributes
does that make sense?
Piotr Solnica
@solnic
@dgollahon I see, I thought you had two schemas with rules for the same key and wanted somehow to combine those
Daniel Gollahon
@dgollahon
ah, no. but that could also be helpful in some cases.
my app has a reasonable number of schemas now, but they're starting to grow and they have a good bit of duplication so i'm trying to narrow down a few cases.
Piotr Solnica
@solnic
this is easily doable but I won’t add it unless somebody actually has a use case and reports an issue :)
sure, gimme a sec
Daniel Gollahon
@dgollahon
ok, sometime i'll review the schemas we have and think about what i'd ideally want and when i've got a clearer open an issue or discuss here. like i said, it's not out of hand yet... but i could see the duplication getting that way.
just was curious if there were already more mechanism that i didn't know about beyond just doing .schema(other_schema)
Piotr Solnica
@solnic
require 'dry-validation'

OneSchema = Dry::Validation.Schema do
  required(:name).filled(:str?)
  required(:email).filled(:str?)
end

AnotherSchema = Dry::Validation.Schema(OneSchema) do
  required(:age).value(:int?)
end
@dgollahon ^^
this will prepend rules from OneSchema
Daniel Gollahon
@dgollahon
oh, interesting
that's actually exactly what i was asking about
Piotr Solnica
@solnic
if you have more you can do this too:
Dry::Validation.Schema(Dry::Validation::Schema, rules: SomeSchema.class.rules + OtherSchema.class.rules)
so as you can see, it’s awful :laughing:
Daniel Gollahon
@dgollahon
haha
Piotr Solnica
@solnic
there’s an issue about it
Daniel Gollahon
@dgollahon
that last one is a little awkward, yeah
Piotr Solnica
@solnic
well, it’s just not done yet (hence no docs too)
Daniel Gollahon
@dgollahon
but the first case seems potentially useful. i'll have to tinker with it.
yeah
totally understand
Piotr Solnica
@solnic
this is tricky stuff so it’s taking some time
Daniel Gollahon
@dgollahon
yeah, definitely
Piotr Solnica
@solnic
schemas have all sorts of properties, so it’s not just “use inheritance Luke!”
Daniel Gollahon
@dgollahon
but good to know there's an effort about it.
haha, yeah.
Piotr Solnica
@solnic
well, we just need to come up with an API :)
because internals are ready
Daniel Gollahon
@dgollahon
and "use inheritance Luke!" is also not inherently simple.
cool
Piotr Solnica
@solnic
that too :)
schema rules is just an array with rules, so we can append stuff, prepend stuff, compose with others, whatever we want
and since input processors are decoupled, this doesn’t affect the rules at all
furthermore, message compilation is decoupled too, so we can easily tune message settings for a schema that inherits rules from multiple other schemas that have different message settings :)
Daniel Gollahon
@dgollahon
neat, seems great. just time to do some API pondering.
seems like some of the harder parts will be if you have collisions
where both schema A and B operate on key foo.
Piotr Solnica
@solnic
we can do stuff like [a_rules.select { |r| r.name == :foo } + b_rules.select { |r| r.name == :foo }].reduce(:and)
errrr s/select/detect/
this stuff is crazy flexible so we can either do this, or make it configurable, or raise an error, dunno :)
Daniel Gollahon
@dgollahon
yeah. i would expect you could inadvertently get contradictory rules with an and. but maybe that's just your fault and ohwell :P