vd

package module
v0.0.0-...-617f752 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2023 License: Apache-2.0 Imports: 12 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnumValues

func EnumValues(v interface{}) (enum []string)

func PathIndex

func PathIndex(path string, index int) string

Types

type BoolSpec

type BoolSpec struct {
	Name string
	Path string
}
type OptionBool struct {
	valid bool
	bool bool
}
func (o OptionBool) String() string {
	if !o.valid {return ""}
	if o.bool {return "true"} else { return "false"}
}

func (o OptionBool) Valid() bool { return o.valid }

func (o OptionBool) Unwrap() bool {
	if !o.valid {panic("OptionBool: can not wrap invalid OptionBool")}
	return o.bool
}
func Bool(b bool) OptionBool {
	return OptionBool{true, b}
}

type CNFormat

type CNFormat struct{}

func (CNFormat) BanPattern

func (CNFormat) BanPattern(name string, path string, value string, banPattern []string, failBanPattern string) string

func (CNFormat) DateRangeDefaultName

func (CNFormat) DateRangeDefaultName() (beginName string, endTime string)

func (CNFormat) FloatMax

func (CNFormat) FloatMax(name string, path string, value float64, max float64) string

func (CNFormat) FloatMin

func (CNFormat) FloatMin(name string, path string, value float64, min float64) string

func (CNFormat) IntMax

func (CNFormat) IntMax(name string, path string, value int, max int) string

func (CNFormat) IntMin

func (CNFormat) IntMin(name string, path string, value int, min int) string

func (CNFormat) IntNotAllowEmpty

func (CNFormat) IntNotAllowEmpty(name string, path string) string

func (CNFormat) Pattern

func (CNFormat) Pattern(name string, path string, value string, pattern []string, failPattern string) string

func (CNFormat) SliceMaxLen

func (CNFormat) SliceMaxLen(name string, path string, len int, maxLen int) string

func (CNFormat) SliceMinLen

func (CNFormat) SliceMinLen(name string, path string, len int, minLen int) string

func (CNFormat) SliceNotAllowEmpty

func (CNFormat) SliceNotAllowEmpty(name string, path string) string

func (CNFormat) SliceUnique

func (CNFormat) SliceUnique(name string, path string, repeatElement interface{}) string

func (CNFormat) StringEnum

func (CNFormat) StringEnum(name string, path string, value string, enum []string) string

func (CNFormat) StringMaxRuneLen

func (CNFormat) StringMaxRuneLen(name string, path string, value string, length uint64) string

func (CNFormat) StringMinRuneLen

func (CNFormat) StringMinRuneLen(name string, path string, value string, length uint64) string

func (CNFormat) StringNotAllowEmpty

func (CNFormat) StringNotAllowEmpty(name string, path string) string

func (CNFormat) StringRuneLen

func (f CNFormat) StringRuneLen(name string, path string, value string, length uint64) string

func (CNFormat) TimeAfterIt

func (CNFormat) TimeAfterIt(name string, path string, value time.Time, afterIt time.Time) string

func (CNFormat) TimeAfterOrEqualIt

func (CNFormat) TimeAfterOrEqualIt(name string, path string, value time.Time, afterOrEqualIt time.Time) string

func (CNFormat) TimeBeforeIt

func (CNFormat) TimeBeforeIt(name string, path string, value time.Time, beforeIt time.Time) string

func (CNFormat) TimeBeforeOrEqualIt

func (CNFormat) TimeBeforeOrEqualIt(name string, path string, value time.Time, beforeOrEqualIt time.Time) string

func (CNFormat) TimeNotAllowZero

func (CNFormat) TimeNotAllowZero(name string, path string) string

func (CNFormat) TimeRangeDefaultName

func (CNFormat) TimeRangeDefaultName() (beginName string, endTime string)

type Checker

type Checker struct {
	Format Formatter
}

func NewCN

func NewCN() Checker

func (Checker) Check

func (checker Checker) Check(data Data) (report Report, err error)
Example
package main

import (
	"context"

	vd "github.com/goclub/validator"
	"log"
)

func main() {
	ctx := context.Background()
	_ = ctx
	err := func() (err error) {
		checker := vd.NewCN()
		createUser := RequestCreateUser{
			Email:    "xxx@domain.com",
			Name:     "张三",
			Nickname: "三儿",
			Age:      20,
			Skills:   []string{"clang", "go"},
			Address: RequestCreateUserAddress{
				Province: "上海",
				Detail:   "", //
			},
			AddressList: []RequestCreateUserAddress{
				{
					Province: "上海",
					Detail:   "人民广场一号",
				},
				{
					Province: "上海",
					Detail:   "", // 人民广场一号
				},
			},
		}
		report, err := checker.Check(createUser)
		if err != nil {
			return
		}
		if report.Fail {
			log.Print("fail")
			log.Print("path:", report.Path)
			log.Print("message:", report.Message)
		} else {
			log.Print("验证通过")
		}
		return
	}()
	if err != nil {
		log.Printf("%+v", err)
	}
}

type RequestCreateUser struct {
	Email       string
	Name        string
	Nickname    string
	Age         int
	Skills      []string
	Address     RequestCreateUserAddress `json:"address"`
	AddressList []RequestCreateUserAddress
}

func (v RequestCreateUser) VD(r *vd.Rule) (err error) {
	r.String(v.Email, vd.StringSpec{
		Name: "邮箱地址",
		Path: "email",
		Ext:  []vd.StringSpec{vd.ExtString{}.Email()},
	})
	r.String(v.Name, vd.StringSpec{
		Name:       "姓名",
		Path:       "name",
		MinRuneLen: 2,
		MaxRuneLen: 20,
	})
	r.String(v.Nickname, vd.StringSpec{
		Name:           "昵称",
		Path:           "nickname",
		AllowEmpty:     true,
		BanPattern:     []string{`\d`},
		PatternMessage: "昵称不允许包含数字",
		MinRuneLen:     2,
		MaxRuneLen:     10,
	})
	r.Int(v.Age, vd.IntSpec{
		Name:       "年龄",
		Path:       "age",
		Min:        vd.Int(18),
		MinMessage: "只允许成年人注册",
	})
	r.Int(len(v.Skills), vd.IntSpec{
		Name:       "技能",
		Path:       "skills",
		Max:        vd.Int(10),
		MaxMessage: "最多填写{{Max}}项",
	})

	for index, skill := range v.Skills {
		r.String(skill, vd.StringSpec{
			Name: "技能项",
			Path: vd.PathIndex("skill", index),
		})
	}

	return nil
}

type RequestCreateUserAddress struct {
	Province string
	Detail   string
}

func (v RequestCreateUserAddress) VD(r *vd.Rule) (err error) {
	r.String(v.Province, vd.StringSpec{
		Path: "province",
		Name: "省",
	})
	r.String(v.Detail, vd.StringSpec{
		Name:           "详细地址",
		Path:           "detail",
		Pattern:        []string{`号`},
		PatternMessage: "地址必须包含门牌号,例如:某路110号",
	})
	return
}
Output:

type Data

type Data interface {
	VD(r *Rule) (err error)
}

type ExtString

type ExtString struct {
}

func (ExtString) ASCII

func (ExtString) ASCII() StringSpec

func (ExtString) Base64

func (ExtString) Base64() StringSpec

func (ExtString) Base64URL

func (ExtString) Base64URL() StringSpec

func (ExtString) ChinaMobile

func (ExtString) ChinaMobile() StringSpec

func (ExtString) DataURI

func (ExtString) DataURI() StringSpec

func (ExtString) Email

func (ExtString) Email() StringSpec

func (ExtString) HSL

func (ExtString) HSL() StringSpec

func (ExtString) HSLA

func (ExtString) HSLA() StringSpec

func (ExtString) Hex

func (ExtString) Hex() StringSpec

func (ExtString) HexColor

func (ExtString) HexColor() StringSpec

func (ExtString) Latitude

func (ExtString) Latitude() StringSpec

func (ExtString) Longitude

func (ExtString) Longitude() StringSpec

func (ExtString) PrintableASCII

func (ExtString) PrintableASCII() StringSpec

func (ExtString) RGB

func (ExtString) RGB() StringSpec

func (ExtString) RGBA

func (ExtString) RGBA() StringSpec

func (ExtString) UUID

func (ExtString) UUID() StringSpec

type FloatSpec

type FloatSpec struct {
	Name string
	Path string
	// AllowZero bool // 暂时取消 AllowZero,目的是降低使用者学习成本,观察一段时间后再决定是否完全去掉 (2020年08月07日 by @nimoc)
	Min            OptionFloat
	MinMessage     string
	Max            OptionFloat
	MaxMessage     string
	Pattern        []string
	BanPattern     []string
	PatternMessage string
}

func (FloatSpec) CheckBanPattern

func (spec FloatSpec) CheckBanPattern(v float64, r *Rule) (fail bool)

func (FloatSpec) CheckMax

func (spec FloatSpec) CheckMax(v float64, r *Rule) (fail bool)

func (FloatSpec) CheckMin

func (spec FloatSpec) CheckMin(v float64, r *Rule) (fail bool)

func (FloatSpec) CheckPattern

func (spec FloatSpec) CheckPattern(v float64, r *Rule) (fail bool)

type FloatSpecRender

type FloatSpecRender struct {
	Value interface{}
	FloatSpec
}

type Formatter

type Formatter interface {
	Pattern(name string, path string, value string, pattern []string, failPattern string) string
	BanPattern(name string, path string, value string, banPattern []string, failBanPattern string) string

	StringNotAllowEmpty(name string, path string) string
	StringRuneLen(name string, path string, value string, length uint64) string
	StringMinRuneLen(name string, path string, value string, length uint64) string
	StringMaxRuneLen(name string, path string, value string, length uint64) string
	StringEnum(name string, path string, value string, enum []string) string

	IntNotAllowEmpty(name string, path string) string
	IntMin(name string, path string, value int, min int) string
	IntMax(name string, path string, value int, max int) string

	FloatMin(name string, path string, value float64, min float64) string
	FloatMax(name string, path string, value float64, max float64) string

	SliceMinLen(name string, path string, len int, minLen int) string
	SliceMaxLen(name string, path string, len int, maxLen int) string
	SliceNotAllowEmpty(name string, path string) string
	SliceUnique(name string, path string, repeatElement interface{}) string

	TimeRangeDefaultName() (beginName string, endTime string)
	DateRangeDefaultName() (beginName string, endTime string)

	TimeNotAllowZero(name string, path string) string
	TimeBeforeIt(name string, path string, value time.Time, beforeIt time.Time) string
	TimeAfterIt(name string, path string, value time.Time, afterIt time.Time) string
	TimeBeforeOrEqualIt(name string, path string, value time.Time, beforeOrEqualIt time.Time) string
	TimeAfterOrEqualIt(name string, path string, value time.Time, afterOrEqualIt time.Time) string
}

type IntSpec

type IntSpec struct {
	Name string
	Path string
	// AllowZero bool // 暂时取消 AllowZero,目的是降低使用者学习成本,观察一段时间后再决定是否完全去掉 (2020年08月07日 by @nimoc)
	Min            OptionInt
	MinMessage     string
	Max            OptionInt
	MaxMessage     string
	Pattern        []string
	BanPattern     []string
	PatternMessage string
}

func (IntSpec) CheckBanPattern

func (spec IntSpec) CheckBanPattern(v int, r *Rule) (fail bool)

func (IntSpec) CheckMax

func (spec IntSpec) CheckMax(v int, r *Rule) (fail bool)

func (IntSpec) CheckMin

func (spec IntSpec) CheckMin(v int, r *Rule) (fail bool)

func (IntSpec) CheckPattern

func (spec IntSpec) CheckPattern(v int, r *Rule) (fail bool)

type OptionFloat

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

option Float simulate Float? (Float or nil) vd.Float(18.1) equal OptionFloat{valid: true, float: 18.1}

func Float

func Float(f float64) OptionFloat

func (OptionFloat) String

func (o OptionFloat) String() string

func (OptionFloat) Unwrap

func (o OptionFloat) Unwrap() float64

func (OptionFloat) Valid

func (o OptionFloat) Valid() bool

type OptionInt

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

option int simulate int? (int or nil) vd.Int(18) equal OptionInt{valid: true, int: 18}

func Int

func Int(i int) OptionInt

func (OptionInt) String

func (o OptionInt) String() string

func (OptionInt) Unwrap

func (o OptionInt) Unwrap() int

func (OptionInt) Valid

func (o OptionInt) Valid() bool

type Report

type Report struct {
	Fail    bool
	Message string
	Path    string
}

type Rule

type Rule struct {
	Fail    bool
	Message string
	Path    []string
	Format  Formatter
	// contains filtered or unexported fields
}

func (*Rule) Bool

func (r *Rule) Bool(v bool, spec BoolSpec)

func (*Rule) Break

func (r *Rule) Break(message string, path string)

func (Rule) CreateMessage

func (r Rule) CreateMessage(message string, customMessage func() string) string

func (*Rule) Error

func (r *Rule) Error(err error)

func (*Rule) Float

func (r *Rule) Float(v float64, spec FloatSpec)

func (*Rule) Int

func (r *Rule) Int(v int, spec IntSpec)

func (*Rule) Int16

func (r *Rule) Int16(v int16, spec IntSpec)

func (*Rule) Int32

func (r *Rule) Int32(v int32, spec IntSpec)

func (*Rule) Int64

func (r *Rule) Int64(v int64, spec IntSpec)

func (*Rule) Int8

func (r *Rule) Int8(v int8, spec IntSpec)

func (*Rule) String

func (r *Rule) String(v string, spec StringSpec)

func (*Rule) Time

func (r *Rule) Time(v time.Time, spec TimeSpec)

func (*Rule) Uint

func (r *Rule) Uint(v uint, spec IntSpec)

func (*Rule) Uint16

func (r *Rule) Uint16(v uint16, spec IntSpec)

func (*Rule) Uint32

func (r *Rule) Uint32(v uint32, spec IntSpec)

func (*Rule) Uint64

func (r *Rule) Uint64(v uint64, spec IntSpec)

func (*Rule) Uint8

func (r *Rule) Uint8(v uint8, spec IntSpec)

func (*Rule) Validator

func (r *Rule) Validator(v interface {
	Validator(err ...error) error
}, failMessage string, path string)

type StringSpec

type StringSpec struct {
	Name              string
	Path              string
	AllowEmpty        bool
	RuneLen           uint64
	RuneLenMessage    string
	MinRuneLen        uint64
	MinRuneLenMessage string
	MaxRuneLen        uint64
	MaxRuneLenMessage string
	Pattern           []string
	BanPattern        []string
	PatternMessage    string
	Enum              []string
	Ext               []StringSpec
}

func Email

func Email() StringSpec

向前兼容

func (StringSpec) CheckBanPattern

func (spec StringSpec) CheckBanPattern(v string, r *Rule) (fail bool)

func (StringSpec) CheckEnum

func (spec StringSpec) CheckEnum(v string, r *Rule) (fail bool)

func (StringSpec) CheckMaxRuneLen

func (spec StringSpec) CheckMaxRuneLen(v string, r *Rule) (fail bool)

func (StringSpec) CheckMinRuneLen

func (spec StringSpec) CheckMinRuneLen(v string, r *Rule) (fail bool)

func (StringSpec) CheckPattern

func (spec StringSpec) CheckPattern(v string, r *Rule) (fail bool)

func (StringSpec) CheckRuneLen

func (spec StringSpec) CheckRuneLen(v string, r *Rule) (fail bool)

func (StringSpec) NameIs

func (s StringSpec) NameIs(name string) StringSpec

type TimeSpec

type TimeSpec struct {
	Name            string
	Path            string
	AllowZero       bool
	BeforeIt        time.Time
	AfterIt         time.Time
	BeforeOrEqualIt time.Time
	AfterOrEqualIt  time.Time
}

func (TimeSpec) CheckAfterIt

func (spec TimeSpec) CheckAfterIt(v time.Time, r *Rule) (fail bool)

func (TimeSpec) CheckAfterOrEqualIt

func (spec TimeSpec) CheckAfterOrEqualIt(v time.Time, r *Rule) (fail bool)

func (TimeSpec) CheckBeforeIt

func (spec TimeSpec) CheckBeforeIt(v time.Time, r *Rule) (fail bool)

func (TimeSpec) CheckBeforeOrEqualIt

func (spec TimeSpec) CheckBeforeOrEqualIt(v time.Time, r *Rule) (fail bool)

Directories

Path Synopsis
generics module

Jump to

Keyboard shortcuts

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