validate

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package validate provides configuration validation utilities for the xg2g application.

Package validate provides configuration validation utilities for the xg2g application.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidLogLevel = &Error{
		Field:   "logLevel",
		Message: "invalid log level (must be: debug, info, warn, error)",
	}
)

Common validation errors

Functions

This section is empty.

Types

type Error

type Error struct {
	Field   string      // Field name that failed validation
	Value   interface{} // The invalid value
	Message string      // Human-readable error message
}

Error represents a validation error

func (Error) Error

func (e Error) Error() string

Error implements the error interface

type LogLevel

type LogLevel string

LogLevel represents valid log levels

const (
	// LogLevelDebug enables debug-level logging
	LogLevelDebug LogLevel = "debug"
	// LogLevelInfo enables info-level logging
	LogLevelInfo LogLevel = "info"
	// LogLevelWarn enables warn-level logging
	LogLevelWarn LogLevel = "warn"
	// LogLevelError enables error-level logging
	LogLevelError LogLevel = "error"
)

Log level constants define the available logging severity levels.

func ParseLogLevel

func ParseLogLevel(s string) (LogLevel, error)

ParseLogLevel parses a string into a LogLevel

func (LogLevel) IsValid

func (l LogLevel) IsValid() bool

IsValid checks if the log level is valid

func (LogLevel) String

func (l LogLevel) String() string

String returns the string representation

type ValidationError

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

ValidationError bundles multiple validation errors into a single error value.

func (ValidationError) Error

func (e ValidationError) Error() string

Error implements the error interface for ValidationError.

func (ValidationError) Errors

func (e ValidationError) Errors() []Error

Errors returns the individual validation errors making up the validation failure.

type Validator

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

Validator accumulates validation errors and can produce a ValidationError when invalid.

func New

func New() *Validator

New creates a new validator

func (*Validator) AddError

func (v *Validator) AddError(field, message string, value interface{})

AddError adds a validation error

func (*Validator) Custom

func (v *Validator) Custom(field string, value interface{}, validator func(interface{}) error)

Custom allows custom validation logic The validator function should return an error if validation fails

func (*Validator) Directory

func (v *Validator) Directory(field, path string, mustExist bool)

Directory validates a directory path If mustExist is true, the directory must already exist If mustExist is false, the directory will be created if it doesn't exist

func (*Validator) Err

func (v *Validator) Err() error

Err converts the accumulated validation errors into an error value.

func (*Validator) Errors

func (v *Validator) Errors() []Error

Errors returns all accumulated validation errors

func (*Validator) IsValid

func (v *Validator) IsValid() bool

IsValid returns true if no errors have been accumulated

func (*Validator) NonNegative

func (v *Validator) NonNegative(field string, value int)

NonNegative validates that a number is non-negative (>= 0)

func (*Validator) NotEmpty

func (v *Validator) NotEmpty(field, value string)

NotEmpty validates that a string is not empty or whitespace-only

func (*Validator) OneOf

func (v *Validator) OneOf(field, value string, allowed []string)

OneOf validates that a value is one of the allowed values

func (*Validator) Path

func (v *Validator) Path(field, path string)

Path validates a file path for security issues This function protects against path traversal attacks

func (*Validator) PathWithinRoot

func (v *Validator) PathWithinRoot(field, path, rootDir string)

PathWithinRoot validates that a path stays within a specified root directory This provides stronger guarantees against directory escape attacks

func (*Validator) Port

func (v *Validator) Port(field string, port int)

Port validates a port number (1-65535)

func (*Validator) Positive

func (v *Validator) Positive(field string, value int)

Positive validates that a number is positive (> 0)

func (*Validator) Range

func (v *Validator) Range(field string, value, minVal, maxVal int)

Range validates that an integer is within a specified range (inclusive)

func (*Validator) StreamURL

func (v *Validator) StreamURL(field, streamURL string)

StreamURL validates a stream URL for IPTV/streaming purposes Checks: valid URL syntax, http/https scheme, host present, path not empty

func (*Validator) URL

func (v *Validator) URL(field, value string, allowedSchemes []string)

URL validates a URL string

Jump to

Keyboard shortcuts

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