Giter VIP home page Giter VIP logo

hvalid's Introduction


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 codecov

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: "[email protected]",
		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.

hvalid's People

Contributors

lyonnee avatar

Stargazers

 avatar ccoVeille avatar Clayton Kehoe avatar  avatar  avatar Niko avatar 无言 avatar Freak avatar Cloud avatar

Watchers

ccoVeille avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.