validation

package
v1.19.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	ServiceValidator = "service.validator"

	ErrorInvalidRuleSyntax    = "invalidRuleSyntax"
	ErrorUnknownRule          = "unknownRule"
	ErrorNestingDepthExceeded = "nestingDepthExceeded"
)
View Source
const (
	/* Deprecated: use ConstraintAlphaErrorNotAlpha instead. */
	ErrorNotAlpha = ConstraintAlphaErrorNotAlpha

	/* Deprecated: use ConstraintAlphanumericErrorNotAlphanumeric instead. */
	ErrorNotAlphanumeric = ConstraintAlphanumericErrorNotAlphanumeric

	/* Deprecated: use ConstraintEmailErrorInvalidEmail instead. */
	ErrorInvalidEmail = ConstraintEmailErrorInvalidEmail

	/* Deprecated: use ConstraintMaxLength instead. */
	ConstraintMax = ConstraintMaxLength
	/* Deprecated: use ConstraintMaxLengthErrorTooLong instead. */
	ErrorMaxLength = ConstraintMaxLengthErrorTooLong

	/* Deprecated: use ConstraintMinLength instead. */
	ConstraintMin = ConstraintMinLength
	/* Deprecated: use ConstraintMinLengthErrorInsufficientLength instead. */
	ErrorMinLength = ConstraintMinLengthErrorInsufficientLength

	/* Deprecated: use ConstraintNotBlankErrorIsBlank instead. */
	ErrorNotBlank = ConstraintNotBlankErrorIsBlank

	/* Deprecated: use ConstraintNotEmptyErrorEmpty instead. */
	ErrorEmpty = ConstraintNotEmptyErrorEmpty

	/* Deprecated: use ConstraintNumericErrorNotNumeric instead. */
	ErrorNotNumeric = ConstraintNumericErrorNotNumeric

	/* Deprecated: use ConstraintRegexErrorMismatch instead. */
	ErrorRegexMismatch = ConstraintRegexErrorMismatch
	/* Deprecated: use ConstraintRegexErrorInvalidPattern instead. */
	ErrorInvalidPattern = ConstraintRegexErrorInvalidPattern
)
View Source
const (
	ConstraintAlpha              = "alpha"
	ConstraintAlphaErrorNotAlpha = "notAlpha"
)
View Source
const (
	ConstraintAlphanumeric                     = "alphanumeric"
	ConstraintAlphanumericErrorNotAlphanumeric = "notAlphanumeric"
)
View Source
const (
	ConstraintEmail                  = "email"
	ConstraintEmailErrorInvalidEmail = "invalidEmail"
)
View Source
const (
	ConstraintGreaterThan                 = "greaterThan"
	ConstraintGreaterThanErrorSmallerThan = "smallerThan"
)
View Source
const (
	ConstraintLessThan                 = "lessThan"
	ConstraintLessThanErrorGreaterThan = "greaterThanMax"
)
View Source
const (
	ConstraintMaxLength             = "max"
	ConstraintMaxLengthErrorTooLong = "tooLong"
)
View Source
const (
	ConstraintMinLength                        = "min"
	ConstraintMinLengthErrorInsufficientLength = "insufficientLength"
)
View Source
const (
	ConstraintNotBlank             = "notBlank"
	ConstraintNotBlankErrorIsBlank = "isBlank"
)
View Source
const (
	ConstraintNotEmpty           = "notEmpty"
	ConstraintNotEmptyErrorEmpty = "empty"
)
View Source
const (
	ConstraintNumeric                = "numeric"
	ConstraintNumericErrorNotNumeric = "notNumeric"
)
View Source
const (
	ConstraintRegex                    = "regex"
	ConstraintRegexErrorMismatch       = "regexMismatch"
	ConstraintRegexErrorInvalidPattern = "invalidPattern"
)

Variables

This section is empty.

Functions

func IsRuleWiringErrorCode added in v1.19.0

func IsRuleWiringErrorCode(code string) bool
IsRuleWiringErrorCode reports whether a code names a mistake in the DECLARATION rather than in the value: a rule the registry does not know, a parameter set the constraint refuses, a tag the parser cannot read, or a pattern that does not compile. None of them can be produced by any input a client sends — the tag is what is wrong, and it is wrong for every request that route will ever serve.

The distinction has two consumers. A record filed for one of these is a program failure and belongs at error, not at the warning a deliberate 4xx earns, where a permanently broken route was indistinguishable from a user mistyping an address. And the context these errors carry names the developer's own typo, the parameters that were refused and the constraint's reason — the operator's material, not the client's.

The refusal itself stays a field error by design: the validator fails closed on a rule it cannot honour rather than passing the value, and VALIDATION.md documents the codes as validation codes with full rights.

Types

type Alpha

type Alpha struct{}

func (*Alpha) Validate

func (instance *Alpha) Validate(value any, field string) validationcontract.ValidationError

type Alphanumeric

type Alphanumeric struct{}

func (*Alphanumeric) Validate

func (instance *Alphanumeric) Validate(value any, field string) validationcontract.ValidationError

type Email

type Email struct{}

func (*Email) Validate

func (instance *Email) Validate(value any, field string) validationcontract.ValidationError

type GreaterThan added in v1.7.0

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

func NewGreaterThan added in v1.7.0

func NewGreaterThan(min int) *GreaterThan

func (*GreaterThan) Min added in v1.7.0

func (instance *GreaterThan) Min() int

func (*GreaterThan) Validate added in v1.7.0

func (instance *GreaterThan) Validate(value any, field string) validationcontract.ValidationError

func (*GreaterThan) WithParams added in v1.15.0

func (instance *GreaterThan) WithParams(params map[string]string) (validationcontract.Constraint, error)

type LessThan added in v1.15.0

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

func NewLessThan added in v1.15.0

func NewLessThan(max int) *LessThan

func (*LessThan) Max added in v1.15.0

func (instance *LessThan) Max() int

func (*LessThan) Validate added in v1.15.0

func (instance *LessThan) Validate(value any, field string) validationcontract.ValidationError

func (*LessThan) WithParams added in v1.15.0

func (instance *LessThan) WithParams(params map[string]string) (validationcontract.Constraint, error)

type MaxLength

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

func NewMaxLength

func NewMaxLength(max int) *MaxLength

NewMaxLength refuses a negative bound at construction, which is where the tag door beside it already refuses the same typo: a length is never negative, so a negative maximum can only be a mistake, and the constraint it used to build refused every value — the empty string included — with a message naming an impossible limit, which is what the client was handed. The refusal is a panic because a constructor cannot answer an error without changing every caller, and because this is a declaration mistake rather than an input.

func (*MaxLength) Max

func (instance *MaxLength) Max() int

func (*MaxLength) Validate

func (instance *MaxLength) Validate(value any, field string) validationcontract.ValidationError

func (*MaxLength) WithParams added in v1.15.0

func (instance *MaxLength) WithParams(params map[string]string) (validationcontract.Constraint, error)

type MinLength

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

func NewMinLength

func NewMinLength(min int) *MinLength

NewMinLength refuses a negative bound at construction, which is where the tag door beside it already refuses the same typo: a length is never negative, so a negative minimum can only be a mistake, and the constraint it used to build accepted every value in silence — a validation rule that reads as enforced and validates nothing, with no record anywhere. The refusal is a panic because a constructor cannot answer an error without changing every caller, and because this is a declaration mistake rather than an input: it is the shape the cron runner already uses for the same class, and the deliberation a warning presumes is missing when the rule is written wrong.

func (*MinLength) Min

func (instance *MinLength) Min() int

func (*MinLength) Validate

func (instance *MinLength) Validate(value any, field string) validationcontract.ValidationError

func (*MinLength) WithParams added in v1.15.0

func (instance *MinLength) WithParams(params map[string]string) (validationcontract.Constraint, error)

type NotBlank

type NotBlank struct{}

NotBlank judges a string. A value of any other type is refused rather than skipped; notEmpty is the constraint that understands collections.

func (*NotBlank) Validate

func (instance *NotBlank) Validate(value any, field string) validationcontract.ValidationError

type NotEmpty added in v1.7.0

type NotEmpty struct{}

func NewNotEmpty added in v1.7.0

func NewNotEmpty() *NotEmpty

func (*NotEmpty) Validate added in v1.7.0

func (instance *NotEmpty) Validate(value any, field string) validationcontract.ValidationError

type Numeric

type Numeric struct{}

Numeric judges the digits of a string. A value of any other type is refused rather than skipped; a field that holds a number wants greaterThan or lessThan instead.

func (*Numeric) Validate

func (instance *Numeric) Validate(value any, field string) validationcontract.ValidationError

type Regex

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

func NewRegex

func NewRegex(pattern string) *Regex

func (*Regex) Compiled added in v1.7.0

func (instance *Regex) Compiled() *regexp.Regexp

func (*Regex) Error added in v1.7.0

func (instance *Regex) Error() error

func (*Regex) Pattern

func (instance *Regex) Pattern() string

func (*Regex) Validate

func (instance *Regex) Validate(value any, field string) validationcontract.ValidationError

func (*Regex) WithParams added in v1.15.0

func (instance *Regex) WithParams(params map[string]string) (validationcontract.Constraint, error)

type ValidationError

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

func NewValidationError

func NewValidationError(field string, message string, code string, context map[string]any) *ValidationError

func (*ValidationError) Code

func (instance *ValidationError) Code() string

func (*ValidationError) Context

func (instance *ValidationError) Context() map[string]any

func (*ValidationError) Error

func (instance *ValidationError) Error() string

func (*ValidationError) Field

func (instance *ValidationError) Field() string

func (*ValidationError) MarshalJSON

func (instance *ValidationError) MarshalJSON() ([]byte, error)

func (*ValidationError) Message

func (instance *ValidationError) Message() string

func (*ValidationError) ToExceptionError

func (instance *ValidationError) ToExceptionError() error

type ValidationErrors

type ValidationErrors []validationcontract.ValidationError

func (ValidationErrors) Error

func (instance ValidationErrors) Error() string

func (ValidationErrors) HasErrors

func (instance ValidationErrors) HasErrors() bool

func (ValidationErrors) HasRuleWiringError added in v1.19.0

func (instance ValidationErrors) HasRuleWiringError() bool

HasRuleWiringError reports whether any member of the collection blames the declaration rather than the value.

func (ValidationErrors) MarshalJSON added in v1.19.0

func (instance ValidationErrors) MarshalJSON() ([]byte, error)

MarshalJSON renders the collection as the array it is, each element through its own marshaler, so a log record carrying it says the same thing the http response body says: the flattened Error() string was the only form the log ever saw, and the per-field structure — field, message, code, context — survived in one place and not the other. The json logger hands an error implementing json.Marshaler to the encoder for exactly this opt-in.

func (ValidationErrors) WithoutRuleWiringContext added in v1.19.0

func (instance ValidationErrors) WithoutRuleWiringContext() ValidationErrors

WithoutRuleWiringContext answers the collection as the client may see it: an entry that blames the declaration keeps its field, message and code and loses its context, which names the developer's typo, the refused parameters and the constraint's own reason. Every other entry is handed back untouched, so the bounds a numeric constraint reports — material the client needs to correct its request — still travel. The receiver is not modified: the record is rendered from the original and keeps everything.

type Validator

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

func NewValidator

func NewValidator() *Validator

func ValidatorFromContainer

func ValidatorFromContainer(serviceContainer containercontract.Container) *Validator

func ValidatorMustFromContainer

func ValidatorMustFromContainer(serviceContainer containercontract.Container) *Validator

func (*Validator) RegisterConstraint

func (instance *Validator) RegisterConstraint(name string, constraint validationcontract.Constraint)

func (*Validator) Validate

func (instance *Validator) Validate(data any) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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