added validators in project
This commit is contained in:
28
BookHive/Validators/CustomValidators.cs
Normal file
28
BookHive/Validators/CustomValidators.cs
Normal 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("L’ISBN 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.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user