Skip to content

Commit

Permalink
Improve node id validation error message. (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinjhuang authored Jun 3, 2024
1 parent 5224d14 commit dae2fbb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
19 changes: 14 additions & 5 deletions mapper/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,32 @@ func ValidateNode(node *drip.Node) error {
if len(*node.Id) > 100 {
return fmt.Errorf("node id is too long")
}
if !IsValidNodeID(*node.Id) {
return fmt.Errorf("invalid node id")
isValid, err := IsValidNodeID(*node.Id)
if !isValid {
return fmt.Errorf(err)
}
}
return nil
}

func IsValidNodeID(nodeID string) bool {
func IsValidNodeID(nodeID string) (bool, string) {
if len(nodeID) == 0 || len(nodeID) > 50 {
return false
return false, "node id must be between 1 and 50 characters"
}
// Check if there are capital letters in the string
if strings.ToLower(nodeID) != nodeID {
return false, "Node ID can only contain lowercase letters"
}
// Regular expression pattern for Node ID validation (lowercase letters only)
pattern := `^[a-z][a-z0-9-_]+(\.[a-z0-9-_]+)*$`
// Compile the regular expression pattern
regex := regexp.MustCompile(pattern)
// Check if the string matches the pattern
return regex.MatchString(nodeID)
matches := regex.MatchString(nodeID)
if !matches {
return false, "Node ID can only contain lowercase letters, numbers, hyphens, underscores, and dots. Dots cannot be consecutive or be at the start or end of the id."
}
return true, ""
}

func DbNodeToApiNode(node *ent.Node) *drip.Node {
Expand Down
30 changes: 13 additions & 17 deletions mapper/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

// TestIsValidNodeID tests the isValidNodeID function with various inputs.
func TestIsValidNodeID(t *testing.T) {
regexErrorMessage := "Node ID can only contain lowercase letters, numbers, hyphens, underscores, and dots. Dots cannot be consecutive or be at the start or end of the id."
testCases := []struct {
name string
node *drip.Node
Expand All @@ -26,7 +27,7 @@ func TestIsValidNodeID(t *testing.T) {
{
name: "Invalid Node ID",
node: &drip.Node{Id: stringPtr("123")},
expectedError: "invalid node id",
expectedError: regexErrorMessage,
},

{
Expand All @@ -42,38 +43,33 @@ func TestIsValidNodeID(t *testing.T) {
{
name: "Invalid with uppercase",
node: &drip.Node{Id: stringPtr("Node")},
expectedError: "invalid node id",
expectedError: "Node ID can only contain lowercase letters",
},
{
name: "Invalid with special characters",
node: &drip.Node{Id: stringPtr("node_@")},
expectedError: "invalid node id",
expectedError: regexErrorMessage,
},
{
name: "Invalid start with number",
node: &drip.Node{Id: stringPtr("1node")},
expectedError: "invalid node id",
expectedError: regexErrorMessage,
},
{
name: "Invalid start with dash",
node: &drip.Node{Id: stringPtr("-node")},
expectedError: "invalid node id",
expectedError: regexErrorMessage,
},
{
name: "Empty input",
node: &drip.Node{Id: stringPtr("")},
expectedError: "invalid node id",
expectedError: "node id must be between 1 and 50 characters",
},
{
name: "Valid all lowercase letters",
node: &drip.Node{Id: stringPtr("abcdefghijklmnopqrstuvwxyz")},
expectedError: "",
},
{
name: "Valid all uppercase letters",
node: &drip.Node{Id: stringPtr("ABCD")},
expectedError: "invalid node id",
},
{
name: "Valid containing underscore",
node: &drip.Node{Id: stringPtr("comfy_ui")},
Expand All @@ -97,17 +93,17 @@ func TestIsValidNodeID(t *testing.T) {
{
name: "Invalid ID with number first",
node: &drip.Node{Id: stringPtr("1invalidnodeid")},
expectedError: "invalid node id",
expectedError: regexErrorMessage,
},
{
name: "Invalid ID with consecutive dots",
node: &drip.Node{Id: stringPtr("invalid..nodeid")},
expectedError: "invalid node id",
expectedError: regexErrorMessage,
},
{
name: "Invalid ID with special character first",
node: &drip.Node{Id: stringPtr("-invalidnodeid")},
expectedError: "invalid node id",
expectedError: regexErrorMessage,
},
{
name: "Valid complex ID",
Expand All @@ -117,17 +113,17 @@ func TestIsValidNodeID(t *testing.T) {
{
name: "Invalid ID with special characters only",
node: &drip.Node{Id: stringPtr("$$$$")},
expectedError: "invalid node id",
expectedError: regexErrorMessage,
},
{
name: "Invalid ID with leading dot",
node: &drip.Node{Id: stringPtr(".invalidnodeid")},
expectedError: "invalid node id",
expectedError: regexErrorMessage,
},
{
name: "Invalid ID with ending dot",
node: &drip.Node{Id: stringPtr("invalidnodeid.")},
expectedError: "invalid node id",
expectedError: regexErrorMessage,
},
}

Expand Down

0 comments on commit dae2fbb

Please sign in to comment.