summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2024-05-17 17:12:50 +0200
committerxengineering <me@xengineering.eu>2024-05-17 17:14:40 +0200
commit5eca3267d37cdc51f775b5452727efebbb7e7e9e (patch)
treec80dd1512270386e3912beeb05bda3347a464e36
parent0416f520cff9716792ac0f3ade5f71187d07ac2e (diff)
downloadceres-5eca3267d37cdc51f775b5452727efebbb7e7e9e.tar
ceres-5eca3267d37cdc51f775b5452727efebbb7e7e9e.tar.zst
ceres-5eca3267d37cdc51f775b5452727efebbb7e7e9e.zip
model: Rework recipe validation
This reduces code duplication and enforces time stamps.
-rw-r--r--model/recipe.go43
-rw-r--r--model/validation.go28
2 files changed, 44 insertions, 27 deletions
diff --git a/model/recipe.go b/model/recipe.go
index 4594287..a9bd0fe 100644
--- a/model/recipe.go
+++ b/model/recipe.go
@@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
- "strconv"
)
type Recipe struct {
@@ -25,14 +24,12 @@ func (r Recipe) String() string {
}
func (r *Recipe) Validate() error {
+ var err error
+
if r.Id != "" {
- id, err := strconv.Atoi(r.Id)
+ err = isPositiveOrZeroInt(r.Id)
if err != nil {
- return fmt.Errorf("Recipe has non-empty ID which is no integer: %w", err)
- }
-
- if id < 0 {
- return fmt.Errorf("Recipes has negative ID: %d", id)
+ return fmt.Errorf("Invalid recipe ID: %w", err)
}
}
@@ -41,28 +38,20 @@ func (r *Recipe) Validate() error {
}
if r.Portions != "" {
- portions, err := strconv.Atoi(r.Portions)
+ err = isPositiveOrZeroInt(r.Portions)
if err != nil {
- return fmt.Errorf("Recipe has non-empty portions which is no integer: %w", err)
- }
-
- if portions < 0 {
- return fmt.Errorf("Recipes has negative number of portions: %d", portions)
+ return fmt.Errorf("Invalid recipe portions: %w", err)
}
}
- if r.Created != "" {
- _, err := strconv.Atoi(r.Created)
- if err != nil {
- return fmt.Errorf("Recipe has non-empty time stamp 'created' which is no integer: %w", err)
- }
+ err = isInt(r.Created)
+ if err != nil {
+ return fmt.Errorf("Invalid creation time stamp: %w", err)
}
- if r.LastChanged != "" {
- _, err := strconv.Atoi(r.LastChanged)
- if err != nil {
- return fmt.Errorf("Recipe has non-empty time stamp 'last_changed' which is no integer: %w", err)
- }
+ err = isInt(r.LastChanged)
+ if err != nil {
+ return fmt.Errorf("Invalid last changed time stamp: %w", err)
}
return nil
@@ -288,8 +277,8 @@ func RecipeTestData() []Recipe {
Portions: "4",
Url: "https://example.org",
Notes: "Very fluffy",
- Created: "",
- LastChanged: "",
+ Created: "1715957069",
+ LastChanged: "1715958069",
Steps: []Step{
{
Text: "Stir the dough",
@@ -313,8 +302,8 @@ func RecipeTestData() []Recipe {
Portions: "2",
Url: "https://xengineering.eu/git/ceres",
Notes: "Delicious!",
- Created: "",
- LastChanged: "",
+ Created: "1715658069",
+ LastChanged: "1715958070",
Steps: []Step{},
},
}
diff --git a/model/validation.go b/model/validation.go
new file mode 100644
index 0000000..d49141d
--- /dev/null
+++ b/model/validation.go
@@ -0,0 +1,28 @@
+package model
+
+import (
+ "fmt"
+ "strconv"
+)
+
+func isInt(s string) error {
+ _, err := strconv.Atoi(s)
+ if err != nil {
+ return fmt.Errorf("'%s' cannot be casted to integer", s)
+ }
+
+ return nil
+}
+
+func isPositiveOrZeroInt(s string) error {
+ i, err := strconv.Atoi(s)
+ if err != nil {
+ return fmt.Errorf("'%s' cannot be casted to integer", s)
+ }
+
+ if i < 0 {
+ return fmt.Errorf("'%s' is negative", s)
+ }
+
+ return nil
+}