added validators in project
This commit is contained in:
46
BookHive/Validators/Books/CreateBookDtoValidator.cs
Normal file
46
BookHive/Validators/Books/CreateBookDtoValidator.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using BookHive.DTO.Book;
|
||||
using FastEndpoints;
|
||||
using FluentValidation;
|
||||
|
||||
namespace BookHive.Validators.Books;
|
||||
|
||||
public class CreateBookDtoValidator : Validator<CreateBookDto>
|
||||
{
|
||||
public CreateBookDtoValidator()
|
||||
{
|
||||
RuleFor(x => x.Title)
|
||||
.NotEmpty().WithMessage("Le titre est obligatoire.")
|
||||
.MaximumLength(200)
|
||||
.WithMessage("Le titre ne peut pas dépasser 200 caractères.");
|
||||
|
||||
RuleFor(x => x.Isbn)
|
||||
.NotEmpty().WithMessage("L’ISBN est obligatoire.")
|
||||
.Matches(@"^\d{13}$")
|
||||
.WithMessage("L’ISBN doit contenir exactement 13 chiffres.");
|
||||
|
||||
RuleFor(x => x.PageCount)
|
||||
.GreaterThan(0)
|
||||
.WithMessage("Le nombre de pages doit être supérieur à 0.");
|
||||
|
||||
RuleFor(x => x.PublishedDate)
|
||||
.NotEmpty().WithMessage("La date de publication est obligatoire.")
|
||||
.Must(d => d <= DateOnly.FromDateTime(DateTime.Now))
|
||||
.WithMessage("La date de publication ne peut pas être dans le futur.");
|
||||
|
||||
RuleFor(x => x.Genre)
|
||||
.NotEmpty()
|
||||
.WithMessage("Le genre est obligatoire.")
|
||||
.MaximumLength(50)
|
||||
.WithMessage("Le genre ne peut pas dépasser 50 caractères.");
|
||||
|
||||
RuleFor(x => x.AuthorId)
|
||||
.GreaterThan(0)
|
||||
.WithMessage("L’identifiant de l’auteur est invalide.");
|
||||
|
||||
When(x => x.Summary != null, () =>
|
||||
{
|
||||
RuleFor(x => x.Summary)
|
||||
.MaximumLength(3000).WithMessage("Le résumé ne peut pas dépasser 3000 caractères.");
|
||||
});
|
||||
}
|
||||
}
|
||||
28
BookHive/Validators/Books/GetBookDtoValidator.cs
Normal file
28
BookHive/Validators/Books/GetBookDtoValidator.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using BookHive.DTO.Book;
|
||||
using FastEndpoints;
|
||||
using FluentValidation;
|
||||
|
||||
namespace BookHive.Validators.Books;
|
||||
|
||||
public class GetBookDtoValidator : Validator<GetBookDto>
|
||||
{
|
||||
public GetBookDtoValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.GreaterThan(0)
|
||||
.WithMessage("Id is invalid");
|
||||
|
||||
RuleFor(x => x.Title)
|
||||
.NotEmpty()
|
||||
.WithMessage("Title is required");
|
||||
|
||||
RuleFor(x => x.Isbn)
|
||||
.NotEmpty().WithMessage("L’ISBN est obligatoire.")
|
||||
.Matches(@"^\d{13}$")
|
||||
.WithMessage("L’ISBN doit contenir exactement 13 chiffres.");
|
||||
|
||||
RuleFor(x => x.AuthorFullName)
|
||||
.NotEmpty()
|
||||
.WithMessage("AuthorFullName is required");
|
||||
}
|
||||
}
|
||||
50
BookHive/Validators/Books/UpdateBookDtoValidator.cs
Normal file
50
BookHive/Validators/Books/UpdateBookDtoValidator.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using BookHive.DTO.Book;
|
||||
using FastEndpoints;
|
||||
using FluentValidation;
|
||||
|
||||
namespace BookHive.Validators.Books;
|
||||
|
||||
public class UpdateBookDtoValidator : Validator<UpdateBookDto>
|
||||
{
|
||||
public UpdateBookDtoValidator()
|
||||
{
|
||||
RuleFor(x => x.Id)
|
||||
.GreaterThan(0)
|
||||
.WithMessage("Id is invalid.");
|
||||
|
||||
RuleFor(x => x.Title)
|
||||
.NotEmpty().WithMessage("Le titre est obligatoire.")
|
||||
.MaximumLength(200)
|
||||
.WithMessage("Le titre ne peut pas dépasser 200 caractères.");
|
||||
|
||||
RuleFor(x => x.Isbn)
|
||||
.NotEmpty().WithMessage("L’ISBN est obligatoire.")
|
||||
.Matches(@"^\d{13}$")
|
||||
.WithMessage("L’ISBN doit contenir exactement 13 chiffres.");
|
||||
|
||||
RuleFor(x => x.PageCount)
|
||||
.GreaterThan(0)
|
||||
.WithMessage("Le nombre de pages doit être supérieur à 0.");
|
||||
|
||||
RuleFor(x => x.PublishedDate)
|
||||
.NotEmpty().WithMessage("La date de publication est obligatoire.")
|
||||
.Must(d => d <= DateOnly.FromDateTime(DateTime.Now))
|
||||
.WithMessage("La date de publication ne peut pas être dans le futur.");
|
||||
|
||||
RuleFor(x => x.Genre)
|
||||
.NotEmpty()
|
||||
.WithMessage("Le genre est obligatoire.")
|
||||
.MaximumLength(50)
|
||||
.WithMessage("Le genre ne peut pas dépasser 50 caractères.");
|
||||
|
||||
RuleFor(x => x.AuthorId)
|
||||
.GreaterThan(0)
|
||||
.WithMessage("L’identifiant de l’auteur est invalide.");
|
||||
|
||||
When(x => x.Summary != null, () =>
|
||||
{
|
||||
RuleFor(x => x.Summary)
|
||||
.MaximumLength(3000).WithMessage("Le résumé ne peut pas dépasser 3000 caractères.");
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user