flamingo.me/flamingo-commerce/v3@v3.11.0/cart/domain/validation/cartValidator.go (about) 1 package validation 2 3 import ( 4 "context" 5 6 "flamingo.me/flamingo-commerce/v3/cart/domain/decorator" 7 8 "flamingo.me/flamingo/v3/framework/web" 9 ) 10 11 type ( 12 // Result groups the validation result 13 Result struct { 14 HasCommonError bool 15 CommonErrorMessageKey string 16 ItemResults []ItemValidationError 17 } 18 19 // ItemValidationError applies for a single item 20 ItemValidationError struct { 21 ItemID string 22 ErrorMessageKey string 23 } 24 25 // Validator checks a complete decorated cart 26 Validator interface { 27 Validate(ctx context.Context, session *web.Session, cart *decorator.DecoratedCart) Result 28 } 29 ) 30 31 // IsValid is valid is true if no errors occurred 32 func (c Result) IsValid() bool { 33 if c.HasCommonError { 34 return false 35 } 36 if len(c.ItemResults) > 0 { 37 return false 38 } 39 return true 40 } 41 42 // HasErrorForItem checks if a specified item has an error 43 func (c Result) HasErrorForItem(id string) bool { 44 for _, itemMessage := range c.ItemResults { 45 if itemMessage.ItemID == id { 46 return true 47 } 48 } 49 return false 50 } 51 52 // GetErrorMessageKeyForItem returns the specific error message for that item 53 func (c Result) GetErrorMessageKeyForItem(id string) string { 54 for _, itemMessage := range c.ItemResults { 55 if itemMessage.ItemID == id { 56 return itemMessage.ErrorMessageKey 57 } 58 } 59 return "" 60 }