validation

command
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: MIT Imports: 9 Imported by: 0

README

Validation Example

This example demonstrates comprehensive validation features in goconfig, including:

  • Min/Max validation for integers, floats, and durations
  • Pattern validation for strings using regular expressions
  • Custom validators for complex business logic
  • Nested struct validation using dot notation

Running the Example

With Valid Configuration
export API_KEY="sk-1234567890abcdefghij"
export SERVER_PORT="8080"
export DB_USER="appuser"
go run .
Testing Validation Errors
Port out of range (below minimum)
export SERVER_PORT="80"  # Error: below minimum 1024
export API_KEY="sk-1234567890abcdefghij"
go run .
Port out of range (above maximum)
export SERVER_PORT="70000"  # Error: above maximum 65535
export API_KEY="sk-1234567890abcdefghij"
go run .
Invalid duration (below minimum)
export READ_TIMEOUT="100ms"  # Error: below minimum 1s
export API_KEY="sk-1234567890abcdefghij"
go run .
Invalid pattern (username with special characters)
export DB_USER="user@host"  # Error: doesn't match pattern ^[a-zA-Z0-9_]+$
export API_KEY="sk-1234567890abcdefghij"
go run .
Custom validation (API key too short)
export API_KEY="sk-short"  # Error: must be at least 20 characters
go run .
Custom validation (API key wrong prefix)
export API_KEY="pk-1234567890abcdefghij"  # Error: must start with 'sk-'
go run .

Features Demonstrated

Integer Range Validation
Port int `key:"SERVER_PORT" default:"8080" min:"1024" max:"65535"`
Float Range Validation
LoadFactor float64 `key:"LOAD_FACTOR" default:"0.75" min:"0.0" max:"1.0"`
Duration Range Validation
ReadTimeout time.Duration `key:"READ_TIMEOUT" default:"30s" min:"1s" max:"5m"`
String Pattern Validation
Hostname string `key:"HOSTNAME" default:"localhost" pattern:"^[a-zA-Z0-9.-]+$"`
Custom Validators
goconfig.WithValidator("API.APIKey", func(value any) error {
    key := value.(string)
    if !strings.HasPrefix(key, "sk-") {
        return fmt.Errorf("API key must start with 'sk-'")
    }
    return nil
})
Nested Field Validation
// Validates the Host field within the Database struct
goconfig.WithValidator("Database.Host", func(value any) error {
    // validation logic
    return nil
})

Validation Order

Validations are applied in this order:

  1. Tag-based validation (min, max, pattern)
  2. Custom validators (in the order they are registered)

All validations must pass for the configuration to be considered valid.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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