This commit is contained in:
Fred Boniface
2023-09-01 19:08:59 +01:00
commit 53cae5ebe6
4 changed files with 131 additions and 0 deletions

28
validation/validate.go Normal file
View File

@@ -0,0 +1,28 @@
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
}