Documentation
¶
Overview ¶
Package rapidval provides a high-performance, zero-dependency validation library for Go. It focuses on simplicity, extensibility, and performance while providing a clean and intuitive API.
Basic usage:
type User struct { Name string Email string Age int } func (u *User) Validate(v *rapidval.Validator) error { return v.Validate(rapidval.P{ rapidval.Required("Name", u.Name), rapidval.MinLength("Name", u.Name, 2), rapidval.Email("Email", u.Email), rapidval.Between("Age", u.Age, 18, 100), }) }
Index ¶
- Constants
- type P
- type Translator
- type Validateable
- type ValidationError
- func Between(field string, value int, min, max int) *ValidationError
- func DateGreaterThan(field string, value, min time.Time) *ValidationError
- func DateLessThan(field string, value, max time.Time) *ValidationError
- func Email(field string, value string) *ValidationError
- func MaxLength(field string, value string, max int) *ValidationError
- func MinLength(field string, value string, min int) *ValidationError
- func Required(field string, value interface{}) *ValidationError
- type ValidationErrors
- type Validator
Constants ¶
const ( MsgRequired = "validation.required" MsgInvalidEmail = "validation.email" MsgMinLength = "validation.min_length" MsgMaxLength = "validation.max_length" MsgBetween = "validation.between" MsgDateGreaterThan = "validation.date_greater_than" MsgDateLessThan = "validation.date_less_than" )
Message Keys
const ( Field = "Field" Min = "Min" Max = "Max" Value = "Value" )
MessageParam keys
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type P ¶
type P []*ValidationError
P (Params) is a collection of validation errors used for grouping validations.
type Translator ¶
type Translator struct {
// contains filtered or unexported fields
}
Translator handles the translation of validation error messages. It uses Go's text/template package to support parameterized messages.
func NewTranslator ¶
func NewTranslator() *Translator
NewTranslator creates a new Translator with default messages.
func NewTranslatorWithMessages ¶
func NewTranslatorWithMessages(messages map[string]string) *Translator
NewTranslatorWithMessages creates a new Translator with custom messages. The messages map should use message keys as keys and message templates as values. Message templates can use Go template syntax with .Field, .Min, .Max, and .Value parameters.
func (*Translator) Translate ¶
func (t *Translator) Translate(err *ValidationError) string
Translate converts a ValidationError into a human-readable message using the configured templates. If the message key is not found in the templates, it returns the message key itself.
type Validateable ¶ added in v0.0.2
type Validateable interface { // Validations is a method that can be implemented by any struct to add custom validation logic. // It allows for custom validation logic to be added to a struct without having to implement the Validate method. Validations() P }
Validateable is an interface that can be implemented by any struct to add custom validation logic. It allows for custom validation logic to be added to a struct without having to implement the Validate method.
type ValidationError ¶
type ValidationError struct { Field string MessageKey string MessageParams map[string]interface{} CurrentValue interface{} }
ValidationError represents a single validation error. It contains the field name, message key, and any parameters needed for translation.
func Between ¶
func Between(field string, value int, min, max int) *ValidationError
Between validates if a number is between the specified minimum and maximum values (inclusive).
func DateGreaterThan ¶
func DateGreaterThan(field string, value, min time.Time) *ValidationError
DateGreaterThan validates if a time.Time is after the specified minimum time.
func DateLessThan ¶
func DateLessThan(field string, value, max time.Time) *ValidationError
DateLessThan validates if a time.Time is before the specified maximum time.
func Email ¶
func Email(field string, value string) *ValidationError
Email validates if a string is a valid email address. Currently checks for @ and . characters.
func MaxLength ¶
func MaxLength(field string, value string, max int) *ValidationError
MaxLength validates if a string's length is at most the specified maximum.
func MinLength ¶
func MinLength(field string, value string, min int) *ValidationError
MinLength validates if a string's length is at least the specified minimum.
func Required ¶
func Required(field string, value interface{}) *ValidationError
Required checks if a value is not zero according to its type. For strings, it checks if the string is not empty. For numbers, it checks if the number is not zero. For time.Time, it checks if the time is not zero. For pointers and interfaces, it checks if the value is not nil.
func (*ValidationError) Error ¶
func (ve *ValidationError) Error() string
Error implements the error interface. It returns the message key by default, which can be translated using a Translator.
type ValidationErrors ¶
type ValidationErrors []*ValidationError
ValidationErrors represents a collection of validation errors.
func (ValidationErrors) Error ¶
func (ve ValidationErrors) Error() string
Error implements the error interface. It joins all validation error messages with semicolons.
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator handles the validation process and collects validation errors.
func (*Validator) Validate ¶
func (v *Validator) Validate(val Validateable) error
Validate processes all validation rules and returns any validation errors. If there are no errors, it returns nil.