コード例 #1
0
ファイル: whole_test.go プロジェクト: jmptrader/govalidations
func UserGateKeeper() (gk *govalidations.GateKeeper) {
	gk = govalidations.NewGateKeeper()

	gk.Add(govalidations.Regexp(func(object interface{}) interface{} {
		return object.(*User).Email
	}, regexp.MustCompile(`^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$`), "Email", "Must be a valid email"))

	gk.Add(govalidations.Presence(func(object interface{}) interface{} {
		return object.(*User).Username
	}, "Username", "Username can not be blank"))

	gk.Add(govalidations.Limitation(func(object interface{}) interface{} {
		return object.(*User).Username
	}, 0, 10, "Username", "Username can not be too long"))

	gk.Add(govalidations.Prohibition(func(object interface{}) interface{} {
		return object.(*User).Username
	}, 10, 20, "Username", "Username must less than 10 or more than 20"))

	gk.Add(govalidations.AvoidScriptTag(func(object interface{}) interface{} {
		return object.(*User).Username
	}, "Username", "Username can contains html script tag"))

	gk.Add(govalidations.Custom(func(object interface{}) bool {
		age := object.(*User).Age
		if age < 18 {
			return false
		}
		return true
	}, "Age", "You must be a grown man"))

	return
}
コード例 #2
0
ファイル: whole_test.go プロジェクト: jmptrader/govalidations
func MessageSwitcherGateKeeper() (gk *govalidations.GateKeeper) {
	gk = govalidations.NewGateKeeper()
	gk.Add(govalidations.MessageSwitcher(func(object interface{}) string {
		name := object.(*User).Username
		if name == "Kioshi" {
			return BlackListMessage
		}
		if name == "Roku" {
			return WhiteListMessage
		}
		return ""
	}, "Username"))
	return
}