65 lines
1.9 KiB
Go
65 lines
1.9 KiB
Go
|
|
package validator
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"apskel-pos-be/internal/constants"
|
||
|
|
"apskel-pos-be/internal/contract"
|
||
|
|
|
||
|
|
"github.com/google/uuid"
|
||
|
|
)
|
||
|
|
|
||
|
|
type ProductVariantValidator interface {
|
||
|
|
ValidateCreateProductVariantRequest(req *contract.CreateProductVariantRequest) (error, string)
|
||
|
|
ValidateUpdateProductVariantRequest(req *contract.UpdateProductVariantRequest) (error, string)
|
||
|
|
}
|
||
|
|
|
||
|
|
type ProductVariantValidatorImpl struct{}
|
||
|
|
|
||
|
|
func NewProductVariantValidator() *ProductVariantValidatorImpl {
|
||
|
|
return &ProductVariantValidatorImpl{}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (v *ProductVariantValidatorImpl) ValidateCreateProductVariantRequest(req *contract.CreateProductVariantRequest) (error, string) {
|
||
|
|
if req == nil {
|
||
|
|
return errors.New("request body is required"), constants.MissingFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
if req.ProductID == uuid.Nil {
|
||
|
|
return errors.New("product_id is required"), constants.MissingFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
if strings.TrimSpace(req.Name) == "" {
|
||
|
|
return errors.New("name is required"), constants.MissingFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
if len(req.Name) < 1 || len(req.Name) > 255 {
|
||
|
|
return errors.New("name must be between 1 and 255 characters"), constants.MalformedFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil, ""
|
||
|
|
}
|
||
|
|
|
||
|
|
func (v *ProductVariantValidatorImpl) ValidateUpdateProductVariantRequest(req *contract.UpdateProductVariantRequest) (error, string) {
|
||
|
|
if req == nil {
|
||
|
|
return errors.New("request body is required"), constants.MissingFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
// At least one field should be provided for update
|
||
|
|
if req.Name == nil && req.PriceModifier == nil && req.Metadata == nil {
|
||
|
|
return errors.New("at least one field must be provided for update"), constants.MissingFieldErrorCode
|
||
|
|
}
|
||
|
|
|
||
|
|
if req.Name != nil {
|
||
|
|
if strings.TrimSpace(*req.Name) == "" {
|
||
|
|
return errors.New("name cannot be empty"), constants.MalformedFieldErrorCode
|
||
|
|
}
|
||
|
|
if len(*req.Name) < 1 || len(*req.Name) > 255 {
|
||
|
|
return errors.New("name must be between 1 and 255 characters"), constants.MalformedFieldErrorCode
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil, ""
|
||
|
|
}
|