using FluentValidation; using Somnia.GuestLog.Models.DTOs; using Somnia.GuestLog.Services; using Somnia.GuestLog.Utils; namespace Somnia.GuestLog.Validators; /// /// Validator for GuestDocumentDTO which contains multiple rules and can be shared with the back end API /// public class GuestDocumentDTOValidator : ValueValidator { public GuestDocumentDTOValidator(IDataListService dataListService) { RuleFor(x => x.DocumentType) .Cascade(CascadeMode.Stop) .NotEmpty() .Must(val => val != null && dataListService.IsValueInList(GuestConsts.DataList_DocumentTypes, val)); RuleFor(x => x.DocumentNum) .NotEmpty() .Length(6, 32); RuleFor(x => x.IssueDate) .Cascade(CascadeMode.Stop) .NotEmpty() .When(x => x.Nationality != GuestConsts.CountryCode_ESP) .LessThanOrEqualTo(DateTime.Today); RuleFor(x => x.SupportNum) .Cascade(CascadeMode.Stop) .NotEmpty() .Matches(RegexUtils.RegexGuestDocSupportNumberDNI()).WithMessage("Invalid Format for DNI. Expected 'AAA000000'.") .When(x => x.DocumentType == GuestConsts.DocumentType_DNI); RuleFor(x => x.SupportNum) .Cascade(CascadeMode.Stop) .NotEmpty() .Matches(RegexUtils.RegexGuestDocSupportNumberNIE()).WithMessage("Invalid Format for NIE. Expected 'E00000000'.") .When(x => x.DocumentType == GuestConsts.DocumentType_NIE); RuleFor(x => x.Name) .NotEmpty() .Length(1, 100); RuleFor(x => x.Surname) .NotEmpty() .Length(1, 100); RuleFor(x => x.BirthDate) .NotEmpty() .LessThanOrEqualTo(DateTime.Today); RuleFor(x => x.Nationality) .Cascade(CascadeMode.Stop) .NotEmpty() .Must(val => val != null && dataListService.IsValueInList(GuestConsts.DataList_CountryCodes, val)); //Contact Fields RuleFor(x => x.EmailAddress) .EmailAddress() .When(x => !string.IsNullOrEmpty(x.EmailAddress)); RuleFor(x => x.PhoneNumber) .Matches(RegexUtils.RegexPhoneNumber()).WithMessage("Invalid format") .When(x => !string.IsNullOrEmpty(x.PhoneNumber)); RuleFor(x => x.AddressCountry) .NotEmpty() .Must(val => val != null && dataListService.IsValueInList(GuestConsts.DataList_CountryCodes, val)); // RuleFor(x => x.CCNumber) // .NotEmpty() // .Length(1, 100) // .CreditCard(); // RuleFor(x => x.Address.Address) // .NotEmpty() // .Length(1, 100); // RuleFor(x => x.Address.City) // .NotEmpty() // .Length(1, 100); // RuleFor(x => x.Address.Country) // .NotEmpty() // .Length(1, 100); // RuleForEach(x => x.OrderDetails) // .SetValidator(new OrderDetailsModelFluentValidator()); } }