validator

package
v0.17.0-rc.3 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2024 License: Apache-2.0 Imports: 9 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"
	// PrivateKeyPEMTag contains the rule to validate a private key.
	PrivateKeyPEMTag = "privateKeyPEM"
	CertPEMTag       = "certPEM"
)

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-zA-Z0-9_-]){1,64}$`).MatchString(field.Field().String())
		},
		Error: fmt.Errorf("the device name can only contain `_`, `-` and alpha numeric characters"),
	},

	{
		Tag: "api-key_name",
		Handler: func(field validator.FieldLevel) bool {
			name := field.Field().String()

			if len(name) < 3 || len(name) > 20 {
				return false
			}

			for _, c := range field.Field().String() {
				if unicode.IsSpace(c) {
					return false
				}
			}

			return true
		},
		Error: fmt.Errorf("name must contain at least 3 characters, at most 20 characters, and no whitespaces"),
	},

	{
		Tag: "api-key_expires-at",
		Handler: func(field validator.FieldLevel) bool {
			if !field.Field().CanInt() {
				return false
			}

			expiresAt := field.Field().Int()

			return expiresAt == -1 || expiresAt == 30 || expiresAt == 60 || expiresAt == 90 || expiresAt == 365
		},
		Error: fmt.Errorf("expires_at must be in [ -1 30 60 90 365 ]"),
	},

	{
		Tag: "member_role",
		Handler: func(field validator.FieldLevel) bool {
			return authorizer.RoleFromString(field.Field().String()) != authorizer.RoleInvalid
		},
		Error: fmt.Errorf("role must be \"owner\", \"administrator\", \"operator\" or \"observer\""),
	},
	{
		Tag: PrivateKeyPEMTag,
		Handler: func(field validator.FieldLevel) bool {
			block, _ := pem.Decode([]byte(field.Field().String()))
			if block == nil {
				return false
			}

			key, err := x509.ParsePKCS8PrivateKey(block.Bytes)

			return err == nil && key != nil
		},
		Error: fmt.Errorf("the private key is invalid"),
	},
	{
		Tag: CertPEMTag,
		Handler: func(field validator.FieldLevel) bool {
			block, _ := pem.Decode([]byte(field.Field().String()))
			if block == nil {
				return false
			}

			cert, err := x509.ParseCertificate(block.Bytes)

			return err == nil && cert != nil
		},
		Error: fmt.Errorf("the cert is invalid"),
	},
}

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) StructWithFields added in v0.15.0

func (v *Validator) StructWithFields(structure any) (bool, map[string]interface{}, error)

StructWithFields validades a structure using ShellHub validation's tags, returnig the invalid fields and its 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