validator

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2025 License: GPL-3.0 Imports: 15 Imported by: 4

Documentation

Index

Constants

View Source
const (
	ErrFieldTagNameNotFound                   = "field tag name not found: %s"
	ErrFieldIsRequiredNotFound                = "field is required not found: %s"
	ErrStructValidationsAndMapperTypeMismatch = "struct validations and mapper type mismatch, both must be of the same type, mapper type: %s, struct validations type: %s"
)

Variables

View Source
var (
	ErrNilService                      = errors.New("mapper validator service cannot be nil")
	ErrNilDestination                  = errors.New("destination cannot be nil")
	ErrDestinationNotPointer           = errors.New("destination must be a pointer")
	ErrNilMapper                       = errors.New("mapper cannot be nil")
	ErrNilValidator                    = errors.New("mapper validator cannot be nil")
	ErrStructValidationsIsNotRootLevel = errors.New("struct validations is not root level")
	ErrRequiredField                   = "%s is required"
)

Functions

This section is empty.

Types

type BirthdateOptions

type BirthdateOptions struct {
	MinimumAge int
	MaximumAge int
}

BirthdateOptions is the birthdate options struct

type DefaultService

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

DefaultService struct

func NewDefaultService

func NewDefaultService(
	rawParser govalidatormapperparser.RawParser,
	endParser govalidatormapperparser.EndParser,
	validator Validator,
	birthdateOptions *BirthdateOptions,
	passwordOptions *PasswordOptions,
	logger *slog.Logger,
) (*DefaultService, error)

NewDefaultService creates a new default validator service

Parameters:

  • rawParser: the raw parser to use
  • endParser: the end parser to use
  • validator: the validator to use
  • birthdateOptions: the default birthdate options (optional, can be nil)
  • passwordOptions: the default password options (optional, can be nil)
  • logger: the logger to use

Returns:

  • *DefaultService: the default validator service
  • error: if there was an error creating the service

func (*DefaultService) Birthdate

func (d *DefaultService) Birthdate(
	birthdateField string,
	birthdate time.Time,
	validations *govalidatormappervalidation.StructValidations,
)

Birthdate validates the birthdate field

Parameters:

- birthdateField: the birthdate field name - birthdate: the birthdate to validate - validations: the struct validations

func (*DefaultService) CreateValidateFn

func (d *DefaultService) CreateValidateFn(
	mapper *govalidatormapper.Mapper,
	cache bool,
	auxiliaryValidatorFns ...any,
) (
	ValidateFn, error,
)

CreateValidateFn creates a validate function for a given mapper

Parameters:

  • mapper: the mapper to use
  • cache: whether to cache the validate function or not
  • auxiliaryValidatorFns: the auxiliary validator functions to use

Returns:

  • ValidateFn: the validate function
  • error: if there was an error creating the validate function

func (*DefaultService) Email

func (d *DefaultService) Email(
	emailField string,
	email string,
	validations *govalidatormappervalidation.StructValidations,
)

Email validates the email address field

Parameters:

  • emailField: the email field name
  • email: the email to validate
  • validations: the struct validations

func (*DefaultService) ParseValidations

func (d *DefaultService) ParseValidations(
	rootStructValidations *govalidatormappervalidation.StructValidations,
) (any, error)

ParseValidations parses the validations

Parameters:

  • rootStructValidations: the root struct validations

Returns:

  • any: the parsed validations
  • error: if there was an error parsing the validations

func (*DefaultService) Password

func (d *DefaultService) Password(
	passwordField string,
	password string,
	validations *govalidatormappervalidation.StructValidations,
)

Password validates the password field

Parameters:

- passwordField: the password field name - password: the password to validate - validations: the struct validations

func (*DefaultService) Username

func (d *DefaultService) Username(
	usernameField string,
	username string,
	validations *govalidatormappervalidation.StructValidations,
)

Username validates the username field

Parameters:

  • usernameField: the username field name
  • username: the username to validate
  • validations: the struct validations

func (*DefaultService) Validate

func (d *DefaultService) Validate(
	mapper *govalidatormapper.Mapper,
	auxiliaryValidatorFns ...any,
) (any, error)

Validate is the function that creates (if not cached), caches and executes the validation

Parameters:

  • mapper: the mapper to use
  • auxiliaryValidatorFns: auxiliary validator functions to use in the validation

Returns:

  • any: the parsed validations
  • error: if there was an error validating the request

func (*DefaultService) ValidateRequiredFields

func (d *DefaultService) ValidateRequiredFields(
	rootStructValidations *govalidatormappervalidation.StructValidations,
	mapper *govalidatormapper.Mapper,
) error

ValidateRequiredFields validates the required fields

Parameters:

  • rootStructValidations: the root struct validations
  • mapper: the mapper to use

Returns:

- error: if there was an error validating the required fields

type DefaultValidator

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

DefaultValidator struct

func NewDefaultValidator

func NewDefaultValidator(
	logger *slog.Logger,
) *DefaultValidator

NewDefaultValidator creates a new default mapper validator

Parameters:

  • logger: the logger to use

Returns:

  • *DefaultValidator: the default mapper validator

func (DefaultValidator) IsFieldInitialized

func (d DefaultValidator) IsFieldInitialized(
	fieldValue reflect.Value,
) (isInitialized bool)

IsFieldInitialized checks if a field is initialized

Parameters:

  • fieldValue: the field value to check

Returns:

  • isInitialized: true if the field is initialized, false otherwise

func (DefaultValidator) ValidateRequiredFields

func (d DefaultValidator) ValidateRequiredFields(
	rootStructValidations *govalidatormappervalidation.StructValidations,
	mapper *govalidatormapper.Mapper,
) error

ValidateRequiredFields validates the required fields of a struct

Parameters:

  • rootStructValidations: the root struct validations to validate
  • mapper: the struct mapper to use

Returns:

  • err: error if any

type PasswordOptions

type PasswordOptions struct {
	MinimumLength       int
	MinimumSpecialCount int
	MinimumNumbersCount int
	MinimumCapsCount    int
}

PasswordOptions is the password options struct

type Service

type Service interface {
	ValidateRequiredFields(
		rootStructValidations *govalidatormappervalidation.StructValidations,
		mapper *govalidatormapper.Mapper,
	) error
	ParseValidations(rootStructValidations *govalidatormappervalidation.StructValidations) (
		any,
		error,
	)
	Email(
		emailField string,
		email string,
		validations *govalidatormappervalidation.StructValidations,
	)
	Username(
		usernameField string,
		username string,
		validations *govalidatormappervalidation.StructValidations,
	)
	Birthdate(
		birthdateField string,
		birthdate time.Time,
		validations *govalidatormappervalidation.StructValidations,
	)
	Password(
		passwordField string,
		password string,
		validations *govalidatormappervalidation.StructValidations,
	)
	CreateValidateFn(
		mapper *govalidatormapper.Mapper,
		cache bool,
		auxiliaryValidatorFns ...any,
	) (
		ValidateFn, error,
	)
	Validate(
		mapper *govalidatormapper.Mapper,
		auxiliaryValidatorFns ...any,
	) (any, error)
}

Service interface for the validator service

type ValidateFn

type ValidateFn func(toValidate any) (any, error)

ValidateFn is the type for the validate function

type Validator

type Validator interface {
	ValidateRequiredFields(
		rootStructValidations *govalidatormappervalidation.StructValidations,
		mapper *govalidatormapper.Mapper,
	) (err error)
}

Validator interface

Jump to

Keyboard shortcuts

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