The fastest way to validate your Golang schemas

    Zius Schema Validator

    With custom messages and custom validation

    Getting Started

    Install zius using Go Modules:

    $go get github.com/vinibgoulart/zius

    Import the zius in your project:

    import (
     zius "github.com/vinibgoulart/zius"
    )

    Define zius in your Struct:

    type Simple struct {
     Name string `zius:"required:Name is required,string"`
    }

    Start to validate your structs:

    func main() {
     _, err := zius.Validate(Simple{ Name: "Vinicius" })
    }

    Validations

    TypeDescriptionTag ValueExample
    required
    The field is requiredfalserequired:Field is required
    int
    The field must be an integerfalseint:Field must be an integer
    string
    The field must be a stringfalsestring:Field must be a string
    number
    The field must be a numberfalsenumber:Field must be a number
    email
    The field must be an emailfalseemail:Field must be an email
    phone
    The field must be a phone numberfalsephone:Field must be a phone number
    regex
    The field must match the regex patterntrueregex={^[a-zA-Z]\d{1,2}$}:Field must match the pattern
    equals
    The field must be equal to another fieldtrueequals={BOY|GIRL}:Field must be equal to BOY or GIRL
    struct
    The field must be a structfalsestruct:Field must be a struct
    array
    The field must be an arrayfalsearray:Field must be an array

    Custom Messages

    Define custom messages for each field in your struct

    type Simple struct {
     Name string `zius:"required:Name is required,string"`
     Age string `zius:"required:Age is required,int"`
    }

    Handling Errors

    You can handle the errors returned by the validation function, you can handle all errors at once or each one individually.

    func main() {
     errors, err := zius.Validate(Simple{ Name: "Vinicius" })
    }

    The Validate function returns an array of errors and an error, you can check if the error is not nil and handle it.

    The error contains the following fields:

    type Error struct {
     Struct string
     Field string
     Error error
    }

    Examples

    Check out some examples of how to use Zius our the repository. Zius Repository.

    Feel free to contribute to the repository, suggest new features, or ask for help in the repository issues.