added validators in project

This commit is contained in:
2026-03-10 09:52:31 +01:00
parent 2e7b9e5154
commit 9f858df5f8
17 changed files with 558 additions and 188 deletions

View File

@@ -0,0 +1,31 @@
using BookHive.DTO.Loan;
using FastEndpoints;
using FluentValidation;
namespace BookHive.Validators.Loans;
public class CreateLoanDtoValidator : Validator<CreateLoanDto>
{
public CreateLoanDtoValidator()
{
RuleFor(x => x.BookId)
.GreaterThan(0)
.WithMessage("Invalid book id.");
RuleFor(x => x.MemberId)
.GreaterThan(0)
.WithMessage("Invalid member id.");
RuleFor(x => x.LoanDate)
.NotEmpty()
.WithMessage("Date is required.");
RuleFor(x => x.DueDate)
.NotEmpty()
.WithMessage("Due date is required.")
.GreaterThan(d => d.LoanDate)
.WithMessage("Due date must be in the future.")
.Must((loan, dueDate) => dueDate <= loan.LoanDate.AddDays(30))
.WithMessage("Due date must be in the future.");;
}
}

View File

@@ -0,0 +1,15 @@
using BookHive.Endpoints.Loans;
using FastEndpoints;
using FluentValidation;
namespace BookHive.Validators.Loans;
public class PatchLoanDtoValidator : Validator<PatchReturnLoanRequest>
{
public PatchLoanDtoValidator()
{
RuleFor(x => x.Id)
.GreaterThan(0)
.WithMessage("Id is invalid");
}
}

View File

@@ -0,0 +1,42 @@
using BookHive.DTO.Loan;
using FastEndpoints;
using FluentValidation;
namespace BookHive.Validators.Loans;
public class UpdateLoanDtoValidator : Validator<UpdateLoanDto>
{
public UpdateLoanDtoValidator()
{
RuleFor(x => x.Id)
.GreaterThan(0)
.WithMessage("Id is invalid.");
RuleFor(x => x.BookId)
.GreaterThan(0)
.WithMessage("Invalid book id.");
RuleFor(x => x.MemberId)
.GreaterThan(0)
.WithMessage("Invalid member id.");
RuleFor(x => x.LoanDate)
.NotEmpty()
.WithMessage("Date is required.");
RuleFor(x => x.DueDate)
.NotEmpty()
.WithMessage("Due date is required.")
.GreaterThan(d => d.LoanDate)
.WithMessage("Due date must be in the future.")
.Must((loan, dueDate) => dueDate <= loan.LoanDate.AddDays(30))
.WithMessage("Due date must be in the future.");
When(x => x.ReturnDate != null, () =>
{
RuleFor(x => x.ReturnDate)
.GreaterThan(d => d.LoanDate)
.WithMessage("Return date must be in the future.");
});
}
}