Giter Site home page Giter Site logo

go-validator's Introduction

go-validator

数据验证

  • 使用
    import "github.com/Ysll233/go-validator/validator"
    
    data := make(map[string]interface{})
    data["ID"] = 1
    data["Name"] = "lin han"
    data["Disabled"] = true
    data["Money"] = 10.65
    
    rules := make(map[string]interface{})
    rules["ID"] = "required|min:0|integer"
    rules["Name"] = "required|min:0|integer|active_url"
    rules["age"] = "required|integer|min:0|max:120"
    
    validator := validate.Make(data, rules)
    if validator.Fails() {
        fmt.Println(validator.GetErrors())
    }
  • 自定义错误消息
    import "github.com/Ysll233/go-validator/validator"
    
    data := make(map[string]interface{})
    data["name"] = "1890000000"
    
    rules := make(map[string]interface{})
    rules["name"] = []string{"nullable", "match:^1([38][0-9]|14[57]|5[^4])\\d{8}$"}
    
    customMessages := make(map[string]interface{})
    customMessages["match"] = "格式不正确"
    customNames := make(map[string]string)
    v := validator.MakeAndCustom(data, rules, customMessages, customNames)
    v.Fails()
    fmt.Println(v.GetErrors())
  • 自定义验证字段名称
    import "github.com/Ysll233/go-validator/validator"
    
    data := make(map[string]interface{})
    data["name"] = "1890000000"
    
    rules := make(map[string]interface{})
    rules["name"] = []string{"nullable", "match:^1([38][0-9]|14[57]|5[^4])\\d{8}$"}
    
    customMessages := make(map[string]interface{})
    customMessages["match"] = "格式不正确"
    customNames := make(map[string]string)
    customNames["name"] = "名称"
    v := validator.MakeAndCustom(data, rules, customMessages, customNames)
    v.Fails()
    fmt.Println(v.GetErrors())

go-validator's People

Contributors

huangyongxuan avatar

Stargazers

 avatar gorpher avatar  avatar

go-validator's Issues

rule integer bug

func TestValidateInteger(t *testing.T) {
	data := make(map[string]interface{})
	data["name"] = 1
	rules := make(map[string]interface{})
	rules["name"] = []string{"required", "integer"}

	customMessages := make(map[string]interface{})
	customNames := make(map[string]string)
	v := validator.MakeAndCustom(data, rules, customMessages, customNames)
	v.Fails()
	fmt.Println(v.GetErrors())
}```

  提示验证失败
`return InterfaceIsInteger(params.params)` 在677 行

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.