Nested predicates
Kukunin opened this issue · comments
Can we build predicate which can validate attributes against nested predicates?
It'd be useful for Dry::Struct, something like:
class User < Dry::Struct
attribute :age, Types::Strict::Int
end
Adult = User.constrained(attribute: {age: {gteq: 18}})
Of course, we can invent better DSL
A couple caveats:
- There is already
attr?
predicate - what's the syntax for nested predicates?
dry-types
uses keys without?
sign, whiledry-validates
expects keys with?
. - That fact, that hash or rule compiler is implemented outside of
dry-logic
: https://github.com/dry-rb/dry-types/blob/f618eda432bbab75a33c7a1457eb267e1ed5dac5/lib/dry/types/constraints.rb#L9, isn't it better to implement such predicate there?
- It'd be good to compose nested predicate failing message based on sub-predicates
There is already attr? predicate
This should be removed
what's the syntax for nested predicates?
What's a "nested predicate"?
dry-types uses keys without ? sign, while dry-validates expects keys with ?.
Yes, because constraints != validation
It'd be good to compose nested predicate failing message based on sub-predicates
Could you explain a bit more? :)
That fact, that hash or rule compiler is implemented outside of dry-logic: https://github.com/dry-rb/dry-types/blob/f618eda432bbab75a33c7a1457eb267e1ed5dac5/lib/dry/types/constraints.rb#L9, isn't it better to implement such predicate there?
This is functionality specific to dry-types. in dry-validation DSL is gazillion times more complex due to all kinds of additional features that dry-v has.
I see. probably the best way is to implement attribute:
predicate in dry-struct
with syntax above
I don't think we'll ever do this. Closing for now. Feel free to bring this up again on our discussion forum.