-
Notifications
You must be signed in to change notification settings - Fork 1
/
recipe.go
161 lines (142 loc) · 4.28 KB
/
recipe.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package types
import (
"fmt"
"time"
"unicode/utf8"
)
//
// Recipe Creation Types
//
type Recipe struct {
Id string `json:"id"`
Author string `json:"author"`
Image string `json:"image" validate:"url"`
URL string `json:"url"`
Title string `json:"title" validate:"recipetitle"`
Notes string `json:"notes" validate:"recipenotes"`
Ingredients Ingredients `json:"ingredients" validate:"existence"`
CookTime int `json:"cooktime" validate:"time"`
CookTimeUnit string `json:"cooktimeunit" validate:"timeunit"`
PrepTime int `json:"preptime" validate:"time"`
PrepTimeUnit string `json:"preptimeunit" validate:"timeunit"`
Steps Steps `json:"steps" validate:"existence"`
Tags Tags `json:"tags"`
Private bool `json:"private"`
Created time.Time `json:"created"`
LastModified time.Time `json:"last_modified"`
}
//
// Recipe Component Types
//
type Ingredient struct {
Id int `json:"id"`
Name string `json:"name" validate:"ingredient"`
Amount float64 `json:"amount"`
AmountUnit string `json:"amountunit"`
URL string `json:"url" validate:"url"`
}
type Step struct {
Id int `json:"id"`
Instruction string `json:"instruction" validate:"step"`
Time int `json:"time" validate:"time"`
TimeUnit string `json:"timeunit" validate:"timeunit"`
}
type Recipes []Recipe
type Ingredients []Ingredient
type Steps []Step
//
// Recipe Tag Types
//
type Tag struct {
Name string `json:"name" validate:"tag"`
}
type Tags []Tag
//
// Recipe Validator Functions
//
func (v RicettaValidator) validateTimeUnit(i interface{}) error {
timeUnit := i.(string)
if timeUnit == "" {
return fmt.Errorf("Required field")
} else if !v.Constants.TIME_UNIT_REGEX.MatchString(timeUnit) {
return fmt.Errorf(timeUnit + " is not a valid unit of time - [sec(s), min(s), hr(s), day(s), week(s)]")
} else {
return nil
}
}
func (v RicettaValidator) validateTime(i interface{}) error {
time, ok := i.(int)
if !ok {
return fmt.Errorf("Required field")
} else if time <= 0 || time > 1000 {
return fmt.Errorf("Invalid time unit: %d", time)
} else {
return nil
}
}
func (v RicettaValidator) validateRecipeTitle(i interface{}) error {
title := i.(string)
titlelen := utf8.RuneCountInString(title)
if title == "" {
return fmt.Errorf("Required field")
} else if titlelen > v.Constants.MAX_RECIPE_TITLE_LENGTH {
return fmt.Errorf("Exceeds max title length of %d", v.Constants.MAX_RECIPE_TITLE_LENGTH)
} else {
return nil
}
}
func (v RicettaValidator) validateRecipeNotes(i interface{}) error {
notes := i.(string)
noteslen := utf8.RuneCountInString(notes)
if notes == "" {
return fmt.Errorf("Required field")
} else if noteslen > v.Constants.MAX_RECIPE_NOTES_LENGTH {
return fmt.Errorf("Exceeds max notes length of %d", v.Constants.MAX_RECIPE_NOTES_LENGTH)
} else {
return nil
}
}
func (v RicettaValidator) validateIngredient(i interface{}) error {
ingredient := i.(string)
ingredientlen := utf8.RuneCountInString(ingredient)
if ingredient == "" {
return fmt.Errorf("Required field")
} else if ingredientlen > v.Constants.MAX_INGREDIENT_LENGTH {
return fmt.Errorf("Exceeds max ingredient length of %d", v.Constants.MAX_INGREDIENT_LENGTH)
} else {
return nil
}
}
func (v RicettaValidator) validateStep(i interface{}) error {
step := i.(string)
steplen := utf8.RuneCountInString(step)
if step == "" {
return fmt.Errorf("Required field")
} else if steplen > v.Constants.MAX_STEP_LENGTH {
return fmt.Errorf("Exceeds max step length of %d", v.Constants.MAX_STEP_LENGTH)
} else {
return nil
}
}
func (v RicettaValidator) validateURL(i interface{}) error {
url := i.(string)
if url == "" {
// This demonstrates how to validate an optional field
return nil
} else if !v.Constants.URL_REGEX.MatchString(url) {
return fmt.Errorf("Not a valid URL string: %s", url)
} else {
return nil
}
}
func (v RicettaValidator) validateTag(i interface{}) error {
tag := i.(string)
taglen := utf8.RuneCountInString(tag)
if tag == "" {
return fmt.Errorf("Required field")
} else if taglen > v.Constants.MAX_TAG_LENGTH {
return fmt.Errorf("Exceeds max tag length of %d", v.Constants.MAX_TAG_LENGTH)
} else {
return nil
}
}