2023-09-01 19:08:59 +01:00
|
|
|
package validation
|
|
|
|
|
2023-09-02 21:04:29 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
2023-09-01 19:08:59 +01:00
|
|
|
|
2023-09-02 21:04:29 +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 {
|
2023-09-02 21:04:29 +01:00
|
|
|
fmt.Printf("Error: No rule found for format '%s'\n", params.Format)
|
2023-09-01 19:08:59 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-09-02 21:04:29 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-02 21:04:29 +01:00
|
|
|
if !rule.charset.MatchString(params.Content) {
|
2023-09-01 19:08:59 +01:00
|
|
|
fmt.Println("Validation Error: Contains illegal characters")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-09-02 21:04:29 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-02 21:04:29 +01:00
|
|
|
fmt.Printf("Validation passed for barcode type %s\n", params.Format)
|
2023-09-01 19:08:59 +01:00
|
|
|
return true
|
|
|
|
}
|