/
validations.go
45 lines (39 loc) · 958 Bytes
/
validations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package dto
import (
"fmt"
"github.com/anytimesoon/eurovision-party/pkg/enum"
"github.com/anytimesoon/eurovision-party/pkg/errs"
"strings"
)
var topScore uint8 = 10
func isPresent(attr string, attrName string) string {
if attr == "" {
return attrName + " must not be blank"
} else {
return ""
}
}
func isWithinRange(vote uint8, attrName string) string {
if vote > topScore {
return fmt.Sprintf("Your vote for %s is too high. The maximum is 10", attrName)
} else if vote < 1 {
return fmt.Sprintf("Your must vote for %s is too low. The minimum is 1", attrName)
} else {
return ""
}
}
func isValidCat(cat string) string {
switch cat {
case enum.Song, enum.Costume, enum.Performance, enum.Props:
return ""
default:
return cat + " is not a valid category"
}
}
func messagesToError(messages []string) *errs.AppError {
if len(messages) > 0 {
return errs.NewInvalidError(strings.Join(messages, ","))
} else {
return nil
}
}