validator

package module
v2.2.0-beta Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: BSD-3-Clause Imports: 18 Imported by: 0

README

validator.go

Documentation

Index

Constants

View Source
const (
	ImageMimeTypePNG = "image/png"
	ImageMimeTypeJPG = "image/jpeg"
	ImageMimeTypeGIF = "image/gif"
)
View Source
const AllowAnyURLSchema = "*"
View Source
const (
	NestedShortcut = "*"
)

Variables

View Source
var (
	NotExistsDataSetIntoContextError = errors.New("not exists data set into context")
	UnknownOperatorError             = errors.New("unknown operator")
	CallbackUnexpectedValueTypeError = errors.New("callback unexpected value type")
)

Functions

func DataSetFromContext

func DataSetFromContext[T DataSet](ctx *Context) (T, bool)

func ExtractDataSet

func ExtractDataSet[T any](ctx context.Context) (T, bool)

func IsError

func IsError(err error) (map[string][]string, bool)

IsError - проверяет на ошибку валидации и возвращает аттрибуты, где ключ равняется полю, а значения ошибкам валидации.

{
	"client_id": [
		"Value cannot be blank.",
		"Value is invalid."
	]
}

func SetTranslator

func SetTranslator(t Translator)

func Validate

func Validate(ctx context.Context, dataSet any, rules RuleSet) error

func ValidateValue

func ValidateValue(ctx context.Context, value any, rules ...Rule) error

Types

type Callback

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

func NewCallback

func NewCallback[T any](f CallbackFunc[T]) *Callback[T]

func (*Callback[T]) SkipOnEmpty

func (r *Callback[T]) SkipOnEmpty() *Callback[T]

func (*Callback[T]) SkipOnError

func (r *Callback[T]) SkipOnError() *Callback[T]

func (*Callback[T]) ValidateValue

func (c *Callback[T]) ValidateValue(ctx context.Context, value any) error

func (*Callback[T]) When

func (r *Callback[T]) When(v WhenFunc) *Callback[T]

type CallbackFunc

type CallbackFunc[T any] func(ctx context.Context, value T) error

type Compare

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

func NewCompare

func NewCompare(targetValue any, targetAttribute, operator string) *Compare

func (*Compare) SkipOnEmpty

func (r *Compare) SkipOnEmpty() *Compare

func (*Compare) SkipOnError

func (r *Compare) SkipOnError() *Compare

func (*Compare) ValidateValue

func (r *Compare) ValidateValue(ctx context.Context, value any) error

func (*Compare) When

func (r *Compare) When(v WhenFunc) *Compare

func (*Compare) WithMessage

func (r *Compare) WithMessage(v string) *Compare

type Context

type Context struct {
	context.Context
	// contains filtered or unexported fields
}

func NewContext

func NewContext(ctx context.Context) *Context

func (*Context) Value

func (c *Context) Value(key any) any

type DataSet

type DataSet interface {
	FieldValue(name string) (any, error)
	FieldAliasName(name string) string
	Name() set.Name
	Data() any
}

type DummyTranslator

type DummyTranslator struct {
}

func (*DummyTranslator) Translate

func (d *DummyTranslator) Translate(_ context.Context, id string, params map[string]any) string

type Each

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

func NewEach

func NewEach(rules ...Rule) *Each

func (*Each) SkipOnEmpty

func (r *Each) SkipOnEmpty() *Each

func (*Each) SkipOnError

func (r *Each) SkipOnError() *Each

func (*Each) ValidateValue

func (r *Each) ValidateValue(ctx context.Context, value any) error

func (*Each) When

func (r *Each) When(v WhenFunc) *Each

func (*Each) WithIncorrectInputMessage

func (r *Each) WithIncorrectInputMessage(incorrectInputMessage string) *Each

func (*Each) WithMessage

func (r *Each) WithMessage(message string) *Each

type Email

type Email struct {
	*MatchRegularExpression
}

func NewEmail

func NewEmail() *Email

type HumanText

type HumanText struct {
	*MatchRegularExpression
}

func NewHumanText

func NewHumanText() *HumanText

type IP

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

func NewIP

func NewIP() *IP

func (*IP) SkipOnEmpty

func (r *IP) SkipOnEmpty() *IP

func (*IP) SkipOnError

func (r *IP) SkipOnError() *IP

func (*IP) ValidateValue

func (r *IP) ValidateValue(_ context.Context, value any) error

func (*IP) When

func (r *IP) When(v WhenFunc) *IP

func (*IP) WithMessage

func (r *IP) WithMessage(v string) *IP

type ImageMeta

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

func NewImageMeta

func NewImageMeta() *ImageMeta

func (*ImageMeta) SkipOnEmpty

func (r *ImageMeta) SkipOnEmpty() *ImageMeta

func (*ImageMeta) SkipOnError

func (r *ImageMeta) SkipOnError() *ImageMeta

func (*ImageMeta) ValidateValue

func (r *ImageMeta) ValidateValue(_ context.Context, value any) error

func (*ImageMeta) When

func (r *ImageMeta) When(v WhenFunc) *ImageMeta

func (*ImageMeta) WithMessage

func (r *ImageMeta) WithMessage(message string) *ImageMeta

type ImageMetaData

type ImageMetaData struct {
	Name     string
	MimeType string
	Size     uint64
	Width    int
	Height   int
}

type InRange

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

func NewInRange

func NewInRange(rangeValues []any) *InRange

func (*InRange) Not

func (r *InRange) Not() *InRange

func (*InRange) SkipOnEmpty

func (r *InRange) SkipOnEmpty() *InRange

func (*InRange) SkipOnError

func (r *InRange) SkipOnError() *InRange

func (*InRange) ValidateValue

func (r *InRange) ValidateValue(_ context.Context, value any) error

func (*InRange) When

func (r *InRange) When(v WhenFunc) *InRange

func (*InRange) WithMessage

func (r *InRange) WithMessage(message string) *InRange

type JSON

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

func NewJSON

func NewJSON() *JSON

func (*JSON) SkipOnEmpty

func (j *JSON) SkipOnEmpty() *JSON

func (*JSON) SkipOnError

func (j *JSON) SkipOnError() *JSON

func (*JSON) ValidateValue

func (j *JSON) ValidateValue(_ context.Context, value any) error

func (*JSON) When

func (j *JSON) When(v WhenFunc) *JSON

type Key

type Key uint8
const (
	KeyDataSet Key = iota + 1
	PreviousRulesErrored
)

type MSISDN

type MSISDN struct {
	*MatchRegularExpression
}

func NewMSISDN

func NewMSISDN() MSISDN

type MatchRegularExpression

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

func NewMatchRegularExpression

func NewMatchRegularExpression(pattern string) *MatchRegularExpression

func (*MatchRegularExpression) SkipOnEmpty

func (*MatchRegularExpression) SkipOnError

func (*MatchRegularExpression) ValidateValue

func (r *MatchRegularExpression) ValidateValue(_ context.Context, value any) error

func (*MatchRegularExpression) When

func (*MatchRegularExpression) WithMessage

func (r *MatchRegularExpression) WithMessage(message string) *MatchRegularExpression

type Nested

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

func NewNested

func NewNested(rules RuleSet) *Nested

func (*Nested) SkipOnEmpty

func (r *Nested) SkipOnEmpty() *Nested

func (*Nested) SkipOnError

func (r *Nested) SkipOnError() *Nested

func (*Nested) ValidateValue

func (r *Nested) ValidateValue(ctx context.Context, value any) error

func (*Nested) When

func (r *Nested) When(v WhenFunc) *Nested

func (*Nested) WithMessage

func (r *Nested) WithMessage(message string) *Nested

type Number

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

func NewNumber

func NewNumber(min, max int64) *Number

func (*Number) SkipOnEmpty

func (r *Number) SkipOnEmpty() *Number

func (*Number) SkipOnError

func (r *Number) SkipOnError() *Number

func (*Number) ValidateValue

func (r *Number) ValidateValue(_ context.Context, value any) error

func (*Number) When

func (r *Number) When(v WhenFunc) *Number

func (*Number) WithNotNumberMessage

func (r *Number) WithNotNumberMessage(message string) *Number

func (*Number) WithTooBigMessage

func (r *Number) WithTooBigMessage(message string) *Number

func (*Number) WithTooSmallMessage

func (r *Number) WithTooSmallMessage(message string) *Number

type Required

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

func NewRequired

func NewRequired() *Required

func (*Required) SkipOnError

func (r *Required) SkipOnError() *Required

func (*Required) ValidateValue

func (r *Required) ValidateValue(_ context.Context, value any) error

func (*Required) When

func (r *Required) When(v WhenFunc) *Required

func (*Required) WithAllowZeroValue

func (r *Required) WithAllowZeroValue() *Required

deprecated: should be removed

func (*Required) WithMessage

func (r *Required) WithMessage(message string) *Required

type Result

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

func NewResult

func NewResult() Result

func (Result) AttributeErrorMessagesIndexedByPath

func (s Result) AttributeErrorMessagesIndexedByPath(attribute string) map[string][]string

func (Result) CommonErrorMessages

func (s Result) CommonErrorMessages() []string

func (Result) Error

func (s Result) Error() string

func (Result) ErrorMessagesIndexedByPath

func (s Result) ErrorMessagesIndexedByPath() map[string][]string

func (Result) Errors

func (s Result) Errors() []*ValidationError

func (Result) IsValid

func (s Result) IsValid() bool

func (Result) WithError

func (s Result) WithError(errs ...*ValidationError) Result

type Rule

type Rule interface {
	ValidateValue(ctx context.Context, value any) error
}

type RuleSet

type RuleSet map[string]Rules

type RuleSkipEmpty

type RuleSkipEmpty interface {
	// contains filtered or unexported methods
}

type RuleSkipError

type RuleSkipError interface {
	// contains filtered or unexported methods
}

type RuleWhen

type RuleWhen interface {
	// contains filtered or unexported methods
}

type Rules

type Rules []Rule

func (Rules) SkipOnError

func (rs Rules) SkipOnError()

type StringLength

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

func NewStringLength

func NewStringLength(min, max int) *StringLength

func (*StringLength) SkipOnEmpty

func (r *StringLength) SkipOnEmpty() *StringLength

func (*StringLength) SkipOnError

func (r *StringLength) SkipOnError() *StringLength

func (*StringLength) ValidateValue

func (r *StringLength) ValidateValue(_ context.Context, value any) error

func (*StringLength) When

func (r *StringLength) When(v WhenFunc) *StringLength

func (*StringLength) WithMessage

func (r *StringLength) WithMessage(message string) *StringLength

func (*StringLength) WithTooLongMessage

func (r *StringLength) WithTooLongMessage(message string) *StringLength

func (*StringLength) WithTooShortMessage

func (r *StringLength) WithTooShortMessage(message string) *StringLength

type Time

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

func NewTime

func NewTime() *Time

func (*Time) SkipOnEmpty

func (r *Time) SkipOnEmpty() *Time

func (*Time) SkipOnError

func (r *Time) SkipOnError() *Time

func (*Time) ValidateValue

func (r *Time) ValidateValue(_ context.Context, value any) error

func (*Time) When

func (r *Time) When(v WhenFunc) *Time

func (*Time) WithFormat

func (r *Time) WithFormat(format string) *Time

func (*Time) WithFormatMessage

func (r *Time) WithFormatMessage(message string) *Time

func (*Time) WithMax

func (r *Time) WithMax(max TimeFunc) *Time

func (*Time) WithMessage

func (r *Time) WithMessage(message string) *Time

func (*Time) WithMin

func (r *Time) WithMin(min TimeFunc) *Time

func (*Time) WithTooBigMessage

func (r *Time) WithTooBigMessage(message string) *Time

func (*Time) WithTooSmallMessage

func (r *Time) WithTooSmallMessage(message string) *Time

type TimeFunc

type TimeFunc func() time.Time

type Translator

type Translator interface {
	Translate(
		ctx context.Context,
		id string,
		params map[string]any,
	) string
}
var DefaultTranslator Translator = &DummyTranslator{}

type URL

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

func NewURL

func NewURL() *URL

func (*URL) SkipOnEmpty

func (r *URL) SkipOnEmpty() *URL

func (*URL) SkipOnError

func (r *URL) SkipOnError() *URL

func (*URL) ValidateValue

func (r *URL) ValidateValue(_ context.Context, value any) error

func (*URL) When

func (r *URL) When(v WhenFunc) *URL

func (*URL) WithEnableIDN

func (r *URL) WithEnableIDN() *URL

func (*URL) WithMessage

func (r *URL) WithMessage(message string) *URL

func (*URL) WithValidScheme

func (r *URL) WithValidScheme(scheme ...string) *URL

type UUID

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

func NewUUID

func NewUUID() *UUID

func (*UUID) SkipOnEmpty

func (r *UUID) SkipOnEmpty() *UUID

func (*UUID) SkipOnError

func (r *UUID) SkipOnError() *UUID

func (*UUID) ValidateValue

func (r *UUID) ValidateValue(_ context.Context, value any) error

func (*UUID) When

func (r *UUID) When(v WhenFunc) *UUID

func (*UUID) WithInvalidVersionMessage

func (r *UUID) WithInvalidVersionMessage(message string) *UUID

func (*UUID) WithMessage

func (r *UUID) WithMessage(message string) *UUID

func (*UUID) WithVersion

func (r *UUID) WithVersion(version UUIDVersion) *UUID

type UUIDVersion

type UUIDVersion byte
const (
	UUIDVersionV1 UUIDVersion = 1
	UUIDVersionV3 UUIDVersion = 3
	UUIDVersionV4 UUIDVersion = 4
	UUIDVersionV5 UUIDVersion = 5
	UUIDVersionV6 UUIDVersion = 6
	UUIDVersionV7 UUIDVersion = 7
)

type UniqueValues

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

func NewUniqueValues

func NewUniqueValues() *UniqueValues

func (*UniqueValues) SkipOnEmpty

func (r *UniqueValues) SkipOnEmpty() *UniqueValues

func (*UniqueValues) SkipOnError

func (r *UniqueValues) SkipOnError() *UniqueValues

func (*UniqueValues) ValidateValue

func (r *UniqueValues) ValidateValue(_ context.Context, value any) error

func (*UniqueValues) When

func (r *UniqueValues) When(v WhenFunc) *UniqueValues

func (*UniqueValues) WithMessage

func (r *UniqueValues) WithMessage(message string) *UniqueValues

type ValidationError

type ValidationError struct {
	Message   string
	Params    map[string]any
	ValuePath []string
}

func NewValidationError

func NewValidationError(message string) *ValidationError

func (*ValidationError) Error

func (v *ValidationError) Error() string

func (*ValidationError) WithParams

func (v *ValidationError) WithParams(params map[string]any) *ValidationError

func (*ValidationError) WithValuePath

func (v *ValidationError) WithValuePath(valuePath []string) *ValidationError

type WhenFunc

type WhenFunc func(ctx context.Context, value any) bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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