bufbuild / protovalidate-go

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

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

Closed FocusHuang closed 9 months ago

FocusHuang commented 9 months ago

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

Possible Solution

Additional Context

nicksnyder commented 9 months ago

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 https://github.com/bufbuild/protovalidate-go/issues/67#issuecomment-1785844721

Panlq commented 8 months ago

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

rodaine commented 8 months ago

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.