field_level.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. // returns the top level struct, if any
  7. Top() reflect.Value
  8. // returns the current fields parent struct, if any or
  9. // the comparison value if called 'VarWithValue'
  10. Parent() reflect.Value
  11. // returns current field for validation
  12. Field() reflect.Value
  13. // returns the field's name with the tag
  14. // name taking precedence over the fields actual name.
  15. FieldName() string
  16. // returns the struct field's name
  17. StructFieldName() string
  18. // returns param for validation against current field
  19. Param() string
  20. // ExtractType gets the actual underlying type of field value.
  21. // It will dive into pointers, customTypes and return you the
  22. // underlying value and it's kind.
  23. ExtractType(field reflect.Value) (value reflect.Value, kind reflect.Kind, nullable bool)
  24. // traverses the parent struct to retrieve a specific field denoted by the provided namespace
  25. // in the param and returns the field, field kind and whether is was successful in retrieving
  26. // the field at all.
  27. //
  28. // NOTE: when not successful ok will be false, this can happen when a nested struct is nil and so the field
  29. // could not be retrieved because it didn't exist.
  30. GetStructFieldOK() (reflect.Value, reflect.Kind, bool)
  31. // GetStructFieldOKAdvanced is the same as GetStructFieldOK except that it accepts the parent struct to start looking for
  32. // the field and namespace allowing more extensibility for validators.
  33. GetStructFieldOKAdvanced(val reflect.Value, namespace string) (reflect.Value, reflect.Kind, bool)
  34. }
  35. var _ FieldLevel = new(validate)
  36. // Field returns current field for validation
  37. func (v *validate) Field() reflect.Value {
  38. return v.flField
  39. }
  40. // FieldName returns the field's name with the tag
  41. // name takeing precedence over the fields actual name.
  42. func (v *validate) FieldName() string {
  43. return v.cf.altName
  44. }
  45. // StructFieldName returns the struct field's name
  46. func (v *validate) StructFieldName() string {
  47. return v.cf.name
  48. }
  49. // Param returns param for validation against current field
  50. func (v *validate) Param() string {
  51. return v.ct.param
  52. }
  53. // GetStructFieldOK returns Param returns param for validation against current field
  54. func (v *validate) GetStructFieldOK() (reflect.Value, reflect.Kind, bool) {
  55. return v.getStructFieldOKInternal(v.slflParent, v.ct.param)
  56. }
  57. // GetStructFieldOKAdvanced is the same as GetStructFieldOK except that it accepts the parent struct to start looking for
  58. // the field and namespace allowing more extensibility for validators.
  59. func (v *validate) GetStructFieldOKAdvanced(val reflect.Value, namespace string) (reflect.Value, reflect.Kind, bool) {
  60. return v.getStructFieldOKInternal(val, namespace)
  61. }