Documentation ¶
Index ¶
- func Validate(s any, data string) error
- type Validator
- func Email(errorMessages ...string) Validator
- func EndsNotWith(endsNotWith string, errorMessage ...string) Validator
- func EndsWith(endsWith string, errorMessage ...string) Validator
- func IsAlpha(errorMessage ...string) Validator
- func IsAlphaNum(errorMessage ...string) Validator
- func IsAlphaNumSpace(errorMessage ...string) Validator
- func IsAlphaSpace(errorMessage ...string) Validator
- func IsArray(validators []Validator, errorMessage ...string) Validator
- func IsBool(errorMessage ...string) Validator
- func IsFloat(errorMessage ...string) Validator
- func IsInt(errorMessage ...string) Validator
- func IsNullArray(validators []Validator, errorMessage ...string) Validator
- func IsNullBool(errorMessage ...string) Validator
- func IsNullFloat(errorMessage ...string) Validator
- func IsNullInt(errorMessage ...string) Validator
- func IsNullNumber(errorMessage ...string) Validator
- func IsNullString(errorMessage ...string) Validator
- func IsNumber(errorMessage ...string) Validator
- func IsString(errorMessage ...string) Validator
- func Length(length any, errorMessage ...string) Validator
- func Max(max any, errorMessage ...string) Validator
- func MaxLength(maxLength any, errorMessage ...string) Validator
- func Min(min any, errorMessage ...string) Validator
- func MinLength(minLength any, errorMessage ...string) Validator
- func OneOf(options any, errorMessage ...string) Validator
- func Password(errorMessage ...string) Validator
- func Regex(regex string, errorMessage string) Validator
- func Required(errorMessage ...string) Validator
- func StartsNotWith(startsNotWith string, errorMessage ...string) Validator
- func StartsWith(startWith string, errorMessage ...string) Validator
- func Url(errorMessage ...string) Validator
- func Uuid(version int, errorMessage ...string) Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Validate ¶
Validates the provided JSON based on the validators defined for each field.
Parameters:
- s (any): pointer to a struct.
- data (string): the JSON to be validated.
Usage examples:
type UserModel struct { Name string `json:"name" validates:"required;isString;minLength=3;maxLength=20"` Age int `json:"age" validates:"required;isInt;min=18;max=100"` Email string `json:"email" validates:"required;email"` } user := UserModel{} json := `{ "name": "Name", "age": 18, "email": "emailexample@gmail.com" }` // Success err := Validate(&user, json) // Output: nil if err != nil { fmt.Println(err) return } // Error json := `{ "name": "Name", "age": 16, "email": "emailexample@gmail.com" }` err := Validate(&user, json) // Output: {"age":["The minimum value is 18, but it received 16."]} if err != nil { fmt.Println(err) return }
Types ¶
type Validator ¶
A function that takes a value to be validated and returns an error message along with a stop indicator for further validations. If the validation is successful, the error message should be nil and the stop indicator should be false. Otherwise, an error message is returned along with a boolean indicating whether to stop subsequent validations or not.
func Email ¶
Checks if the value is a validated email.
Configuration parameters:
1. errorMessages (optional):
- Invalid email.
- value is not string.
Input value (string): value to be validated.
Usage examples:
value := "emailexample@gmail.com" v.Email()(value) // Output: nil, false value = "emailexample" v.Email()(value) // Output: [error message], false v.Email("error")(value) // Output: "error", false v.Email("", "error2")(nil) // Output: "error2", false
func EndsNotWith ¶ added in v0.1.1
Checks if the value does not end with a certain sequence.
Configuration parameters:
1. endsNotWith(string): character sequence that the value should not end with.
2. errorMessage (string): custom error message (optional).
Input value (string): value to be validated.
Usage examples:
value := "message" v.EndsNotWith("mes")(value) // Output: nil, false v.EndsNotWith("age")(value) // Output: [error message], false v.EndsNotWith("age", "error")(value) // Output: "error", false
func EndsWith ¶ added in v0.1.1
Checks whether the value ends with a given string.
Configuration parameters:
1. endsWith(string): character sequence that the value must start with.
2. errorMessage (string): custom error message (optional).
Input value (string): value to be validated.
Usage examples:
value := "message" v.EndsWith("age")(value) // Output: nil, false value := "send message" v.EndsWith("end")(value) // Output: [error message], false v.EndsWith("end", "error")(value) // Output: "error", false
func IsAlpha ¶
Checks if the value contains only letters.
Configuration parameters:
1. errorMessage (string): custom error message (optional).
Input value (string): value to be validated.
Usage examples:
value := "abcABC" v.IsAlpha()(value) // Output: nil, false value = "abcABC " v.IsAlpha()(value) // Output: [error message], false value = "abcABC0123!@" v.IsAlpha()(value) // Output: [error message], false
func IsAlphaNum ¶ added in v0.2.0
Checks if the value contains only letters and numbers.
Configuration parameters:
1. errorMessage (string): custom error message (optional).
Input value (string): value to be validated.
Usage examples:
value := "abcABC" v.IsAlpha()(value) // Output: nil, false value = "abcABC " v.IsAlpha()(value) // Output: [error message], false value = "abcABC012!@" v.IsAlpha()(value) // Output: [error message], false
func IsAlphaNumSpace ¶ added in v0.2.0
Checks if the value contains only letters, numbers and spaces.
Configuration parameters:
1. errorMessage (string): custom error message (optional).
Input value (string): value to be validated.
Usage examples:
value := "abcABC" v.IsAlphaNumSpace()(value) // Output: nil, false value = "abcABC " v.IsAlphaNumSpace()(value) // Output: nil, false value = "abcABC012!@" v.IsAlphaNumSpace()(value) // Output: [error message], false
func IsAlphaSpace ¶
Checks if the value contains only letters and spaces.
Configuration parameters:
1. errorMessage (string): custom error message (optional).
Input value (string): value to be validated.
Usage examples:
value := "abcABC" v.IsAlphaSpace()(value) // Output: nil, false value = "abcABC " v.IsAlphaSpace()(value) // Output: nil, false value = "abcABC0123!@" v.IsAlphaSpace()(value) // Output: [error message], false
func IsArray ¶
Checks if the value is a valid array.
Configuration parameters:
1. validators ([]Validator): validators that will be applied to each value in the array.
2. errorMessage (string): custom error message (optional).
Input value (slice | array): value to be validated.
Usage examples:
value1 := []string{""} IsArray( []Validator{ IsString(), }, )(value1) // Output: nil, false value2 := [1]string{""} IsArray( []Validator{ IsString(), }, )(value2) // Output: nil, false value3 := "" IsArray( []Validator{ IsString(), }, )(value3) // Output: [error message], true value4 := nil IsNullArray( []Validator{ IsString(), }, )(value4) // Output: [error message], true
func IsBool ¶
Checks if the value is a boolean.
Configuration parameters:
1. errorMessage (string): custom error message (optional).
Input value (bool): Value to be validated.
Usage examples:
value1 := true v.IsBool()(value1) // Output: nil, false value2 := nil v.IsBool()(value2) // Output: [error message], true value3 := 0 v.IsBool()(value3) // Output: [error message], true v.IsBool("error")(value3) // Output: "error", true
func IsFloat ¶
Checks if the value is a number or null.
Configuration parameters:
1. errorMessage (string): custom error message (optional).
Input value (float): value to be validated.
Usage examples:
value2 := 1.0 v.IsFloat()(value2) // Output: nil, false value1 := 1 v.IsFloat()(value1) // Output: [error message], true value3 := nil v.IsFloat()(value3) // Output: [error message], true value4 := "" v.IsFloat()(value4) // Output: [error message], true v.IsFloat("error")(value4) // Output: "error", true
func IsInt ¶
Checks if the value is a number or null.
Configuration parameters:
1. errorMessage (string): custom error message (optional).
Input value (int): value to be validated.
Usage examples:
value1 := 1 v.IsInt()(value1) // Output: nil, false value3 := nil v.IsInt()(value3) // Output: [error message], true value2 := 1.0 v.IsInt()(value2) // Output: [error message], true value4 := "" v.IsInt()(value4) // Output: [error message], true v.IsInt("error")(value4) // Output: "error", true
func IsNullArray ¶
Checks if the value is a valid array or null.
Configuration parameters:
1. validators ([]Validator): validators that will be applied to each value in the array.
2. errorMessage (string): custom error message (optional).
Input value (nil | slice | array): value to be validated.
Usage examples:
value1 := nil IsNullArray( []Validator{ IsString(), }, )(value1) // Output: nil, true value2 := []string{""} IsNullArray( []Validator{ IsString(), }, )(value2) // Output: nil, false value3 := [1]string{""} IsNullArray( []Validator{ IsString(), }, )(value3) // Output: nil, false value4 := "" IsNullArray( []Validator{ IsString(), }, )(value4) // Output: [error message], true
func IsNullBool ¶
Checks if the value is a boolean or null.
Configuration parameters:
1. errorMessage (string): custom error message (optional).
Input value (nil | bool): Value to be validated.
Usage examples:
value1 := true v.IsNullBool()(value1) // Output: nil, false value2 := nil v.IsNullBool()(value2) // Output: nil, true value3 := 0 v.IsNullBool()(value3) // Output: [error message], true v.IsNullBool("error")(value3) // Output: "error", true
func IsNullFloat ¶
Checks if the value is a number or null.
Configuration parameters:
1. errorMessage (string): custom error message (optional).
Input value (nil | float): value to be validated.
Usage examples:
value1 := nil v.IsNullFloat()(value1) // Output: nil, true value2 := 1.0 v.IsNullFloat()(value2) // Output: nil, false value3 := 1 v.IsNullFloat()(value3) // Output: [error message], true value4 := "" v.IsNullFloat()(value4) // Output: [error message], true v.IsNullFloat("error")(value4) // Output: "error", true
func IsNullInt ¶
Checks if the value is a number or null.
Configuration parameters:
1. errorMessage (string): custom error message (optional).
Input value (nil | int): value to be validated.
Usage examples:
value1 := nil v.IsNullInt()(value1) // Output: nil, true value2 := 1 v.IsNullInt()(value2) // Output: nil, false value3 := 1.0 v.IsNullInt()(value3) // Output: [error message], true value4 := "" v.IsNullInt()(value4) // Output: [error message], true v.IsNullInt("error")(value4) // Output: "error", true
func IsNullNumber ¶
Checks if the value is a number or null.
Configuration parameters:
1. errorMessage (string): custom error message (optional).
Input value (nil | number): value to be validated.
Usage examples:
value1 := nil v.IsNullNumber()(value1) // Output: nil, true value2 := 1 v.IsNullNumber()(value2) // Output: nil, false value3 := 1.0 v.IsNullNumber()(value3) // Output: nil, false value4 := "" v.IsNullNumber()(value4) // Output: [error message], true v.IsNullNumber("error")(value4) // Output: "error", true
func IsNullString ¶
Checks if the value is a string or null.
Configuration parameters:
- errorMessage (string): custom error message (optional).
Input value (nil | string): value to be validated.
Usage examples:
value1 := nil v.IsNullString()(value1) // Output: nil, true value2 := "Name" v.IsNullString()(value2) // Output: nil, false value3 := 0 v.IsNullString()(value3) // Output: [error message], true v.IsNullString("error")(value3) // Output: "error", true
func IsNumber ¶
Checks if the value is a number.
Configuration parameters:
1. errorMessage (string): custom error message (optional).
Input value (number): value to be validated.
Usage examples:
value1 := 1 v.IsNumber()(value1) // Output: nil, false value2 := 1.0 v.IsNumber()(value2) // Output: nil, false value3 := nil v.IsNumber()(value3) // Output: [error message], true value4 := "" v.IsNumber()(value4) // Output: [error message], true v.IsNumber("error")(value4) // Output: "error", true
func IsString ¶
Checks if the value is a string.
Configuration parameters:
1. errorMessage (string): custom error message (optional).
Input value (string): value to be validated.
Usage examples:
value1 := "Name" v.IsString()(value1) // Output: nil, false value2 := nil v.IsString()(value2) // Output: [error message], true value3 := 0 v.IsString()(value3) // Output: [error message], true v.IsString("error")(value3) // Output: "error", true
func Length ¶ added in v0.2.0
Checks if a string/slice/array has the specified length.
Configuration parameters:
1. length (int): length that the value must have.
2. errorMessage (string): custom error message (optional).
Input value (string | slice | array): value to be validated.
Usage examples:
String
value := "Name" v.Length(4)(value) // Output: nil, false v.Length(3)(value) // Output: [error message], false v.Length(3, "error")(value) // Output: "error", false
Slice
value := []string{"Name", "is"} v.Length(2)(value) // Output: nil, false v.Length(1)(value) // Output: [error message], false v.Length(1, "error")(value) // Output: "error", false
Array
value := [2]string{"Name", "is"} v.Length(2)(value) // Output: nil, false v.Length(1)(value) // Output: [error message], false v.Length(1, "error")(value) // Output: "error", false
func Max ¶
Checks if the value is greater than the specified maximum value.
Configuration parameters:
1. max(int | int32 | int64 | float32 | float64): maximum value that the value must have.
2. errorMessage (string): custom error message (optional).
Input value (int | int32 | int64 | float32 | float64): value to be validated.
Usage examples:
value := 3 v.Max(5)(value) // Output: nil, false value := 6 v.Max(5)(value) // Output: [error message], false v.Max(5, "error")(value) // Output: "error", false
func MaxLength ¶
Checks if a string/slice/array has the specified maximum length.
Configuration parameters:
1. maxLength (int): maximum length that the string must have.
2. errorMessage (string): custom error message (optional).
Input value (string | slice | array): value to be validated.
Usage examples:
String
value := "Name" v.MaxLength(5)(value) // Output: nil, false v.MaxLength(3)(value) // Output: [error message], false v.MaxLength(3, "error")(value) // Output: "error", false
Slice
value := []string{"Name", "is"} v.MaxLength(3)(value) // Output: nil, false v.MaxLength(1)(value) // Output: [error message], false v.MaxLength(1, "error")(value) // Output: "error", false
Array
value := [2]string{"Name", "is"} v.MaxLength(3)(value) // Output: nil, false v.MaxLength(1)(value) // Output: [error message], false v.MaxLength(1, "error")(value) // Output: "error", false
func Min ¶
Checks if the value is less than the specified minimum value.
Configuration parameters:
1. min(int | int32 | int64 | float32 | float64): minimum value that the value must have.
2. errorMessage (string): custom error message (optional).
Input value (int | int32 | int64 | float32 | float64): value to be validated.
Usage examples:
value := 6 v.Min(5)(value) // Output: nil, false value := 3 v.Min(5)(value) // Output: [error message], false v.Min(5, "error")(value) // Output: "error", false
func MinLength ¶
Checks if a string/slice/array has the specified minimum length.
Configuration parameters:
1. minLength (int): minimum length that the value must have.
2. errorMessage (string): custom error message (optional).
Input value (string | slice | array): value to be validated.
Usage examples:
String
value := "Name" v.MinLength(3)(value) // Output: nil, false v.MinLength(5)(value) // Output: [error message], false v.MinLength(5, "error")(value) // Output: "error", false
Slice
value := []string{"Name", "is"} v.MinLength(1)(value) // Output: nil, false v.MinLength(3)(value) // Output: [error message], false v.MinLength(3, "error")(value) // Output: "error", false
Array
value := [2]string{"Name", "is"} v.MinLength(1)(value) // Output: nil, false v.MinLength(3)(value) // Output: [error message], false v.MinLength(3, "error")(value) // Output: "error", false
func OneOf ¶ added in v0.1.1
Checks if the value is within certain options.
Configuration parameters:
1. options (slice | array): value options.
2. errorMessage (string): custom error message (optional).
Input value (string | int | float64): value to be validated.
Usage examples:
options := []string{"one", "two", "three"} value := "three" v.OneOf(options)(value) // Output: nil, false value = "four" v.OneOf(options)(value) // Output: [error message], false v.OneOf(options, "error")(value) // Output: "error", false
func Password ¶
Checks whether the value contains lowercase and uppercase letters, numbers and special characters.
Configuration parameters:
1. errorMessage (string): custom error message.
Input value (string): value to be validated.
Usage examples:
value := "abcABC0123!@" v.Password()(value) // Output: nil, false value = "abc" v.Password()(value) // Output: [error message], false v.Password("error")(value) // Output: "error", false
func Regex ¶
Checks if the value meets the given regex.
Configuration parameters:
1. regex (string): regex that will be used to validate value.
2. errorMessage (string): custom error message.
Input value (string): value to be validated.
Usage examples:
regex := "[A-Z]" errorMessage := "The value must be in capital letters" value := "ABC" v.Regex(regex, errorMessage)(value) // Output: nil, false value = "abc" v.Regex(regex, errorMessage)(value) // Output: The value must be in capital letters, false
func Required ¶ added in v0.2.0
Checks if the value was provided.
Configuration parameters:
1. errorMessage (string): custom error message (optional).
Input value (any): value to be validated.
Usage examples:
value1 := "Name" v.Required()(value1) // Output: nil, false value2 := nil v.Required()(value2) // Output: [error message], true
func StartsNotWith ¶ added in v0.1.1
Checks if the value does not start with a certain sequence.
Configuration parameters:
1. startWith(string): character sequence that the value should not start with.
2. errorMessage (string): custom error message (optional).
Input value (string): value to be validated.
Usage examples:
value := "message" v.StartsNotWith("es")(value) // Output: nil, false value := "send message" v.StartsNotWith("send")(value) // Output: [error message], false v.StartsNotWith("send", "error")(value) // Output: "error", false
func StartsWith ¶ added in v0.1.1
Checks if the value starts with a given sequence.
Configuration parameters:
1. startWith(string): character sequence that the value must start with.
2. errorMessage (string): custom error message (optional).
Input value (string): value to be validated.
Usage examples:
value := "message" v.StartsWith("mes")(value) // Output: nil, false value := "send message" v.StartsWith("end")(value) // Output: [error message], false v.StartsWith("end", "error")(value) // Output: "error", false
func Url ¶ added in v0.2.0
Checks if the value is a valid Url.
Configuration parameters:
1. errorMessage (string): custom error message.
Input value (string): value to be validated.
Usage examples:
value := "golang.org" v.Url()(value) // Output: nil, false value = "golang" v.Url()(value) // Output: [error message], false v.Url("error")(value) // Output: "error", false
func Uuid ¶ added in v0.2.0
Checks if the value is a valid UUID.
Configuration parameters:
1. version (int): UUID version.
2. errorMessage (string): custom error message.
Input value (string): value to be validated.
Usage examples:
value := "f47ac10b-58cc-4372-a567-0e02b2c3d479" v.Uuid(4)(value) // Output: nil, false v.Uuid(3)(value) // Output: [error message], false v.Uuid(3, "error")(value) // Output: "error", false value = "00" v.Uuid(3)(value) // Output: [error message], false v.Uuid(3, "error")(value) // Output: "error", false
Source Files ¶
- email_validator.go
- ends_not_with_validator.go
- ends_with_validator.go
- is_alpha_num_space_validator.go
- is_alpha_num_validator.go
- is_alpha_space_validator.go
- is_alpha_validator.go
- is_array_validator.go
- is_bool_validator.go
- is_float_validator.go
- is_int_validator.go
- is_null_array_validator.go
- is_null_bool_validator.go
- is_null_float_validator.go
- is_null_int_validator.go
- is_null_number_validator.go
- is_null_string_validator.go
- is_null_validator.go
- is_number_validator.go
- is_string_validator.go
- length.go
- main.go
- max_length_validator.go
- max_validator.go
- min_length_validator.go
- min_validator.go
- one_of_validator.go
- password_validator.go
- regex_validator.go
- required_validator.go
- starts_not_with_validator.go
- starts_with_validator.go
- url_validator.go
- utils.go
- uuid_validator.go
- validate.go