barcodes/validation/validate.go
2023-09-01 19:08:59 +01:00

29 lines
709 B
Go

package validation
import "fmt"
func Validate(input string, format string) bool {
rule, exists := barcodeRules[format]
if !exists {
fmt.Printf("Error: No rule found for format '%s'\n", format)
return false
}
if uint64(len(input)) < rule.minLength || uint64(len(input)) > rule.maxLength {
fmt.Printf("Validation Error: Length must be between %d and %d\n", rule.minLength, rule.maxLength)
return false
}
if !rule.charset.MatchString(input) {
fmt.Println("Validation Error: Contains illegal characters")
return false
}
if uint64(len(input))%uint64(rule.divisible) != 0 {
fmt.Printf("Validation Error: Length must be a multiple of %d", rule.divisible)
return false
}
return true
}