Skip to content

Commit

Permalink
Fix deprecated field
Browse files Browse the repository at this point in the history
  • Loading branch information
eleftherias committed Dec 12, 2024
1 parent fd3d38d commit cf170dd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/aws/aws-sdk-go-v2/config v1.28.6
github.com/aws/aws-sdk-go-v2/service/sesv2 v1.38.4
github.com/barkimedes/go-deepcopy v0.0.0-20220514131651-17c30cfc62df
github.com/bufbuild/protovalidate-go v0.7.3
github.com/bufbuild/protovalidate-go v0.8.0
github.com/cenkalti/backoff/v4 v4.3.0
github.com/charmbracelet/bubbles v0.20.0
github.com/charmbracelet/bubbletea v1.2.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ github.com/bradleyjkemp/cupaloy/v2 v2.8.0 h1:any4BmKE+jGIaMpnU8YgH/I2LPiLBufr6oM
github.com/bradleyjkemp/cupaloy/v2 v2.8.0/go.mod h1:bm7JXdkRd4BHJk9HpwqAI8BoAY1lps46Enkdqw6aRX0=
github.com/bufbuild/protovalidate-go v0.7.3 h1:kKnoSueygR3xxppvuBpm9SEwIsP359MMRfMBGmRByPg=
github.com/bufbuild/protovalidate-go v0.7.3/go.mod h1:CFv34wMqiBzAHdQ4q/tWYi9ILFYKuaC3/4zh6eqdUck=
github.com/bufbuild/protovalidate-go v0.8.0 h1:Xs3kCLCJ4tQiogJ0iOXm+ClKw/KviW3nLAryCGW2I3Y=
github.com/bufbuild/protovalidate-go v0.8.0/go.mod h1:JPWZInGm2y2NBg3vKDKdDIkvDjyLv31J3hLH5GIFc/Q=
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/bytecodealliance/wasmtime-go/v3 v3.0.2 h1:3uZCA/BLTIu+DqCfguByNMJa2HVHpXvjfy0Dy7g6fuA=
github.com/bytecodealliance/wasmtime-go/v3 v3.0.2/go.mod h1:RnUjnIXxEJcL6BgCvNyzCCRzZcxCgsZCi+RNlvYor5Q=
Expand Down
28 changes: 27 additions & 1 deletion internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,33 @@ func NewValidator() (*protovalidate.Validator, error) {
func formatViolations(violations *validate.Violations) string {
var res []string
for _, v := range violations.Violations {
res = append(res, fmt.Sprintf("- Field '%s': %s", *v.FieldPath, *v.Message))
res = append(res, fmt.Sprintf("- Field '%s': %s", getFullPath(v.Field), *v.Message))
}
return strings.Join(res, "\n")
}

func getFullPath(field *validate.FieldPath) string {
var pathElements []string
for _, element := range field.GetElements() {
if element.GetFieldName() != "" {
pathElements = append(pathElements, element.GetFieldName())
} else if element.GetFieldNumber() != 0 {
pathElements = append(pathElements, fmt.Sprintf("%d", element.GetFieldNumber()))
}
if element.GetSubscript() != nil {
switch subscript := element.GetSubscript().(type) {
case *validate.FieldPathElement_Index:
pathElements[len(pathElements)-1] = fmt.Sprintf("%s[%d]", pathElements[len(pathElements)-1], subscript.Index)
case *validate.FieldPathElement_BoolKey:
pathElements[len(pathElements)-1] = fmt.Sprintf("%s[%t]", pathElements[len(pathElements)-1], subscript.BoolKey)
case *validate.FieldPathElement_IntKey:
pathElements[len(pathElements)-1] = fmt.Sprintf("%s[%d]", pathElements[len(pathElements)-1], subscript.IntKey)
case *validate.FieldPathElement_UintKey:
pathElements[len(pathElements)-1] = fmt.Sprintf("%s[%d]", pathElements[len(pathElements)-1], subscript.UintKey)
case *validate.FieldPathElement_StringKey:
pathElements[len(pathElements)-1] = fmt.Sprintf("%s[%s]", pathElements[len(pathElements)-1], subscript.StringKey)
}
}
}
return strings.Join(pathElements, ".")
}
15 changes: 15 additions & 0 deletions internal/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ func TestProtoValidationInterceptor(t *testing.T) {
errMsg: "Validation failed:\n- Field 'name': value does not match regex pattern",
errCode: codes.InvalidArgument,
},
{
name: "invalid request with nested field",
req: &v1.ListEvaluationResultsRequest{
Context: &v1.Context{
Project: ptr.Ptr(uuid.New().String()),
},
Entity: []*v1.EntityTypedId{
{
Id: "invalid-id",
},
},
},
errMsg: "Validation failed:\n- Field 'entity[0].id': value must be a valid UUID",
errCode: codes.InvalidArgument,
},
}

validator, err := NewValidator()
Expand Down

0 comments on commit cf170dd

Please sign in to comment.