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

[BUG] *.pb.validate.go Validate AND ValidateAll cannot use. #93

Closed
FocusHuang opened this issue Jan 20, 2024 · 3 comments
Closed

[BUG] *.pb.validate.go Validate AND ValidateAll cannot use. #93

FocusHuang opened this issue Jan 20, 2024 · 3 comments
Labels
Question questions about the project

Comments

@FocusHuang
Copy link

Description

when i use - plugin: buf.build/bufbuild/validate-go to generate validation, a *.pb.validate.go is generated, but no validation rule in function Validate or ValidateAll. if i want to use validate, i need to new a protovalidateor to validate the struct. is it excessive?

Steps to Reproduce

buf.gen.yaml

version: v1
managed:
  enabled: false
plugins:
  # generate go struct code
  - plugin: buf.build/protocolbuffers/go
    out: .
    opt:
      - paths=source_relative
  # generate message validator code
  - plugin: buf.build/bufbuild/validate-go
    out: .
    opt:
      - paths=source_relative
message Task {
  int64 id = 1;
  string name = 2 [
    (buf.validate.field).required = true,
    (buf.validate.field).string = {min_len: 1}
  ];
  string description = 3 [
    (buf.validate.field).ignore_empty = true,
    (buf.validate.field).string = {min_len: 1}
  ];
}

task.pb.validate.go

// Validate checks the field values on Task with the rules defined in the proto
// definition for this message. If any rules are violated, the first error
// encountered is returned, or nil if there are no violations.
func (m *Task) Validate() error {
	return m.validate(false)
}

// ValidateAll checks the field values on Task with the rules defined in the
// proto definition for this message. If any rules are violated, the result is
// a list of violation errors wrapped in TaskMultiError, or nil if none found.
func (m *Task) ValidateAll() error {
	return m.validate(true)
}

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

	var errors []error

	// no validation rules for Id

	// no validation rules for Name

	// no validation rules for Description

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

	return nil
}

Expected Behavior

i can use task.Validate() or task.ValidateAll() to validate fields

Actual Behavior

all pass directly

Screenshots/Logs

Environment

  • Operating System: Linux
  • Version: Ubuntu 22.04
  • Compiler/Toolchain: Buf
  • Protobuf Compiler & Version:
  • Protoc-gen-validate Version:
  • Protovalidate Version: e097f827e65240ac9fd4b1158849a8fc
  • BufVersion: 1.28.1

Possible Solution

Additional Context

@FocusHuang FocusHuang added the Bug Something isn't working label Jan 20, 2024
@nicksnyder
Copy link
Member

Hi @FocusHuang, the buf.build/bufbuild/validate-go plugin is for protoc-gen-validate which is a different project.

protovalidate (this project) does not require any code generation. You can look at the README for an example on how to perform validation.

Duplicate of #67 (comment)

@nicksnyder nicksnyder added Question questions about the project and removed Bug Something isn't working labels Jan 22, 2024
@Panlq
Copy link

Panlq commented Feb 22, 2024

Why was the code generation function of protoc-gen-validate abandoned? @nicksnyder

@rodaine
Copy link
Member

rodaine commented Feb 22, 2024

Hi @Panlq! We covered the impetus for moving to a non-codegen approach in our blog post announcing the protovalidate project. If you have further questions, please don't hesitate to open a new issue. We're more than happy to provide further clarification.

@bufbuild bufbuild locked as resolved and limited conversation to collaborators Feb 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question questions about the project
Projects
None yet
Development

No branches or pull requests

4 participants