Fix validation logic and make code more readable

This commit is contained in:
2024-12-31 00:19:53 +01:00
parent 7beadf1538
commit 57bd72506b
2 changed files with 17 additions and 5 deletions

View File

@ -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.GreaterThan(len(r.Choices), 1), "choices", "there must be more than 1 choice")
r.CheckField(
!(r.AreVotersKnown && r.MaxVoters < 1),
"maxVoters",
"must be greater than 0 when voters are known",
)
if r.AreVotersKnown {
r.CheckField(
validator.GreaterThan(r.MaxVoters, 0),
"maxVoters",
"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()
}

View File

@ -60,6 +60,10 @@ func GreaterThan(value int, n int) bool {
return value > n
}
func GreaterThanOrEquals(value int, n int) bool {
return value >= n
}
func After(value time.Time, n time.Time) bool {
return value.After(n)
}