Skip to content

Commit

Permalink
Fluent Validation Added
Browse files Browse the repository at this point in the history
  • Loading branch information
rahiyansafin committed May 4, 2023
1 parent 22e52e6 commit 19b01d4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace HR.LeaveManagement.Application.Contracts.Persistence;

public interface ILeaveTypeRepository : IGenericRepository<LeaveType>
{

Task<bool> IsLeaveTypeUnique(string name);
}
13 changes: 12 additions & 1 deletion HR.LeaveManagement.Application/Exceptions/BadRequestException.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
namespace HR.LeaveManagement.Application.Exceptions;
using FluentValidation.Results;

namespace HR.LeaveManagement.Application.Exceptions;

public class BadRequestException : Exception
{
public BadRequestException(string message) : base(message)
{

}

public BadRequestException(string message, ValidationResult validationResult) : base(message)
{
Errors = new();
foreach (var error in validationResult.Errors)
Errors.Add(error.ErrorMessage);
}

public List<string> Errors { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AutoMapper;

using HR.LeaveManagement.Application.Contracts.Persistence;
using HR.LeaveManagement.Application.Exceptions;

using MediatR;

Expand All @@ -18,6 +19,13 @@ public CreateLeaveTypeCommandHandler(IMapper mapper, ILeaveTypeRepository leaveT

public async Task<int> Handle(CreateLeaveTypeCommand request, CancellationToken cancellationToken)
{
// Validate incoming data
var validator = new CreateLeaveTypeCommandValidator(_leaveTypeRepository);
var validationResult = await validator.ValidateAsync(request);

if (validationResult.Errors.Any())
throw new BadRequestException("Invalid leave type", validationResult);

// Convert to domain entity object
var leaveTypeToCreate = _mapper.Map<Domain.LeaveType>(request);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using FluentValidation;

using HR.LeaveManagement.Application.Contracts.Persistence;

namespace HR.LeaveManagement.Application.Features.LeaveType.Commands.CreateLeaveType;
public class CreateLeaveTypeCommandValidator : AbstractValidator<CreateLeaveTypeCommand>
{
private readonly ILeaveTypeRepository _leaveTypeRepository;

public CreateLeaveTypeCommandValidator(ILeaveTypeRepository leaveTypeRepository)
{
RuleFor(p => p.Name)
.NotEmpty().WithMessage("{PropertyName} is required")
.NotNull()
.MaximumLength(70).WithMessage("{PropertyName} must be fewer than 70 characters");

RuleFor(p => p.DefaultDays)
.LessThan(100).WithMessage("{PropertyName} cannot exceed 100")
.GreaterThan(1).WithMessage("{PropertyName} cannot be less than 1");

RuleFor(q => q)
.MustAsync(LeaveTypeNameUnique)
.WithMessage("Leave type already exists!");

_leaveTypeRepository = leaveTypeRepository;
}

private Task<bool> LeaveTypeNameUnique(CreateLeaveTypeCommand command, CancellationToken ctoken)
{
return _leaveTypeRepository.IsLeaveTypeUnique(command.Name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="FluentValidation" Version="11.5.2" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.5.2" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.1.0" />
</ItemGroup>

Expand Down

0 comments on commit 19b01d4

Please sign in to comment.