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 (this *EfficacyGateKeeper) AddUniqueValidator() {
	this.Add(govalidations.Custom(func(object interface{}) bool {
		efficacy := object.(*Efficacy)
		c, _ := FindUniqueEfficacy(efficacy.Name, efficacy.ParentId)
		return c == nil
	}, "Name", global.EFFICACY_01))

	return
}
Beispiel #3
0
func (this *CategoryGateKeeper) AddUniqueValidator() {
	this.Add(govalidations.Custom(func(object interface{}) bool {
		category := object.(*Category)
		c, _ := FindUniqueCategory(category.Name, category.Level, category.ParentId)
		return c == nil
	}, "Name", global.CATEGORY_01))

	return
}
Beispiel #4
0
func (agk *UserGateKeeper) AddPasswordMatchValidator(user *User) {
	email, password := user.Email, user.Password
	user, _ = FindByEmail(email)
	agk.Add(govalidations.Custom(func(object interface{}) bool {
		if user == nil {
			return false
		}
		return true
	}, "Password", global.USER_01))

	agk.Add(govalidations.Custom(func(object interface{}) bool {
		if user != nil && !user.IsPwdMatch(password) {
			return false
		}
		return true
	}, "Password", global.USER_01))

	return
}
Beispiel #5
0
func (agk *UserGateKeeper) AddConfirmPasswordValidator() {
	agk.Add(govalidations.Custom(func(object interface{}) bool {
		p := object.(*User)
		if p.Password != p.ConfirmPassword {
			return false
		}
		return true
	}, "ConfirmPassword", global.USER_09))
	return
}
Beispiel #6
0
func (agk *UserGateKeeper) AddEmailExistValidator() {
	agk.Add(govalidations.Custom(func(object interface{}) bool {
		email := object.(*User).Email
		if user, _ := FindByEmail(email); user != nil {
			return false
		}
		return true
	}, "Email", global.USER_02))

	return
}
Beispiel #7
0
func (this *CategoryGateKeeper) AddNameAndLevelValidator() {
	this.Add(govalidations.Presence(func(object interface{}) interface{} {
		return object.(*Category).Name
	}, "Name", global.CATEGORY_02))

	this.Add(govalidations.Custom(func(object interface{}) bool {
		return IsValidLevel(object.(*Category).Level)
	}, "Level", global.CATEGORY_03))

	return
}
Beispiel #8
0
func (this *ReviewGateKeeper) AddLikeValidator(userId bson.ObjectId) {
	this.Add(govalidations.Custom(func(object interface{}) bool {
		return !utils.IsInObjectIds(userId, object.(*Review).LikedByIds)
	}, "LikedByIds", global.REVIEW_01))
}