validator

package module
v1.0.14 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CodePasswordRequired = 2001
	CodeMobileInvalid    = 2002
	CodeFieldInvalid     = 2003
)

Validation error codes.

Variables

View Source
var (
	ErrPasswordRequired = &ValidationError{Code: CodePasswordRequired, Message: "password must contain uppercase and lowercase letters and digits, with a minimum length of 8"}
	ErrMobileInvalid    = &ValidationError{Code: CodeMobileInvalid, Message: "invalid mobile phone number format"}
	ErrFieldInvalid     = &ValidationError{Code: CodeFieldInvalid, Message: "validation failed"}
)

Pre-defined validation errors. Use errors.Is/As to match.

Functions

func IsValidationError

func IsValidationError(err error) bool

IsValidationError returns true if err is or wraps a ValidationError.

func ValidationErrorCode

func ValidationErrorCode(err error) int

ValidationErrorCode extracts the error code from a ValidationError. Returns 0 if err is not a ValidationError.

Types

type ValidationError

type ValidationError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Field   string `json:"field,omitempty"`
}

ValidationError is a structured error for validation failures. Supports errors.Is matching by error code.

func NewValidationError

func NewValidationError(code int, msg string) *ValidationError

NewValidationError creates a new ValidationError with the given code and message.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error implements the error interface.

func (*ValidationError) Is

func (e *ValidationError) Is(target error) bool

Is enables errors.Is matching by error code. Returns true if target is a *ValidationError with the same Code.

type Validator

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

Validator provides struct field validation with custom phone and password rules.

func (*Validator) Init

func (v *Validator) Init(ctx *core.Context) error

func (*Validator) Validate

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

Validate validates the given struct against registered validation rules. Returns a *ValidationError on failure, which can be checked with errors.Is/As.

Jump to

Keyboard shortcuts

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