I create this packages for use it as part of my future template with multi packages separeted of the principal template.
Its a simple validator for use in your projects who contains two principal interfaces and structs who implements it.
type Validator interface {
Struct(s EvaluableStruct) error
}
type ValidatorFunc func(args ...interface{}) error
type EvaluableStruct interface {
Validate(args ...interface{}) error
}
package main
import (
"fmt"
"github.com/solrac97gr/validator"
"github.com/solrac97gr/validations"
)
type User struct {
Name string
Age int
}
func (u *User) Validate(args ...interface{}) error {
err := validations.StringRequired(u)
if err != nil {
return err
}
err = validations.IntGratherThan(u.Age,18)
if err != nil {
return err
}
return nil
}
func main() {
user := &User{
Name: "Solrac",
Age: 17,
}
val := validator.New()
err := val.Struct(user)
if err != nil {
fmt.Println(err)
}
}
- Format
- String
- Slice
- Float
- Network
- Int
You must create a struct who implements the EvaluableStruct interface and add the Validate method who contains the logic for validate the struct.
The validator must be part of your dependencies in your depencency injection container.
This package is in development and is not ready for production.
MIT