Baked-in validation to return an error if the request body contains a specific field in Go

55 Views Asked by At

I am using go struct validator library https://github.com/go-playground/validator to validate struct which is used to decode the request body of a patch request. I want return an error if a specific field is present in the request. I can do this by writing a separte validation method. But I want to know if theres a baked-in validations I can use for this.

Following is my struct. I need to return an error if type is present in the request body.

type ClientPhone struct {
    ID          uuid.UUID `json:"id"`
    CountryCode string    `json:"countryCode" validate:"omitempty,min=2,max=3,number"`
    PhoneNumber string    `json:"phoneNumber" validate:"omitempty,number,len=10"`
    Type        string    `json:"type"`
}

I went through the Go struct validator library but I coudnt find a way to use a baked-in validation for this.

0

There are 0 best solutions below