-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature Request] Is it possible to add validated input parameters and rule values in the error Violation? #17
Comments
Hi @shuqingzai, and thanks for the feedback! If you only need to support a single language, for now I'd recommend creating a custom constraint so that you can control the message. Will explore options with the team (exposing the |
you can get it, but with ugly reflection code func main() {
msg := &pb.PageParams{
Page: 0,
PageSize: 10,
}
v, err := protovalidate.New()
if err != nil {
fmt.Println("failed to initialize validator:", err)
}
if err = v.Validate(msg); err != nil {
var valErr *protovalidate.ValidationError
if ok := errors.As(err, &valErr); ok {
errPb := valErr.ToProto()
fmt.Printf("\n%+v\n\n", errPb.GetViolations())
a := errPb.GetViolations()[0]
fieldDesc := msg.ProtoReflect().Descriptor().Fields().ByTextName(a.FieldPath)
// import "buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
typeDesc := (*validate.E_Field).TypeDescriptor()
x := fieldDesc.Options().ProtoReflect().Get(typeDesc).Message().Interface().(*validate.FieldConstraints)
fmt.Println(x.Type) // type is oneof
fmt.Println(*(x.Type.(*validate.FieldConstraints_Uint32).Uint32.Lte))
}
}
} |
Your approach is possible, but when |
Using reflection is inevitable when you append fields (so many rules here). Perhaps a custom error message formatter interface for rules would suit your needs better? |
I18n for fields validating is very necessary. I hope this package can handle it gracefully. |
…ufbuild#17) Supplies a fallback mechanism for the deprecated extension index. Essentially we search for the index that we want, if its not found we look for the old one. depends on bufbuild#43
We addressed this in #265 and in various implementations. |
Feature description:
Example Proto
go validate
When I validate, the error returned by
page
is*validate.Violation
, but I can’t get the request value0
and the rule values:0
and1000
, can the package add new fields (value
andattributes
?? ) return?Problem it solves or use case:
Users can get these values for more custom operations
I need to perform
i18n
now, because the semantics of each language are different when translating, and I need to customize the translation, but I can’t get these values, which makes the work very difficultProposed implementation or solution:
Contribution:
Examples or references:
Additional context:
The text was updated successfully, but these errors were encountered: