Skip to content

go-universal/validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Validator

GitHub Tag Go Reference License Go Report Card Contributors Issues

validator is a Go package that provides a flexible and extensible validation framework with support for localization. It leverages the go-playground/validator package for validation and go-universal/i18n for localization.

Features

  • Custom validation rules
  • Localized error messages
  • Struct and variable validation
  • Support for various data types and formats

Installation

To install validator, use the following command:

go get github.com/go-universal/validator

Usage

Basic Usage

Here's a basic example of how to use validator:

package main

import (
    "fmt"
    "github.com/go-playground/validator/v10"
    "github.com/go-universal/i18n"
    "github.com/go-universal/validator"
    "golang.org/x/text/language"
)

func main() {
    // Initialize the validator
    v := validator.NewValidator(
        validator.New(),
        validator.WithTranslator(
            i18n.NewTranslator("en", language.English),
            "",
        ),
    )

    // Add custom validation rule
    v.AddValidation("is_valid", func(fl validator.FieldLevel) bool {
        return fl.Field().String() == "valid"
    })
    v.AddTranslation("en", "is_valid", "{field} must be valid")

    // Validate a variable
    err := v.Var("en", "my_field", "valid", "required,is_valid")
    if err.HasInternalError() {
        fmt.Println("Internal error:", err.InternalError())
    } else if err.HasValidationErrors() {
        fmt.Println("Validation errors:", err)
    } else {
        fmt.Println("Validation passed")
    }
}

Struct Validation

You can also validate structs with validator:

package main

import (
    "fmt"
    "github.com/go-playground/validator/v10"
    "github.com/go-universal/i18n"
    "github.com/go-universal/validator"
    "golang.org/x/text/language"
)

type TestStruct struct {
    Field string `validate:"required,is_valid"`
}

func main() {
    // Initialize the validator
    v := validator.NewValidator(
        validator.New(),
        validator.WithTranslator(
            i18n.NewTranslator("en", language.English),
            "",
        ),
    )

    // Add custom validation rule
    v.AddValidation("is_valid", func(fl validator.FieldLevel) bool {
        return fl.Field().String() == "valid"
    })
    v.AddTranslation("en", "is_valid", "{field} must be valid")

    // Validate a struct
    ts := TestStruct{Field: "valid"}
    err := v.Struct("en", ts)
    if err.HasInternalError() {
        fmt.Println("Internal error:", err.InternalError())
    } else if err.HasValidationErrors() {
        fmt.Println("Validation errors:", err)
    } else {
        fmt.Println("Validation passed")
    }
}

Custom Validators

You can add custom validators to validator:

package main

import (
    "fmt"
    "github.com/go-playground/validator/v10"
    "github.com/go-universal/i18n"
    "github.com/go-universal/validator"
    "golang.org/x/text/language"
)

func main() {
    // Initialize the validator
    v := validator.NewValidator(
        validator.New(),
        validator.WithTranslator(
            i18n.NewTranslator("en", language.English),
            "",
        ),
    )

    // Add custom validation rule
    v.AddValidation("is_valid", func(fl validator.FieldLevel) bool {
        return fl.Field().String() == "valid"
    })
    v.AddTranslation("en", "is_valid", "{field} must be valid")

    // Validate a variable
    err := v.Var("en", "my_field", "invalid", "required,is_valid")
    if err.HasInternalError() {
        fmt.Println("Internal error:", err.InternalError())
    } else if err.HasValidationErrors() {
        fmt.Println("Validation errors:", err)
    } else {
        fmt.Println("Validation passed")
    }
}

License

This project is licensed under the ISC License. See the LICENSE file for details.

Acknowledgements

About

💯 Translation-based validator for Go.

Topics

Resources

License

Stars

Watchers

Forks

Languages