Should `credit_card` validator support dash-separated card numbers?
What happened?
The credit_card validator currently only handles space-separated credit card numbers. Card numbers formatted with dashes (e.g., 4111-1111-1111-1111) are incorrectly rejected because digitsHaveLuhnChecksum calls strconv.Atoi on - characters, which fails and returns false.
Example Code
package main
import (
"fmt"
"github.com/go-playground/validator/v10"
)
type Payment struct {
CardNumber string `validate:"credit_card"`
}
func main() {
validate := validator.New()
// These are VALID card numbers but currently FAIL validation:
payments := []string{
"4111-1111-1111-1111", // Visa, dash-separated
"3782-822463-10005", // Amex, dash-separated
"4624-7482-3324-9780", // Discover, dash-separated
}
// These work correctly today:
p := Payment{CardNumber: "4111 1111 1111 1111"} // space-separated
fmt.Printf("Card: %s -> Error: %v\n", p.CardNumber, validate.Struct(p))
}Expected Behavior
Dash-separated card numbers should be validated the same as space-separated ones, since dashes are a legitimate card number format (used in Android payment forms, some banking apps, and recognized by ISO/IEC 7812).
Actual Behavior
Dash-separated numbers are rejected as invalid.
Version
v10.26.0
Question
Would the maintainers accept a PR that strips dashes from the input before processing? The change would be a single strings.ReplaceAll call in isCreditCard, plus test cases. It wouldn't break any existing behavior since space-separated and plain-digit formats are already handled correctly.
Source: go-playground/validator