Skip to content

Commit

Permalink
Merge pull request #22 from tybritten/master
Browse files Browse the repository at this point in the history
Add FreshChat URN
  • Loading branch information
nicpottier authored Jun 20, 2019
2 parents 1d7c5b9 + 295257e commit 8c3081b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
13 changes: 12 additions & 1 deletion urns/urns.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const (
// FCMScheme is the scheme used for Firebase Cloud Messaging identifiers
FCMScheme string = "fcm"

// FreshChatScheme is the scheme used for FreshChat Cloud Messaging identifiers
FreshChatScheme string = "freshchat"

// JiochatScheme is the scheme used for Jiochat identifiers
JiochatScheme string = "jiochat"

Expand Down Expand Up @@ -60,6 +63,7 @@ var ValidSchemes = map[string]bool{
ExternalScheme: true,
FacebookScheme: true,
FCMScheme: true,
FreshChatScheme: true,
JiochatScheme: true,
LineScheme: true,
TelegramScheme: true,
Expand All @@ -84,6 +88,7 @@ var emailRegex = regexp.MustCompile(`^[^\s@]+@[^\s@]+$`)
var viberRegex = regexp.MustCompile(`^[a-zA-Z0-9_=/+]{1,24}$`)
var lineRegex = regexp.MustCompile(`^[a-zA-Z0-9_]{1,36}$`)
var allDigitsRegex = regexp.MustCompile(`^[0-9]+$`)
var freshchatRegex = regexp.MustCompile(`^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}/[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$`)

// URN represents a Universal Resource Name, we use this for contact identifiers like phone numbers etc..
type URN string
Expand Down Expand Up @@ -249,9 +254,15 @@ func (u URN) Validate() error {
if !allDigitsRegex.MatchString(path) {
return fmt.Errorf("invalid whatsapp id: %s", path)
}
}

case FreshChatScheme:
// validate path and query is a uuid
if !freshchatRegex.MatchString(path) {
return fmt.Errorf("invalid freshchat id: %s", path)
}
}
return nil // anything goes for external schemes

}

// Scheme returns the scheme portion for the URN
Expand Down
7 changes: 6 additions & 1 deletion urns/urns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func TestValidate(t *testing.T) {

// invalid tel numbers
{"tel:07883 83383", "invalid tel number"}, // can't have spaces
{"tel:", "cannot be empty"}, // need a path
{"tel:", "cannot be empty"}, // need a path

// twitter handles
{"twitter:jimmyjo", ""},
Expand Down Expand Up @@ -259,6 +259,11 @@ func TestValidate(t *testing.T) {
{"whatsapp:12354", ""},
{"whatsapp:abcde", "invalid whatsapp id"},
{"whatsapp:+12067799294", "invalid whatsapp id"},

// freschat has to be two uuids separated by a colon
{"freshchat:6a2f41a3-c54c-fce8-32d2-0324e1c32e22/6a2f41a3-c54c-fce8-32d2-0324e1c32e22", ""},
{"freshchat:6a2f41a3-c54c-fce8-32d2-0324e1c32e22", "invalid freshchat id"},
{"freshchat:+12067799294", "invalid freshchat id"},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 8c3081b

Please sign in to comment.