validate

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package validate provides an easy way to validate the data contained in structs, typically being DTOs containing user input.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check[T any](v *Validator, key string, value T, validatorFunc ValidatorFunc[T])

Check checks if value passes the validatorFunc. The provided key is used for creating the errors map of the Validator.

func CheckOptional added in v0.1.9

func CheckOptional[T any](
	v *Validator,
	key string,
	value *T,
	validatorFunc ValidatorFunc[T],
)

CheckOptional checks if value passes the validatorFunc, when the value is provided. The provided key is used for creating the errors map of the Validator.

func IsNotEmpty

func IsNotEmpty(value string) (bool, string)

IsNotEmpty checks that the provided value is not empty.

func IsValidTimeZone

func IsValidTimeZone(value string) (bool, string)

IsValidTimeZone checks if the provided value is a valid IANA timezone.

Types

type ValidatedType added in v0.1.1

type ValidatedType interface {
	Validate() (bool, map[string]string)
}

ValidatedType is implemented by any struct with a Validate method.

type Validator

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

Validator is used to validate contents of structs using Check.

func New

func New() *Validator

New creates a new Validator.

func (*Validator) Errors

func (v *Validator) Errors() map[string]string

Errors returns the Validator errors.

func (*Validator) Valid

func (v *Validator) Valid() bool

Valid checks if a Validator has any errors.

type ValidatorFunc

type ValidatorFunc[T any] func(value T) (bool, string)

ValidatorFunc is the expected format used for validating data using Check.

func IsGreaterThan added in v0.1.9

func IsGreaterThan[T shared.IntType](value1 T) ValidatorFunc[T]

IsGreaterThan checks if the provided value2 > value1.

func IsGreaterThanOrEqual added in v0.1.9

func IsGreaterThanOrEqual[T shared.IntType](value1 T) ValidatorFunc[T]

IsGreaterThanOrEqual checks if the provided value2 >= value1.

func IsInSlice added in v0.1.9

func IsInSlice[T comparable](slice []T) ValidatorFunc[T]

IsInSlice checks if the provided value is part of the provided slice.

func IsLesserThan added in v0.1.9

func IsLesserThan[T shared.IntType](value1 T) ValidatorFunc[T]

IsLesserThan checks if the provided value2 < value1.

func IsLesserThanOrEqual added in v0.1.9

func IsLesserThanOrEqual[T shared.IntType](value1 T) ValidatorFunc[T]

IsLesserThanOrEqual checks if the provided value2 <= value1.

Jump to

Keyboard shortcuts

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