alpha is kind of pointless as if it's not alpha it won't match the countries.
Having said all that for lists like in your example that check equality I recommend creating a custom validation and storing the values in a map or array that can be checked within...that way if your requirements ever change all you have to do is add or remove items from your list, the validation tags don't have to change nor any error messages derived from the your custom validation tag...
Thanks @joeybloggs .
creating a custom validation is pretty easy, but if you need a hand just let me know
I'm trying to figure out the best way to validate some simple-ish strings.
Effectively, they can be alphanumeric (the easy bit), or also contain a few other characters: . - _
At first thought, contains=[char] seemed to work. But, in further testing that's not what contains seems to be for (eg a bad idea).
The only other approach which seems to allow this is by creating custom validators.
Which seems like massive overkill just for adding a few extra chars to the accept list.
Is there something obvious I'm not seeing?
@justinclift the best way to do what you want is a custom validation. It's not hard and will take less time to write than reading this whole reply. In this case you'd want to create a regexes, base it off of existing alphanumericunicode one https://github.com/go-playground/validator/blob/v9/regexes.go#L9 and add your few more accepted characters
thx, that actually what I want to do grab all error, and return in single list og messages
hey guys, quick Q, what's the pattern used to validate a struct with unexported fields? ideally i'd love to just define a func Validate() on it and have it do it, but that doesn't seem to be the way validator is structured
(in my case the top level struct has exported structs I want to validate which has unexported fields)
Registering a struct level validation should allow you to do what your looking to do