SHA256
121 lines
4.2 KiB
C#
121 lines
4.2 KiB
C#
using FluentValidation;
|
|
using Somnia.GuestLog.Models.DTOs;
|
|
using Somnia.GuestLog.Resources;
|
|
using Somnia.GuestLog.Services;
|
|
using Somnia.GuestLog.Utils;
|
|
|
|
namespace Somnia.GuestLog.Validators;
|
|
|
|
/// <summary>
|
|
/// Validator for GuestDocumentDTO which contains multiple rules and can be shared with the back end API
|
|
/// </summary>
|
|
public class GuestDocumentDTOValidator : ValueValidator<GuestDocumentDTO>
|
|
{
|
|
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(x => DateTime.Today);
|
|
|
|
RuleFor(x => x.SupportNum)
|
|
.Cascade(CascadeMode.Stop)
|
|
.NotEmpty()
|
|
.Matches(RegexUtils.RegexGuestDocSupportNumberDNI())
|
|
.WithMessage((_) => LanguageLocale.dto_GuestDocument_validation_FormatSupportNumDNI)
|
|
.When(x => x.DocumentType == GuestConsts.DocumentType_DNI);
|
|
|
|
RuleFor(x => x.SupportNum)
|
|
.Cascade(CascadeMode.Stop)
|
|
.NotEmpty()
|
|
.Matches(RegexUtils.RegexGuestDocSupportNumberNIE())
|
|
.WithMessage((_) => LanguageLocale.dto_GuestDocument_validation_FormatSupportNumNIE)
|
|
.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(x => 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((_) => LanguageLocale.general_validation_invalidFormat)
|
|
.When(x => !string.IsNullOrEmpty(x.PhoneNumber));
|
|
|
|
// Phone is required if mail is empty.
|
|
RuleFor(x => x.PhoneNumber)
|
|
.NotNull()
|
|
.When(x => string.IsNullOrEmpty(x.EmailAddress))
|
|
.WithMessage((_) => LanguageLocale.dto_GuestDocument_validation_Contact);
|
|
|
|
|
|
RuleFor(x => x.Address)
|
|
.NotEmpty()
|
|
.Length(1, 100);
|
|
|
|
//For Spain Address -> City and Province are required.
|
|
RuleFor(x => x.AddressCity)
|
|
.Cascade(CascadeMode.Stop)
|
|
.NotEmpty()
|
|
.When(x => x.AddressCountry == GuestConsts.CountryCode_ESP)
|
|
.MaximumLength(100);
|
|
RuleFor(x => x.AddressProvince)
|
|
.NotEmpty()
|
|
.When(x => x.AddressCountry == GuestConsts.CountryCode_ESP)
|
|
.MaximumLength(100);
|
|
|
|
//For other countries -> At least 1 field
|
|
RuleFor(x => x.AddressCity)
|
|
.Cascade(CascadeMode.Stop)
|
|
.NotEmpty()
|
|
.When(x => x.AddressCountry != GuestConsts.CountryCode_ESP
|
|
&& string.IsNullOrEmpty(x.AddressProvince) && string.IsNullOrEmpty(x.AddressState))
|
|
.WithMessage((_) => LanguageLocale.dto_GuestDocument_validation_Address)
|
|
.MaximumLength(100);
|
|
|
|
RuleFor(x => x.ZipCode)
|
|
.NotEmpty()
|
|
.Length(1, 20);
|
|
|
|
RuleFor(x => x.AddressCountry)
|
|
.NotEmpty()
|
|
.Must(val => val != null && dataListService.IsValueInList(GuestConsts.DataList_CountryCodes, val));
|
|
|
|
// RuleFor(x => x.CCNumber)
|
|
// .NotEmpty()
|
|
// .Length(1, 100)
|
|
// .CreditCard();
|
|
|
|
// RuleForEach(x => x.OrderDetails)
|
|
// .SetValidator(new OrderDetailsModelFluentValidator());
|
|
}
|
|
}
|