Files
Extrudex/backend/internal/services/validation.go
Joshua fca2ef5b84
Some checks failed
Dev Build / build-test (pull_request) Failing after 2m4s
CUB-113: implement core CRUD API endpoints
- Add dtos package with request/response structs
- Add repositories: Material, Filament, Printer, PrintJob, UsageLog
- Add services: FilamentService, PrinterService, PrintJobService
- Add handlers for all 5 resources with consistent error responses
- Wire all endpoints into Chi router under /api
- Validation on POST/PUT filament endpoints
- Filter/pagination support on list endpoints
- Soft-delete for filaments (DELETE /api/filaments/{id})
- go build ./... && go vet ./... → PASS
2026-05-06 14:24:58 -04:00

75 lines
2.2 KiB
Go

package services
import (
"errors"
"fmt"
"regexp"
"github.com/CubeCraft-Creations/Extrudex/backend/internal/dtos"
"github.com/CubeCraft-Creations/Extrudex/backend/internal/models"
)
// colorHexPattern validates hex color strings like #FF0000 or #ff0000.
var colorHexPattern = regexp.MustCompile(`^#[0-9A-Fa-f]{6}$`)
// validateFilamentSpool performs validation on a FilamentSpool entity.
// Returns a descriptive error on failure.
func validateFilamentSpool(s *models.FilamentSpool) error {
if s.Name == "" {
return errors.New("name is required")
}
if s.MaterialBaseID <= 0 {
return errors.New("material_base_id is required")
}
if s.MaterialFinishID <= 0 {
return errors.New("material_finish_id is required")
}
if !colorHexPattern.MatchString(s.ColorHex) {
return fmt.Errorf("color_hex must be a valid hex color (e.g., #FF0000)")
}
if s.InitialGrams <= 0 {
return errors.New("initial_grams must be greater than 0")
}
if s.RemainingGrams < 0 {
return errors.New("remaining_grams must be >= 0")
}
return nil
}
// ValidateCreateFilamentRequest validates a creation DTO.
func ValidateCreateFilamentRequest(req dtos.CreateFilamentRequest) error {
if req.Name == "" {
return errors.New("name is required")
}
if req.MaterialBaseID <= 0 {
return errors.New("material_base_id is required")
}
if req.MaterialFinishID <= 0 {
return errors.New("material_finish_id is required")
}
if !colorHexPattern.MatchString(req.ColorHex) {
return fmt.Errorf("color_hex must be a valid hex color (e.g., #FF0000)")
}
if req.InitialGrams <= 0 {
return errors.New("initial_grams must be greater than 0")
}
if req.RemainingGrams < 0 {
return errors.New("remaining_grams must be >= 0")
}
return nil
}
// ValidateUpdateFilamentRequest validates partial update fields.
func ValidateUpdateFilamentRequest(req dtos.UpdateFilamentRequest) error {
if req.ColorHex != nil && !colorHexPattern.MatchString(*req.ColorHex) {
return fmt.Errorf("color_hex must be a valid hex color (e.g., #FF0000)")
}
if req.InitialGrams != nil && *req.InitialGrams <= 0 {
return errors.New("initial_grams must be greater than 0")
}
if req.RemainingGrams != nil && *req.RemainingGrams < 0 {
return errors.New("remaining_grams must be >= 0")
}
return nil
}