Skip to content

Commit

Permalink
feat: implement validation logic that ensures that receipt total is a…
Browse files Browse the repository at this point in the history
… valid number
  • Loading branch information
kweeuhree committed Jan 5, 2025
1 parent af77f83 commit 1476224
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/handlers/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ func (input *ReceiptInput) Validate() {
input.CheckField(v.ValidTime(input.PurchaseTime), "purchaseTime", "This field must be valid time")
input.CheckField(v.NotBlank(input.PurchaseTime), "purchaseTime", "This field cannot be blank")
input.CheckField(v.NotBlank(input.Total), "total", "This field cannot be blank")
input.CheckField(v.ValidNumber(input.Total), "total", "This field must be a valid number")
input.CheckField(v.ItemsNotEmpty(input.Items), "items", "This field must have at least one object")
}
7 changes: 7 additions & 0 deletions internal/validator/validator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validator

import (
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -64,3 +65,9 @@ func (v *Validator) ValidTime(timeString string) bool {
_, err := time.Parse(layout, timeString)
return err == nil
}

// Returns true if a total is valid number
func (v *Validator) ValidNumber(total string) bool {
_, err := strconv.ParseFloat(total, 64)
return err == nil
}

0 comments on commit 1476224

Please sign in to comment.