Codebase list golang-github-go-playground-validator-v10 / b297167
Merge pull request #361 from jservice-rvbd/unique-for-maps Extend the unique tag to also cover map values. Dean Karn authored 5 years ago GitHub committed 5 years ago
3 changed file(s) with 35 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
00 package validator
11
22 import (
3 "bytes"
34 "context"
5 "crypto/sha256"
46 "fmt"
57 "net"
68 "net/url"
1012 "sync"
1113 "time"
1214 "unicode/utf8"
13 "crypto/sha256"
14 "bytes"
1515 )
1616
1717 // Func accepts a FieldLevel interface for all validation needs. The return
186186 return false
187187 }
188188
189 // isUnique is the validation function for validating if each array|slice element is unique
189 // isUnique is the validation function for validating if each array|slice|map value is unique
190190 func isUnique(fl FieldLevel) bool {
191191
192192 field := fl.Field()
194194
195195 switch field.Kind() {
196196 case reflect.Slice, reflect.Array:
197 m := reflect.MakeMap(reflect.MapOf(fl.Field().Type().Elem(), v.Type()))
197 m := reflect.MakeMap(reflect.MapOf(field.Type().Elem(), v.Type()))
198198
199199 for i := 0; i < field.Len(); i++ {
200200 m.SetMapIndex(field.Index(i), v)
201 }
202 return field.Len() == m.Len()
203 case reflect.Map:
204 m := reflect.MakeMap(reflect.MapOf(field.Type().Elem(), v.Type()))
205
206 for _, k := range field.MapKeys() {
207 m.SetMapIndex(field.MapIndex(k), v)
201208 }
202209 return field.Len() == m.Len()
203210 default:
505505 Unique
506506
507507 For arrays & slices, unique will ensure that there are no duplicates.
508 For maps, unique will ensure that there are no duplicate values.
508509
509510 Usage: unique
510511
76957695 param interface{}
76967696 expected bool
76977697 }{
7698 // Arrays
7699 {[2]string{"a", "b"}, true},
7700 {[2]int{1, 2}, true},
7701 {[2]float64{1, 2}, true},
7702 {[2]interface{}{"a", "b"}, true},
7703 {[2]interface{}{"a", 1}, true},
7704 {[2]float64{1, 1}, false},
7705 {[2]int{1, 1}, false},
7706 {[2]string{"a", "a"}, false},
7707 {[2]interface{}{"a", "a"}, false},
7708 {[4]interface{}{"a", 1, "b", 1}, false},
7709 // Slices
76987710 {[]string{"a", "b"}, true},
76997711 {[]int{1, 2}, true},
77007712 {[]float64{1, 2}, true},
77057717 {[]string{"a", "a"}, false},
77067718 {[]interface{}{"a", "a"}, false},
77077719 {[]interface{}{"a", 1, "b", 1}, false},
7720 // Maps
7721 {map[string]string{"one": "a", "two": "b"}, true},
7722 {map[string]int{"one": 1, "two": 2}, true},
7723 {map[string]float64{"one": 1, "two": 2}, true},
7724 {map[string]interface{}{"one": "a", "two": "b"}, true},
7725 {map[string]interface{}{"one": "a", "two": 1}, true},
7726 {map[string]float64{"one": 1, "two": 1}, false},
7727 {map[string]int{"one": 1, "two": 1}, false},
7728 {map[string]string{"one": "a", "two": "a"}, false},
7729 {map[string]interface{}{"one": "a", "two": "a"}, false},
7730 {map[string]interface{}{"one": "a", "two": 1, "three": "b", "four": 1}, false},
77087731 }
77097732
77107733 validate := New()