Skip to content

Add schema validation for name in azure.yaml and add name validity check in AZD #5380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cli/azd/pkg/project/service_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"log"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/azure/azure-dev/cli/azd/pkg/alpha"
Expand Down Expand Up @@ -278,6 +279,11 @@ func (sm *serviceManager) Package(
options = &PackageOptions{}
}

// Check if the application name is valid.
if err := validateAppName(serviceConfig.Project.Name); err != nil {
return nil, err
}

Comment on lines +283 to +286
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be too late as a validation.

We need to make sure that AZD is not allowing folks to name the project with something invalid when they run azd init hoping to azd-fy an existing project.

See: https://github.com/azure/azure-dev/blob/main/cli/azd/internal/repository/initializer.go#L338

AZD should not allow giving a name for the project like uno/dos only to later fail during package about having an invalid name.

cachedResult, ok := sm.getOperationResult(serviceConfig, string(ServiceEventPackage))
if ok && cachedResult != nil {
return cachedResult.(*ServicePackageResult), nil
Expand Down Expand Up @@ -685,3 +691,21 @@ func moveFile(sourcePath string, destinationPath string) error {

return nil
}

func validateAppName(name string) error {
if len(name) < 2 {
return fmt.Errorf("invalid application name '%s': must be at least 2 characters", name)
}
matched, err := regexp.MatchString(`^[a-zA-Z0-9_-]+$`, name)
if err != nil {
return err
}
if !matched {
return fmt.Errorf(
"invalid application name '%s': only letters, numbers, underscores (_), and hyphens (-) are allowed",
name,
)

}
return nil
}
4 changes: 3 additions & 1 deletion schemas/alpha/azure.yaml.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"name": {
"type": "string",
"minLength": 2,
"title": "Name of the application"
"title": "Name of the application",
"pattern": "^[a-zA-Z0-9_-]+$",
"description": "The application name. Only letters, numbers, underscores, and hyphens are allowed. Characters like /, \\, spaces, or other special symbols are not permitted."
},
"resourceGroup": {
"type": "string",
Expand Down
4 changes: 3 additions & 1 deletion schemas/v1.0/azure.yaml.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"name": {
"type": "string",
"minLength": 2,
"title": "Name of the application"
"title": "Name of the application",
"pattern": "^[a-zA-Z0-9_-]+$",
"description": "The application name. Only letters, numbers, underscores, and hyphens are allowed. Characters like /, \\, spaces, or other special symbols are not permitted."
},
"resourceGroup": {
"type": "string",
Expand Down
Loading