autobus-api_old/AutobusApi.Application/Companies/Commands/CreateCompany/CreateCompanyCommandValidator.cs

48 lines
2.3 KiB
C#

using AutobusApi.Domain.Enums;
using FluentValidation;
namespace AutobusApi.Application.Companies.Commands.CreateCompany;
public class CreateCompanyCommandValidator : AbstractValidator<CreateCompanyCommand>
{
public CreateCompanyCommandValidator()
{
RuleFor(v => v.Name).MinimumLength(2).MaximumLength(64);
RuleFor(v => v.LegalAddress).MinimumLength(2).MaximumLength(256);
RuleFor(v => v.ContactPhoneNumber)
.NotEmpty().WithMessage("Phone number is required.")
.Matches(@"^\s*(?:\+?(\d{1,3}))?([-. (]*(\d{3})[-. )]*)?((\d{3})[-. ]*(\d{2,4})(?:[-.x ]*(\d+))?)\s*$").WithMessage("Phone number is invalid.");
RuleFor(v => v.ContactEmail)
.NotEmpty().WithMessage("Email address is required.")
.Matches(@"\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b").WithMessage("Email address is invalid.");
RuleFor(v => v.OwnerEmail)
.NotEmpty().WithMessage("Email address is required.")
.Matches(@"\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b").WithMessage("Email address is invalid.");
RuleFor(v => v.OwnerPassword)
.NotEmpty().WithMessage("Password is required.")
.MinimumLength(8).WithMessage("Password must be at least 8 characters long.")
.MaximumLength(64).WithMessage("Password must be at most 64 characters long.")
.Matches(@"(?=.*[A-Z]).*").WithMessage("Password must contain at least one uppercase letter.")
.Matches(@"(?=.*[a-z]).*").WithMessage("Password must contain at least one lowercase letter.")
.Matches(@"(?=.*[\d]).*").WithMessage("Password must contain at least one digit.")
.Matches(@"(?=.*[!@#$%^&*()]).*").WithMessage("Password must contain at least one of the following special charactters: !@#$%^&*().");
RuleFor(v => v.OwnerFirstName).MinimumLength(2).MaximumLength(32);
RuleFor(v => v.OwnerLastName).MinimumLength(2).MaximumLength(32);
RuleFor(v => v.OwnerPatronymic).MinimumLength(2).MaximumLength(32);
RuleFor(v => v.OwnerSex).Must(value => Enum.TryParse<Sex>(value, true, out _));
RuleFor(v => v.OwnerDocumentType).Must(value => Enum.TryParse<EmployeeDocumentType>(value, true, out _));
RuleFor(v => v.OwnerDocumentInformation).MinimumLength(2).MaximumLength(256);
}
}