package validation import "strings" // FieldError represents a single field validation error type FieldError struct { Field string `json:"field"` // Field name Tag string `json:"tag"` // Validation tag that failed Param string `json:"param,omitempty"` // Optional parameter (e.g., "100" for "max=100") Message string `json:"message"` // Human-readable error message } // Error implements the error interface func (e FieldError) Error() string { if e.Param != "" { return e.Field + ": " + e.Message + " (" + e.Tag + "=" + e.Param + ")" } return e.Field + ": " + e.Message } // ValidationErrors represents multiple field validation errors type ValidationErrors []FieldError // Error implements the error interface func (ve ValidationErrors) Error() string { if len(ve) == 0 { return "" } var sb strings.Builder sb.WriteString("validation failed: ") for i, err := range ve { if i > 0 { sb.WriteString(", ") } sb.WriteString(err.Error()) } return sb.String() } // HasErrors returns true if there are any validation errors func (ve ValidationErrors) HasErrors() bool { return len(ve) > 0 } // GetFieldError returns the first error for a specific field, or nil if none func (ve ValidationErrors) GetFieldError(field string) *FieldError { for _, err := range ve { if err.Field == field { return &err } } return nil } // GetFieldErrors returns all errors for a specific field func (ve ValidationErrors) GetFieldErrors(field string) []FieldError { var errors []FieldError for _, err := range ve { if err.Field == field { errors = append(errors, err) } } return errors }