validate

package
v1.18.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: Apache-2.0 Imports: 11 Imported by: 249

Documentation

Overview

Package validate contains validation helpers.

Index

Constants

This section is empty.

Variables

View Source
var ErrBodyRequired = errors.New("body required")

ErrBodyRequired reports that request body is required but server got empty request.

View Source
var ErrFieldRequired = errors.New("field required")

ErrFieldRequired reports that a field is required, but not found.

View Source
var ErrNilPointer = errors.New("nil pointer")

ErrNilPointer reports that use Validate, but receiver pointer is nil.

Functions

func InvalidContentType added in v0.11.1

func InvalidContentType(contentType string) error

InvalidContentType creates new InvalidContentTypeError.

func Ogen added in v1.15.0

func Ogen(name string, value, params any) error

Ogen validates using the default global registry.

func RegisterValidator added in v1.15.0

func RegisterValidator(name string, validator OgenValidator) error

RegisterValidator registers a validator in the default global registry.

func UnexpectedStatusCode deprecated added in v0.11.1

func UnexpectedStatusCode(statusCode int) error

UnexpectedStatusCode creates new UnexpectedStatusCode.

Deprecated: client codes generated a while ago used this function. Kept here solely for backward compatibility to them.

func UnexpectedStatusCodeWithResponse added in v1.15.0

func UnexpectedStatusCodeWithResponse(response *http.Response) error

UnexpectedStatusCodeWithResponse creates new UnexpectedStatusCode.

func UniqueItems added in v0.67.0

func UniqueItems[S ~[]T, T comparable](arr S) error

UniqueItems ensures given array has no duplicates.

Types

type Array

type Array struct {
	MinLength    int
	MinLengthSet bool
	MaxLength    int
	MaxLengthSet bool
	UniqueItems  bool
}

Array validates array length.

func (Array) Set

func (t Array) Set() bool

Set reports whether any validations are set.

func (*Array) SetMaxLength

func (t *Array) SetMaxLength(v int)

SetMaxLength sets MaxLength validation.

func (*Array) SetMinLength

func (t *Array) SetMinLength(v int)

SetMinLength sets MinLength validation.

func (*Array) SetUniqueItems added in v0.67.0

func (t *Array) SetUniqueItems(v bool)

SetUniqueItems sets UniqueItems validation.

func (Array) ValidateLength

func (t Array) ValidateLength(v int) error

ValidateLength returns error if array length v is invalid.

type Decimal added in v1.16.0

type Decimal struct {
	MultipleOf    decimal.Decimal
	MultipleOfSet bool

	Min          decimal.Decimal
	MinSet       bool
	MinExclusive bool

	Max          decimal.Decimal
	MaxSet       bool
	MaxExclusive bool
}

Decimal validates decimal numbers.

func (Decimal) Set added in v1.16.0

func (t Decimal) Set() bool

Set reports whether any validations are set.

func (*Decimal) SetExclusiveMaximum added in v1.16.0

func (t *Decimal) SetExclusiveMaximum(v decimal.Decimal)

SetExclusiveMaximum sets exclusive maximum value.

func (*Decimal) SetExclusiveMinimum added in v1.16.0

func (t *Decimal) SetExclusiveMinimum(v decimal.Decimal)

SetExclusiveMinimum sets exclusive minimum value.

func (*Decimal) SetMaximum added in v1.16.0

func (t *Decimal) SetMaximum(v decimal.Decimal)

SetMaximum sets maximum value.

func (*Decimal) SetMinimum added in v1.16.0

func (t *Decimal) SetMinimum(v decimal.Decimal)

SetMinimum sets minimum value.

func (*Decimal) SetMultipleOf added in v1.16.0

func (t *Decimal) SetMultipleOf(d decimal.Decimal)

SetMultipleOf sets multipleOf validator.

func (Decimal) Validate added in v1.16.0

func (t Decimal) Validate(v decimal.Decimal) error

Validate returns error if v does not match validation rules.

type DepthLimitError added in v1.17.0

type DepthLimitError struct {
	// MaxDepth is the configured maximum depth.
	MaxDepth int

	// TypeName is the type being compared when limit was hit.
	TypeName string
}

DepthLimitError indicates nesting depth limit was exceeded.

func (*DepthLimitError) Error added in v1.17.0

func (e *DepthLimitError) Error() string

Error implements error.

type DuplicateItemsError added in v1.17.0

type DuplicateItemsError struct {
	// Indices contains all indices where duplicates were found.
	// First element is the original, subsequent are duplicates.
	Indices []int
}

DuplicateItemsError indicates duplicate items in a uniqueItems array.

func (*DuplicateItemsError) Error added in v1.17.0

func (e *DuplicateItemsError) Error() string

Error implements error.

type Error

type Error struct {
	Fields []FieldError
}

Error represents validation error.

func (*Error) Error

func (e *Error) Error() string

Error implements error.

type FieldError

type FieldError struct {
	Name  string
	Error error
}

FieldError is failed validation on field.

type Float added in v0.16.0

type Float struct {
	MultipleOf    *big.Rat
	MultipleOfSet bool

	Min          float64
	MinSet       bool
	MinExclusive bool

	Max          float64
	MaxSet       bool
	MaxExclusive bool

	// Pattern constraint for validating string representation
	Pattern ogenregex.Regexp
}

Float validates float numbers.

func (Float) Set added in v0.16.0

func (t Float) Set() bool

Set reports whether any validations are set.

func (*Float) SetExclusiveMaximum added in v0.16.0

func (t *Float) SetExclusiveMaximum(v float64)

SetExclusiveMaximum sets exclusive maximum value.

func (*Float) SetExclusiveMinimum added in v0.16.0

func (t *Float) SetExclusiveMinimum(v float64)

SetExclusiveMinimum sets exclusive minimum value.

func (*Float) SetMaximum added in v0.16.0

func (t *Float) SetMaximum(v float64)

SetMaximum sets maximum value.

func (*Float) SetMinimum added in v0.16.0

func (t *Float) SetMinimum(v float64)

SetMinimum sets minimum value.

func (*Float) SetMultipleOf added in v0.16.0

func (t *Float) SetMultipleOf(rat *big.Rat)

SetMultipleOf sets multipleOf validator.

func (*Float) SetPattern added in v1.17.0

func (t *Float) SetPattern(v ogenregex.Regexp)

SetPattern sets pattern constraint for validating string representation.

func (Float) Validate added in v0.16.0

func (t Float) Validate(v float64) error

Validate returns error if v does not match validation rules.

func (Float) ValidateStringified added in v0.70.0

func (t Float) ValidateStringified(v float64) error

ValidateStringified returns error if v does not match validation rules.

type Int

type Int struct {
	MultipleOf    uint64
	MultipleOfSet bool

	Min          int64
	MinSet       bool
	MinExclusive bool

	Max          int64
	MaxSet       bool
	MaxExclusive bool

	// Pattern constraint for validating string representation
	Pattern ogenregex.Regexp
}

Int validates integers.

func (Int) Set

func (t Int) Set() bool

Set reports whether any validations are set.

func (*Int) SetExclusiveMaximum

func (t *Int) SetExclusiveMaximum(v int64)

SetExclusiveMaximum sets exclusive maximum value.

func (*Int) SetExclusiveMinimum

func (t *Int) SetExclusiveMinimum(v int64)

SetExclusiveMinimum sets exclusive minimum value.

func (*Int) SetMaximum

func (t *Int) SetMaximum(v int64)

SetMaximum sets maximum value.

func (*Int) SetMinimum

func (t *Int) SetMinimum(v int64)

SetMinimum sets minimum value.

func (*Int) SetMultipleOf

func (t *Int) SetMultipleOf(v uint64)

SetMultipleOf sets multipleOf validator.

func (*Int) SetPattern added in v1.17.0

func (t *Int) SetPattern(v ogenregex.Regexp)

SetPattern sets pattern constraint for validating string representation.

func (Int) Validate

func (t Int) Validate(v int64) error

Validate returns error if v does not match validation rules.

type InvalidContentTypeError added in v0.11.1

type InvalidContentTypeError struct {
	ContentType string
}

InvalidContentTypeError reports that decoder got unexpected content type.

func (*InvalidContentTypeError) Error added in v0.11.1

func (e *InvalidContentTypeError) Error() string

InvalidContentTypeError implements error.

type MaxLengthError added in v0.78.0

type MaxLengthError struct {
	Len       int
	MaxLength int
}

MaxLengthError reports that len greater than maximum.

func (*MaxLengthError) Error added in v0.78.0

func (e *MaxLengthError) Error() string

MaxLengthError implements error.

type MinLengthError added in v0.78.0

type MinLengthError struct {
	Len       int
	MinLength int
}

MinLengthError reports that len less than minimum.

func (*MinLengthError) Error added in v0.78.0

func (e *MinLengthError) Error() string

MinLengthError implements error.

type NoRegexMatchError added in v0.79.0

type NoRegexMatchError struct {
	Pattern ogenregex.Regexp
}

NoRegexMatchError reports that value have no regexp match.

func (*NoRegexMatchError) Error added in v0.79.0

func (e *NoRegexMatchError) Error() string

MaxLengthError implements error.

type Object added in v0.12.0

type Object struct {
	MinProperties    int
	MinPropertiesSet bool
	MaxProperties    int
	MaxPropertiesSet bool
	// TODO: add validate to gen
	MinLength    int
	MinLengthSet bool
	MaxLength    int
	MaxLengthSet bool
}

Object validates map length.

func (Object) Set added in v0.12.0

func (o Object) Set() bool

Set reports whether any validations are seo.

func (*Object) SetMaxLength added in v1.15.0

func (o *Object) SetMaxLength(v int)

SetMaxLength sets MaxLength validation.

func (*Object) SetMaxProperties added in v0.12.0

func (o *Object) SetMaxProperties(v int)

SetMaxProperties sets MaxProperties validation.

func (*Object) SetMinLength added in v1.15.0

func (o *Object) SetMinLength(v int)

SetMinLength sets MinLength validation.

func (*Object) SetMinProperties added in v0.12.0

func (o *Object) SetMinProperties(v int)

SetMinProperties sets MinProperties validation.

func (Object) ValidateProperties added in v0.12.0

func (o Object) ValidateProperties(v int) error

ValidateProperties returns error if object length (properties number) v is invalid.

type OgenValidator added in v1.15.0

type OgenValidator func(value any, params any) error

OgenValidator is a function that performs custom validation.

func GetValidator added in v1.15.0

func GetValidator(name string) (OgenValidator, bool)

GetValidator returns a validator from the default global registry.

type OgenValidatorRegistry added in v1.15.0

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

OgenValidatorRegistry holds custom validators that can be registered and used for validation.

func NewOgenValidatorRegistry added in v1.15.0

func NewOgenValidatorRegistry() *OgenValidatorRegistry

NewOgenValidatorRegistry creates a new OgenValidatorRegistry.

func (*OgenValidatorRegistry) Get added in v1.15.0

func (*OgenValidatorRegistry) Register added in v1.15.0

func (r *OgenValidatorRegistry) Register(name string, validator OgenValidator) error

func (*OgenValidatorRegistry) Validate added in v1.15.0

func (r *OgenValidatorRegistry) Validate(validatorName string, value, params any) error

Validate validates a value using the specified validator and parameters.

type String

type String struct {
	MinLength    int
	MinLengthSet bool
	MaxLength    int
	MaxLengthSet bool
	Email        bool
	Regex        ogenregex.Regexp
	Hostname     bool

	// Numeric constraints for strings representing numbers
	MinNumeric    float64
	MinNumericSet bool
	MaxNumeric    float64
	MaxNumericSet bool
}

String validator.

func (String) Set

func (t String) Set() bool

Set reports whether any validations are set.

func (*String) SetMaxLength

func (t *String) SetMaxLength(v int)

SetMaxLength sets maximum string length (in Unicode code points).

func (*String) SetMaximumNumeric added in v1.17.0

func (t *String) SetMaximumNumeric(v float64)

SetMaximumNumeric sets maximum numeric value for numeric strings.

func (*String) SetMinLength

func (t *String) SetMinLength(v int)

SetMinLength sets minimum string length (in Unicode code points).

func (*String) SetMinimumNumeric added in v1.17.0

func (t *String) SetMinimumNumeric(v float64)

SetMinimumNumeric sets minimum numeric value for numeric strings.

func (String) Validate

func (t String) Validate(v string) error

Validate returns error if v does not match validation rules.

type UnexpectedStatusCodeError added in v0.11.1

type UnexpectedStatusCodeError struct {
	StatusCode int
	Payload    *http.Response
}

UnexpectedStatusCodeError reports that client got unexpected status code.

func (*UnexpectedStatusCodeError) Error added in v0.11.1

func (e *UnexpectedStatusCodeError) Error() string

UnexpectedStatusCodeError implements error.

type ValidationError added in v1.15.0

type ValidationError struct {
	ValidatorName string
	Value         any
	Params        any
	Message       string
}

ValidationError represents a validation error from a custom validator.

func (*ValidationError) Error added in v1.15.0

func (e *ValidationError) Error() string

Error implements the error interface.

Jump to

Keyboard shortcuts

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