另客网go项目公用的代码库

validator.go 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /**
  2. * Package validator
  3. *
  4. * MISC:
  5. * - anonymous structs - they don't have names so expect the Struct name within StructErrors to be blank
  6. *
  7. */
  8. package validator
  9. import (
  10. "bytes"
  11. "errors"
  12. "fmt"
  13. "reflect"
  14. "strings"
  15. "sync"
  16. "time"
  17. )
  18. const (
  19. utf8HexComma = "0x2C"
  20. utf8Pipe = "0x7C"
  21. tagSeparator = ","
  22. orSeparator = "|"
  23. tagKeySeparator = "="
  24. structOnlyTag = "structonly"
  25. noStructLevelTag = "nostructlevel"
  26. omitempty = "omitempty"
  27. skipValidationTag = "-"
  28. diveTag = "dive"
  29. existsTag = "exists"
  30. fieldErrMsg = "Key: '%s' Error:Field validation for '%s' failed on the '%s' tag"
  31. arrayIndexFieldName = "%s" + leftBracket + "%d" + rightBracket
  32. mapIndexFieldName = "%s" + leftBracket + "%v" + rightBracket
  33. invalidValidation = "Invalid validation tag on field %s"
  34. undefinedValidation = "Undefined validation function on field %s"
  35. validatorNotInitialized = "Validator instance not initialized"
  36. fieldNameRequired = "Field Name Required"
  37. tagRequired = "Tag Required"
  38. )
  39. var (
  40. timeType = reflect.TypeOf(time.Time{})
  41. timePtrType = reflect.TypeOf(&time.Time{})
  42. defaultCField = new(cField)
  43. )
  44. // StructLevel contains all of the information and helper methods
  45. // for reporting errors during struct level validation
  46. type StructLevel struct {
  47. TopStruct reflect.Value
  48. CurrentStruct reflect.Value
  49. errPrefix string
  50. nsPrefix string
  51. errs ValidationErrors
  52. v *Validate
  53. }
  54. // ReportValidationErrors accepts the key relative to the top level struct and validatin errors.
  55. // Example: had a triple nested struct User, ContactInfo, Country and ran errs := validate.Struct(country)
  56. // from within a User struct level validation would call this method like so:
  57. // ReportValidationErrors("ContactInfo.", errs)
  58. // NOTE: relativeKey can contain both the Field Relative and Custom name relative paths
  59. // i.e. ReportValidationErrors("ContactInfo.|cInfo", errs) where cInfo represents say the JSON name of
  60. // the relative path; this will be split into 2 variables in the next valiator version.
  61. func (sl *StructLevel) ReportValidationErrors(relativeKey string, errs ValidationErrors) {
  62. for _, e := range errs {
  63. idx := strings.Index(relativeKey, "|")
  64. var rel string
  65. var cRel string
  66. if idx != -1 {
  67. rel = relativeKey[:idx]
  68. cRel = relativeKey[idx+1:]
  69. } else {
  70. rel = relativeKey
  71. }
  72. key := sl.errPrefix + rel + e.Field
  73. e.FieldNamespace = key
  74. e.NameNamespace = sl.nsPrefix + cRel + e.Name
  75. sl.errs[key] = e
  76. }
  77. }
  78. // ReportError reports an error just by passing the field and tag information
  79. // NOTE: tag can be an existing validation tag or just something you make up
  80. // and precess on the flip side it's up to you.
  81. func (sl *StructLevel) ReportError(field reflect.Value, fieldName string, customName string, tag string) {
  82. field, kind := sl.v.ExtractType(field)
  83. if fieldName == blank {
  84. panic(fieldNameRequired)
  85. }
  86. if customName == blank {
  87. customName = fieldName
  88. }
  89. if tag == blank {
  90. panic(tagRequired)
  91. }
  92. ns := sl.errPrefix + fieldName
  93. switch kind {
  94. case reflect.Invalid:
  95. sl.errs[ns] = &FieldError{
  96. FieldNamespace: ns,
  97. NameNamespace: sl.nsPrefix + customName,
  98. Name: customName,
  99. Field: fieldName,
  100. Tag: tag,
  101. ActualTag: tag,
  102. Param: blank,
  103. Kind: kind,
  104. }
  105. default:
  106. sl.errs[ns] = &FieldError{
  107. FieldNamespace: ns,
  108. NameNamespace: sl.nsPrefix + customName,
  109. Name: customName,
  110. Field: fieldName,
  111. Tag: tag,
  112. ActualTag: tag,
  113. Param: blank,
  114. Value: field.Interface(),
  115. Kind: kind,
  116. Type: field.Type(),
  117. }
  118. }
  119. }
  120. // Validate contains the validator settings passed in using the Config struct
  121. type Validate struct {
  122. tagName string
  123. fieldNameTag string
  124. validationFuncs map[string]Func
  125. structLevelFuncs map[reflect.Type]StructLevelFunc
  126. customTypeFuncs map[reflect.Type]CustomTypeFunc
  127. aliasValidators map[string]string
  128. hasCustomFuncs bool
  129. hasAliasValidators bool
  130. hasStructLevelFuncs bool
  131. tagCache *tagCache
  132. structCache *structCache
  133. errsPool *sync.Pool
  134. }
  135. func (v *Validate) initCheck() {
  136. if v == nil {
  137. panic(validatorNotInitialized)
  138. }
  139. }
  140. // Config contains the options that a Validator instance will use.
  141. // It is passed to the New() function
  142. type Config struct {
  143. TagName string
  144. FieldNameTag string
  145. }
  146. // CustomTypeFunc allows for overriding or adding custom field type handler functions
  147. // field = field value of the type to return a value to be validated
  148. // example Valuer from sql drive see https://golang.org/src/database/sql/driver/types.go?s=1210:1293#L29
  149. type CustomTypeFunc func(field reflect.Value) interface{}
  150. // Func accepts all values needed for file and cross field validation
  151. // v = validator instance, needed but some built in functions for it's custom types
  152. // topStruct = top level struct when validating by struct otherwise nil
  153. // currentStruct = current level struct when validating by struct otherwise optional comparison value
  154. // field = field value for validation
  155. // param = parameter used in validation i.e. gt=0 param would be 0
  156. type Func func(v *Validate, topStruct reflect.Value, currentStruct reflect.Value, field reflect.Value, fieldtype reflect.Type, fieldKind reflect.Kind, param string) bool
  157. // StructLevelFunc accepts all values needed for struct level validation
  158. type StructLevelFunc func(v *Validate, structLevel *StructLevel)
  159. // ValidationErrors is a type of map[string]*FieldError
  160. // it exists to allow for multiple errors to be passed from this library
  161. // and yet still subscribe to the error interface
  162. type ValidationErrors map[string]*FieldError
  163. // Error is intended for use in development + debugging and not intended to be a production error message.
  164. // It allows ValidationErrors to subscribe to the Error interface.
  165. // All information to create an error message specific to your application is contained within
  166. // the FieldError found within the ValidationErrors map
  167. func (ve ValidationErrors) Error() string {
  168. buff := bytes.NewBufferString(blank)
  169. for key, err := range ve {
  170. buff.WriteString(fmt.Sprintf(fieldErrMsg, key, err.Field, err.Tag))
  171. buff.WriteString("\n")
  172. }
  173. return strings.TrimSpace(buff.String())
  174. }
  175. // FieldError contains a single field's validation error along
  176. // with other properties that may be needed for error message creation
  177. type FieldError struct {
  178. FieldNamespace string
  179. NameNamespace string
  180. Field string
  181. Name string
  182. Tag string
  183. ActualTag string
  184. Kind reflect.Kind
  185. Type reflect.Type
  186. Param string
  187. Value interface{}
  188. }
  189. // New creates a new Validate instance for use.
  190. func New(config *Config) *Validate {
  191. tc := new(tagCache)
  192. tc.m.Store(make(map[string]*cTag))
  193. sc := new(structCache)
  194. sc.m.Store(make(map[reflect.Type]*cStruct))
  195. v := &Validate{
  196. tagName: config.TagName,
  197. fieldNameTag: config.FieldNameTag,
  198. tagCache: tc,
  199. structCache: sc,
  200. errsPool: &sync.Pool{New: func() interface{} {
  201. return ValidationErrors{}
  202. }}}
  203. if len(v.aliasValidators) == 0 {
  204. // must copy alias validators for separate validations to be used in each validator instance
  205. v.aliasValidators = map[string]string{}
  206. for k, val := range bakedInAliasValidators {
  207. v.RegisterAliasValidation(k, val)
  208. }
  209. }
  210. if len(v.validationFuncs) == 0 {
  211. // must copy validators for separate validations to be used in each instance
  212. v.validationFuncs = map[string]Func{}
  213. for k, val := range bakedInValidators {
  214. v.RegisterValidation(k, val)
  215. }
  216. }
  217. return v
  218. }
  219. // RegisterStructValidation registers a StructLevelFunc against a number of types
  220. // NOTE: this method is not thread-safe it is intended that these all be registered prior to any validation
  221. func (v *Validate) RegisterStructValidation(fn StructLevelFunc, types ...interface{}) {
  222. v.initCheck()
  223. if v.structLevelFuncs == nil {
  224. v.structLevelFuncs = map[reflect.Type]StructLevelFunc{}
  225. }
  226. for _, t := range types {
  227. v.structLevelFuncs[reflect.TypeOf(t)] = fn
  228. }
  229. v.hasStructLevelFuncs = true
  230. }
  231. // RegisterValidation adds a validation Func to a Validate's map of validators denoted by the key
  232. // NOTE: if the key already exists, the previous validation function will be replaced.
  233. // NOTE: this method is not thread-safe it is intended that these all be registered prior to any validation
  234. func (v *Validate) RegisterValidation(key string, fn Func) error {
  235. v.initCheck()
  236. if key == blank {
  237. return errors.New("Function Key cannot be empty")
  238. }
  239. if fn == nil {
  240. return errors.New("Function cannot be empty")
  241. }
  242. _, ok := restrictedTags[key]
  243. if ok || strings.ContainsAny(key, restrictedTagChars) {
  244. panic(fmt.Sprintf(restrictedTagErr, key))
  245. }
  246. v.validationFuncs[key] = fn
  247. return nil
  248. }
  249. // RegisterCustomTypeFunc registers a CustomTypeFunc against a number of types
  250. // NOTE: this method is not thread-safe it is intended that these all be registered prior to any validation
  251. func (v *Validate) RegisterCustomTypeFunc(fn CustomTypeFunc, types ...interface{}) {
  252. v.initCheck()
  253. if v.customTypeFuncs == nil {
  254. v.customTypeFuncs = map[reflect.Type]CustomTypeFunc{}
  255. }
  256. for _, t := range types {
  257. v.customTypeFuncs[reflect.TypeOf(t)] = fn
  258. }
  259. v.hasCustomFuncs = true
  260. }
  261. // RegisterAliasValidation registers a mapping of a single validationstag that
  262. // defines a common or complex set of validation(s) to simplify adding validation
  263. // to structs. NOTE: when returning an error the tag returned in FieldError will be
  264. // the alias tag unless the dive tag is part of the alias; everything after the
  265. // dive tag is not reported as the alias tag. Also the ActualTag in the before case
  266. // will be the actual tag within the alias that failed.
  267. // NOTE: this method is not thread-safe it is intended that these all be registered prior to any validation
  268. func (v *Validate) RegisterAliasValidation(alias, tags string) {
  269. v.initCheck()
  270. _, ok := restrictedTags[alias]
  271. if ok || strings.ContainsAny(alias, restrictedTagChars) {
  272. panic(fmt.Sprintf(restrictedAliasErr, alias))
  273. }
  274. v.aliasValidators[alias] = tags
  275. v.hasAliasValidators = true
  276. }
  277. // Field validates a single field using tag style validation and returns nil or ValidationErrors as type error.
  278. // You will need to assert the error if it's not nil i.e. err.(validator.ValidationErrors) to access the map of errors.
  279. // NOTE: it returns ValidationErrors instead of a single FieldError because this can also
  280. // validate Array, Slice and maps fields which may contain more than one error
  281. func (v *Validate) Field(field interface{}, tag string) error {
  282. v.initCheck()
  283. if len(tag) == 0 || tag == skipValidationTag {
  284. return nil
  285. }
  286. errs := v.errsPool.Get().(ValidationErrors)
  287. fieldVal := reflect.ValueOf(field)
  288. ctag, ok := v.tagCache.Get(tag)
  289. if !ok {
  290. v.tagCache.lock.Lock()
  291. defer v.tagCache.lock.Unlock()
  292. // could have been multiple trying to access, but once first is done this ensures tag
  293. // isn't parsed again.
  294. ctag, ok = v.tagCache.Get(tag)
  295. if !ok {
  296. ctag, _ = v.parseFieldTagsRecursive(tag, blank, blank, false)
  297. v.tagCache.Set(tag, ctag)
  298. }
  299. }
  300. v.traverseField(fieldVal, fieldVal, fieldVal, blank, blank, errs, false, false, nil, nil, defaultCField, ctag)
  301. if len(errs) == 0 {
  302. v.errsPool.Put(errs)
  303. return nil
  304. }
  305. return errs
  306. }
  307. // FieldWithValue validates a single field, against another fields value using tag style validation and returns nil or ValidationErrors.
  308. // You will need to assert the error if it's not nil i.e. err.(validator.ValidationErrors) to access the map of errors.
  309. // NOTE: it returns ValidationErrors instead of a single FieldError because this can also
  310. // validate Array, Slice and maps fields which may contain more than one error
  311. func (v *Validate) FieldWithValue(val interface{}, field interface{}, tag string) error {
  312. v.initCheck()
  313. if len(tag) == 0 || tag == skipValidationTag {
  314. return nil
  315. }
  316. errs := v.errsPool.Get().(ValidationErrors)
  317. topVal := reflect.ValueOf(val)
  318. ctag, ok := v.tagCache.Get(tag)
  319. if !ok {
  320. v.tagCache.lock.Lock()
  321. defer v.tagCache.lock.Unlock()
  322. // could have been multiple trying to access, but once first is done this ensures tag
  323. // isn't parsed again.
  324. ctag, ok = v.tagCache.Get(tag)
  325. if !ok {
  326. ctag, _ = v.parseFieldTagsRecursive(tag, blank, blank, false)
  327. v.tagCache.Set(tag, ctag)
  328. }
  329. }
  330. v.traverseField(topVal, topVal, reflect.ValueOf(field), blank, blank, errs, false, false, nil, nil, defaultCField, ctag)
  331. if len(errs) == 0 {
  332. v.errsPool.Put(errs)
  333. return nil
  334. }
  335. return errs
  336. }
  337. // StructPartial validates the fields passed in only, ignoring all others.
  338. // Fields may be provided in a namespaced fashion relative to the struct provided
  339. // i.e. NestedStruct.Field or NestedArrayField[0].Struct.Name and returns nil or ValidationErrors as error
  340. // You will need to assert the error if it's not nil i.e. err.(validator.ValidationErrors) to access the map of errors.
  341. func (v *Validate) StructPartial(current interface{}, fields ...string) error {
  342. v.initCheck()
  343. sv, _ := v.ExtractType(reflect.ValueOf(current))
  344. name := sv.Type().Name()
  345. m := map[string]struct{}{}
  346. if fields != nil {
  347. for _, k := range fields {
  348. flds := strings.Split(k, namespaceSeparator)
  349. if len(flds) > 0 {
  350. key := name + namespaceSeparator
  351. for _, s := range flds {
  352. idx := strings.Index(s, leftBracket)
  353. if idx != -1 {
  354. for idx != -1 {
  355. key += s[:idx]
  356. m[key] = struct{}{}
  357. idx2 := strings.Index(s, rightBracket)
  358. idx2++
  359. key += s[idx:idx2]
  360. m[key] = struct{}{}
  361. s = s[idx2:]
  362. idx = strings.Index(s, leftBracket)
  363. }
  364. } else {
  365. key += s
  366. m[key] = struct{}{}
  367. }
  368. key += namespaceSeparator
  369. }
  370. }
  371. }
  372. }
  373. errs := v.errsPool.Get().(ValidationErrors)
  374. v.ensureValidStruct(sv, sv, sv, blank, blank, errs, true, len(m) != 0, false, m, false)
  375. if len(errs) == 0 {
  376. v.errsPool.Put(errs)
  377. return nil
  378. }
  379. return errs
  380. }
  381. // StructExcept validates all fields except the ones passed in.
  382. // Fields may be provided in a namespaced fashion relative to the struct provided
  383. // i.e. NestedStruct.Field or NestedArrayField[0].Struct.Name and returns nil or ValidationErrors as error
  384. // You will need to assert the error if it's not nil i.e. err.(validator.ValidationErrors) to access the map of errors.
  385. func (v *Validate) StructExcept(current interface{}, fields ...string) error {
  386. v.initCheck()
  387. sv, _ := v.ExtractType(reflect.ValueOf(current))
  388. name := sv.Type().Name()
  389. m := map[string]struct{}{}
  390. for _, key := range fields {
  391. m[name+namespaceSeparator+key] = struct{}{}
  392. }
  393. errs := v.errsPool.Get().(ValidationErrors)
  394. v.ensureValidStruct(sv, sv, sv, blank, blank, errs, true, len(m) != 0, true, m, false)
  395. if len(errs) == 0 {
  396. v.errsPool.Put(errs)
  397. return nil
  398. }
  399. return errs
  400. }
  401. // Struct validates a structs exposed fields, and automatically validates nested structs, unless otherwise specified.
  402. // it returns nil or ValidationErrors as error.
  403. // You will need to assert the error if it's not nil i.e. err.(validator.ValidationErrors) to access the map of errors.
  404. func (v *Validate) Struct(current interface{}) error {
  405. v.initCheck()
  406. errs := v.errsPool.Get().(ValidationErrors)
  407. sv := reflect.ValueOf(current)
  408. v.ensureValidStruct(sv, sv, sv, blank, blank, errs, true, false, false, nil, false)
  409. if len(errs) == 0 {
  410. v.errsPool.Put(errs)
  411. return nil
  412. }
  413. return errs
  414. }
  415. func (v *Validate) ensureValidStruct(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, nsPrefix string, errs ValidationErrors, useStructName bool, partial bool, exclude bool, includeExclude map[string]struct{}, isStructOnly bool) {
  416. if current.Kind() == reflect.Ptr && !current.IsNil() {
  417. current = current.Elem()
  418. }
  419. if current.Kind() != reflect.Struct && current.Kind() != reflect.Interface {
  420. panic("value passed for validation is not a struct")
  421. }
  422. v.tranverseStruct(topStruct, currentStruct, current, errPrefix, nsPrefix, errs, useStructName, partial, exclude, includeExclude, nil, nil)
  423. }
  424. // tranverseStruct traverses a structs fields and then passes them to be validated by traverseField
  425. func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, nsPrefix string, errs ValidationErrors, useStructName bool, partial bool, exclude bool, includeExclude map[string]struct{}, cs *cStruct, ct *cTag) {
  426. var ok bool
  427. first := len(nsPrefix) == 0
  428. typ := current.Type()
  429. cs, ok = v.structCache.Get(typ)
  430. if !ok {
  431. cs = v.extractStructCache(current, typ.Name())
  432. }
  433. if useStructName {
  434. errPrefix += cs.Name + namespaceSeparator
  435. if len(v.fieldNameTag) != 0 {
  436. nsPrefix += cs.Name + namespaceSeparator
  437. }
  438. }
  439. // structonly tag present don't tranverseFields
  440. // but must still check and run below struct level validation
  441. // if present
  442. if first || ct == nil || ct.typeof != typeStructOnly {
  443. for _, f := range cs.fields {
  444. if partial {
  445. _, ok = includeExclude[errPrefix+f.Name]
  446. if (ok && exclude) || (!ok && !exclude) {
  447. continue
  448. }
  449. }
  450. v.traverseField(topStruct, currentStruct, current.Field(f.Idx), errPrefix, nsPrefix, errs, partial, exclude, includeExclude, cs, f, f.cTags)
  451. }
  452. }
  453. // check if any struct level validations, after all field validations already checked.
  454. if cs.fn != nil {
  455. cs.fn(v, &StructLevel{v: v, TopStruct: topStruct, CurrentStruct: current, errPrefix: errPrefix, nsPrefix: nsPrefix, errs: errs})
  456. }
  457. }
  458. // traverseField validates any field, be it a struct or single field, ensures it's validity and passes it along to be validated via it's tag options
  459. func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, nsPrefix string, errs ValidationErrors, partial bool, exclude bool, includeExclude map[string]struct{}, cs *cStruct, cf *cField, ct *cTag) {
  460. current, kind, nullable := v.extractTypeInternal(current, false)
  461. var typ reflect.Type
  462. switch kind {
  463. case reflect.Ptr, reflect.Interface, reflect.Invalid:
  464. if ct == nil {
  465. return
  466. }
  467. if ct.typeof == typeOmitEmpty {
  468. return
  469. }
  470. if ct.hasTag {
  471. ns := errPrefix + cf.Name
  472. if kind == reflect.Invalid {
  473. errs[ns] = &FieldError{
  474. FieldNamespace: ns,
  475. NameNamespace: nsPrefix + cf.AltName,
  476. Name: cf.AltName,
  477. Field: cf.Name,
  478. Tag: ct.aliasTag,
  479. ActualTag: ct.tag,
  480. Param: ct.param,
  481. Kind: kind,
  482. }
  483. return
  484. }
  485. errs[ns] = &FieldError{
  486. FieldNamespace: ns,
  487. NameNamespace: nsPrefix + cf.AltName,
  488. Name: cf.AltName,
  489. Field: cf.Name,
  490. Tag: ct.aliasTag,
  491. ActualTag: ct.tag,
  492. Param: ct.param,
  493. Value: current.Interface(),
  494. Kind: kind,
  495. Type: current.Type(),
  496. }
  497. return
  498. }
  499. case reflect.Struct:
  500. typ = current.Type()
  501. if typ != timeType {
  502. if ct != nil {
  503. ct = ct.next
  504. }
  505. if ct != nil && ct.typeof == typeNoStructLevel {
  506. return
  507. }
  508. v.tranverseStruct(topStruct, current, current, errPrefix+cf.Name+namespaceSeparator, nsPrefix+cf.AltName+namespaceSeparator, errs, false, partial, exclude, includeExclude, cs, ct)
  509. return
  510. }
  511. }
  512. if !ct.hasTag {
  513. return
  514. }
  515. typ = current.Type()
  516. OUTER:
  517. for {
  518. if ct == nil {
  519. return
  520. }
  521. switch ct.typeof {
  522. case typeExists:
  523. ct = ct.next
  524. continue
  525. case typeOmitEmpty:
  526. if !nullable && !HasValue(v, topStruct, currentStruct, current, typ, kind, blank) {
  527. return
  528. }
  529. ct = ct.next
  530. continue
  531. case typeDive:
  532. ct = ct.next
  533. // traverse slice or map here
  534. // or panic ;)
  535. switch kind {
  536. case reflect.Slice, reflect.Array:
  537. for i := 0; i < current.Len(); i++ {
  538. v.traverseField(topStruct, currentStruct, current.Index(i), errPrefix, nsPrefix, errs, partial, exclude, includeExclude, cs, &cField{Name: fmt.Sprintf(arrayIndexFieldName, cf.Name, i), AltName: fmt.Sprintf(arrayIndexFieldName, cf.AltName, i)}, ct)
  539. }
  540. case reflect.Map:
  541. for _, key := range current.MapKeys() {
  542. v.traverseField(topStruct, currentStruct, current.MapIndex(key), errPrefix, nsPrefix, errs, partial, exclude, includeExclude, cs, &cField{Name: fmt.Sprintf(mapIndexFieldName, cf.Name, key.Interface()), AltName: fmt.Sprintf(mapIndexFieldName, cf.AltName, key.Interface())}, ct)
  543. }
  544. default:
  545. // throw error, if not a slice or map then should not have gotten here
  546. // bad dive tag
  547. panic("dive error! can't dive on a non slice or map")
  548. }
  549. return
  550. case typeOr:
  551. errTag := blank
  552. for {
  553. if ct.fn(v, topStruct, currentStruct, current, typ, kind, ct.param) {
  554. // drain rest of the 'or' values, then continue or leave
  555. for {
  556. ct = ct.next
  557. if ct == nil {
  558. return
  559. }
  560. if ct.typeof != typeOr {
  561. continue OUTER
  562. }
  563. }
  564. }
  565. errTag += orSeparator + ct.tag
  566. if ct.next == nil {
  567. // if we get here, no valid 'or' value and no more tags
  568. ns := errPrefix + cf.Name
  569. if ct.hasAlias {
  570. errs[ns] = &FieldError{
  571. FieldNamespace: ns,
  572. NameNamespace: nsPrefix + cf.AltName,
  573. Name: cf.AltName,
  574. Field: cf.Name,
  575. Tag: ct.aliasTag,
  576. ActualTag: ct.actualAliasTag,
  577. Value: current.Interface(),
  578. Type: typ,
  579. Kind: kind,
  580. }
  581. } else {
  582. errs[errPrefix+cf.Name] = &FieldError{
  583. FieldNamespace: ns,
  584. NameNamespace: nsPrefix + cf.AltName,
  585. Name: cf.AltName,
  586. Field: cf.Name,
  587. Tag: errTag[1:],
  588. ActualTag: errTag[1:],
  589. Value: current.Interface(),
  590. Type: typ,
  591. Kind: kind,
  592. }
  593. }
  594. return
  595. }
  596. ct = ct.next
  597. }
  598. default:
  599. if !ct.fn(v, topStruct, currentStruct, current, typ, kind, ct.param) {
  600. ns := errPrefix + cf.Name
  601. errs[ns] = &FieldError{
  602. FieldNamespace: ns,
  603. NameNamespace: nsPrefix + cf.AltName,
  604. Name: cf.AltName,
  605. Field: cf.Name,
  606. Tag: ct.aliasTag,
  607. ActualTag: ct.tag,
  608. Value: current.Interface(),
  609. Param: ct.param,
  610. Type: typ,
  611. Kind: kind,
  612. }
  613. return
  614. }
  615. ct = ct.next
  616. }
  617. }
  618. }