Init
This commit is contained in:
commit
53cae5ebe6
11
validation/charsets.go
Normal file
11
validation/charsets.go
Normal file
@ -0,0 +1,11 @@
|
||||
package validation
|
||||
|
||||
import "regexp"
|
||||
|
||||
var (
|
||||
ASCII = regexp.MustCompile(`^[\x00-\x7F]+$`)
|
||||
ANY = regexp.MustCompile(`.`)
|
||||
CODE39 = regexp.MustCompile(`^[0-9A-Z\-\.\ $%*+/]+$`)
|
||||
NUMERAL = regexp.MustCompile(`^[0-9]+$`)
|
||||
ISO88591 = regexp.MustCompile(`^[\x00-\xFF]+$`)
|
||||
)
|
89
validation/rules.go
Normal file
89
validation/rules.go
Normal file
@ -0,0 +1,89 @@
|
||||
package validation
|
||||
|
||||
import "regexp"
|
||||
|
||||
// This needs to be a map of structs
|
||||
|
||||
type rule struct {
|
||||
minLength uint64
|
||||
maxLength uint64
|
||||
charset regexp.Regexp
|
||||
divisible uint8
|
||||
}
|
||||
|
||||
var aztec = rule{
|
||||
minLength: 1,
|
||||
maxLength: 3067,
|
||||
charset: *ANY,
|
||||
divisible: 1,
|
||||
}
|
||||
|
||||
var code39 = rule{
|
||||
minLength: 1,
|
||||
maxLength: 80,
|
||||
charset: *CODE39,
|
||||
divisible: 1,
|
||||
}
|
||||
|
||||
var code128 = rule{
|
||||
minLength: 1,
|
||||
maxLength: 80,
|
||||
charset: *ASCII,
|
||||
divisible: 1,
|
||||
}
|
||||
|
||||
var datamatrix = rule{
|
||||
minLength: 1,
|
||||
maxLength: 2335,
|
||||
charset: *ANY,
|
||||
divisible: 1,
|
||||
}
|
||||
|
||||
var ean8 = rule{
|
||||
minLength: 7,
|
||||
maxLength: 7,
|
||||
charset: *NUMERAL,
|
||||
divisible: 1,
|
||||
}
|
||||
|
||||
var ean13 = rule{
|
||||
minLength: 12,
|
||||
maxLength: 12,
|
||||
charset: *NUMERAL,
|
||||
divisible: 1,
|
||||
}
|
||||
|
||||
var ean14 = rule{
|
||||
minLength: 2,
|
||||
maxLength: 80,
|
||||
charset: *NUMERAL,
|
||||
divisible: 2,
|
||||
}
|
||||
|
||||
var pdf417 = rule{
|
||||
minLength: 1,
|
||||
maxLength: 1100,
|
||||
charset: *ANY,
|
||||
divisible: 1,
|
||||
}
|
||||
|
||||
var pzn7 = rule{
|
||||
minLength: 7,
|
||||
maxLength: 7,
|
||||
charset: *NUMERAL,
|
||||
divisible: 1,
|
||||
}
|
||||
|
||||
var qr = rule{
|
||||
minLength: 1,
|
||||
maxLength: 4296,
|
||||
charset: *ISO88591,
|
||||
divisible: 1,
|
||||
}
|
||||
|
||||
var upca = rule{
|
||||
minLength: 11,
|
||||
maxLength: 11,
|
||||
charset: *NUMERAL,
|
||||
divisible: 1,
|
||||
}
|
28
validation/validate.go
Normal file
28
validation/validate.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user