Categorygithub.com/RussellLuo/validating/v3
modulepackage
3.0.0
Repository: https://github.com/russellluo/validating.git
Documentation: pkg.go.dev

# README

validating

A Go library for validating structs, maps and slices.

Features

  1. Simple

    Simple and stupid, no magic involved.

  2. Type-safe

    Schema is defined in Go, which is type-safer (and more powerful) than traditional struct tags.

  3. Flexible

    • Validators are composable.
    • Nested struct validation is well supported.
    • Schema can be defined inside or outside struct.
    • Validator customizations are made easy.
  4. No reflection

Installation

$ go get github.com/RussellLuo/validating/v3@latest

Quick Start

package main

import (
	"fmt"

	v "github.com/RussellLuo/validating/v3"
)

type Address struct {
	Country string
	City    string
}

func (a Address) Schema() v.Schema {
	return v.Schema{
		v.F("country", a.Country): v.Nonzero[string]().Msg("empty country"),
		v.F("city", a.City):       v.In("A", "B", "C").Msg("must be A or B or C"),
	}
}

type Person struct {
	Name    string
	Age     int
	Address Address
}

func (p Person) Schema() v.Schema {
	return v.Schema{
		v.F("name", p.Name):       v.LenString(1, 5).Msg("bad name"),
		v.F("age", p.Age):         v.Gte(10).Msg("must be older than 10 years old"),
		v.F("address", p.Address): p.Address.Schema(),
	}
}

func main() {
	p := Person{}
	errs := v.Validate(p.Schema())
	for _, err := range errs {
		fmt.Println(err)
	}
}
$ go run main.go
name: INVALID(bad name)
age: INVALID(must be older than 10 years old)
address.country: INVALID(empty country)
address.city: INVALID(must be A or B or C)

Validator factories and validators

To be strict, this library has a conceptual distinction between validator factory and validator.

A validator factory is a function used to create a validator, which will do the actual validation.

Built-in validator factories

Extension validator factories

Validator customizations

Examples

Documentation

Check out the Godoc.

Thanks

This library borrows some ideas from the following libraries:

License

MIT

# Functions

All is a composite validator factory used to create a validator, which will succeed only when all sub-validators succeed.
Any is a composite validator factory used to create a validator, which will succeed as long as any sub-validator succeeds.
Array is an alias of Slice.
EachMap is a composite validator factory used to create a validator, which will apply the given validator to each element (i.e.
EachSlice is a composite validator factory used to create a validator, which will apply the given validator to each element of the slice field.
Eq is a leaf validator factory used to create a validator, which will succeed when the field's value equals the given value.
F is a shortcut for creating a pointer to Field.
Gt is a leaf validator factory used to create a validator, which will succeed when the field's value is greater than the given value.
Gte is a leaf validator factory used to create a validator, which will succeed when the field's value is greater than or equal to the given value.
In is a leaf validator factory used to create a validator, which will succeed when the field's value is equal to one of the given values.
Is is a leaf validator factory used to create a validator, which will succeed when the predicate function f returns true for the field's value.
LenSlice is a leaf validator factory used to create a validator, which will succeed when the length of the slice field is between min and max.
LenString is a leaf validator factory used to create a validator, which will succeed when the length of the string field is between min and max.
Lt is a leaf validator factory used to create a validator, which will succeed when the field's value is lower than the given value.
Lte is a leaf validator factory used to create a validator, which will succeed when the field's value is lower than or equal to the given value.
Map is a composite validator factory used to create a validator, which will do the validation per the schemas associated with a map.
Match is a leaf validator factory used to create a validator, which will succeed when the field's value matches the given regular expression.
Ne is a leaf validator factory used to create a validator, which will succeed when the field's value does not equal the given value.
Nested is a composite validator factory used to create a validator, which will delegate the actual validation to the validator returned by f.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Nin is a leaf validator factory used to create a validator, which will succeed when the field's value is not equal to any of the given values.
Nonzero is a leaf validator factory used to create a validator, which will succeed when the field's value is nonzero.
Not is a composite validator factory used to create a validator, which will succeed when the given validator fails.
Range is a leaf validator factory used to create a validator, which will succeed when the field's value is between min and max.
RuneCount is a leaf validator factory used to create a validator, which will succeed when the number of runes in the field's value is between min and max.
Slice is a composite validator factory used to create a validator, which will do the validation per the schemas associated with a slice.
Validate invokes v.Validate with an empty field.
Value is a shortcut function used to create a schema for a simple value.
Zero is a leaf validator factory used to create a validator, which will succeed when the field's value is zero.
ZeroOr is a composite validator factory used to create a validator, which will succeed if the field's value is zero, or if the given validator succeeds.

# Constants

No description provided by the author
No description provided by the author

# Variables

And is an alias of All.
Or is an alias of Any.

# Structs

AnyValidator is a validator that allows users to change the returned errors by calling LastError().
Field represents a (Name, Value) pair that needs to be validated.
MessageValidator is a validator that allows users to customize the INVALID error message by calling Msg().

# Interfaces

No description provided by the author
Validator is an interface for representing a validating's validator.

# Type aliases

No description provided by the author
Func is an adapter to allow the use of ordinary functions as validators.
Schema is a field mapping, which defines the corresponding validator for each field.