validator

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2026 License: Apache-2.0 Imports: 7 Imported by: 2 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Validator Library

This is a shared package in use by various Choria projects that provide input validation:

Validation is done using an expression language called Expr meaning multiple expressions can be combined.

It provides:

  • A generic Go validator library
  • A Validator compatible with Fisk
  • A Validator compatible with Survey

It provides the following validators:

Validator Description
is_ip / isIP Validates the value is a valid IP address (IPv4 or IPv6)
is_ipv4 / isIPv4 Validates the value is a valid IPv4 address
is_ipv6 / isIPv6 Validates the value is a valid IPv6 address
is_int / isInt Validates the value is an integer of any size
is_float / isFloat Validates the value is a floating point number of any size
is_duration / isDuration Validates the value is a valid duration using fisk.ParseDuration
is_regex / isRegex Validates the value matches a regular expression, e.g. is_regex(value, "^[a-z]+$")
is_shellsafe / isShellSafe Validates the value does not contain shell-unsafe characters
is_hostname / isHostname Validates the value is a valid hostname per RFC 1123
is_fqdn / isFQDN Validates the value is a valid fully qualified domain name per RFC 1123

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DurationValidator

func DurationValidator() []expr.Option

func FQDNValidator added in v0.0.2

func FQDNValidator() []expr.Option

func FiskValidator

func FiskValidator(validation string) fisk.OptionValidator

FiskValidator is a fisk.OptionValidator that compatible with Validator() on arguments and flags

Example
v := FiskValidator("is_int(value)")

err := v("42")
fmt.Printf("err: %v\n", err)

err = v("bob")
fmt.Printf("err: %v\n", err)
Output:
err: <nil>
err: validation using "is_int(value)" did not pass

func FloatValidator

func FloatValidator() []expr.Option

func HostnameValidator added in v0.0.2

func HostnameValidator() []expr.Option

func IPv4Validator

func IPv4Validator() []expr.Option

func IPv6Validator

func IPv6Validator() []expr.Option

func IPvValidator

func IPvValidator() []expr.Option

func IntValidator

func IntValidator() []expr.Option

func RegexValidator

func RegexValidator() []expr.Option

func ShellSafeValidator

func ShellSafeValidator() []expr.Option

func SurveyValidator

func SurveyValidator(validation string, required bool) func(any) error

SurveyValidator is a validator for github.com/AlecAivazis/survey

Example
v := SurveyValidator("is_int(value)", true)

err := v("42")
fmt.Printf("err: %v\n", err)

err = v("bob")
fmt.Printf("err: %v\n", err)
Output:
err: <nil>
err: validation using "is_int(value)" did not pass

func Validate

func Validate(value any, validation string) (bool, error)

Validate validates value using the expr expression validation

Example (Compound)
ok, err := Validate("hello", "is_shellsafe(value) && value == 'hello'")
fmt.Printf("valid: %v, err: %v\n", ok, err)
Output:
valid: true, err: <nil>
Example (Duration)
ok, _ := Validate("1h30m", "is_duration(value)")
fmt.Printf("valid: %v\n", ok)

ok, _ = Validate("bob", "is_duration(value)")
fmt.Printf("valid: %v\n", ok)
Output:
valid: true
valid: false
Example (IpAddress)
ok, err := Validate("1.1.1.1", "is_ipv4(value)")
fmt.Printf("valid: %v, err: %v\n", ok, err)

ok, _ = Validate("bob", "is_ipv4(value)")
fmt.Printf("valid: %v\n", ok)
Output:
valid: true, err: <nil>
valid: false
Example (Numeric)
ok, _ := Validate("42", "is_int(value)")
fmt.Printf("is_int: %v\n", ok)

ok, _ = Validate("3.14", "is_float(value)")
fmt.Printf("is_float: %v\n", ok)
Output:
is_int: true
is_float: true
Example (Regex)
ok, err := Validate("hello123", `is_regex(value, "^[a-z]+[0-9]+$")`)
fmt.Printf("valid: %v, err: %v\n", ok, err)

ok, err = Validate("HELLO", `is_regex(value, "^[a-z]+$")`)
fmt.Printf("valid: %v, err: %v\n", ok, err)
Output:
valid: true, err: <nil>
valid: false, err: <nil>
Example (ShellSafe)
ok, err := Validate("safe-input", "is_shellsafe(value)")
fmt.Printf("valid: %v, err: %v\n", ok, err)

ok, _ = Validate("bad;input", "is_shellsafe(value)")
fmt.Printf("valid: %v\n", ok)
Output:
valid: true, err: <nil>
valid: false

Types

This section is empty.

Jump to

Keyboard shortcuts

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