dry-rb / dry-logic

Predicate logic with rule composition

Home Page:https://dry-rb.org/gems/dry-logic/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

  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?
  1. 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.