validation

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// Validation constants
	MaxLabels      = 10
	MaxLabelLength = 50
	MaxProperties  = 100
	MaxPropertyKey = 100
	MaxBatchSize   = 1000
	MinBatchSize   = 1
)

Functions

func ClampDuration

func ClampDuration(value, min, max time.Duration) time.Duration

ClampDuration clamps a duration to the specified range [min, max].

func ClampInt

func ClampInt(value, min, max int) int

ClampInt clamps a value to the specified range [min, max].

func DefaultOr

func DefaultOr[T comparable](value, defaultValue T) T

DefaultOr returns the value if it's non-zero, otherwise returns the default.

func DefaultOrDuration

func DefaultOrDuration(value, defaultValue time.Duration) time.Duration

DefaultOrDuration returns the value if it's positive, otherwise returns the default.

func DefaultOrInt

func DefaultOrInt(value, defaultValue int) int

DefaultOrInt returns the value if it's positive, otherwise returns the default.

func ValidateBatchSize

func ValidateBatchSize(size int) error

ValidateBatchSize validates the size of a batch request

func ValidateConfig

func ValidateConfig(config Validatable) error

ValidateConfig validates any type that implements Validatable.

func ValidateEdgeRequest

func ValidateEdgeRequest(req *EdgeRequest) error

ValidateEdgeRequest validates an edge creation/update request

func ValidateNodeRequest

func ValidateNodeRequest(req *NodeRequest) error

ValidateNodeRequest validates a node creation/update request

func ValidatePropertyKey

func ValidatePropertyKey(key string) error

ValidatePropertyKey validates a property key

Types

type ConfigValidator

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

ConfigValidator provides a fluent interface for validating configuration values. It collects all validation errors rather than failing on the first one.

func NewConfigValidator

func NewConfigValidator(configName string) *ConfigValidator

NewConfigValidator creates a new config validator with the given config name.

func (*ConfigValidator) Custom

func (cv *ConfigValidator) Custom(field string, fn func() error) *ConfigValidator

Custom applies a custom validation function.

func (*ConfigValidator) Error

func (cv *ConfigValidator) Error() error

Error returns the first validation error, or nil if no errors.

func (*ConfigValidator) Errors

func (cv *ConfigValidator) Errors() []error

Errors returns all validation errors.

func (*ConfigValidator) HasErrors

func (cv *ConfigValidator) HasErrors() bool

HasErrors returns true if any validation errors occurred.

func (*ConfigValidator) MaxDuration

func (cv *ConfigValidator) MaxDuration(field string, value, max time.Duration) *ConfigValidator

MaxDuration validates that a duration does not exceed the maximum.

func (*ConfigValidator) MaxInt

func (cv *ConfigValidator) MaxInt(field string, value, max int) *ConfigValidator

MaxInt validates that an int field does not exceed the maximum value.

func (*ConfigValidator) MinDuration

func (cv *ConfigValidator) MinDuration(field string, value, min time.Duration) *ConfigValidator

MinDuration validates that a duration is at least the minimum.

func (*ConfigValidator) MinInt

func (cv *ConfigValidator) MinInt(field string, value, min int) *ConfigValidator

MinInt validates that an int field is at least the minimum value.

func (*ConfigValidator) NonNegative

func (cv *ConfigValidator) NonNegative(field string, value int) *ConfigValidator

NonNegative validates that an int field is non-negative (>= 0).

func (*ConfigValidator) NonNegativeFloat

func (cv *ConfigValidator) NonNegativeFloat(field string, value float64) *ConfigValidator

NonNegativeFloat validates that a float field is non-negative (>= 0).

func (*ConfigValidator) OneOf

func (cv *ConfigValidator) OneOf(field, value string, allowed []string) *ConfigValidator

OneOf validates that a string field is one of the allowed values.

func (*ConfigValidator) Positive

func (cv *ConfigValidator) Positive(field string, value int) *ConfigValidator

Positive validates that an int field is positive (> 0).

func (*ConfigValidator) PositiveFloat

func (cv *ConfigValidator) PositiveFloat(field string, value float64) *ConfigValidator

PositiveFloat validates that a float field is positive (> 0).

func (*ConfigValidator) RangeDuration

func (cv *ConfigValidator) RangeDuration(field string, value, min, max time.Duration) *ConfigValidator

RangeDuration validates that a duration is within the specified range.

func (*ConfigValidator) RangeInt

func (cv *ConfigValidator) RangeInt(field string, value, min, max int) *ConfigValidator

RangeInt validates that an int field is within the specified range.

func (*ConfigValidator) Required

func (cv *ConfigValidator) Required(field, value string) *ConfigValidator

Required validates that a string field is not empty.

func (*ConfigValidator) RequiredDuration

func (cv *ConfigValidator) RequiredDuration(field string, value time.Duration) *ConfigValidator

RequiredDuration validates that a duration field is not zero.

func (*ConfigValidator) RequiredInt

func (cv *ConfigValidator) RequiredInt(field string, value int) *ConfigValidator

RequiredInt validates that an int field is not zero.

func (*ConfigValidator) Validate

func (cv *ConfigValidator) Validate() error

Validate returns a combined error if any validations failed.

func (*ConfigValidator) When

func (cv *ConfigValidator) When(condition bool, validations func(*ConfigValidator)) *ConfigValidator

When conditionally applies validations if the condition is true.

type EdgeRequest

type EdgeRequest struct {
	FromNodeID uint64         `json:"fromNodeId" validate:"required,min=1"`
	ToNodeID   uint64         `json:"toNodeId" validate:"required,min=1"`
	Type       string         `json:"type" validate:"required,min=1,max=50"`
	Weight     *float64       `json:"weight" validate:"omitempty"`
	Properties map[string]any `json:"properties" validate:"omitempty,max=100"`
}

EdgeRequest represents a request to create or update an edge

type NodeRequest

type NodeRequest struct {
	Labels     []string       `json:"labels" validate:"required,min=1,max=10,dive,max=50"`
	Properties map[string]any `json:"properties" validate:"omitempty,max=100"`
}

NodeRequest represents a request to create or update a node

type Validatable

type Validatable interface {
	Validate() error
}

Validatable is an interface for types that can validate themselves.

Jump to

Keyboard shortcuts

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