Go开发的数据条件验证工具,使用场景应该挺多

开源地址:github.com/miaogaolin/condition

使用

package main

import (
    "fmt"

    "github.com/miaogaolin/condition"
)

func main() {
    data := map[string]interface{}{
        "col1": 1,
        "col2": "hello world",
        "col3": "male",
    }
    res, err := condition.Validate(data, `({col1}==1 and {col2} =~ "world") or {col3} in ["male"]`)
    if err != nil {
        panic(err)
    }

    if res {
        fmt.Println("success")
    }
}

如果在验证的数据不是 Map 类型,是 Struct 类型,推荐使用 github.com/fatih/structs
将 Struct 转为 Map。

关键字

== 
> 
< 
>=
<=
!=
in  
not in 
=~  // 包含
!= // 不包含

欢迎多提提问题!

本作品采用《CC 协议》,转载必须注明作者和本文链接
潇洒哥老苗
讨论数量: 2

看这头像就进来了,老表,这个是验证器吗?, 先个给 ✨再说了

1年前 评论
潇洒哥老苗 (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!