Skip to content

How can I add regex=? (regular expression) into the struct tagname and customer the validate function. #561

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

Open
LuckyChen666 opened this issue Dec 27, 2019 · 5 comments
Assignees
Labels

Comments

@LuckyChen666
Copy link

LuckyChen666 commented Dec 27, 2019

Example:
type Test struct{
Name string validate:"regexp=??"
}

@LuckyChen666 LuckyChen666 changed the title How can I add regexp=? into the struct tagname How can I add regexp=? into the struct tagname and customer the validate function Dec 27, 2019
@LuckyChen666 LuckyChen666 changed the title How can I add regexp=? into the struct tagname and customer the validate function How can I add regexp=? into the struct tagname and customer the validate function. Dec 27, 2019
@LuckyChen666 LuckyChen666 changed the title How can I add regexp=? into the struct tagname and customer the validate function. How can I add regex=? (regular expression) into the struct tagname and customer the validate function. Dec 27, 2019
@deankarn
Copy link
Contributor

Hey @celcbratelife

You could create your own regex validation, but I'd recommend against it and create a custom validation with the regex precompiled.

I won't be adding a regex to the built-in ones, please see the documentation for the reasons:

regex
	a regex validator won't be added because commas and = signs can be part
	of a regex which conflict with the validation definitions. Although
	workarounds can be made, they take away from using pure regex's.
	Furthermore it's quick and dirty but the regex's become harder to
	maintain and are not reusable, so it's as much a programming philosophy
	as anything.

	In place of this new validator functions should be created; a regex can
	be used within the validator function and even be precompiled for better
	efficiency within regexes.go.

	And the best reason, you can submit a pull request and we can keep on
	adding to the validation library of this package!

@deankarn deankarn self-assigned this Jan 17, 2020
@kfirfer
Copy link

kfirfer commented Oct 10, 2020

This is workaround:

I couldnt make comma's to work:

type (
	Pagination struct {
		Page int    `query:"page" validate:"gt=0,lte=100000"`
		Size int    `query:"size" validate:"gt=0,lte=1000"`
		Sort string `query:"sort" validate:"max=30,^[a-zA-Z]$"`
	}
)


err := customValidator.RegisterValidation("regexp", util.RegexTag)


func RegexTag(fl validator.FieldLevel) bool {
	field := fl.Field().String()
	if field == "" {
		return true
	}
	regexString := fl.Param()
	regex := regexp.MustCompile(regexString)
	match := regex.MatchString(field)
	return match
}



@kfirfer
Copy link

kfirfer commented Oct 10, 2020

Hi @deankarn

Mabye this issue could help to implement:
go-validator/validator#57

@deankarn
Copy link
Contributor

Hi @kfirfer

Thanks for the interest in adding a regex tag. I still however maintain that adding one would:

  • will make the code less readable
  • overcomplicate the internals of the library

Adding a tag with a pre-compiled regex only takes a minute and then is re-usable on many structs if desired.

@tymonx
Copy link

tymonx commented Feb 1, 2021

One scenario when this generic regexp= tag is desired when you want to use it with API generators like OpenAPI.

The OpenAPI specification is based on the JSON Scheme that defines the pattern field that is based on the ECMA 262 regular expression dialect.

The pattern property in the OpenAPI specification is used for validating defined schema objects.

It is handy, because you can use this pattern property described in your OpenAPI YAML to tell generator to put the pattern (regular expression) inside desired struct tag field of generated struct models to automatically validate them across your API.

Hint: for someone who still want to use the go-playground/validator with the OpenAPI pattern property. You must modify your OpenAPI mustache or handlebars templates for code generation:

  1. Modify template for generating models. For Go servers it is model.mustache
  2. Add the init() function, create a pre-compiled regex from the {{pattern}} tag and register it to global validator under {{#vars}}{{classname}}{{name}}{{/vars}} (in other words under model name + variable field name)
  3. Add the struct tag with that name {{#vars}}validate:{{classname}}{{name}}{{/vars}} for validating

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants