Files
Extrudex/backend/API/Validators/FilamentValidators.cs
rex-bot ac033859a8
Some checks failed
Dev Build / build-test (pull_request) Failing after 1m3s
Dev Build / deploy-dev (pull_request) Has been skipped
Dev Build / notify-success (pull_request) Has been skipped
Dev Build / notify-failure (pull_request) Successful in 3s
feat(CUB-28): [Extrudex] Define filament inventory database entities
Add storage_location and is_archived fields to Spool entity to complete
the filament inventory entity definition per CUB-28 requirements.

Changes:
- Spool entity: add IsArchived (bool, default false) and StorageLocation
  (nullable string, max 200) for physical inventory tracking
- SpoolConfiguration: add snake_case column mappings, defaults, and indexes
  (ix_spools_is_archived, ix_spools_active_archived composite)
- FilamentDtos: add IsArchived + StorageLocation to Response, Create, Update
- FilamentQueryDtos: add IncludeArchived and StorageLocation query filters
- FilamentsController: wire new fields into query, create, update, mapping
- FilamentValidators: add StorageLocation max-length validation

Build: PASS (0 errors)
2026-04-27 18:24:52 -04:00

120 lines
4.8 KiB
C#

using Extrudex.API.DTOs.Filaments;
using FluentValidation;
namespace Extrudex.API.Validators;
/// <summary>
/// Validation rules for creating a Filament (Spool) via the /filaments route.
/// Mirrors the domain rules enforced in the controller and ensures consistent
/// validation regardless of the request pipeline entry point.
/// </summary>
public class CreateFilamentRequestValidator : AbstractValidator<CreateFilamentRequest>
{
public CreateFilamentRequestValidator()
{
RuleFor(x => x.MaterialBaseId)
.NotEmpty().WithMessage("MaterialBaseId is required.");
RuleFor(x => x.MaterialFinishId)
.NotEmpty().WithMessage("MaterialFinishId is required.");
RuleFor(x => x.Brand)
.NotEmpty().WithMessage("Brand is required.")
.MaximumLength(200).WithMessage("Brand must not exceed 200 characters.");
RuleFor(x => x.ColorName)
.NotEmpty().WithMessage("ColorName is required.")
.MaximumLength(200).WithMessage("ColorName must not exceed 200 characters.");
RuleFor(x => x.ColorHex)
.NotEmpty().WithMessage("ColorHex is required.")
.Matches(@"^#[0-9A-Fa-f]{6}$").WithMessage("ColorHex must be a valid hex color code (e.g., #FF0000).");
RuleFor(x => x.WeightTotalGrams)
.GreaterThan(0).WithMessage("Total weight must be greater than zero.");
RuleFor(x => x.WeightRemainingGrams)
.GreaterThanOrEqualTo(0).WithMessage("Remaining weight must be non-negative.");
RuleFor(x => x.WeightRemainingGrams)
.LessThanOrEqualTo(x => x.WeightTotalGrams)
.WithMessage("WeightRemainingGrams cannot exceed WeightTotalGrams.");
RuleFor(x => x.FilamentDiameterMm)
.GreaterThan(0).WithMessage("Filament diameter must be greater than zero.");
RuleFor(x => x.SpoolSerial)
.NotEmpty().WithMessage("SpoolSerial is required.")
.MaximumLength(200).WithMessage("SpoolSerial must not exceed 200 characters.");
When(x => x.PurchasePrice.HasValue, () =>
{
RuleFor(x => x.PurchasePrice!.Value)
.GreaterThanOrEqualTo(0).WithMessage("Purchase price must be non-negative.");
});
When(x => x.StorageLocation != null, () =>
{
RuleFor(x => x.StorageLocation!)
.MaximumLength(200).WithMessage("StorageLocation must not exceed 200 characters.");
});
}
}
/// <summary>
/// Validation rules for updating a Filament (Spool) via the /filaments route.
/// Enforces the same domain rules as creation, plus ensures the updated
/// WeightRemainingGrams does not exceed the updated WeightTotalGrams.
/// </summary>
public class UpdateFilamentRequestValidator : AbstractValidator<UpdateFilamentRequest>
{
public UpdateFilamentRequestValidator()
{
RuleFor(x => x.MaterialBaseId)
.NotEmpty().WithMessage("MaterialBaseId is required.");
RuleFor(x => x.MaterialFinishId)
.NotEmpty().WithMessage("MaterialFinishId is required.");
RuleFor(x => x.Brand)
.NotEmpty().WithMessage("Brand is required.")
.MaximumLength(200).WithMessage("Brand must not exceed 200 characters.");
RuleFor(x => x.ColorName)
.NotEmpty().WithMessage("ColorName is required.")
.MaximumLength(200).WithMessage("ColorName must not exceed 200 characters.");
RuleFor(x => x.ColorHex)
.NotEmpty().WithMessage("ColorHex is required.")
.Matches(@"^#[0-9A-Fa-f]{6}$").WithMessage("ColorHex must be a valid hex color code (e.g., #FF0000).");
RuleFor(x => x.WeightTotalGrams)
.GreaterThan(0).WithMessage("Total weight must be greater than zero.");
RuleFor(x => x.WeightRemainingGrams)
.GreaterThanOrEqualTo(0).WithMessage("Remaining weight must be non-negative.");
RuleFor(x => x.WeightRemainingGrams)
.LessThanOrEqualTo(x => x.WeightTotalGrams)
.WithMessage("WeightRemainingGrams cannot exceed WeightTotalGrams.");
RuleFor(x => x.FilamentDiameterMm)
.GreaterThan(0).WithMessage("Filament diameter must be greater than zero.");
RuleFor(x => x.SpoolSerial)
.NotEmpty().WithMessage("SpoolSerial is required.")
.MaximumLength(200).WithMessage("SpoolSerial must not exceed 200 characters.");
When(x => x.PurchasePrice.HasValue, () =>
{
RuleFor(x => x.PurchasePrice!.Value)
.GreaterThanOrEqualTo(0).WithMessage("Purchase price must be non-negative.");
});
When(x => x.StorageLocation != null, () =>
{
RuleFor(x => x.StorageLocation!)
.MaximumLength(200).WithMessage("StorageLocation must not exceed 200 characters.");
});
}
}