-
Notifications
You must be signed in to change notification settings - Fork 75
/
cartValidator.go
60 lines (51 loc) · 1.28 KB
/
cartValidator.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
package validation
import (
"context"
"flamingo.me/flamingo-commerce/v3/cart/domain/decorator"
"flamingo.me/flamingo/v3/framework/web"
)
type (
// Result groups the validation result
Result struct {
HasCommonError bool
CommonErrorMessageKey string
ItemResults []ItemValidationError
}
// ItemValidationError applies for a single item
ItemValidationError struct {
ItemID string
ErrorMessageKey string
}
// Validator checks a complete decorated cart
Validator interface {
Validate(ctx context.Context, session *web.Session, cart *decorator.DecoratedCart) Result
}
)
// IsValid is valid is true if no errors occurred
func (c Result) IsValid() bool {
if c.HasCommonError {
return false
}
if len(c.ItemResults) > 0 {
return false
}
return true
}
// HasErrorForItem checks if a specified item has an error
func (c Result) HasErrorForItem(id string) bool {
for _, itemMessage := range c.ItemResults {
if itemMessage.ItemID == id {
return true
}
}
return false
}
// GetErrorMessageKeyForItem returns the specific error message for that item
func (c Result) GetErrorMessageKeyForItem(id string) string {
for _, itemMessage := range c.ItemResults {
if itemMessage.ItemID == id {
return itemMessage.ErrorMessageKey
}
}
return ""
}