Skip to content

go-the-way/validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

validator

A lightweight model validator written in Go.

CircleCI GitHub go.mod Go version codecov Go Report Card GoDoc Release Mentioned in Awesome Go

Features

  • Supports multiple language.

quickstart

package main

import (
	"fmt"
	v "github.com/go-the-way/validator"
)

func main() {
	result := v.New(&struct {
		int `validate:"min(10,fail)"`
	}{}).Validate()
	fmt.Println(result.Passed)
	fmt.Println(result.Messages())
}

Custom validation implementation

package main

import (
	"fmt"
	v "github.com/go-the-way/validator"
	"reflect"
)

func main() {
	v.Custom("mycustom", func(value reflect.Value) (bool, string) { return false, "mycustom validation." })
	vv := v.New(&struct {
		Name string `validate:"custom(mycustom)"`
	}{}).Validate()
	fmt.Println(vv.Passed)
	fmt.Println(vv.Messages())
}

Validators

Name Support Example Description
Min ([])(*)uint{8,64}, ([])(*)int{8,64}, ([])(*)float{32,64} validate:"min(N,invalid)" Every value must be >= N
Max ([])(*)uint{8,64}, ([])(*)int{8,64}, ([])(*)float{32,64} validate:"max(N,invalid)" Every value must be <= N
Length (*)string, (*)Array[(*)string], (*)Slice[(*)string] validate:"length(N,invalid)" (*)string: Value's Len must be == N
(*)Array[(*)string] or (*)Slice[(*)string]: Every Value's Len must be == N
ArrLength (*)Array[(*)Any], (*)Slice[(*)Any] validate:"arr_length(N,invalid)" (*)Array[(*)Any] or (*)Slice[(*)Any]: Array or Slice's Len must be == N
MinLength (*)string, (*)Array[(*)string], (*)Slice[(*)string] validate:"minlength(N,invalid)" (*)string: Value's Len must be >= N
(*)Array[(*)string] or (*)Slice[(*)string]: Every Value's Len must be >= N
ArrMinLength (*)Array[(*)Any], (*)Slice[(*)Any] validate:"arr_minlength(N,invalid)" (*)Array[(*)Any] or (*)Slice[(*)Any]: Array or Slice's Len must be >= N
MaxLength (*)string, (*)Array[(*)string], (*)Slice[(*)string] validate:"maxlength(N,invalid)" (*)string: Value's Len must be <= N
(*)Array[(*)string] or (*)Slice[(*)string]: Every Value's Len must be <= N
ArrMaxLength (*)Array[(*)Any], (*)Slice[(*)Any] validate:"arr_maxlength(N,invalid)" (*)Array[(*)Any] or (*)Slice[(*)Any]: Array or Slice's Len must be <= N
Enum ([])(*)uint{8,64}, ([])(*)int{8,64}, ([])(*)float{32,64}, ([])(*)string validate:"enum(O,invalid)" Every value must be one of O
Regex ([])(*)string validate:"regex(RE,invalid)" Every value must be match RE
Valid *struct{} validate:"valid(T,invalid)" Value must be not nil
Custom any validate:"custom(CUSTOM)" CUSTOM validation