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

Add support for custom validations #741

Closed
Gictorbit opened this issue Nov 25, 2022 · 3 comments
Closed

Add support for custom validations #741

Gictorbit opened this issue Nov 25, 2022 · 3 comments
Labels
Feature Request Requesting a new feature be added

Comments

@Gictorbit
Copy link

Gictorbit commented Nov 25, 2022

Is there a way to add custom validation functions to generated protobuf messages in go?
for example, we can have another function to receive a validation function which receives a message struct and will be validated after calling validate or validateAll functions

message Person{
  string name=1;
  int age=2;
}
func main(){
  me := &pb.Person {
    Name: "viktor",
    Age: 23,
  }
  me.AddCustomValidation(func(person *pb.Person)error{
    if person.Age > 100 {
      return errAge
    }
      return nil
  })
  if err := me.Validate();err!=nil{
    panic(err)
  }
}
@elliotmjackson
Copy link
Contributor

I'm not sure there is a place for this feature within protoc-gen-validate. Sure, we would achieve being able to execute anon funcs to a method on the base-type but I dont struggle to see why this is necessary. Given your example above, why not just name the anonymous function?

main() {
  me := &pb.Person {
    Name: "viktor",
    Age: 23,
  }
  if err := CustomValidation(me);err!=nil{
    panic(err)
  }
  if err := me.Validate();err!=nil{
    panic(err)
  }
} 
func CustomValidation(person *pb.Person)error{
    if person.Age > 100 {
      return errAge
    }
      return nil
}

I am nevertheless too conscious of my own idiocy that i simply may not grasp the true power of your feature request, if you think this is the case - please dont hesitate to call me out.

@elliotmjackson elliotmjackson added Feature Request Requesting a new feature be added triaged Good First Issue Newcomer-friendly issue labels Dec 5, 2022
@mcgaw
Copy link

mcgaw commented Feb 28, 2023

It would be nice to be able to add some custom validation code that is then "compiled in" to the standard Validate method. This is perhaps what @Gictorbit was alluding to.

@elliotmjackson
Copy link
Contributor

We added custom validation expressions to protovalidate, go check it out! we likely won't add this sort of functionality as we want to make the constraints portable across any consumers of the protos.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request Requesting a new feature be added
Projects
None yet
Development

No branches or pull requests

4 participants