Fix validation logic and make code more readable
This commit is contained in:
@ -68,11 +68,19 @@ func isRequestValid(r *createElectionRequestWithValidator) bool {
|
|||||||
r.CheckField(validator.After(r.ExpiresAt, time.Now()), "expiresAt", "must expire in a future date")
|
r.CheckField(validator.After(r.ExpiresAt, time.Now()), "expiresAt", "must expire in a future date")
|
||||||
r.CheckField(validator.GreaterThan(len(r.Choices), 1), "choices", "there must be more than 1 choice")
|
r.CheckField(validator.GreaterThan(len(r.Choices), 1), "choices", "there must be more than 1 choice")
|
||||||
|
|
||||||
|
if r.AreVotersKnown {
|
||||||
r.CheckField(
|
r.CheckField(
|
||||||
!(r.AreVotersKnown && r.MaxVoters < 1),
|
validator.GreaterThan(r.MaxVoters, 0),
|
||||||
"maxVoters",
|
"maxVoters",
|
||||||
"must be greater than 0 when voters are known",
|
"must be greater than 0 when voters are known",
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
|
r.CheckField(
|
||||||
|
validator.GreaterThanOrEquals(r.MaxVoters, 0),
|
||||||
|
"maxVoters",
|
||||||
|
"must be a positive number",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
return r.Valid()
|
return r.Valid()
|
||||||
}
|
}
|
||||||
|
@ -60,6 +60,10 @@ func GreaterThan(value int, n int) bool {
|
|||||||
return value > n
|
return value > n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GreaterThanOrEquals(value int, n int) bool {
|
||||||
|
return value >= n
|
||||||
|
}
|
||||||
|
|
||||||
func After(value time.Time, n time.Time) bool {
|
func After(value time.Time, n time.Time) bool {
|
||||||
return value.After(n)
|
return value.After(n)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user