validator

package
v0.14.3-rc.1 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: Apache-2.0 Imports: 5 Imported by: 3

Documentation

Index

Constants

View Source
const (
	// RegexpTag indicates that the regexp must be valide.
	RegexpTag = "regexp"
	// NameTag contains the rule to validate the user's name.
	NameTag = "name"
	// UserNameTag contains the rule to validate the user's username.
	UserNameTag = "username"
	// UserPasswordTag contains the rule to validate the user's password.
	UserPasswordTag = "password"
	// DeviceNameTag contains the rule to validate the device's name.
	DeviceNameTag = "device_name"
)

Variables

View Source
var (
	ErrStructureInvalid = errors.New("invalid structure")
	ErrVarInvalid       = errors.New("invalid var")
)
View Source
var Rules = []Rule{
	{
		Tag: RegexpTag,
		Handler: func(field validator.FieldLevel) bool {
			_, err := regexp.Compile(field.Field().String())

			return err == nil
		},
		Error: fmt.Errorf("the regexp is invalid"),
	},
	{
		Tag: NameTag,
		Handler: func(field validator.FieldLevel) bool {
			return regexp.MustCompile(`^(.){1,64}$`).MatchString(field.Field().String())
		},
		Error: fmt.Errorf("the name must be between 1 and 64 characters"),
	},
	{
		Tag: UserNameTag,
		Handler: func(field validator.FieldLevel) bool {
			return regexp.MustCompile(`^([a-z0-9-_.@]){3,32}$`).MatchString(field.Field().String())
		},
		Error: fmt.Errorf("the username must be between 3 and 32 characters, and can only contain letters, numbers, and the following characters: -_.@"),
	},
	{
		Tag: UserPasswordTag,
		Handler: func(field validator.FieldLevel) bool {
			return regexp.MustCompile(`^(.){5,32}$`).MatchString(field.Field().String())
		},
		Error: fmt.Errorf("the password cannot be empty and must be between 5 and 32 characters"),
	},
	{
		Tag: DeviceNameTag,
		Handler: func(field validator.FieldLevel) bool {
			return regexp.MustCompile(`^([a-z0-9_-]){1,64}$`).MatchString(field.Field().String())
		},
		Error: fmt.Errorf("the device name can only contain `_`, `-` and alpha numeric characters"),
	},
}

Rules is a slice that contains all validation rules.

Functions

This section is empty.

Types

type Rule added in v0.11.8

type Rule struct {
	Tag     string
	Handler func(field validator.FieldLevel) bool
	Error   error
}

Rule is a struct that contains a validation rule.

type Tag added in v0.14.0

type Tag string

Tag is the rule used to validate a variable or a structure's field.

func GetTagFromStructure added in v0.14.0

func GetTagFromStructure(structure any, field string) (Tag, bool)

GetTagFromStructure returns the validation's tag from structure.

type Validator added in v0.10.9

type Validator struct {
	Validate *validator.Validate
}

Validator is the ShellHub validator. It uses the go-playground/validator package internally and add custom validation rules for ShellHub types.

func New added in v0.10.9

func New() *Validator

New creates a new ShellHub validator.

The ShellHub validator contains custom validation rules for ShellHub types.

func (*Validator) Struct added in v0.10.9

func (v *Validator) Struct(structure any) (bool, error)

Struct validates a structure using ShellHub validation's tags.

func (*Validator) Var added in v0.10.9

func (v *Validator) Var(value any, tag Tag) (bool, error)

Var validates a variable using a ShellHub validation's tags.

Jump to

Keyboard shortcuts

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