shoc-backend/SeaHaven.Services/Validation/VendorValidation.cs
Alexandre Brandizzi 1162c68596
feat(vendors): add directory filters and details API (#25)
* feat(vendors): add directory filters and details API

* fix(vendors): preserve omitted status

* fix(vendors): align facet filtering

* fix(vendors): address directory review findings
2026-07-23 15:55:47 +00:00

129 lines
5.4 KiB
C#

using FluentValidation;
using SeaHaven.Services.DTOs;
namespace SeaHaven.Services.Validation
{
public interface ICreateVendorValidation : IValidator<CreateVendorDTO>
{
}
public class CreateVendorValidation : AbstractValidator<CreateVendorDTO>, ICreateVendorValidation
{
public CreateVendorValidation()
{
RuleFor(x => x.Name)
.NotEmpty().WithMessage("Vendor name is required")
.MaximumLength(200).WithMessage("Vendor name cannot exceed 200 characters");
RuleFor(x => x.Email)
.EmailAddress().WithMessage("Invalid email address")
.When(x => !string.IsNullOrEmpty(x.Email));
RuleFor(x => x.ContactName)
.NotEmpty().WithMessage("Technician name is required")
.MaximumLength(200).WithMessage("Contact name cannot exceed 200 characters");
RuleFor(x => x.TradeSpecialties)
.MaximumLength(1000).WithMessage("Trade specialties cannot exceed 1000 characters")
.When(x => !string.IsNullOrEmpty(x.TradeSpecialties));
RuleFor(x => x.Phone)
.MaximumLength(20).WithMessage("Phone number cannot exceed 20 characters")
.When(x => !string.IsNullOrEmpty(x.Phone));
RuleFor(x => x.CompanyPhone)
.MaximumLength(20).WithMessage("Company phone cannot exceed 20 characters")
.When(x => !string.IsNullOrEmpty(x.CompanyPhone));
RuleFor(x => x.Address)
.MaximumLength(500).WithMessage("Address cannot exceed 500 characters")
.When(x => !string.IsNullOrEmpty(x.Address));
RuleFor(x => x.City)
.MaximumLength(100).WithMessage("City cannot exceed 100 characters")
.When(x => !string.IsNullOrEmpty(x.City));
RuleFor(x => x.State)
.MaximumLength(2).WithMessage("State must be 2 characters")
.When(x => !string.IsNullOrEmpty(x.State));
RuleFor(x => x.Zipcode)
.MaximumLength(10).WithMessage("Zipcode cannot exceed 10 characters")
.When(x => !string.IsNullOrEmpty(x.Zipcode));
RuleFor(x => x.GoogleMapsUrl)
.Must(VendorValidationRules.BeValidHttpsUrl).WithMessage("Google Maps URL must be a valid HTTPS URL")
.When(x => !string.IsNullOrEmpty(x.GoogleMapsUrl));
RuleFor(x => x.Notes)
.MaximumLength(2000).WithMessage("Notes cannot exceed 2000 characters")
.When(x => !string.IsNullOrEmpty(x.Notes));
}
}
public interface IUpdateVendorValidation : IValidator<UpdateVendorDTO>
{
}
public class UpdateVendorValidation : AbstractValidator<UpdateVendorDTO>, IUpdateVendorValidation
{
public UpdateVendorValidation()
{
RuleFor(x => x.Name)
.MaximumLength(200).WithMessage("Vendor name cannot exceed 200 characters")
.When(x => !string.IsNullOrEmpty(x.Name));
RuleFor(x => x.Email)
.EmailAddress().WithMessage("Invalid email address")
.When(x => !string.IsNullOrEmpty(x.Email));
RuleFor(x => x.ContactName)
.NotEmpty().WithMessage("Technician name is required")
.MaximumLength(200).WithMessage("Contact name cannot exceed 200 characters")
.When(x => x.ContactName != null);
RuleFor(x => x.TradeSpecialties)
.MaximumLength(1000).WithMessage("Trade specialties cannot exceed 1000 characters")
.When(x => !string.IsNullOrEmpty(x.TradeSpecialties));
RuleFor(x => x.Phone)
.MaximumLength(20).WithMessage("Phone number cannot exceed 20 characters")
.When(x => !string.IsNullOrEmpty(x.Phone));
RuleFor(x => x.CompanyPhone)
.MaximumLength(20).WithMessage("Company phone cannot exceed 20 characters")
.When(x => !string.IsNullOrEmpty(x.CompanyPhone));
RuleFor(x => x.Address)
.MaximumLength(500).WithMessage("Address cannot exceed 500 characters")
.When(x => !string.IsNullOrEmpty(x.Address));
RuleFor(x => x.City)
.MaximumLength(100).WithMessage("City cannot exceed 100 characters")
.When(x => !string.IsNullOrEmpty(x.City));
RuleFor(x => x.State)
.MaximumLength(2).WithMessage("State must be 2 characters")
.When(x => !string.IsNullOrEmpty(x.State));
RuleFor(x => x.Zipcode)
.MaximumLength(10).WithMessage("Zipcode cannot exceed 10 characters")
.When(x => !string.IsNullOrEmpty(x.Zipcode));
RuleFor(x => x.GoogleMapsUrl)
.Must(VendorValidationRules.BeValidHttpsUrl).WithMessage("Google Maps URL must be a valid HTTPS URL")
.When(x => !string.IsNullOrEmpty(x.GoogleMapsUrl));
RuleFor(x => x.Notes)
.MaximumLength(2000).WithMessage("Notes cannot exceed 2000 characters")
.When(x => !string.IsNullOrEmpty(x.Notes));
}
}
internal static class VendorValidationRules
{
internal static bool BeValidHttpsUrl(string? value) =>
Uri.TryCreate(value, UriKind.Absolute, out var uri) && uri.Scheme == Uri.UriSchemeHttps;
}
}