The package provides a set of validation utilities to check various conditions in data fields. These validators can be used to ensure data integrity and enforce specific requirements in your Go applications, making use only of native libraries to perform the validations.
The aim is to offer a simple and flexible way to perform data validation in Go applications in a customizable manner. By providing a set of validators, developers can easily incorporate data validation logic into their applications to ensure data consistency and meet specific validation requirements, without the need to write validation code from scratch, customizing validations according to the project's needs.
To use the package in your Go projects, type the following command in your terminal:
go get github.com/dariomatias-dev/go-validadores
Validators should be organized following the following order: presence validator, type validator, and value validators. They should follow this order because otherwise, an error may occur if the sent value is not accepted by a validator that is placed later, even if it is a valid value.
This organization ensures that basic requirements, such as presence and type, are validated first before more specific validations about the value itself. By validating in this order, we can detect any potential errors early in the process, leading to a more robust and error-free validation system.
Just as there are no reasons to check if the value is of a specific type in value validators, which require the sent value to be of a certain type, as there are dedicated validators for this purpose, thus reducing the number of checks, making the validation process more efficient.
To use the package, first import it into your project with the following import statement:
import "github.com/dariomatias-dev/go-validators"
I advise you to give it an alias to make package usage simpler and more efficient, like this:
import v "github.com/dariomatias-dev/go-validators"
The validators have been created based on configurable functions, where the first set of parameters within the first pair of parentheses is used to configure the behavior of the validator, while the second set of parentheses receives the value to be validated. In the table of validators-available, it's referenced which validators require which value to be provided in order to perform validation.
To use the validations, use v.
followed by the desired validation. In the first parenthesis, provide what is being requested, and if you don't want the default error message, insert the desired message afterwards.
The validators will return two values: the first will be the error message if the provided value did not pass validation, and the second will be a boolean value indicating whether the validations should be halted or not. The second value is used in situations where, if the value did not pass the validator, subsequent validations cannot be executed because they will result in an error.
Validations can be performed in three distinct ways: individually, or within a json.
A single validator is applied to a specific value.
Examples:
// Success
value := 4
err, _ := v.Min(3)(value) // nil, false
if err != nil {
fmt.Println(err)
return
}
// Error
value = 2
err, _ = v.Min(3)(value) // The minimum value is 3, but it received 2, false
if err != nil {
fmt.Println(err)
return
}
Validates the provided JSON based on the validators defined for each field, returning an error if there is an inconsistency, or assigning the values from the JSON to the provided struct if there are no errors.
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": "[email protected]"
}`
// Success
err := Validate(&user, json) // Output: nil
if err != nil {
fmt.Println(err)
return
}
// Error
json := `{
"name": "Name",
"age": 16,
"email": "[email protected]"
}`
err := Validate(&user, json) // Output: {"age":["The minimum value is 18, but it received 16."]}
if err != nil {
fmt.Println(err)
return
}
Validator | Type | Input | Value Type |
---|---|---|---|
Required | Presence |
|
any |
IsString | Type |
|
string |
IsNumber | Type |
|
int | float |
IsInt | Type |
|
int |
IsFloat | Type |
|
float |
IsBool | Type |
|
bool |
IsArray | Type |
|
slice | array |
IsNullString | Type |
|
nil | string |
IsNullNumber | Type |
|
nil | int | float |
IsNullInt | Type |
|
nil | int |
IsNullFloat | Type |
|
nil | float |
IsNullBool | Type |
|
nil | bool |
IsNullArray | Type |
|
nil | slice | array |
Value |
|
string | |
Password | Value |
|
string |
Min | Value |
|
int | int32 | int64 | float32 | float64 |
Max | Value |
|
int | int32 | int64 | float32 | float64 |
Length | Value |
|
string | slice | array |
MinLength | Value |
|
string | slice | array |
MaxLength | Value |
|
string | slice | array |
IsAlpha | Value |
|
string |
IsAlphaSpace | Value |
|
string |
StartsWith | Value |
|
string |
StartsNotWith | Value |
|
string |
EndsWith | Value |
|
string |
EndsNotWith | Value |
|
string |
Regex | Value |
|
string |
Url | Value |
|
string |
OneOf | Value |
|
string |
Custom | Value |
|
any |
Help maintain the project with donations.