Skip to content

Commit 22c55f6

Browse files
committed
Update id and name validation
Signed-off-by: Maysun J Faisal <[email protected]>
1 parent ddac378 commit 22c55f6

9 files changed

+0
-101
lines changed

pkg/validation/commands.go

-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ func ValidateCommands(commands []v1alpha2.Command, components []v1alpha2.Compone
2222
}
2323

2424
for _, command := range commands {
25-
// Check if command id is all numeric
26-
isIDNumeric := isInt(command.Id)
27-
if isIDNumeric {
28-
return &InvalidNameOrIdError{id: command.Id, resourceType: "command"}
29-
}
30-
3125
// parentCommands is a map to keep a track of all the parent commands when validating the composite command's subcommands recursively
3226
parentCommands := make(map[string]string)
3327
err = validateCommand(command, parentCommands, commandMap, components)

pkg/validation/components.go

-6
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ func ValidateComponents(components []v1alpha2.Component) error {
3434
}
3535

3636
for _, component := range components {
37-
// Check if component name is all numeric
38-
isNameNumeric := isInt(component.Name)
39-
if isNameNumeric {
40-
return &InvalidNameOrIdError{name: component.Name, resourceType: "component"}
41-
}
42-
4337
switch {
4438
case component.Container != nil:
4539
// Process all the volume mounts in container components to validate them later

pkg/validation/components_test.go

-8
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ func TestValidateComponents(t *testing.T) {
136136
sameEndpointNameErr := "devfile contains multiple endpoint entries with same name.*"
137137
sameTargetPortErr := "devfile contains multiple containers with same TargetPort.*"
138138
invalidURIErr := ".*invalid URI for request"
139-
nameNumericErr := "name cannot be with all numeric characters"
140139

141140
tests := []struct {
142141
name string
@@ -231,13 +230,6 @@ func TestValidateComponents(t *testing.T) {
231230
},
232231
wantErr: &sameEndpointNameErr,
233232
},
234-
{
235-
name: "Invalid component name with all numeric values",
236-
components: []v1alpha2.Component{
237-
generateDummyVolumeComponent("123", "1Gi"),
238-
},
239-
wantErr: &nameNumericErr,
240-
},
241233
{
242234
name: "Invalid plugin registry url",
243235
components: []v1alpha2.Component{

pkg/validation/endpoints.go

-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ func validateEndpoints(endpoints []v1alpha2.Endpoint, processedEndPointPort map[
1111
currentComponentEndPointPort := make(map[int]bool)
1212

1313
for _, endPoint := range endpoints {
14-
// Check if endpoint name is all numeric
15-
isNameNumeric := isInt(endPoint.Name)
16-
if isNameNumeric {
17-
return &InvalidNameOrIdError{name: endPoint.Name, resourceType: "endpoint"}
18-
}
19-
2014
if _, ok := processedEndPointName[endPoint.Name]; ok {
2115
return &InvalidEndpointError{name: endPoint.Name}
2216
}

pkg/validation/endpoints_test.go

-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
func TestValidateEndpoints(t *testing.T) {
1111

12-
nameNumericErr := "name cannot be with all numeric characters"
1312
duplicateNameErr := "multiple endpoint entries with same name"
1413
duplicatePortErr := "devfile contains multiple containers with same TargetPort"
1514

@@ -62,15 +61,6 @@ func TestValidateEndpoints(t *testing.T) {
6261
},
6362
wantErr: &duplicatePortErr,
6463
},
65-
{
66-
name: "Numeric endpoint name",
67-
endpoints: []v1alpha2.Endpoint{
68-
generateDummyEndpoint("123", 8080),
69-
},
70-
processedEndpointName: map[string]bool{},
71-
processedEndpointPort: map[int]bool{},
72-
wantErr: &nameNumericErr,
73-
},
7464
}
7565
for _, tt := range tests {
7666
t.Run(tt.name, func(t *testing.T) {

pkg/validation/errors.go

-17
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,3 @@ type InvalidComponentError struct {
7777
func (e *InvalidComponentError) Error() string {
7878
return fmt.Sprintf("the component %q is invalid - %s", e.componentName, e.reason)
7979
}
80-
81-
// InvalidNameOrIdError returns an error if the name or id is invalid
82-
type InvalidNameOrIdError struct {
83-
name string
84-
id string
85-
resourceType string
86-
}
87-
88-
func (e *InvalidNameOrIdError) Error() string {
89-
var errMsg string
90-
if e.name != "" {
91-
errMsg = fmt.Sprintf("%s name: %s is invalid, the name cannot be with all numeric characters", e.resourceType, e.name)
92-
} else if e.id != "" {
93-
errMsg = fmt.Sprintf("%s id: %s is invalid, the id cannot be with all numeric characters", e.resourceType, e.id)
94-
}
95-
return errMsg
96-
}

pkg/validation/utils.go

-9
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ package validation
22

33
import (
44
"net/url"
5-
"regexp"
65
"strings"
76

87
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
98
)
109

11-
var alphaRegexp = regexp.MustCompile(`^[0-9]+$`)
12-
1310
// getCommandsMap iterates through the commands and returns a map of command
1411
func getCommandsMap(commands []v1alpha2.Command) map[string]v1alpha2.Command {
1512
commandMap := make(map[string]v1alpha2.Command, len(commands))
@@ -22,12 +19,6 @@ func getCommandsMap(commands []v1alpha2.Command) map[string]v1alpha2.Command {
2219
return commandMap
2320
}
2421

25-
// isInt returns true if the string is an integer, false otherwise
26-
func isInt(str string) bool {
27-
match := alphaRegexp.MatchString(str)
28-
return match
29-
}
30-
3122
// ValidateURI checks if the string is with valid uri format, return error if not valid
3223
func ValidateURI(uri string) error {
3324
if strings.HasPrefix(uri, "http") {

pkg/validation/utils_test.go

-38
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,6 @@ import (
44
"testing"
55
)
66

7-
func TestIsInt(t *testing.T) {
8-
9-
tests := []struct {
10-
name string
11-
arg string
12-
wantResult bool
13-
}{
14-
{
15-
name: "Numeric string",
16-
arg: "1234",
17-
wantResult: true,
18-
},
19-
{
20-
name: "Alphanumeric string",
21-
arg: "pp1234abc-1223",
22-
wantResult: false,
23-
},
24-
{
25-
name: "String with numbers and character",
26-
arg: "12-34",
27-
wantResult: false,
28-
},
29-
{
30-
name: "Hexadecimal string",
31-
arg: "0xff",
32-
wantResult: false,
33-
},
34-
}
35-
for _, tt := range tests {
36-
t.Run(tt.name, func(t *testing.T) {
37-
result := isInt(tt.arg)
38-
if result != tt.wantResult {
39-
t.Errorf("TestIsInt result: %v, wantResult: %v", result, tt.wantResult)
40-
}
41-
})
42-
}
43-
}
44-
457
func TestValidateURI(t *testing.T) {
468

479
tests := []struct {

pkg/validation/validation-rule.md

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ The validation will be done as part of schema validation, the rule will be intro
1010
- no special characters allowed except dash(-)
1111
- start with an alphanumeric character
1212
- end with an alphanumeric character
13-
- must not be with all numbers (not included in schema validation, will have extra validation function)
1413

1514

1615
### Endpoints:

0 commit comments

Comments
 (0)