dry-rb / dry-logic

Predicate logic with rule composition
https://dry-rb.org/gems/dry-logic/
MIT License
179 stars 66 forks source link

Nested predicates #29

Closed Kukunin closed 5 years ago

Kukunin commented 7 years ago

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

Kukunin commented 7 years ago

A couple caveats:

  1. There is already attr? predicate
  2. what's the syntax for nested predicates? dry-types uses keys without ? sign, while dry-validates expects keys with ?.
  3. 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?
Kukunin commented 7 years ago
  1. It'd be good to compose nested predicate failing message based on sub-predicates
solnic commented 7 years ago

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? :)

solnic commented 7 years ago

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.

Kukunin commented 7 years ago

I see. probably the best way is to implement attribute: predicate in dry-struct with syntax above

solnic commented 5 years ago

I don't think we'll ever do this. Closing for now. Feel free to bring this up again on our discussion forum.