Beispiel #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
}
Beispiel #2
0
func (agk *UserGateKeeper) AddEmailValidator() {
	agk.Add(govalidations.Presence(func(object interface{}) interface{} {
		return object.(*User).Email
	}, "Email", global.USER_03))

	agk.Add(govalidations.Regexp(func(object interface{}) interface{} {
		return object.(*User).Email
	}, regexp.MustCompile(global.EMAIL_REGEXP), "Email", global.USER_04))

	return
}