34 lines
892 B
Go
34 lines
892 B
Go
package validation
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.fjla.uk/fred.boniface/barcodes/generation"
|
|
)
|
|
|
|
func Validate(params generation.Parameters) bool {
|
|
rule, exists := FormatRules[string(params.Format)]
|
|
if !exists {
|
|
fmt.Printf("Error: No rule found for format '%s'\n", params.Format)
|
|
return false
|
|
}
|
|
|
|
if uint64(len(params.Content)) < rule.minLength || uint64(len(params.Content)) > rule.maxLength {
|
|
fmt.Printf("Validation Error: Length must be between %d and %d\n", rule.minLength, rule.maxLength)
|
|
return false
|
|
}
|
|
|
|
if !rule.charset.MatchString(params.Content) {
|
|
fmt.Println("Validation Error: Contains illegal characters")
|
|
return false
|
|
}
|
|
|
|
if uint64(len(params.Content))%uint64(rule.divisible) != 0 {
|
|
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)
|
|
return true
|
|
}
|