passes method
Validates the value using the given context.
Returns true if valid, otherwise false.
Implementation
@override
FutureOr<bool> passes(ValidationContext context) {
final value = context.value;
if (value == null || value is! String) return false;
// Remove all non-digits
final clean = value.replaceAll(RegExp(r'\D'), '');
if (clean.isEmpty) return false;
// Luhn algorithm
int sum = 0;
bool alternate = false;
for (int i = clean.length - 1; i >= 0; i--) {
int n = int.parse(clean[i]);
if (alternate) {
n *= 2;
if (n > 9) {
n = (n % 10) + 1;
}
}
sum += n;
alternate = !alternate;
}
return (sum % 10 == 0);
}