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

implement ValidateMap #338

Merged
merged 8 commits into from
Jan 8, 2020
Merged

implement ValidateMap #338

merged 8 commits into from
Jan 8, 2020

Conversation

mie00
Copy link
Contributor

@mie00 mie00 commented May 10, 2019

Adds ValidateMap which can be used to validate map[string]interface{} against a validation map in the form map[string]interface{}. The validation map values can be either a string in the same form as the tags used in structs (without the key valid) or a map[string]interface{} for recursive validation. The map to be validated can contain values of any kind.

Example usage added in the README (copied from #224)

var mapTemplate = map[string]interface{}{
	"name":"required,alpha",
	"family":"required,alpha",
	"email":"required,email",
	"cell-phone":"numeric",
	"address":map[string]interface{}{
		"line1":"required,alphanum",
		"line2":"alphanum",
		"postal-code":"numeric",
	},
}
 var inputMap = map[string]interface{}{
	"name":"Bob",
	"family":"Smith",
	"email":"foo@bar.baz",
	"address":map[string]interface{}{
		"line1":"1234",
		"line2":"",
		"postal-code":"",
	},
}
 result, err := govalidator.ValidateMap(mapTemplate, inputMap)
if err != nil {
	println("error: " + err.Error())
}
println(result)

It also adds custom tag type(type) to validate map values of type interface{}. However, it can be used with any type. Example of types it can take:

string
int
*int
**int
map[string]string
map[string]interface{}
[][]int

fixes #224

mie00 added 8 commits May 10, 2019 12:47
ValidateMap should be used to validate map[string]interface{} against a
validation map in the form map[string]interface{} and the validation map
should be in the same form as the valid tags. The validation map values
can be either a string or a map[string]interface{} for recursive
validation and the map to be validated can contain values of any kind.
add is type to check for types, this is useful when using ValidateMap as
the map is of type map[string]interface{} and it might be needed to have
some way to check for the types of the values.
@asaskevich asaskevich merged commit 5c4ddb2 into asaskevich:master Jan 8, 2020
@asaskevich
Copy link
Owner

Thank you very much, you did huge work!

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

Successfully merging this pull request may close these issues.

Feature request: Use validator on dynamic nested maps
2 participants