Closed kaz closed 1 year ago
hey @kaz, thanks for taking the time to write up so much detail! this really helps us drill down on whats gone wrong - i'll get around to investigating this further later today and get back to you with some answers shortly after.
Description
If a single message contains multiple repeated fields, each of different types, and each type has a unique constraint, a runtime error occurs.
Steps to Reproduce
buf generate
.go test -run TestValidator_ValidateRepeatedFoo
.Expected Behavior
The test passes without a runtime error.
Actual Behavior
A runtime error occurs.
Screenshots/Logs
Environment
Possible Solution
As a workaround, it's suggested to implement the unique constraint as a custom constraint. It seems to work when dynamically dispatched using
dyn
.Additional Context
The method
loadOrCompileStandardConstraint
in*github.com/bufbuild/protovalidate-go/internal/constraints.Cache
uses onlyprotoreflect FieldDescriptor
as the cache key and does not consider the type of the field being constrained. As a result, it seems that it might return anexpression.ASTSet
that calls the incorrect overload of theunique
.For instance, in the example above, looking inside the AST of the unique constraint for the
y
field reveals that the overloaddouble_unique_bool
has been chosen. (It's expected to selectstring_unique_bool
.)I think that this issue could be resolved by either making the implementation of the Standard constraint dynamically dispatched or by including type information when caching the compile results of the Standard constraint.