Skip to content

Commit

Permalink
Add ValidateLocationID function an location id validation
Browse files Browse the repository at this point in the history
  • Loading branch information
arvvoid committed Sep 22, 2024
1 parent 0ec22e6 commit 1484c89
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
13 changes: 13 additions & 0 deletions checklocationid.go
Original file line number Diff line number Diff line change
@@ -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)
}
43 changes: 43 additions & 0 deletions checklocationid_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
5 changes: 5 additions & 0 deletions fiskalhr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 1484c89

Please sign in to comment.