libValidate

package
v0.28.1 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package libValidate provides request field validation with custom and system validators.

Index

Constants

View Source
const (
	// ErrorCodeRequiredField is the error code for missing required fields.
	ErrorCodeRequiredField = "REQUIRED-FIELD"
	// ErrorCodeInvalidInputData is the error code for invalid input data.
	ErrorCodeInvalidInputData = "INVALID-INPUT-DATA"
)

Error code constants

View Source
const RegexPaddedIP string = `^((25[0-5]|2[0-4]\d|1\d\d|0\d\d)\.?\b){4}$` //^((25[0-5]|2[0-4]\d|1\d\d|0\d\d)\.?\b){4}$

RegexPaddedIP is the regular expression for validating padded IP addresses.

Variables

View Source
var (
	// Validator is the global validator instance used for struct validation.
	Validator *validator.Validate
	// Translator is the global translator used for validation error messages.
	Translator ut.Translator
)

Functions

func DisableDefaultSystemValidators added in v0.20.0

func DisableDefaultSystemValidators()

DisableDefaultSystemValidators disables registration of default system validators. Custom system validators can still be registered using SetSystemValidators or RegisterSystemValidator. This should be called before Init() to take effect.

func GetErrorCode added in v0.20.0

func GetErrorCode(tag string) string

GetErrorCode returns the error code for a given tag, or a default if not found.

func GetTranslator

func GetTranslator() ut.Translator

GetTranslator returns the global translator used for validation error messages.

func Init

func Init()

Init initializes the global validator and translator, safe to call multiple times.

func IsCustomValidator added in v0.20.0

func IsCustomValidator(tag string) bool

IsCustomValidator checks if a tag is a custom validator by checking the registry.

func PaddedIPValidator added in v0.28.1

func PaddedIPValidator(fl validator.FieldLevel) bool

PaddedIPValidator validates that a field contains a properly padded IP address.

func RegisterPaddedIPValidator added in v0.28.1

func RegisterPaddedIPValidator() error

RegisterPaddedIPValidator registers the padded_ip validator. This must be called after Init() to register the padded_ip validator. Example:

libValidate.Init()
err := libValidate.RegisterPaddedIPValidator()
if err != nil {
	log.Fatalf("failed to register padded_ip validator: %v", err)
}

func RegisterSystemValidator added in v0.20.0

func RegisterSystemValidator(tag string, errorCode string)

RegisterSystemValidator registers a system validator (from validator package) in the registry. This can be called by library users to register custom system validators. Example:

libValidate.RegisterSystemValidator("email", libValidate.ErrorCodeInvalidInputData)

func SetSystemValidators added in v0.20.0

func SetSystemValidators(validators map[string]string)

SetSystemValidators allows library users to configure which system validators to register. This should be called before Init() to take effect. If validators is nil or empty, default validators will be used. Example:

libValidate.SetSystemValidators(map[string]string{
	"required": libValidate.ErrorCodeRequiredField,
	"email":    libValidate.ErrorCodeInvalidInputData,
})

func ValidateStruct

ValidateStruct validates the given struct and returns validation errors or an invalid-type error.

Types

type ValidatorBuilder added in v0.20.0

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

ValidatorBuilder provides a fluent interface for registering validators

func RegisterValidation added in v0.20.0

func RegisterValidation(tag string, fn validator.Func) *ValidatorBuilder

RegisterValidation starts building a validator registration with method chaining. Example:

err := libValidate.RegisterValidation("nationalcode",
	func(fl validator.FieldLevel) bool {
		nationalCode := fl.Field().String()
		return ValidateNationalCode(nationalCode)
	}).
	WithTranslation(
		func(ut ut.Translator) error {
			return ut.Add("nationalcode", "{0} وارد شده معتبر نمی باشد", true)
		},
		func(ut ut.Translator, fe validator.FieldError) string {
			t, _ := ut.T("nationalcode", fe.Field())
			return t
		}).
	WithErrorCode(libValidate.ErrorCodeInvalidInputData).
	Build()
if err != nil {
	log.Fatalf("failed to register validation for nationalcode: %v", err)
}

func (*ValidatorBuilder) Build added in v0.20.0

func (vb *ValidatorBuilder) Build() error

Build completes the validator registration.

func (*ValidatorBuilder) WithErrorCode added in v0.20.0

func (vb *ValidatorBuilder) WithErrorCode(errorCode string) *ValidatorBuilder

WithErrorCode sets the error code for the validator. If not called, defaults to ErrorCodeInvalidInputData.

func (*ValidatorBuilder) WithTranslation added in v0.20.0

func (vb *ValidatorBuilder) WithTranslation(registerFn func(ut.Translator) error, transFn func(ut.Translator, validator.FieldError) string) *ValidatorBuilder

WithTranslation adds translation functions to the validator builder.

type ValidatorInfo added in v0.20.0

type ValidatorInfo struct {
	Tag       string
	ErrorCode string
	Type      ValidatorType
}

ValidatorInfo stores information about a registered validator

func GetValidatorInfo added in v0.20.0

func GetValidatorInfo(tag string) *ValidatorInfo

GetValidatorInfo returns the validator information for a given tag, or nil if not found.

type ValidatorType added in v0.20.0

type ValidatorType int

ValidatorType represents the type of validator

const (
	// ValidatorTypeCustom represents a custom validator registered via RegisterValidation
	ValidatorTypeCustom ValidatorType = iota
	// ValidatorTypeSystem represents a system validator (from validator package)
	ValidatorTypeSystem
)

Jump to

Keyboard shortcuts

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