hvalid

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 4 Imported by: 0

README


hvalid

English 中文

hvalid is a lightweight validation library written in Go language. It provides a custom validator interface and a series of common validation functions to help developers quickly implement data validation.

Go Report Card GitHub go.mod Go version

Features

  • Generic support: Can validate any type of data, including basic types, structs, slices, etc.
  • Easy to use: Offers a concise API for developers to quickly perform parameter validation.
  • Extensible: Allows custom validation rules to meet different validation needs.
  • Friendly error messages: Returns clear error messages when validation fails, making it easy for developers to locate issues.

Installation

Install using the go get command:

go get github.com/lyonnee/hvalid
Usage Examples
Basic Type Validation
import (
	"errors"
	"github.com/lyonnee/hvalid"
)

func main() {
	// Validate string length
	err := hvalid.Validate[string]("hello", hvalid.MinLen[string](3))
	if err != nil {
		// Handle error
	}

	// Validate number range
	err = hvalid.Validate[int](10, hvalid.Min(5), hvalid.Max(15))
	if err != nil {
		// Handle error
	}
}
Struct Validation
type User struct {
	Name  string
	Email string
	Age   int
}

func UserValidator() hvalid.ValidatorFunc[User] {
	return hvalid.ValidatorFunc[User](func(user User) error {
		if user.Age < 18 {
			return errors.New("Age must be greater than 18")
		}

		return hvalid.Validate[string](user.Email, hvalid.Email())
	})
}

func main() {
	user := User{
		Name:  "Zhang San",
		Email: "zhangsan@example.com",
		Age:   20,
	}

	err := hvalid.Validate[User](user, UserValidator())
	if err != nil {
		// Handle error
	}
}
Custom Validation Rules
func IsPositive(errMsg ...string) hvalid.ValidatorFunc[int] {
	return hvalid.ValidatorFunc[int](func(num int) error {
		if num <= 0 {
			if len(errMsg) > 0 {
				return errors.New(errMsg[0])
			}
			return errors.New("The number must be positive")
		}
		return nil
	})
}

func main() {
	err := hvalid.Validate[int](10, IsPositive())
	if err != nil {
		// Handle error
	}
}

Testing

The project includes unit tests, run all tests with the go test command:

go test ./...

Contributing

Issues and pull requests are welcome to improve hvalid.

License

hvalid is released under the MIT License. See the LICENSE file for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get[T any](input any, validators ...Validator[T]) (T, error)

func Validate

func Validate[T any](field T, validators ...Validator[T]) error

Types

type Validator

type Validator[T any] interface {
	Validate(field T) error
}

type ValidatorFunc

type ValidatorFunc[T any] func(field T) error

func ContainsBytes

func ContainsBytes(subslice []byte) ValidatorFunc[[]byte]

func ContainsStr

func ContainsStr(substr string, errMsg ...string) ValidatorFunc[string]

func Email

func Email(errMsg ...string) ValidatorFunc[string]

func Eq

func Eq[T comparable](comparData T, errMsg ...string) ValidatorFunc[T]

func Max

func Max[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64](max T, errMsg ...string) ValidatorFunc[T]

func MaxLen

func MaxLen[T string | []byte](maxLen int, errMsg ...string) ValidatorFunc[T]

func Min

func Min[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | float32 | float64](min T, errMsg ...string) ValidatorFunc[T]

func MinLen

func MinLen[T string | []byte](minLen int, errMsg ...string) ValidatorFunc[T]

func Regexp

func Regexp(pattern string, errMsg ...string) ValidatorFunc[string]

func (ValidatorFunc[T]) Validate

func (fn ValidatorFunc[T]) Validate(field T) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL