Documentation ¶
Index ¶
- Constants
- Variables
- func FormatUser(user *models.User)
- func GetFirstFieldError(err error) error
- func GetInstance() *validator.Validate
- func GetInvalidFieldValueFromErr(err error) (map[string]string, error)
- func GetInvalidFieldsFromErr(structure interface{}, err error) (map[string]string, error)
- func GetInvalidFieldsValues(err error) (map[string]interface{}, error)
- func GetInvalidValuesFromErr(err error) ([]string, error)
- func HashPassword(password string) string
- func IsEmail(input string) bool
- func ValidateField(structure interface{}, field, value string) bool
- func ValidateFieldEmail(email string) bool
- func ValidateFieldPassword(password string) bool
- func ValidateFieldTag(tag string) bool
- func ValidateFieldUsername(username string) bool
- func ValidateStruct(data interface{}) ([]string, error)
- func ValidateStructFields(data interface{}) (map[string]interface{}, error)
- func ValidateVar(data interface{}, tag string) ([]string, error)
- type Rule
- type Validator
Constants ¶
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 ¶
var ( ErrInvalidFields = errors.New("invalid fields") ErrInvalidError = errors.New("this error is not from a field validation") )
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
FormatUser apply some formation rules to a models.User and encrypt the password.
func GetFirstFieldError ¶ added in v0.11.8
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
GetInvalidFieldValueFromErr gets the invalid field value from an error returned by Struct function.
func GetInvalidFieldsFromErr ¶ added in v0.10.9
GetInvalidFieldsFromErr gets the invalids fields from an error returned by structure validation function.
The returned "map"'s key is the value of JSON tag and its value is the value of the tag "validate" from the structure for each invalid field from the error.
The error should be from validator.ValidationErrors.
func GetInvalidFieldsValues ¶ added in v0.9.2
GetInvalidFieldsValues receive a structure validation error and return a map with invalid fields and values.
func GetInvalidValuesFromErr ¶ added in v0.11.8
GetInvalidValuesFromErr gets the invalids values from an error returned by Struct function.
func HashPassword ¶ added in v0.8.0
func ValidateField ¶ added in v0.9.1
ValidateField validates if a structure's field is valid.
func ValidateFieldEmail ¶ added in v0.9.0
ValidateFieldEmail validate the data for the field Email from structure models.UserData.
func ValidateFieldPassword ¶ added in v0.9.0
ValidateFieldPassword validate the data for the field Password from structure models.UserPassword.
func ValidateFieldTag ¶ added in v0.9.0
ValidateFieldTag validate the data for the field Tag from structure models.Device.
func ValidateFieldUsername ¶ added in v0.9.0
ValidateFieldUsername validate the data for the field Username from structure models.UserData.
func ValidateStruct ¶ added in v0.7.3
func ValidateStructFields ¶ added in v0.9.2
func ValidateVar ¶ added in v0.7.3
Types ¶
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.