validator

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: May 12, 2023 License: Apache-2.0 Imports: 9 Imported by: 3

Documentation

Index

Constants

View Source
const (
	// TagRegexp is the tag used to validate a regexp.
	TagRegexp = "regexp"
	// TagUsername is the tag used to validate ShellHub's username.
	TagUsername = "username"
)

Variables

View Source
var (
	ErrInvalidFields = errors.New("invalid fields")
	ErrInvalidError  = errors.New("this error is not from a field validation")
)
View Source
var Rules = []Rule{
	{
		Tag: "regexp",
		Handler: func(field validator.FieldLevel) bool {
			_, err := regexp.Compile(field.Field().String())

			return err == nil
		},
		Error: fmt.Errorf("the regexp is invalid"),
	},
	{
		Tag: "username",
		Handler: func(field validator.FieldLevel) bool {
			return regexp.MustCompile(`^([a-zA-Z0-9-_.@]){3,30}$`).MatchString(field.Field().String())
		},
		Error: fmt.Errorf("the username must be between 3 and 30 characters, and can only contain letters, numbers, and the following characters: -_.@"),
	},
	{
		Tag: "password",
		Handler: func(field validator.FieldLevel) bool {
			return regexp.MustCompile(`^(.){5,30}$`).MatchString(field.Field().String())
		},
		Error: fmt.Errorf("the password cannot be empty and must be between 5 and 30 characters"),
	},
}

Rules is a slice that contains all validation rules.

Functions

func FormatUser added in v0.8.0

func FormatUser(user *models.User)

FormatUser apply some formation rules to a models.User and encrypt the password.

func GetFirstFieldError added in v0.11.8

func GetFirstFieldError(err error) error

GetFirstFieldError gets the first invalid field error from an error returned by Struct function.

func GetInstance added in v0.10.9

func GetInstance() *validator.Validate

func GetInvalidFieldValueFromErr added in v0.11.8

func GetInvalidFieldValueFromErr(err error) (map[string]string, error)

GetInvalidFieldValueFromErr gets the invalid field value from an error returned by Struct function.

func GetInvalidFieldsFromErr added in v0.10.9

func GetInvalidFieldsFromErr(err error) ([]string, error)

GetInvalidFieldsFromErr gets the invalids fields from an error returned by Struct function. If the error is not from a field validation, it returns an error. Otherwise, it returns the invalid fields.

func GetInvalidFieldsValues added in v0.9.2

func GetInvalidFieldsValues(err error) (map[string]interface{}, error)

GetInvalidFieldsValues receive a structure validation error and return a map with invalid fields and values.

func GetInvalidValuesFromErr added in v0.11.8

func GetInvalidValuesFromErr(err error) ([]string, error)

GetInvalidValuesFromErr gets the invalids values from an error returned by Struct function.

func HashPassword added in v0.8.0

func HashPassword(password string) string

func ValidateField added in v0.9.1

func ValidateField(structure interface{}, field, value string) bool

ValidateField validates if a structure's field is valid.

func ValidateFieldEmail added in v0.9.0

func ValidateFieldEmail(email string) bool

ValidateFieldEmail validate the data for the field Email from structure models.UserData.

func ValidateFieldPassword added in v0.9.0

func ValidateFieldPassword(password string) bool

ValidateFieldPassword validate the data for the field Password from structure models.UserPassword.

func ValidateFieldTag added in v0.9.0

func ValidateFieldTag(tag string) bool

ValidateFieldTag validate the data for the field Tag from structure models.Device.

func ValidateFieldUsername added in v0.9.0

func ValidateFieldUsername(username string) bool

ValidateFieldUsername validate the data for the field Username from structure models.UserData.

func ValidateStruct added in v0.7.3

func ValidateStruct(data interface{}) ([]string, error)

func ValidateStructFields added in v0.9.2

func ValidateStructFields(data interface{}) (map[string]interface{}, error)

func ValidateVar added in v0.7.3

func ValidateVar(data interface{}, tag string) ([]string, error)

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 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 interface{}) (bool, error)

Struct validates a structure using ShellHub validation's tags.

func (*Validator) Var added in v0.10.9

func (v *Validator) Var(value, tag string) (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