shoc-backend/SeaHaven.Services/Validation/EmployeeValidation.cs
2026-05-14 11:00:12 -05:00

81 lines
3.1 KiB
C#

using FluentValidation;
using SeaHaven.Services.DTOs;
namespace SeaHaven.Services.Validation
{
public interface ICreateEmployeeValidation : IValidator<CreateEmployeeDTO>
{
}
public class CreateEmployeeValidation : AbstractValidator<CreateEmployeeDTO>, ICreateEmployeeValidation
{
public CreateEmployeeValidation()
{
RuleFor(x => x.FirstName)
.NotEmpty().WithMessage("First name is required")
.MaximumLength(100).WithMessage("First name cannot exceed 100 characters");
RuleFor(x => x.LastName)
.NotEmpty().WithMessage("Last name is required")
.MaximumLength(100).WithMessage("Last name cannot exceed 100 characters");
RuleFor(x => x.Email)
.EmailAddress().WithMessage("Invalid email address")
.When(x => !string.IsNullOrEmpty(x.Email));
RuleFor(x => x.Phone)
.MaximumLength(20).WithMessage("Phone number cannot exceed 20 characters")
.When(x => !string.IsNullOrEmpty(x.Phone));
RuleFor(x => x.DepartmentId)
.GreaterThan(0).WithMessage("Valid department must be selected")
.When(x => x.DepartmentId.HasValue);
RuleFor(x => x.JobTitleId)
.GreaterThan(0).WithMessage("Valid job title must be selected")
.When(x => x.JobTitleId.HasValue);
RuleFor(x => x.RegionId)
.GreaterThan(0).WithMessage("Valid region must be selected")
.When(x => x.RegionId.HasValue);
}
}
public interface IUpdateEmployeeValidation : IValidator<UpdateEmployeeDTO>
{
}
public class UpdateEmployeeValidation : AbstractValidator<UpdateEmployeeDTO>, IUpdateEmployeeValidation
{
public UpdateEmployeeValidation()
{
RuleFor(x => x.FirstName)
.MaximumLength(100).WithMessage("First name cannot exceed 100 characters")
.When(x => !string.IsNullOrEmpty(x.FirstName));
RuleFor(x => x.LastName)
.MaximumLength(100).WithMessage("Last name cannot exceed 100 characters")
.When(x => !string.IsNullOrEmpty(x.LastName));
RuleFor(x => x.Email)
.EmailAddress().WithMessage("Invalid email address")
.When(x => !string.IsNullOrEmpty(x.Email));
RuleFor(x => x.Phone)
.MaximumLength(20).WithMessage("Phone number cannot exceed 20 characters")
.When(x => !string.IsNullOrEmpty(x.Phone));
RuleFor(x => x.DepartmentId)
.GreaterThan(0).WithMessage("Valid department must be selected")
.When(x => x.DepartmentId.HasValue);
RuleFor(x => x.JobTitleId)
.GreaterThan(0).WithMessage("Valid job title must be selected")
.When(x => x.JobTitleId.HasValue);
RuleFor(x => x.RegionId)
.GreaterThan(0).WithMessage("Valid region must be selected")
.When(x => x.RegionId.HasValue);
}
}
}