validator

package module
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2026 License: MIT Imports: 24 Imported by: 0

README

golang-validator

Validate golang request data with simple rules. Inspired by Laravel's request validation.

validating a simple value
package main

import (
	"fmt"

	validator "github.com/trueifnotfalse/golang-validator"
	"github.com/trueifnotfalse/golang-validator/interface/rule"
)

func main() {
	data := `{
		"name": "John Doe",
		"age" : 104
	}`

	rules := map[string][]rule.Interface{
		"name": {
			validator.Required(), // not nil or empty
			validator.String(),   // string
			validator.Max(255),   // 255 characters length
		},
		"age": {validator.UInt8(), validator.Min(0), validator.Max(100)},
	}

	v := validator.New()
	errs := v.Validate([]byte(data), rules)
	for field, err := range errs {
		fmt.Printf("%s - %v\n", field, err)
	}
}
creating own rule
package main

import (
	"errors"
	"fmt"
	"strings"

	validator "github.com/trueifnotfalse/golang-validator"
	"github.com/trueifnotfalse/golang-validator/interface/locale"
	"github.com/trueifnotfalse/golang-validator/interface/rule"
	"github.com/trueifnotfalse/golang-validator/utils"
)

type MyRule struct{}

func (r *MyRule) SetLocale(v locale.Interface) rule.Interface {
	return r
}

func (r *MyRule) Valid(key string, values map[string]any) error {
	v, ok := values[key]
	if !ok {
		return nil
	}
	if !utils.IsString(v) {
		return nil
	}
	s := utils.ToString(v)
	if s == strings.ToTitle(s) {
		return nil
	}
	return errors.New("All words must be capitalized.")
}

func main() {
	data := `{
		"name": "john Doe",
		"age" : 104
	}`
	r := &MyRule{}
	rules := map[string][]rule.Interface{
		"name": {
			validator.Required(), // not nil or empty
			validator.String(),   // string
			validator.Max(255),   // 255 characters length
			r,
		},
		"age": {validator.UInt8(), validator.Min(0), validator.Max(100)},
	}

	v := validator.New()
	errs := v.Validate([]byte(data), rules)
	for field, err := range errs {
		fmt.Printf("%s - %v\n", field, err)
	}
}
creating own locale
package main

import (
	"fmt"

	validator "github.com/trueifnotfalse/golang-validator"
	"github.com/trueifnotfalse/golang-validator/interface/rule"
	"github.com/trueifnotfalse/golang-validator/locale"
)

func main() {
	data := `{
		"name": "John Doe",
		"age" : 104
	}`
	
	myLocale := locale.Locale{
		"max.numeric": "The age in field `%s` is to high must be not greater than %d.",
	}

	rules := map[string][]rule.Interface{
		"name": {
			validator.Required(), // not nil or empty
			validator.String(),   // string
			validator.Max(255),   // 255 characters length
		},
		"age": {validator.UInt8(), validator.Min(0), validator.Max(100)},
	}

	v := validator.New().SetLocale(myLocale)
	errs := v.Validate([]byte(data), rules)
	for field, err := range errs {
		fmt.Printf("%s - %v\n", field, err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Array

func Array() rule.Interface

func Boolean

func Boolean() rule.Interface

func Date

func Date(format string) rule.Interface

func Each

func Each(rules ...rule.Interface) rule.Interface

func Empty added in v0.2.0

func Empty() rule.Interface

func Float

func Float() rule.Interface

func GeoJSON added in v0.6.0

func GeoJSON(typeList ...string) rule.Interface

func GeoJSONFeatureCollection added in v0.6.0

func GeoJSONFeatureCollection() rule.Interface

func GeoJSONLineString added in v0.6.0

func GeoJSONLineString() rule.Interface

func GeoJSONMultiLineString added in v0.6.0

func GeoJSONMultiLineString() rule.Interface

func GeoJSONMultiPoint added in v0.6.0

func GeoJSONMultiPoint() rule.Interface

func GeoJSONMultiPolygon added in v0.6.0

func GeoJSONMultiPolygon() rule.Interface

func GeoJSONPoint added in v0.6.0

func GeoJSONPoint() rule.Interface

func GeoJSONPolygon added in v0.6.0

func GeoJSONPolygon() rule.Interface

func HttpUrl added in v0.4.0

func HttpUrl() rule.Interface

func In

func In[V int64 | int32 | int16 | int8 | uint64 | uint32 | uint16 | uint8 | string](v []V) rule.Interface

func Int8

func Int8() rule.Interface

func Int16

func Int16() rule.Interface

func Int32

func Int32() rule.Interface

func Int64

func Int64() rule.Interface

func IpV4 added in v0.4.0

func IpV4() rule.Interface

func Max added in v0.3.1

func Max[T int | float64](v T) rule.Interface

func Min added in v0.3.1

func Min[T int | float64](v T) rule.Interface

func NotEmpty added in v0.2.0

func NotEmpty() rule.Interface

func Nullable added in v0.5.0

func Nullable(rules ...rule.Interface) rule.Interface

func Object

func Object() rule.Interface

func Required

func Required() rule.Interface

func String

func String() rule.Interface

func UInt8

func UInt8() rule.Interface

func UInt16

func UInt16() rule.Interface

func UInt32

func UInt32() rule.Interface

func UInt64

func UInt64() rule.Interface

Types

type Errors added in v0.3.0

type Errors map[string][]error

type Rules

type Rules map[string][]rule.Interface

type Validator added in v0.7.0

type Validator struct {
	// contains filtered or unexported fields
}

func New added in v0.7.0

func New() *Validator

func (*Validator) Map added in v0.7.0

func (r *Validator) Map(values map[string]any, rules Rules) Errors

func (*Validator) SetLocale added in v0.7.0

func (r *Validator) SetLocale(v locale.Interface) *Validator

func (*Validator) Validate added in v0.7.0

func (r *Validator) Validate(body []byte, rules Rules) Errors

Jump to

Keyboard shortcuts

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