126 lines
4.5 KiB
C#
126 lines
4.5 KiB
C#
using cuqmbr.TravelGuide.Application.Common.FluentValidation;
|
|
using cuqmbr.TravelGuide.Application.Common.Services;
|
|
using cuqmbr.TravelGuide.Domain.Enums;
|
|
using FluentValidation;
|
|
using Microsoft.Extensions.Localization;
|
|
|
|
namespace cuqmbr.TravelGuide.Application.Employees.Commands.AddEmployee;
|
|
|
|
public class AddEmployeeCommandValidator : AbstractValidator<AddEmployeeCommand>
|
|
{
|
|
public AddEmployeeCommandValidator(
|
|
IStringLocalizer localizer,
|
|
SessionCultureService cultureService)
|
|
{
|
|
RuleFor(e => e.FirstName)
|
|
.NotEmpty()
|
|
.WithMessage(localizer["FluentValidation.NotEmpty"])
|
|
.MaximumLength(32)
|
|
.WithMessage(
|
|
String.Format(
|
|
cultureService.Culture,
|
|
localizer["FluentValidation.MaximumLength"],
|
|
32));
|
|
|
|
RuleFor(e => e.LastName)
|
|
.NotEmpty()
|
|
.WithMessage(localizer["FluentValidation.NotEmpty"])
|
|
.MaximumLength(32)
|
|
.WithMessage(
|
|
String.Format(
|
|
cultureService.Culture,
|
|
localizer["FluentValidation.MaximumLength"],
|
|
32));
|
|
|
|
RuleFor(e => e.Patronymic)
|
|
.NotEmpty()
|
|
.WithMessage(localizer["FluentValidation.NotEmpty"])
|
|
.MaximumLength(32)
|
|
.WithMessage(
|
|
String.Format(
|
|
cultureService.Culture,
|
|
localizer["FluentValidation.MaximumLength"],
|
|
32));
|
|
|
|
RuleFor(e => e.Sex)
|
|
.Must((e, s) => Sex.Enumerations.ContainsValue(s))
|
|
.WithMessage(
|
|
String.Format(
|
|
localizer["FluentValidation.MustBeInEnum"],
|
|
String.Join(
|
|
", ",
|
|
Sex.Enumerations.Values.Select(e => e.Name))));
|
|
|
|
RuleFor(e => e.BirthDate)
|
|
.GreaterThanOrEqualTo(DateOnly.FromDateTime(DateTime.UtcNow.AddYears(-100)))
|
|
.WithMessage(
|
|
String.Format(
|
|
cultureService.Culture,
|
|
localizer["FluentValidation.GreaterThanOrEqualTo"],
|
|
DateOnly.FromDateTime(DateTime.UtcNow.AddYears(-100))));
|
|
|
|
RuleForEach(e => e.Documents).ChildRules(d =>
|
|
{
|
|
d.RuleFor(d => d.DocumentType)
|
|
.Must(dt => DocumentType.Enumerations.ContainsValue(dt))
|
|
.WithMessage(
|
|
String.Format(
|
|
localizer["FluentValidation.MustBeInEnum"],
|
|
String.Join(
|
|
", ",
|
|
DocumentType.Enumerations.Values.Select(e => e.Name))));
|
|
|
|
d.RuleFor(d => d.Information)
|
|
.NotEmpty()
|
|
.WithMessage(localizer["FluentValidation.NotEmpty"])
|
|
.MaximumLength(256)
|
|
.WithMessage(
|
|
String.Format(
|
|
cultureService.Culture,
|
|
localizer["FluentValidation.MaximumLength"],
|
|
256));
|
|
});
|
|
|
|
|
|
RuleFor(v => v.Username)
|
|
.NotEmpty()
|
|
.WithMessage(localizer["FluentValidation.NotEmpty"])
|
|
.MinimumLength(1)
|
|
.WithMessage(
|
|
String.Format(
|
|
cultureService.Culture,
|
|
localizer["FluentValidation.MinimumLength"],
|
|
1))
|
|
.MaximumLength(32)
|
|
.WithMessage(
|
|
String.Format(
|
|
cultureService.Culture,
|
|
localizer["FluentValidation.MaximumLength"],
|
|
32))
|
|
.IsUsername()
|
|
.WithMessage(localizer["FluentValidation.IsUsername"]);
|
|
|
|
RuleFor(v => v.Email)
|
|
.NotEmpty()
|
|
.WithMessage(localizer["FluentValidation.NotEmpty"])
|
|
.IsEmail()
|
|
.WithMessage(localizer["FluentValidation.IsEmail"]);
|
|
|
|
RuleFor(v => v.Password)
|
|
.NotEmpty()
|
|
.WithMessage(localizer["FluentValidation.NotEmpty"])
|
|
.MinimumLength(8)
|
|
.WithMessage(
|
|
String.Format(
|
|
cultureService.Culture,
|
|
localizer["FluentValidation.MinimumLength"],
|
|
8))
|
|
.MaximumLength(64)
|
|
.WithMessage(
|
|
String.Format(
|
|
cultureService.Culture,
|
|
localizer["FluentValidation.MaximumLength"],
|
|
64));
|
|
}
|
|
}
|