Giter Club home page Giter Club logo

validator's Introduction

validator

基于go实现的一个参数验证器,当前仅实现了int,string,是否为空,最大值,最小值判断几个最基础的,后续会逐步添加更多的验证方式。

调用方式:

rules := make([]map[string]interface{},0)
rules = append(rules,map[string]interface{}{"field" : "name", "required" : true, "msg" : "姓名不能为空!",})
rules = append(rules,map[string]interface{}{"field" : "name", "string" : true, "min" : 6, "max" : 12, "msg" :"姓名长度必须在6~12位之间",})
rules = append(rules,map[string]interface{}{"field" : "age", "required" : true, "msg" : "年龄不能为空",})
rules = append(rules,map[string]interface{}{"field" : "age", "int" : true, "min" : 18, "max" : 99, "msg" : "年龄必须在18~99岁之间",})
rules = append(rules,map[string]interface{}{"field" : "desc", "string" : true,"max":100,"msg":"个人说明长度必须在100个字符以内"})

data := make(map[string]interface{})
data["name"] = "李三aaaaaaaaaa"
data["age"] = 0
data["desc"] = "过新年,穿新衣,晚上去打雪仗"


validation := NewValidation(data,rules)

validation.Check()
if validation.IsFail(){
	fmt.Println("第一个错误:" + validation.FirstError().Error())
	fmt.Println("所有错误:" , validation.AllErrors())
}else{
	fmt.Println("验证通过")
}

可应用在对前端传来的form表单进行验证,避免在业务逻辑代码中写大量的 if 判断。具体请看validation_test.go

validator's People

Contributors

jfeige avatar

Watchers

James Cloos avatar  avatar

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.