validation

package
v0.2.12 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error struct {
	ErrorResponse *ErrorResponse
}

Error is an error who occured when trying to validate an entity It contains an ErrorResponse which can be used to write an http error message.

func GetUnmarshalError

func GetUnmarshalError(err *json.UnmarshalTypeError) *Error

GetUnmarshalError returns a validation Error based on given UnmarshalTypeError.

func NewError

func NewError(errorResponse *ErrorResponse) *Error

NewError returns an error based on a validation error response.

func ValidateStringAsInt added in v0.2.4

func ValidateStringAsInt(value string, fieldName string, validator *Validator[int]) *Error

ValidateStringAsInt validates a string can be converted to int and applies int validation.

func (*Error) Error

func (validationError *Error) Error() string

type ErrorDetail

type ErrorDetail struct {
	Field  string `json:"field"`
	Reason string `json:"reason"`
}

ErrorDetail details an error.

func NewParameterErrorDetail

func NewParameterErrorDetail(field, reason string) ErrorDetail

NewParameterErrorDetail returns an error struct containing which field and the reason behind the error.

func (*ErrorDetail) MarshalJSON

func (errorDetail *ErrorDetail) MarshalJSON() ([]byte, error)

MarshalJSON marshals a JSON string from the ErrorResponse.

type ErrorResponse

type ErrorResponse struct {
	Title   string        `json:"title"`
	Detail  string        `json:"detail,omitempty"`
	Status  int           `json:"status"`
	Type    string        `json:"type"`
	Details []ErrorDetail `json:"details,omitempty"`
}

ErrorResponse is a generic response type for errors in HTTP requests.

func NewErrorResponse

func NewErrorResponse(statusCode int, details ...ErrorDetail) *ErrorResponse

NewErrorResponse returns a new ErrorResponse based on given status code and details.

func (*ErrorResponse) MarshalJSON

func (errorResponse *ErrorResponse) MarshalJSON() ([]byte, error)

MarshalJSON marshals a JSON string from the ErrorResponse.

func (*ErrorResponse) WriteHTTPError

func (errorResponse *ErrorResponse) WriteHTTPError(writer http.ResponseWriter)

WriteHTTPError writes error to a writer as JSON.

type Rule added in v0.2.4

type Rule[T any] func(value T, fieldName string) *Error

Rule represents a single validation rule.

func Custom added in v0.2.4

func Custom[T any](fn func(T) bool, message string) Rule[T]

Custom allows defining custom validation logic.

func Email added in v0.2.4

func Email() Rule[string]

Email validates email format (simple validation).

func Max added in v0.2.4

func Max(maximum int) Rule[int]

Max validates maximum integer value.

func MaxLength added in v0.2.4

func MaxLength(maximum int) Rule[string]

MaxLength validates maximum string length.

func Min added in v0.2.4

func Min(minimum int) Rule[int]

Min validates minimum integer value.

func MinLength added in v0.2.4

func MinLength(minimum int) Rule[string]

MinLength validates minimum string length.

func Range added in v0.2.4

func Range(minimum, maximum int) Rule[int]

Range validates integer is within range.

func Required added in v0.2.4

func Required() Rule[string]

Required validates that a string is not empty.

type StructValidator added in v0.2.4

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

StructValidator provides validation for struct fields.

func NewStructValidator added in v0.2.4

func NewStructValidator() *StructValidator

NewStructValidator creates a new struct validator.

func ValidateStruct added in v0.2.4

func ValidateStruct() *StructValidator

ValidateStruct creates a new struct validator Example usage:

validator := validation.ValidateStruct().Field("Name", func(v interface{}) *validation.Error {
    return validation.Validate[string]().Rule(validation.Required()).Validate(v.(string), "Name")
})

func (*StructValidator) Field added in v0.2.4

func (sv *StructValidator) Field(fieldName string, validator func(interface{}) *Error) *StructValidator

Field adds a field validator.

func (*StructValidator) Validate added in v0.2.4

func (sv *StructValidator) Validate(data interface{}) *Error

Validate validates a struct.

type Validator added in v0.2.4

type Validator[T any] struct {
	// contains filtered or unexported fields
}

Validator provides type-safe validation for various data types.

func NewValidator added in v0.2.4

func NewValidator[T any]() *Validator[T]

NewValidator creates a new type-safe validator.

func Validate added in v0.2.4

func Validate[T any]() *Validator[T]

Validate creates a new type-safe validator for the specified type T Example usage:

validator := validation.Validate[string]().Rule(validation.Required()).Rule(validation.MinLength(3))
if err := validator.Validate(userInput, "username"); err != nil { ... }

func (*Validator[T]) Rule added in v0.2.4

func (v *Validator[T]) Rule(rule Rule[T]) *Validator[T]

Rule adds a validation rule to the validator (currying/chaining).

func (*Validator[T]) Validate added in v0.2.4

func (v *Validator[T]) Validate(value T, fieldName string) *Error

Validate validates a value against all rules.

Jump to

Keyboard shortcuts

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