validation

package
v1.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package validation provides validation rules and utilities for DynamORM model data integrity.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateModel

func ValidateModel(model any) error

ValidateModel validates a DynamORM model before saving

func ValidateRequest

func ValidateRequest(request any) error

ValidateRequest validates a request payload using struct tags

func ValidateWithTags

func ValidateWithTags(model any) error

ValidateWithTags validates a struct using struct tags

Types

type AlphaNumRule

type AlphaNumRule struct{}

AlphaNumRule validates that string contains only alphanumeric characters

func (AlphaNumRule) Message

func (r AlphaNumRule) Message() string

Message returns the validation error message for AlphaNumRule

func (AlphaNumRule) Validate

func (r AlphaNumRule) Validate(value any) error

Validate checks that the string contains only alphanumeric characters

type AlphaRule

type AlphaRule struct{}

AlphaRule validates that string contains only alphabetic characters

func (AlphaRule) Message

func (r AlphaRule) Message() string

Message returns the validation error message for AlphaRule

func (AlphaRule) Validate

func (r AlphaRule) Validate(value any) error

Validate checks that the string contains only alphabetic characters

type ContentRule

type ContentRule struct {
	Constraints ValidationConstraints
}

ContentRule validates status content according to Lesser's requirements

func (ContentRule) Message

func (r ContentRule) Message() string

Message returns the validation error message for ContentRule

func (ContentRule) Validate

func (r ContentRule) Validate(value any) error

Validate checks that the content meets Lesser's requirements

type DateRule

type DateRule struct{}

DateRule validates that value is a valid date

func (DateRule) Message

func (r DateRule) Message() string

Message returns the validation error message for DateRule

func (DateRule) Validate

func (r DateRule) Validate(value any) error

Validate checks that the value is a valid date

type EmailRule

type EmailRule struct{}

EmailRule validates email format

func (EmailRule) Message

func (r EmailRule) Message() string

Message returns the validation error message for EmailRule

func (EmailRule) Validate

func (r EmailRule) Validate(value any) error

Validate checks that the string value is a valid email address format

type InRule

type InRule struct {
	AllowedValues []string
}

InRule validates that value is in a list of allowed values

func (InRule) Message

func (r InRule) Message() string

Message returns the validation error message for InRule

func (InRule) Validate

func (r InRule) Validate(value any) error

Validate checks that the value is in the list of allowed values

type IntegerRule

type IntegerRule struct{}

IntegerRule validates that value is an integer

func (IntegerRule) Message

func (r IntegerRule) Message() string

Message returns the validation error message for IntegerRule

func (IntegerRule) Validate

func (r IntegerRule) Validate(value any) error

Validate checks that the value is an integer

type LengthRule

type LengthRule struct {
	Length int
}

LengthRule validates exact string length

func (LengthRule) Message

func (r LengthRule) Message() string

Message returns the validation error message for LengthRule

func (LengthRule) Validate

func (r LengthRule) Validate(value any) error

Validate checks that the string value has exactly the required length

type MaxLengthRule

type MaxLengthRule struct {
	MaxLength int
}

MaxLengthRule validates maximum string length

func (MaxLengthRule) Message

func (r MaxLengthRule) Message() string

Message returns the validation error message for MaxLengthRule

func (MaxLengthRule) Validate

func (r MaxLengthRule) Validate(value any) error

Validate checks that the string or slice value does not exceed the maximum length

type MaxRule

type MaxRule struct {
	Max float64
}

MaxRule validates maximum numeric value

func (MaxRule) Message

func (r MaxRule) Message() string

Message returns the validation error message for MaxRule

func (MaxRule) Validate

func (r MaxRule) Validate(value any) error

Validate checks that the numeric value does not exceed the maximum requirement

type MinLengthRule

type MinLengthRule struct {
	MinLength int
}

MinLengthRule validates minimum string length

func (MinLengthRule) Message

func (r MinLengthRule) Message() string

Message returns the validation error message for MinLengthRule

func (MinLengthRule) Validate

func (r MinLengthRule) Validate(value any) error

Validate checks that the string value meets the minimum length requirement

type MinRule

type MinRule struct {
	Min float64
}

MinRule validates minimum numeric value

func (MinRule) Message

func (r MinRule) Message() string

Message returns the validation error message for MinRule

func (MinRule) Validate

func (r MinRule) Validate(value any) error

Validate checks that the numeric value meets the minimum requirement

type NotInRule

type NotInRule struct {
	DisallowedValues []string
}

NotInRule validates that value is not in a list of disallowed values

func (NotInRule) Message

func (r NotInRule) Message() string

Message returns the validation error message for NotInRule

func (NotInRule) Validate

func (r NotInRule) Validate(value any) error

Validate checks that the value is not in the list of disallowed values

type NumericRule

type NumericRule struct{}

NumericRule validates that value is numeric

func (NumericRule) Message

func (r NumericRule) Message() string

Message returns the validation error message for NumericRule

func (NumericRule) Validate

func (r NumericRule) Validate(value any) error

Validate checks that the value is numeric

type PasswordRule

type PasswordRule struct {
	Constraints ValidationConstraints
}

PasswordRule validates passwords according to Lesser's security requirements

func (PasswordRule) Message

func (r PasswordRule) Message() string

Message returns the validation error message for PasswordRule

func (PasswordRule) Validate

func (r PasswordRule) Validate(value any) error

Validate checks that the password meets Lesser's security requirements

type PatternRule

type PatternRule struct {
	Pattern *regexp.Regexp
	// contains filtered or unexported fields
}

PatternRule validates using a regular expression

func (PatternRule) Message

func (r PatternRule) Message() string

Message returns the validation error message for PatternRule

func (PatternRule) Validate

func (r PatternRule) Validate(value any) error

Validate checks that the string value matches the regular expression pattern

type RequiredRule

type RequiredRule struct{}

RequiredRule validates that a value is not empty/zero

func (RequiredRule) Message

func (r RequiredRule) Message() string

Message returns the validation error message for RequiredRule

func (RequiredRule) Validate

func (r RequiredRule) Validate(value any) error

Validate checks that the value is not empty or zero

type Rule

type Rule interface {
	Validate(value any) error
	Message() string
}

Rule represents a validation rule

type URLRule

type URLRule struct{}

URLRule validates URL format

func (URLRule) Message

func (r URLRule) Message() string

Message returns the validation error message for URLRule

func (URLRule) Validate

func (r URLRule) Validate(value any) error

Validate checks that the string value is a valid URL format

type UsernameRule

type UsernameRule struct {
	Constraints ValidationConstraints
}

UsernameRule validates usernames according to Lesser's requirements

func (UsernameRule) Message

func (r UsernameRule) Message() string

Message returns the validation error message for UsernameRule

func (UsernameRule) Validate

func (r UsernameRule) Validate(value any) error

Validate checks that the username meets Lesser's requirements

type ValidationConstraints

type ValidationConstraints struct {
	Username struct {
		MinLength int
		MaxLength int
		Pattern   *regexp.Regexp
	}
	Email struct {
		MaxLength int
	}
	Password struct {
		MinLength      int
		RequireUpper   bool
		RequireLower   bool
		RequireDigit   bool
		RequireSpecial bool
	}
	Content struct {
		MaxLength int
	}
}

ValidationConstraints provides predefined validation constraints for common model fields

func DefaultConstraints

func DefaultConstraints() ValidationConstraints

DefaultConstraints provides default validation constraints for the Lesser project

type ValidationError

type ValidationError struct {
	Field   string `json:"field"`
	Message string `json:"message"`
	Value   any    `json:"value,omitempty"`
}

ValidationError represents a validation error

func (ValidationError) Error

func (e ValidationError) Error() string

type ValidationErrors

type ValidationErrors struct {
	Errors []ValidationError `json:"errors"`
}

ValidationErrors represents multiple validation errors

func (*ValidationErrors) Add

func (e *ValidationErrors) Add(field, message string, value any)

Add adds a validation error

func (ValidationErrors) Error

func (e ValidationErrors) Error() string

func (ValidationErrors) HasErrors

func (e ValidationErrors) HasErrors() bool

HasErrors returns true if there are validation errors

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator validates struct fields

func CreateModelValidator

func CreateModelValidator(logger *zap.Logger) *Validator

CreateModelValidator creates a validator with common model rules

func NewValidator

func NewValidator(logger *zap.Logger) *Validator

NewValidator creates a new validator

func (*Validator) AddRule

func (v *Validator) AddRule(field string, rule Rule)

AddRule adds a validation rule for a field

func (*Validator) Validate

func (v *Validator) Validate(model any) error

Validate validates a struct using registered rules

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL