From 53cae5ebe67adb8ee7f4011efbc3432527bde800 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Fri, 1 Sep 2023 19:08:59 +0100 Subject: [PATCH] Init --- go.mod | 3 ++ validation/charsets.go | 11 ++++++ validation/rules.go | 89 ++++++++++++++++++++++++++++++++++++++++++ validation/validate.go | 28 +++++++++++++ 4 files changed, 131 insertions(+) create mode 100644 go.mod create mode 100644 validation/charsets.go create mode 100644 validation/rules.go create mode 100644 validation/validate.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a82281d --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.fjla.uk/fred.boniface/barcodes + +go 1.19 diff --git a/validation/charsets.go b/validation/charsets.go new file mode 100644 index 0000000..6dd485b --- /dev/null +++ b/validation/charsets.go @@ -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]+$`) +) diff --git a/validation/rules.go b/validation/rules.go new file mode 100644 index 0000000..48e99b7 --- /dev/null +++ b/validation/rules.go @@ -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, +} diff --git a/validation/validate.go b/validation/validate.go new file mode 100644 index 0000000..ca73a82 --- /dev/null +++ b/validation/validate.go @@ -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 +}