Documentation
¶
Overview ¶
Package validation provides utilities for validating data in a structured and reusable way.
This package offers a collection of validation functions for common validation tasks, such as checking required fields, validating string lengths, matching patterns, validating dates, and more. It also provides a ValidationResult type for collecting and managing validation errors.
The validation functions are designed to be composable, allowing you to build complex validation logic by combining simple validation rules. The package integrates with the errors package to provide detailed validation error information.
Key features:
- String validation (required, min/max length, pattern matching)
- Date validation (past dates, date ranges)
- Collection validation (validate all items, check if all items satisfy a condition)
- Structured validation results with field-specific error messages
- Integration with the errors package for consistent error handling
Example usage:
// Create a validation result
result := validation.NewValidationResult()
// Validate a user struct
validation.Required(user.Name, "name", result)
validation.MinLength(user.Password, 8, "password", result)
validation.Pattern(user.Email, `^[^@]+@[^@]+\.[^@]+$`, "email", result)
// Check if validation passed
if !result.IsValid() {
return result.Error()
}
// Validate a collection
validation.ValidateAll(user.Addresses, func(addr Address, i int, result *validation.ValidationResult) {
validation.Required(addr.Street, fmt.Sprintf("addresses[%d].street", i), result)
validation.Required(addr.City, fmt.Sprintf("addresses[%d].city", i), result)
validation.Required(addr.Country, fmt.Sprintf("addresses[%d].country", i), result)
}, result)
The package is designed to be used throughout the application to ensure data integrity and provide consistent validation error messages to users.
Index ¶
- func AllTrue[T any](items []T, predicate func(T) bool) bool
- func MaxLength(value string, max int, field string, result *ValidationResult)
- func MinLength(value string, min int, field string, result *ValidationResult)
- func PastDate(value time.Time, field string, result *ValidationResult)
- func Pattern(value, pattern, field string, result *ValidationResult)
- func Required(value, field string, result *ValidationResult)
- func ValidDateRange(start, end time.Time, startField, endField string, result *ValidationResult)
- func ValidateAll[T any](items []T, validator func(T, int, *ValidationResult), result *ValidationResult)
- func ValidateID(id, field string, result *ValidationResult)
- type ValidationResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MaxLength ¶
func MaxLength(value string, max int, field string, result *ValidationResult)
MaxLength validates that a string has a maximum length
func MinLength ¶
func MinLength(value string, min int, field string, result *ValidationResult)
MinLength validates that a string has a minimum length
func PastDate ¶
func PastDate(value time.Time, field string, result *ValidationResult)
PastDate validates that a date is in the past
func Pattern ¶
func Pattern(value, pattern, field string, result *ValidationResult)
Pattern validates that a string matches a regular expression
func Required ¶
func Required(value, field string, result *ValidationResult)
Required validates that a string is not empty
func ValidDateRange ¶
func ValidDateRange(start, end time.Time, startField, endField string, result *ValidationResult)
ValidDateRange validates that a start date is before an end date
func ValidateAll ¶
func ValidateAll[T any](items []T, validator func(T, int, *ValidationResult), result *ValidationResult)
ValidateAll validates all items in a slice
func ValidateID ¶
func ValidateID(id, field string, result *ValidationResult)
ValidateID validates that an ID is not empty
Types ¶
type ValidationResult ¶
type ValidationResult struct {
// contains filtered or unexported fields
}
ValidationResult holds the result of a validation operation
func NewValidationResult ¶
func NewValidationResult() *ValidationResult
NewValidationResult creates a new ValidationResult
func (*ValidationResult) AddError ¶
func (v *ValidationResult) AddError(msg, field string)
AddError adds an error to the validation result
func (*ValidationResult) Error ¶
func (v *ValidationResult) Error() error
Error returns the validation errors as an error
func (*ValidationResult) IsValid ¶
func (v *ValidationResult) IsValid() bool
IsValid returns true if there are no validation errors