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

How can I use case insensitive values in oneof validator #840

Closed
vijsourabh opened this issue Sep 21, 2021 · 6 comments · May be fixed by #1321
Closed

How can I use case insensitive values in oneof validator #840

vijsourabh opened this issue Sep 21, 2021 · 6 comments · May be fixed by #1321
Assignees
Labels

Comments

@vijsourabh
Copy link

The case is I need to validate case insensitive oneof values in the field of JSON struct and not able to find anything. Can anyone suggest me anything to achieve the same?

@filikos
Copy link

filikos commented Sep 23, 2021

The field to be compared with the oneof values can be converted to lower case before validating the struct.

@deankarn
Copy link
Contributor

@vijsourabh as @filikos stated you could normalize the data if that's an option; here's a lib to help out with that https://github.com/go-playground/mold

If not then a custom validator is also an option.

@deankarn deankarn self-assigned this Oct 24, 2021
@vijsourabh
Copy link
Author

@deankarn & @filikos, It worked, Thanks :)

@draper-lucid
Copy link

This is a rather obtuse solution "downcase the value before validating" the point of the project is to add magic to model validation right? Adding another meta tag that will handle casing would be rather helpful. There are more applications to model validations that serving up 400s to a client, and using the same model across a platform is of benefit. I could accept "we are taking pull requests!" as a better answer than "downcase the value", it's essentially a non-answer.

@deankarn
Copy link
Contributor

@draper-lucid

  1. Please try not to comment on closed issues unless it's also asking to be re-opened.
  2. This is a rather obtuse solution is it? Depends on your use case. Sanitization of data before storing is the norm in my experience ymmv. In any case, it's not a non-answer.
  3. Nobody's stopping anyone from making a PR for a new validation tag, something like case_insensitive_oneof or alike.
  4. Library also allows creating and registering custom validations for you use case that's not supported by default. In fact for oneof I usually register my own custom one in order to leverage an internal map, array etc.. that holds these concrete values in the code and if any are added or removed there's no forgetting to update the struct tags.

@mmengspv
Copy link

Just another idea

Register your new validation

validate.RegisterValidation("oneof_insensitive", validateOneOfInsensitive)

Validation logic for oneof_insensitive case

func validateOneOfInsensitive(fl validator.FieldLevel) bool {
	fieldValue := fl.Field().String()
	allowedValues := strings.Split(fl.Param(), " ")

	for _, allowedValue := range allowedValues {
		if strings.EqualFold(fieldValue, allowedValue) {
			return true
		}
	}

	return false
}

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

Successfully merging a pull request may close this issue.

5 participants