bufbuild / protovalidate-go

Protocol Buffer Validation for Go
https://pkg.go.dev/github.com/bufbuild/protovalidate-go
Apache License 2.0
294 stars 19 forks source link

Fails to validate a simple message #85

Closed nomad-software closed 12 months ago

nomad-software commented 12 months ago

Description

Calling the validate method on a proto message used to work but since v0.3.3 it fails.

Steps to Reproduce

I have a simple message test written like this:

syntax = "proto3";

package test.v1;

import "buf/validate/validate.proto";

message SmallMessage {
  string code = 1 [(buf.validate.field).required = true];
  uint32 decimal_places = 2 [(buf.validate.field).uint32 = {gte: 0, lte: 4}];
}

and a unit test like this:

package validator

import (
    "testing"

    pv "github.com/bufbuild/protovalidate-go"
    testv1 "github.com/ennismore/go-buf-connect/v2/internal/test/proto/gen/test/v1"
    "github.com/stretchr/testify/assert"
)

func TestValidatorError(t *testing.T) {
    msg := &testv1.SmallMessage{
        Code:          "",
        DecimalPlaces: 5,
    }

    v, _ := pv.New()
    err := v.Validate(msg)

    assert.Error(t, err)
    assert.Regexp(t, `code: value is required`, err)
    assert.Regexp(t, `decimal_places: value must be greater than or equal to 0 and less than or equal to 4`, err)
}

Expected Behavior

To pass. This worked fine with v0.3.2, I expect it to keep working in later releases.

Actual Behavior

Output

=== RUN   TestValidatorError
    validate_test.go:20: 
            Error Trace:    /home/gary/Projects/go-buf-connect/service/request/validator/validate_test.go:20
            Error:          An error is expected but got nil.
            Test:           TestValidatorError
    validate_test.go:21: 
            Error Trace:    /home/gary/Projects/go-buf-connect/service/request/validator/validate_test.go:21
            Error:          Expect "<nil>" to match "code: value is required"
            Test:           TestValidatorError
    validate_test.go:22: 
            Error Trace:    /home/gary/Projects/go-buf-connect/service/request/validator/validate_test.go:22
            Error:          Expect "<nil>" to match "decimal_places: value must be greater than or equal to 0 and less than or equal to 4"
            Test:           TestValidatorError
--- FAIL: TestValidatorError (0.00s)
FAIL
FAIL    github.com/ennismore/go-buf-connect/v2/service/request/validator    0.004s
FAIL

Screenshots/Logs

buf.gen.yaml

version: v1
managed:
  enabled: true
  go_package_prefix:
    default: github.com/ennismore/go-buf-connect/v2/internal/test/proto/gen
    except:
      - buf.build/bufbuild/protovalidate
plugins:
  - plugin: buf.build/protocolbuffers/go
    opt:
      - paths=source_relative
    out: gen
  - plugin: buf.build/bufbuild/connect-go
    opt:
      - paths=source_relative
    out: gen

Environment

Possible Solution

n/a

Additional Context

n/a

rodaine commented 12 months ago

Thanks for the detailed report, @nomad-software! I am unable to reproduce this bug following your steps using the latest version of this module and protos.

Could you verify for me that the version of the protovalidate protos you are depending on are current (buf mod update) and for good measure bump the version of the remote package in your go.mod (go get -u buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go)?

nomad-software commented 12 months ago

Hi @rodaine I've updated everything you suggested and I also had to regenerate the message's proto-buffers too. It's all passing now. Scary that it all just silently failed validation, good job I had the test!

Thanks for your help.