Files
TP-Fluent/BookHive/Validators/CustomValidators.cs

28 lines
910 B
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.");
}
}