barcodes/validation/validate.go

34 lines
892 B
Go
Raw Normal View History

2023-09-01 19:08:59 +01:00
package validation
import (
"fmt"
2023-09-01 19:08:59 +01:00
"git.fjla.uk/fred.boniface/barcodes/generation"
)
func Validate(params generation.Parameters) bool {
rule, exists := FormatRules[string(params.Format)]
2023-09-01 19:08:59 +01:00
if !exists {
fmt.Printf("Error: No rule found for format '%s'\n", params.Format)
2023-09-01 19:08:59 +01:00
return false
}
if uint64(len(params.Content)) < rule.minLength || uint64(len(params.Content)) > rule.maxLength {
2023-09-01 19:08:59 +01:00
fmt.Printf("Validation Error: Length must be between %d and %d\n", rule.minLength, rule.maxLength)
return false
}
if !rule.charset.MatchString(params.Content) {
2023-09-01 19:08:59 +01:00
fmt.Println("Validation Error: Contains illegal characters")
return false
}
if uint64(len(params.Content))%uint64(rule.divisible) != 0 {
2023-09-01 19:08:59 +01:00
fmt.Printf("Validation Error: Length must be a multiple of %d", rule.divisible)
return false
}
fmt.Printf("Validation passed for barcode type %s\n", params.Format)
2023-09-01 19:08:59 +01:00
return true
}