forked from go-playground/validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add map key validation support (go-playground#324)
- Loading branch information
Dean Karn
authored
Nov 13, 2017
1 parent
1304298
commit 61caf9d
Showing
9 changed files
with
557 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"gopkg.in/go-playground/validator.v9" | ||
) | ||
|
||
// Test ... | ||
type Test struct { | ||
Array []string `validate:"required,gt=0,dive,required"` | ||
Map map[string]string `validate:"required,gt=0,dive,keys,keymax,endkeys,required,max=1000"` | ||
} | ||
|
||
// use a single instance of Validate, it caches struct info | ||
var validate *validator.Validate | ||
|
||
func main() { | ||
|
||
validate = validator.New() | ||
|
||
// registering alias so we can see the differences between | ||
// map key, value validation errors | ||
validate.RegisterAlias("keymax", "max=10") | ||
|
||
var test Test | ||
|
||
val(test) | ||
|
||
test.Array = []string{""} | ||
test.Map = map[string]string{"test > than 10": ""} | ||
val(test) | ||
} | ||
|
||
func val(test Test) { | ||
fmt.Println("testing") | ||
err := validate.Struct(test) | ||
fmt.Println(err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.