field_level.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package validator
  2. import "reflect"
  3. // FieldLevel contains all the information and helper functions
  4. // to validate a field
  5. type FieldLevel interface {
  6. // Top returns the top level struct, if any
  7. Top() reflect.Value
  8. // Parent returns the current fields parent struct, if any or
  9. // the comparison value if called 'VarWithValue'
  10. Parent() reflect.Value
  11. // Field returns current field for validation
  12. Field() reflect.Value
  13. // FieldName returns the field's name with the tag
  14. // name taking precedence over the fields actual name.
  15. FieldName() string
  16. // StructFieldName returns the struct field's name
  17. StructFieldName() string
  18. // Param returns param for validation against current field
  19. Param() string
  20. // GetTag returns the current validations tag name
  21. GetTag() string
  22. // ExtractType gets the actual underlying type of field value.
  23. // It will dive into pointers, customTypes and return you the
  24. // underlying value and it's kind.
  25. ExtractType(field reflect.Value) (value reflect.Value, kind reflect.Kind, nullable bool)
  26. // GetStructFieldOK traverses the parent struct to retrieve a specific field denoted by the provided namespace
  27. // in the param and returns the field, field kind and whether is was successful in retrieving
  28. // the field at all.
  29. //
  30. // NOTE: when not successful ok will be false, this can happen when a nested struct is nil and so the field
  31. // could not be retrieved because it didn't exist.
  32. //
  33. // Deprecated: Use GetStructFieldOK2() instead which also return if the value is nullable.
  34. GetStructFieldOK() (reflect.Value, reflect.Kind, bool)
  35. // GetStructFieldOKAdvanced is the same as GetStructFieldOK except that it accepts the parent struct to start looking for
  36. // the field and namespace allowing more extensibility for validators.
  37. //
  38. // Deprecated: Use GetStructFieldOKAdvanced2() instead which also return if the value is nullable.
  39. GetStructFieldOKAdvanced(val reflect.Value, namespace string) (reflect.Value, reflect.Kind, bool)
  40. // GetStructFieldOK2 traverses the parent struct to retrieve a specific field denoted by the provided namespace
  41. // in the param and returns the field, field kind, if it's a nullable type and whether is was successful in retrieving
  42. // the field at all.
  43. //
  44. // NOTE: when not successful ok will be false, this can happen when a nested struct is nil and so the field
  45. // could not be retrieved because it didn't exist.
  46. GetStructFieldOK2() (reflect.Value, reflect.Kind, bool, bool)
  47. // GetStructFieldOKAdvanced2 is the same as GetStructFieldOK except that it accepts the parent struct to start looking for
  48. // the field and namespace allowing more extensibility for validators.
  49. GetStructFieldOKAdvanced2(val reflect.Value, namespace string) (reflect.Value, reflect.Kind, bool, bool)
  50. }
  51. var _ FieldLevel = new(validate)
  52. // Field returns current field for validation
  53. func (v *validate) Field() reflect.Value {
  54. return v.flField
  55. }
  56. // FieldName returns the field's name with the tag
  57. // name taking precedence over the fields actual name.
  58. func (v *validate) FieldName() string {
  59. return v.cf.altName
  60. }
  61. // GetTag returns the current validations tag name
  62. func (v *validate) GetTag() string {
  63. return v.ct.tag
  64. }
  65. // StructFieldName returns the struct field's name
  66. func (v *validate) StructFieldName() string {
  67. return v.cf.name
  68. }
  69. // Param returns param for validation against current field
  70. func (v *validate) Param() string {
  71. return v.ct.param
  72. }
  73. // GetStructFieldOK returns Param returns param for validation against current field
  74. //
  75. // Deprecated: Use GetStructFieldOK2() instead which also return if the value is nullable.
  76. func (v *validate) GetStructFieldOK() (reflect.Value, reflect.Kind, bool) {
  77. current, kind, _, found := v.getStructFieldOKInternal(v.slflParent, v.ct.param)
  78. return current, kind, found
  79. }
  80. // GetStructFieldOKAdvanced is the same as GetStructFieldOK except that it accepts the parent struct to start looking for
  81. // the field and namespace allowing more extensibility for validators.
  82. //
  83. // Deprecated: Use GetStructFieldOKAdvanced2() instead which also return if the value is nullable.
  84. func (v *validate) GetStructFieldOKAdvanced(val reflect.Value, namespace string) (reflect.Value, reflect.Kind, bool) {
  85. current, kind, _, found := v.GetStructFieldOKAdvanced2(val, namespace)
  86. return current, kind, found
  87. }
  88. // GetStructFieldOK2 returns Param returns param for validation against current field
  89. func (v *validate) GetStructFieldOK2() (reflect.Value, reflect.Kind, bool, bool) {
  90. return v.getStructFieldOKInternal(v.slflParent, v.ct.param)
  91. }
  92. // GetStructFieldOKAdvanced2 is the same as GetStructFieldOK except that it accepts the parent struct to start looking for
  93. // the field and namespace allowing more extensibility for validators.
  94. func (v *validate) GetStructFieldOKAdvanced2(val reflect.Value, namespace string) (reflect.Value, reflect.Kind, bool, bool) {
  95. return v.getStructFieldOKInternal(val, namespace)
  96. }