validators

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2023 License: MPL-2.0 Imports: 5 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CheckTCPHighPort = models.Validator{
	Name: "Check if TCP port is high (between 1024 and 65535)",
	Func: func(val interface{}) error {
		var intVal int
		var err error
		var ok bool
		intVal, ok = val.(int)
		if !ok {
			stringValue, ok := val.(string)
			intVal, err = strconv.Atoi(stringValue)
			if !ok && err != nil {
				return fmt.Errorf("expected an int")
			}
		}
		if intVal >= 1024 && intVal <= 65535 {
			return nil
		}
		return fmt.Errorf("value (%d) is not a TCP high port ", intVal)
	},
}

CheckTCPHighPort validate that the port number is a high port

View Source
var StringNotEmpty = models.Validator{
	Name: "String not empty",
	Func: func(input interface{}) error {
		stringVal, ok := input.(string)
		if !ok {
			return fmt.Errorf("expected a string")
		}
		if strings.TrimSpace(stringVal) == "" {
			return fmt.Errorf("empty value is not allowed")
		}
		return nil
	},
}

StringNotEmpty validate if a string value is not empty

View Source
var URLValidator = models.Validator{
	Name: "Url Validator",
	Func: func(val interface{}) error {
		valString, ok := val.(string)
		if !ok {
			return fmt.Errorf("'%v' is not a even a string, can't be an url", val)
		}

		_, err := url.ParseRequestURI(valString)
		if err != nil {
			return fmt.Errorf("failed to parse %q as an url", valString)
		}
		return nil
	},
}

URLValidator validate that string provided is an url

Functions

func AuthorizedValues

func AuthorizedValues[T comparable](authorizedValues ...T) models.Validator

Return a Validator that check if the value received in the validation process is contained in the array of authorized values.

Types

This section is empty.

Jump to

Keyboard shortcuts

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