jsonValidator

package
v0.0.0-...-e50112c Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2024 License: BSD-3-Clause Imports: 11 Imported by: 0

README

Json表达式验证器

  • 在某些情况下,允许 Golang 像弱类型语言、解释型语言那样,在不重新编译代码的情况下,仅通过修改配置数据中的 “Json 验证表达式”,即可灵活的实现逻辑控制。
  • 通过类似 json-path 语法,获取对应的值。

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Validator

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

func New

func New(data interface{}) *Validator

创建一个表达式验证器。

Example
obj := New(`
	 {
	     "code": 1,
	     "message": "test",
	     "data": {
	         "userId": 1,
	 		 "user": {"id": 1, "name": "test"},
	         "id": [1,2,3],
	         "items": [
	             {"id": 1, "name": "a"},
	             {"id": 2, "name": "b"},
	             {"id": 3, "name": "c"},
	             {"id": 4, "name": "d"},
	             {"id": 5, "name": "e"}
	         ]
	     }
	}`)

// float64(1)
val, _ := obj.Value("code")

// int(1)
nVal, _ := obj.Int("code")

// string("test")
val, _ = obj.Value("message")

// map[string]interface{}{...}
val, _ = obj.Value("data")

// map[string]interface{}{"id": 1, "name": "test"}
val, _ = obj.Value("data.user")

// []int64{1,2,3}
val, _ = obj.Value("data.id")

// []int{1,2,3}
arrVal, _ := obj.IntSlice("data.id")

// int64(2)
val, _ = obj.Value("data.id.1")

// []int64{1,2,3,4,5}
val, _ = obj.Value("data.items.*.id")

// []string{"a","b","c","d","e"}
val, _ = obj.Value("data.items.*.name")

print(val, nVal, arrVal)
Output:

func (*Validator) Bool

func (this *Validator) Bool(path string) (val bool, err error)

func (*Validator) BoolSlice

func (this *Validator) BoolSlice(path string) (val []bool, err error)

func (*Validator) Float

func (this *Validator) Float(path string) (val float64, err error)

func (*Validator) FloatSlice

func (this *Validator) FloatSlice(path string) (val []float64, err error)

func (*Validator) Int

func (this *Validator) Int(path string) (val int, err error)

func (*Validator) Int64

func (this *Validator) Int64(path string) (val int64, err error)

func (*Validator) Int64Slice

func (this *Validator) Int64Slice(path string) (val []int64, err error)

func (*Validator) IntSlice

func (this *Validator) IntSlice(path string) (val []int, err error)

func (*Validator) JsonObj

func (this *Validator) JsonObj() jsonIter.Any

获取要验证的 Json 对象

func (*Validator) String

func (this *Validator) String(path string) (val string, err error)

func (*Validator) StringObjectMap

func (this *Validator) StringObjectMap(path string) (val mapUtil.StringObjectMap, err error)

func (*Validator) StringSlice

func (this *Validator) StringSlice(path string) (val []string, err error)

func (*Validator) Validate

func (this *Validator) Validate(path string, operator comparer.Operator, val interface{}, option int) (bool, error)

验证指定路径对应的值是否符合表达式。

若指定路径存在、且能够与 val 进行对应的比较操作,则返回比较结果;
若路径不存在、路径对应的值与 val 之间无法进行对应的比较(比如 int 之间无法进行 contains 比较等),则返回错误。

func (*Validator) Value

func (this *Validator) Value(path string) (val interface{}, err error)

获取指定路径对应的值。

当路径不存在时返回错误。

Jump to

Keyboard shortcuts

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