I’m trying to understand how does the OpenAPI Specification work using go language.
I created yaml file which I want to generate to my go code for requests and responses structs.
But some structs need the custom validator. So wrote it in this way:
var validate *validator.Validate
func init() {
validate = validator.New()
if err := validate.RegisterValidation("email", validateEmail); err != nil {
logger.Fatalf("Couldn't register email validator, err=%v", err)
}
}
func validateEmail(fl validator.FieldLevel) bool {
email := fl.Field().String()
emailRegex := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$`
return regexp.MustCompile(emailRegex).MatchString(email)
}
And not I can write struct with such validator tag
type somStruct struct {
Email string `validate:"email"`
}
Is it possible to add to an OpenAPI description that will be generated to such struct?
2
did you try adding the format
keyword? I think openapi-generator-cli uses the format annotations
components:
schemas:
User:
type: object
properties:
email:
type: string
format: email