| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 | 
							- package valid
 
- var (
 
- 	// Required is a validation rule that checks if a value is not empty.
 
- 	// A value is considered not empty if
 
- 	// - integer, float: not zero
 
- 	// - bool: true
 
- 	// - string, array, slice, map: len() > 0
 
- 	// - interface, pointer: not nil and the referenced value is not empty
 
- 	// - any other types
 
- 	Required = RequiredRule{skipNil: false, condition: true}
 
- 	// NilOrNotEmpty checks if a value is a nil pointer or a value that is not empty.
 
- 	// NilOrNotEmpty differs from Required in that it treats a nil pointer as valid.
 
- 	NilOrNotEmpty = RequiredRule{skipNil: true, condition: true}
 
- 	// ErrRequired is the error that returns when a value is required.
 
- 	ErrRequired = NewError("validation_required", "cannot be blank")
 
- )
 
- // RequiredRule is a rule that checks if a value is not empty.
 
- type RequiredRule struct {
 
- 	condition bool
 
- 	skipNil   bool
 
- 	err       Error
 
- }
 
- // Validate checks if the given value is valid or not.
 
- func (r RequiredRule) Validate(value interface{}) error {
 
- 	if r.condition {
 
- 		value, isNil := Indirect(value)
 
- 		if r.skipNil && !isNil && IsEmpty(value) || !r.skipNil && (isNil || IsEmpty(value)) {
 
- 			if r.err != nil {
 
- 				return r.err
 
- 			}
 
- 			if r.skipNil {
 
- 				return ErrNilOrNotEmpty
 
- 			}
 
- 			return ErrRequired
 
- 		}
 
- 	}
 
- 	return nil
 
- }
 
- // When sets the condition that determines if the validation should be performed.
 
- func (r RequiredRule) When(condition bool) RequiredRule {
 
- 	r.condition = condition
 
- 	return r
 
- }
 
- // Error sets the error message for the rule.
 
- func (r RequiredRule) Error(message string) RequiredRule {
 
- 	if r.err == nil {
 
- 		if r.skipNil {
 
- 			r.err = ErrNilOrNotEmpty
 
- 		} else {
 
- 			r.err = ErrRequired
 
- 		}
 
- 	}
 
- 	r.err = r.err.SetMessage(message)
 
- 	return r
 
- }
 
- // ErrorObject sets the error struct for the rule.
 
- func (r RequiredRule) ErrorObject(err Error) RequiredRule {
 
- 	r.err = err
 
- 	return r
 
- }
 
 
  |