github.com/goravel/framework@v1.13.9/contracts/validation/validation.go (about)

     1  package validation
     2  
     3  type Option func(map[string]any)
     4  
     5  //go:generate mockery --name=Validation
     6  type Validation interface {
     7  	// Make create a new validator instance.
     8  	Make(data any, rules map[string]string, options ...Option) (Validator, error)
     9  	// AddRules add the custom rules.
    10  	AddRules([]Rule) error
    11  	// Rules get the custom rules.
    12  	Rules() []Rule
    13  }
    14  
    15  //go:generate mockery --name=Validator
    16  type Validator interface {
    17  	// Bind the data to the validation.
    18  	Bind(ptr any) error
    19  	// Errors get the validation errors.
    20  	Errors() Errors
    21  	// Fails determine if the validation fails.
    22  	Fails() bool
    23  }
    24  
    25  //go:generate mockery --name=Errors
    26  type Errors interface {
    27  	// One gets the first error message for a given field.
    28  	One(key ...string) string
    29  	// Get gets all the error messages for a given field.
    30  	Get(key string) map[string]string
    31  	// All gets all the error messages.
    32  	All() map[string]map[string]string
    33  	// Has checks if there are any error messages for a given field.
    34  	Has(key string) bool
    35  }
    36  
    37  type Data interface {
    38  	// Get the value from the given key.
    39  	Get(key string) (val any, exist bool)
    40  	// Set the value for a given key.
    41  	Set(key string, val any) error
    42  }
    43  
    44  type Rule interface {
    45  	// Signature set the unique signature of the rule.
    46  	Signature() string
    47  	// Passes determine if the validation rule passes.
    48  	Passes(data Data, val any, options ...any) bool
    49  	// Message gets the validation error message.
    50  	Message() string
    51  }