Categorygithub.com/lufia/go-validator
modulepackage
0.0.0-20241008012400-b1b2d59bb18c
Repository: https://github.com/lufia/go-validator.git
Documentation: pkg.go.dev

# README

go-validator

Yet another validator written in Go.

GoDev Actions Status Coverage Status

Features

  • strongly-typed validators by type parameters
  • i18n support in the standard way
  • handling multiple validation errors

Built-in validators

  • Required: validates comparable types if the value is not zero-value.
  • Length: validates strings if the length of the value is within the range.
  • MinLength: see Length.
  • MaxLength: see Length.
  • InRange: validates ordered types if the value is within the range.
  • Min: see InRange.
  • Max: see InRange.
  • In: validates comparable types if the value is in valid values.
  • Pattern: validates strings if it matches the regular expression.

Supported languages

  • English
  • Japanese

Example

import (
	"context"
	"fmt"

	"github.com/lufia/go-validator"
)

type OIDCProvider int

const (
	Google OIDCProvider = iota + 1
	Apple
	GitHub
)

type CreateUserRequest struct {
	Name     string
	Provider OIDCProvider
	Theme    string
}

var createUserRequestValidator = validator.Struct(func(s validator.StructRule, r *CreateUserRequest) {
	validator.AddField(s, &r.Name, "name", validator.Length[string](5, 20))
	validator.AddField(s, &r.Provider, "provider", validator.In(Google, Apple, GitHub))
	validator.AddField(s, &r.Theme, "theme", validator.In("light", "dark"))
})

func main() {
	var r CreateUserRequest
	err := createUserRequestValidator.Validate(context.Background(), &r)
	fmt.Println(err)
}

For more details, see the documentation.

# Functions

AddField adds the p's field of the struct T.
No description provided by the author
In returns the validator to verify the value is in a.
InRange returns the validator to verify the value is within min and max.
Join bundles vs to a validator.
Length returns the validator to verify the length of the value is within min and max.
Max returns the validator to verify the value is less or equal than n.
MaxLength returns the validator to verify the length of the value is less or equal than n.
Min returns the validator to verify the value is greater or equal than n.
MinLength returns the validator to verify the length of the value is greater or equal than n.
New returns the validator to verify the value with fn.
Pattern returns the validator to verify the value matches re.
PatternString returns the validator to verify the value matches pattern.
Required returns the validator to verify the value is not zero value.
No description provided by the author
Struct returns the validator to verify that the struct satisfies rules constrated with build.
No description provided by the author

# Variables

No description provided by the author

# Structs

OrderedMap is a map that guarantee that the iteration order of entries will be the order in which they were set.
SliceError reports an error is caused in Slice validator.
StructError reports an error is caused in Struct validator.

# Interfaces

No description provided by the author
Error is the interface that wraps Error method.
Printer is the interface that wraps Fprintf method.
StructRule is the interface to add its fields.
Validator is the interface that wraps the basic Validate method.