using FluentValidation; using SeaHaven.Services.DTOs; namespace SeaHaven.Services.Validation { public interface ICreateVendorValidation : IValidator { } public class CreateVendorValidation : AbstractValidator, 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 { } public class UpdateVendorValidation : AbstractValidator, 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; } }