Available Validators
This package includes several predefined validators:
Blank
: Ensures the value is blank (""
ornil
).NotBlank
: Ensures the value is not blank.Nil
: Ensures the value isnil
.NotNil
: Ensures the value is notnil
.IsTrue
: Ensures the value istrue
.IsString
: Ensures the value isstring
.IsFloat
: Ensures the value isfloat
.IsFloat32
: Ensures the value isfloat32
.IsFloat64
: Ensures the value isfloat64
.IsInt
: Ensures the value isint
.IsInt8
: Ensures the value isint8
.IsInt16
: Ensures the value isint16
.IsInt32
: Ensures the value isint32
.IsInt64
: Ensures the value isint64
.IsUint
: Ensures the value isuint
.IsUint8
: Ensures the value isuint8
.IsUint16
: Ensures the value isuint16
.IsUint32
: Ensures the value isuint32
.IsUint64
: Ensures the value isuint64
.IsStruct
: Ensures the value isstruct
.IsFalse
: Ensures the value isfalse
.IsBool
: Ensures the value is a boolean (true
orfalse
).Struct
: Ensures the value isstruct
of type ``. (Example)Currency
: Ensures the value is a valid currency code.Email
: Ensures the value is a valid email address.Language
: Ensures the value is a valid language code without a region (e.g.,en
).LanguageWithRegion
: Ensures the value is a valid language code with a region (e.g.,en-US
).Range
: Ensures the value is within a defined range (e.g., a number betweenmin
andmax
). It support int and float. (Example)ArrayRange
: Ensures the value is within a defined array range (e.g., a number betweenmin
andmax
). It support int and float. (Example)EqualTo
: Ensures the value is equal to.GreaterOrEqual
: Ensures the value is greater or equal. (support int, float)GreaterThen
: Ensures the value is greater. (support int, float)LessOrEqual
: Ensures the value is less or equal. (support int, float)LessThen
: Ensures the value is less then. (support int, float)Options
: Ensures the value is within a defined array. (Example)ArrayOptions
: Ensures the values (array) is within a defined array. (Example)Required
: Ensures the value is not blank ornil
.RegularExpression
: Ensures the value match regular expression. (Example)Date
: Ensures the value is valid date. (Example)Url
: Ensures the value is valid url. (Example)PhoneNumber
: Ensures the value is valid phone number (with option format). (Example)Length
: Ensure the value has length. (Example)Field
: Allow you to validate nested structures, such asmap[string]any
, where each field can have its own constraints. (Example)Composite
: Allow you to combine multiple constraints into one, symplifying validation logic when a single field needs to meet multiple criteria. (Example)Country
: Ensures the value is valid (one of name, alpha-2 (2 letters code), alpha-3 (3 letters code)) by ISO 3166-1.
Examples
Struct
con := constraints.Struct{
Struct: MyStruct{},
}
errs := validator.Validate(value, con)
RegularExpression
regExp := `^[a-z]{3}$`
con := constraints.RegularExpression{
Regexp: regExp,
}
errs := validator.Validate(value, con)
Range
con := constrains.Range{
Min: 5,
Max: 20,
}
value := 7
errs := validator.Validate(value, con)
ArrayRange
con := constrains.ArrayRange{
Min: 5,
Max: 20,
}
value := []any{5, 15, 19}
errs := validator.Validate(value, con)
Options
con := constraints.Options{
Options: []any{"option1", "option2", "option3", 42},
}
value := "option1"
errs := validator.Validate(value, con)
ArrayOptions
con := constraints.ArrayOptions{
Options: []any{"Red", "Green", "Blue", 100},
}
values := []any{"Red", "Green", 100}
errs := validator.Validate(values, con)
Date
dateFormat := "2006-01-02"
minDate, _ := time.Parse(dateFormat, "2023-01-01")
maxDate, _ := time.Parse(dateFormat, "2023-12-31")
con := constraints.Date{
Min: minDate,
Max: maxDate,
Format: dateFormat,
}
value := "01-05-2023"
errs := validator.Validate(value, con)
Url
v := constraints.Url{
AllowEmpty: false,
AllowedSchemes: []string{"http", "https"},
}
value := "https://example.com"
errs := validator.Validate(value, v)
Length
Map
v := constraints.Length{
Max: 4,
}
value := map[string]int{"a": 1, "b": 2, "c": 3}
errs := validator.Validate(value, v)
String
v := constraints.Length{
Max: 4,
}
value := "hello"
errs := validator.Validate(value, v)
### Phone number
```go
v := constraints.PhoneNumber{Format: "(00420) 000 000 000"}
value := "(00420) 123 456 789"
errs := validator.Validate(value, v)
Field
nestedValidator := constraints.Field{
Constraints: map[string]validator.Constraint{
"Name": constraints.NotBlank{},
"Email": constraints.Email{},
"Address": constraints.Field{
Constraints: map[string]validator.Constraint{
"City": constraints.NotBlank{},
"Country": constraints.NotBlank{},
},
},
},
}
errs := validator.Validate(value, nestedValidator)
Composite
nestedValidator := constraints.Field{
Constraints: map[string]validator.Constraint{
"Email": constraints.Composite{
Constraints: []validator.Constraint{
constraints.NotBlank{},
constraints.Length{Min: 5, Max: 50},
constraints.Email{},
},
},
"Name": constraints.Composite{
Constraints: []validator.Constraint{
constraints.NotBlank{},
constraints.Length{Min: 3, Max: 100},
},
},
},
}
errs := validator.Validate(value, nestedValidator)
Custom Validator
If you want to add your own validator, simply implement the Constraint
interface and write the Validate
method to perform the validation.
package constraints
import "errors"
type GreaterThan100 struct{}
func (c GreaterThan100) Validate(value any) error {
num, ok := value.(int)
if !ok {
return errors.New("this value must be an integer")
}
if num <= 100 {
return errors.New("this value must be greater than 100")
}
return nil
}