field_level.go raw

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