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,28 @@
using FluentValidation;
namespace BookHive.Validators;
public static class CustomValidators
{
public static IRuleBuilderOptions<T, string>
IsValidIsbn13<T>(this IRuleBuilder<T, string> ruleBuilder)
{
return ruleBuilder
.Matches(@"^\d{13}$")
.WithMessage("LISBN doit contenir 13 chiffres.")
.Must(isbn =>
{
if (string.IsNullOrEmpty(isbn) || isbn.Length != 13)
return false;
var sum = 0;
for (int i = 0; i < 12; i++)
{
var digit = isbn[i] - '0';
sum += (i % 2 == 0) ? digit : digit * 3;
}
var checkDigit = (10 - (sum % 10)) % 10;
return checkDigit == (isbn[12] - '0');
})
.WithMessage("Le chiffre de contrôle ISBN est invalide.");
}
}