Documentation
¶
Index ¶
- Constants
- func IsRuleWiringErrorCode(code string) bool
- type Alpha
- type Alphanumeric
- type Email
- type GreaterThan
- type LessThan
- type MaxLength
- type MinLength
- type NotBlank
- type NotEmpty
- type Numeric
- type Regex
- func (instance *Regex) Compiled() *regexp.Regexp
- func (instance *Regex) Error() error
- func (instance *Regex) Pattern() string
- func (instance *Regex) Validate(value any, field string) validationcontract.ValidationError
- func (instance *Regex) WithParams(params map[string]string) (validationcontract.Constraint, error)
- type ValidationError
- func (instance *ValidationError) Code() string
- func (instance *ValidationError) Context() map[string]any
- func (instance *ValidationError) Error() string
- func (instance *ValidationError) Field() string
- func (instance *ValidationError) MarshalJSON() ([]byte, error)
- func (instance *ValidationError) Message() string
- func (instance *ValidationError) ToExceptionError() error
- type ValidationErrors
- type Validator
Constants ¶
const ( ServiceValidator = "service.validator" ErrorInvalidRuleSyntax = "invalidRuleSyntax" ErrorUnknownRule = "unknownRule" ErrorNestingDepthExceeded = "nestingDepthExceeded" )
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 )
const ( ConstraintAlpha = "alpha" ConstraintAlphaErrorNotAlpha = "notAlpha" )
const ( ConstraintAlphanumeric = "alphanumeric" ConstraintAlphanumericErrorNotAlphanumeric = "notAlphanumeric" )
const ( ConstraintEmail = "email" ConstraintEmailErrorInvalidEmail = "invalidEmail" )
const ( ConstraintGreaterThan = "greaterThan" ConstraintGreaterThanErrorSmallerThan = "smallerThan" )
const ( ConstraintLessThan = "lessThan" ConstraintLessThanErrorGreaterThan = "greaterThanMax" )
const ( ConstraintMaxLength = "max" ConstraintMaxLengthErrorTooLong = "tooLong" )
const ( ConstraintMinLength = "min" ConstraintMinLengthErrorInsufficientLength = "insufficientLength" )
const ( ConstraintNotBlank = "notBlank" ConstraintNotBlankErrorIsBlank = "isBlank" )
const ( ConstraintNotEmpty = "notEmpty" ConstraintNotEmptyErrorEmpty = "empty" )
const ( ConstraintNumeric = "numeric" ConstraintNumericErrorNotNumeric = "notNumeric" )
const ( ConstraintRegex = "regex" ConstraintRegexErrorMismatch = "regexMismatch" ConstraintRegexErrorInvalidPattern = "invalidPattern" )
Variables ¶
This section is empty.
Functions ¶
func IsRuleWiringErrorCode ¶ added in v1.19.0
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 (*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 ¶
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) 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 ¶
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) 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 (*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 (*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)
Source Files
¶
- const.go
- constraint_alpha.go
- constraint_alphanumeric.go
- constraint_email.go
- constraint_greater_than.go
- constraint_less_than.go
- constraint_max_length.go
- constraint_min_length.go
- constraint_not_blank.go
- constraint_not_empty.go
- constraint_numeric.go
- constraint_regex.go
- error.go
- service_resolver.go
- validation_rule.go
- validator.go
- value.go