From 1484c89e09dceb8e4cdcd12b64cd3bfceb5f2eeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20De=C5=BEulovi=C4=87?= Date: Sun, 22 Sep 2024 13:24:20 +0200 Subject: [PATCH] Add ValidateLocationID function an location id validation --- checklocationid.go | 13 +++++++++++++ checklocationid_test.go | 43 +++++++++++++++++++++++++++++++++++++++++ fiskalhr.go | 5 +++++ 3 files changed, 61 insertions(+) create mode 100644 checklocationid.go create mode 100644 checklocationid_test.go diff --git a/checklocationid.go b/checklocationid.go new file mode 100644 index 0000000..d7fb196 --- /dev/null +++ b/checklocationid.go @@ -0,0 +1,13 @@ +package fiskalhrgo + +import ( + "regexp" +) + +// ValidateLocationID validates the locationID +// It can contain only digits (0-9) and letters (a-z, A-Z), with a maximum length of 20. +func ValidateLocationID(locationID string) bool { + // Regex pattern to match valid locationID + validLocationID := regexp.MustCompile(`^[a-zA-Z0-9]{1,20}$`) + return validLocationID.MatchString(locationID) +} diff --git a/checklocationid_test.go b/checklocationid_test.go new file mode 100644 index 0000000..079c121 --- /dev/null +++ b/checklocationid_test.go @@ -0,0 +1,43 @@ +package fiskalhrgo + +import "testing" + +func TestValidateLocationID(t *testing.T) { + t.Logf("Testing location ID validation...") + + if !ValidateLocationID("12345678") { + t.Fatalf("Expected location ID 12345678 to be valid") + } + + if !ValidateLocationID("TEST3") { + t.Fatalf("Expected location ID TEST3 to be valid") + } + + if !ValidateLocationID("POS1") { + t.Fatalf("Expected location ID POS1 to be valid") + } + + if !ValidateLocationID("1") { + t.Fatalf("Expected location ID 1 to be valid") + } + + if !ValidateLocationID("1234567a") { + t.Fatalf("Expected location ID 1234567a to be valid") + } + + if ValidateLocationID("1234567!") { + t.Fatalf("Expected location ID 1234567! to be invalid") + } + + if ValidateLocationID("1234567.") { + t.Fatalf("Expected location ID 1234567. to be invalid") + } + + if ValidateLocationID("POS 1") { + t.Fatalf("Expected location ID POS 1 to be invalid") + } + + if ValidateLocationID("fdkjhfdhjfdshfshfdkhfd87549549875kjfhhfdshfjhjdshkjdfsk7554875kjgfkjgfsssssssssssssssssssssssss") { + t.Fatalf("Expected location ID fdkjhfdhjfdshfshfdkhfd87549549875kjfhhfdshfjhjdshkjdfsk7554875kjgfkjgfsssssssssssssssssssssssss to be invalid") + } +} diff --git a/fiskalhr.go b/fiskalhr.go index b289c1b..f98134a 100644 --- a/fiskalhr.go +++ b/fiskalhr.go @@ -88,6 +88,11 @@ func NewFiskalEntity(oib string, sustavPDV bool, locationID string, centralizedI return nil, errors.New("invalid OIB") } + //check if locationID is valid + if !ValidateLocationID(locationID) { + return nil, errors.New("invalid locationID") + } + var CIScert *signatureCheckCIScert var CIScerterror error