Пример #1
0
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
func (agk *UserGateKeeper) AddNameValidator() {
	agk.Add(govalidations.Presence(func(object interface{}) interface{} {
		return object.(*User).Name
	}, "Name", global.USER_05))

	agk.Add(govalidations.Limitation(func(object interface{}) interface{} {
		return object.(*User).Name
	}, 0, 20, "Name", global.USER_06))

	return
}