gvalid

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2021 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package gvalid implements powerful and useful data/form validation functionality.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteRule

func DeleteRule(rules ...string)

DeleteRule deletes custom defined validation one or more rules and associated functions from global package.

func RegisterRule

func RegisterRule(rule string, f RuleFunc)

RegisterRule registers custom validation rule and function for package.

Example
package main

import (
	"context"
	"errors"
	"fmt"

	"easyscdp.com/gogf/gf/frame/g"
	"easyscdp.com/gogf/gf/os/gctx"
	"easyscdp.com/gogf/gf/util/gconv"
	"easyscdp.com/gogf/gf/util/gvalid"
)

func main() {
	type User struct {
		Id   int
		Name string `v:"required|unique-name # 请输入用户名称|用户名称已被占用"`
		Pass string `v:"required|length:6,18"`
	}
	user := &User{
		Id:   1,
		Name: "john",
		Pass: "123456",
	}

	rule := "unique-name"
	gvalid.RegisterRule(rule, func(ctx context.Context, in gvalid.RuleFuncInput) error {
		var (
			id   = in.Data.Val().(*User).Id
			name = gconv.String(in.Value)
		)
		n, err := g.Model("user").Where("id != ? and name = ?", id, name).Count()
		if err != nil {
			return err
		}
		if n > 0 {
			return errors.New(in.Message)
		}
		return nil
	})
	err := g.Validator().Data(user).Rules(nil).Run(gctx.New())
	fmt.Println(err.Error())
	// May Output:
	// 用户名称已被占用
}
Output:

func RegisterRuleByMap

func RegisterRuleByMap(m map[string]RuleFunc)

RegisterRuleByMap registers custom validation rules using map for package.

Types

type CustomMsg

type CustomMsg = map[string]interface{}

CustomMsg is the custom error message type, like: map[field] => string|map[rule]string

type Error

type Error interface {
	Code() gcode.Code
	Current() error
	Error() string
	FirstItem() (key string, messages map[string]error)
	FirstRule() (rule string, err error)
	FirstError() (err error)
	Items() (items []map[string]map[string]error)
	Map() map[string]error
	Maps() map[string]map[string]error
	String() string
	Strings() (errs []string)
}

Error is the validation error for validation result.

type RuleFunc

type RuleFunc func(ctx context.Context, in RuleFuncInput) error

RuleFunc is the custom function for data validation.

type RuleFuncInput

type RuleFuncInput struct {
	// Rule specifies the validation rule string, like "required", "between:1,100", etc.
	Rule string

	// Message specifies the custom error message or configured i18n message for this rule.
	Message string

	// Value specifies the value for this rule to validate.
	Value *gvar.Var

	// Data specifies the `data` which is passed to the Validator. It might be a type of map/struct or a nil value.
	// You can ignore the parameter `Data` if you do not really need it in your custom validation rule.
	Data *gvar.Var
}

RuleFuncInput holds the input parameters that passed to custom rule function RuleFunc.

type Validator

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

Validator is the validation manager for chaining operations.

func New

func New() *Validator

New creates and returns a new Validator.

func (*Validator) Assoc

func (v *Validator) Assoc(assoc interface{}) *Validator

Assoc is a chaining operation function, which sets associated validation data for current operation. The optional parameter `assoc` is usually type of map, which specifies the parameter map used in union validation. Calling this function with `assoc` also sets `useDataInsteadOfObjectAttributes` true

func (*Validator) Bail

func (v *Validator) Bail() *Validator

Bail sets the mark for stopping validation after the first validation error.

Example
package main

import (
	"context"
	"fmt"

	"easyscdp.com/gogf/gf/frame/g"
)

func main() {
	type BizReq struct {
		Account   string `v:"bail|required|length:6,16|same:QQ"`
		QQ        string
		Password  string `v:"required|same:Password2"`
		Password2 string `v:"required"`
	}
	var (
		ctx = context.Background()
		req = BizReq{
			Account:   "gf",
			QQ:        "123456",
			Password:  "goframe.org",
			Password2: "goframe.org",
		}
	)
	if err := g.Validator().Data(req).Run(ctx); err != nil {
		fmt.Println(err)
	}

}
Output:

The Account value `gf` length must be between 6 and 16

func (*Validator) Ci

func (v *Validator) Ci() *Validator

Ci sets the mark for Case-Insensitive for those rules that need value comparison.

func (*Validator) Clone

func (v *Validator) Clone() *Validator

Clone creates and returns a new Validator which is a shallow copy of current one.

func (*Validator) Data

func (v *Validator) Data(data interface{}) *Validator

Data is a chaining operation function, which sets validation data for current operation.

func (*Validator) I18n

func (v *Validator) I18n(i18nManager *gi18n.Manager) *Validator

I18n sets the i18n manager for the validator.

func (*Validator) Messages

func (v *Validator) Messages(messages interface{}) *Validator

Messages is a chaining operation function, which sets custom error messages for current operation. The parameter `messages` can be type of string/[]string/map[string]string. It supports sequence in error result if `rules` is type of []string.

func (*Validator) RuleFunc

func (v *Validator) RuleFunc(rule string, f RuleFunc) *Validator

RuleFunc registers one custom rule function to current Validator.

func (*Validator) RuleFuncMap

func (v *Validator) RuleFuncMap(m map[string]RuleFunc) *Validator

RuleFuncMap registers multiple custom rule functions to current Validator.

func (*Validator) Rules

func (v *Validator) Rules(rules interface{}) *Validator

Rules is a chaining operation function, which sets custom validation rules for current operation.

Example
package main

import (
	"fmt"

	"easyscdp.com/gogf/gf/frame/g"
	"easyscdp.com/gogf/gf/os/gctx"
)

func main() {
	data := g.Map{
		"password": "123",
	}
	err := g.Validator().Data("").Assoc(data).
		Rules("required-with:password").
		Messages("请输入确认密码").
		Run(gctx.New())
	fmt.Println(err.String())

}
Output:

请输入确认密码

func (*Validator) Run

func (v *Validator) Run(ctx context.Context) Error

Run starts validating the given data with rules and messages.

Jump to

Keyboard shortcuts

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