Skip to content
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

Fails to validate a simple message #85

Closed
nomad-software opened this issue Dec 4, 2023 · 2 comments
Closed

Fails to validate a simple message #85

nomad-software opened this issue Dec 4, 2023 · 2 comments
Assignees
Labels
Bug Something isn't working

Comments

@nomad-software
Copy link

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

  • Operating System: Linux
  • Version: Ubuntu 22.04
  • Compiler/Toolchain: gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
  • Protobuf Compiler & Version: buf 1.17.0
  • Protoc-gen-validate Version:
  • Protovalidate Version: 0.3.3 - 0.4.3

Possible Solution

n/a

Additional Context

n/a

@nomad-software nomad-software added the Bug Something isn't working label Dec 4, 2023
@rodaine
Copy link
Member

rodaine commented Dec 4, 2023

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants