Skip to content

add more validation to backend id prompt#10215

Open
aalej wants to merge 3 commits intomainfrom
aalej_prompt-backendid-validate
Open

add more validation to backend id prompt#10215
aalej wants to merge 3 commits intomainfrom
aalej_prompt-backendid-validate

Conversation

@aalej
Copy link
Copy Markdown
Contributor

@aalej aalej commented Mar 30, 2026

Description

Add more validation to the backend id prompt. based on the network responses from providing invalid backend ids

  • Char limit
✔ Provide a name for your backend [1-30 characters] a
✔  Name set to a

✔  Created a new Firebase web app named "a"
⠏ Creating your new backend...
Error: Request to https://firebaseapphosting.googleapis.com/v1beta/projects/PROJECT_ID/locations/us-central1/backends?backendId=a had HTTP Error: 400, The request was invalid: Violation in CreateBackendRequest.backend_id: length must be between 3 and 30 characters.
  • Must start with a letter
✔ Provide a name for your backend [1-30 characters] 10040
✔  Name set to 10040

✔  Created a new Firebase web app named "10040_1"
⠏ Creating your new backend...
Error: Request to https://firebaseapphosting.googleapis.com/v1beta/projects/PROJECT_ID/locations/us-central1/backends?backendId=10040 had HTTP Error: 400, The request was invalid: Violation in CreateBackendRequest.backend_id: only lowercase, digits, and hyphens; must begin with letter, and cannot end with hyphen;
⠹ Creating your new backend...
Having trouble? Try firebase [command] --help
  • Cannot end with hyphen
✔ Provide a name for your backend [1-30 characters] hyphen-
✔  Name set to hyphen-

✔  Created a new Firebase web app named "hyphen-"
⠧ Creating your new backend...
Error: Request to https://firebaseapphosting.googleapis.com/v1beta/projects/PROJECT_ID/locations/us-central1/backends?backendId=hyphen- had HTTP Error: 400, The request was invalid: Violation in CreateBackendRequest.backend_id: only lowercase, digits, and hyphens; must begin with letter, and cannot end with hyphen;
⠋ Creating your new backend...
Having trouble? Try firebase [command] --help

Scenarios Tested

  • Char limit
? Provide a name for your backend [3-30 characters] a
> Must be between 3 and 30 characters
  • Must start with a letter
? Provide a name for your backend [3-30 characters] 1foo
> Must begin with a letter, can contain only lowercase, digits, hyphens, and cannot end with hyphen
  • Cannot end with hypen
? Provide a name for your backend [3-30 characters] foo-
> Must begin with a letter, can contain only lowercase, digits, hyphens, and cannot end with hyphen

All conditions followed

✔ Provide a name for your backend [3-30 characters] foo
✔  Name set to foo

✔  Created a new Firebase web app named "foo"
✔ Successfully created backend!
        projects/PROJECT_ID/locations/us-central1/backends/foo

Sample Commands

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request increases the Mocha test timeout and implements stricter validation for backend IDs, including a minimum length of three characters and a specific naming format. A review comment suggests refactoring the validation logic in src/apphosting/backend.ts to improve readability by breaking down the complex regular expression into multiple simpler checks.

Comment on lines +344 to +352
validate: (s) => {
if (!/^[a-z](?:[a-z0-9-]*[a-z0-9])?$/.test(s)) {
return "Must begin with a letter, can contain only lowercase, digits, hyphens, and cannot end with hyphen";
} else if (s.length < 3 || s.length > 30) {
return "Must be between 3 and 30 characters";
}

return true;
},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

While the regular expression is correct, it's a bit complex and can be hard to parse for future maintainers. For better readability and maintainability, you could split the validation into multiple, simpler checks. This makes the intent of each validation step clearer and is generally a good practice.

      validate: (s) => {
        if (s.length < 3 || s.length > 30) {
          return "Must be between 3 and 30 characters";
        }
        if (!/^[a-z]/.test(s) || /[^a-z0-9-]/.test(s) || s.endsWith("-")) {
          return "Must begin with a letter, can contain only lowercase, digits, hyphens, and cannot end with hyphen";
        }

        return true;
      },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants