summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--model/recipe.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/model/recipe.go b/model/recipe.go
index 7aebad4..4594287 100644
--- a/model/recipe.go
+++ b/model/recipe.go
@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
+ "strconv"
)
type Recipe struct {
@@ -24,10 +25,46 @@ func (r Recipe) String() string {
}
func (r *Recipe) Validate() error {
+ if r.Id != "" {
+ id, err := strconv.Atoi(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)
+ }
+ }
+
if r.Title == "" {
return fmt.Errorf("Recipes must have a title")
}
+ if r.Portions != "" {
+ portions, err := strconv.Atoi(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)
+ }
+ }
+
+ 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)
+ }
+ }
+
+ 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)
+ }
+ }
+
return nil
}