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

Part of the validation code is not generated #67

Closed
dorlolo opened this issue Oct 24, 2023 · 2 comments
Closed

Part of the validation code is not generated #67

dorlolo opened this issue Oct 24, 2023 · 2 comments
Labels
Question questions about the project

Comments

@dorlolo
Copy link

dorlolo commented Oct 24, 2023

Description

I am testing the code in the 'example' directory. After using the 'buf generate proto' command, in the 'example.pb.validate.go' file, validation code has been generated for only a few fields. The validation code for fields like 'Id', 'Email', and 'Name' is empty. and the 'Home' field has not generated the primary validation code.

func (m *Person) validate(all bool) error {
	if m == nil {
		return nil
	}

	var errors []error

	// no validation rules for Id

	// no validation rules for Email

	// no validation rules for Name

	if all {
		switch v := interface{}(m.GetHome()).(type) {
		case interface{ ValidateAll() error }:
			if err := v.ValidateAll(); err != nil {
				errors = append(errors, PersonValidationError{
					field:  "Home",
					reason: "embedded message failed validation",
					cause:  err,
				})
			}
		case interface{ Validate() error }:
			if err := v.Validate(); err != nil {
				errors = append(errors, PersonValidationError{
					field:  "Home",
					reason: "embedded message failed validation",
					cause:  err,
				})
			}
		}
	} else if v, ok := interface{}(m.GetHome()).(interface{ Validate() error }); ok {
		if err := v.Validate(); err != nil {
			return PersonValidationError{
				field:  "Home",
				reason: "embedded message failed validation",
				cause:  err,
			}
		}
	}

	if len(errors) > 0 {
		return PersonMultiError(errors)
	}

	return nil
}

this is my buf.gen.yaml :

version: v1
managed:
  enabled: true
  go_package_prefix:
    default: tests.example.v1

plugins:
  - plugin: buf.build/protocolbuffers/go
    out: gen
    opt: paths=source_relative

  - plugin: buf.build/bufbuild/validate-go:v1.0.2
    out: gen
    opt:
      - paths=source_relative

Environment

  • Operating System: Windows 11
  • Compiler/Toolchain: go version go1.20.7 windows/amd64
  • Protobuf Compiler & Version: buf v1.27.1, protoc libprotoc 22.0-rc2
  • Protoc-gen-validate Version: 1.0.2
  • Protovalidate Version: v1.0.2

TestCode

validatetest.zip

@dorlolo dorlolo added the Bug Something isn't working label Oct 24, 2023
@gopkg-dev
Copy link

gopkg-dev commented Oct 25, 2023

buf.gen.yaml

version: v1
managed:
  enabled: true
  go_package_prefix:
    default: tests.example.v1

plugins:
  - plugin: buf.build/protocolbuffers/go
    out: gen
    opt: paths=source_relative

`protovalidate-go` is the Go language implementation
of [`protovalidate`](https://github.com/bufbuild/protovalidate) designed
to validate Protobuf messages at runtime based on user-defined validation constraints.
Powered by Google's Common Expression Language ([CEL](https://github.com/google/cel-spec)), it provides a
flexible and efficient foundation for defining and evaluating custom validation
rules.
The primary goal of `protovalidate` is to help developers ensure data
consistency and integrity across the network without requiring generated code.

example

https://github.com/bufbuild/protovalidate-go#example

https://github.com/gopkg-demo/protovalidate-go-test

@rodaine rodaine removed the Bug Something isn't working label Oct 30, 2023
@rodaine
Copy link
Member

rodaine commented Oct 30, 2023

Hi, @dorlolo! The protovalidate project does not have a code-generation component, the validation rules are determined at runtime via proto reflection by the libraries. The buf.build/bufbuild/validate-go plugin is a generator for protoc-gen-validate which is the predecessor of this project.

The README documentation on protovalidate and protovalidate-go walks through the process of using this project. Let me know if this helps!

@rodaine rodaine changed the title [BUG] Part of the validation code is not generated Part of the validation code is not generated Oct 30, 2023
@rodaine rodaine added the Question questions about the project label Oct 30, 2023
@rodaine rodaine closed this as completed Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question questions about the project
Projects
None yet
Development

No branches or pull requests

3 participants