Skip to content
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

Add username validation for names #41

Merged
merged 1 commit into from
Nov 30, 2023
Merged

Add username validation for names #41

merged 1 commit into from
Nov 30, 2023

Conversation

emcfarlane
Copy link
Contributor

Adds a regex pattern match for usernames "^[a-z][a-z0-9-]*[a-z0-9]$". Must start with a lowercase letter, only consist of alpha numerics or hypens and doesn't end with a hypen. Bumped the min length to 2 as the RE2 syntax doesn't support lookback syntax.

Error on failure will be:

validation failed: validation error:
 - name: value does not match regex pattern `^[a-z][a-z0-9-]*[a-z0-9]$` [string.pattern]

Should we instead use a CEL expression for better errors?

(buf.validate.field).cel = {
  id: "username",
  message: "value must start with a lowercase letter and contain only letters, numbers, or hypens and cannot end with a hypen",
  expression: "this.matches('^[a-z][a-z0-9-]*[a-z0-9]$')"
}
validation failed: validation error:
 - name: value must start with a lowercase letter, contain only letters, numbers, or hypens and cannot end with a hypen [username]

Adds a regex pattern match for usernames "^[a-z][a-z0-9-]*[a-z0-9]$".
Must start with a lowercase letter, only consist of alpha numerics or
hypens and doesn't end with a hypen. Bumped the min length to 2 as the RE2
syntax doesn't support lookback syntax.
@stefanvanburen
Copy link
Member

This seems reasonable to me. (Would be a great place for reusable custom validation!)

I slightly prefer the CEL with the better error description, but 🤷‍♂️ - does converting between the two cause a buf lint error? (Feels like they shouldn't if they ultimately result in the same expression?)

@jchadwick-buf
Copy link
Member

JFYI, you can make the constraint work with single-character owner names with a minor adjustment.

^([a-z]|[a-z][a-z0-9-]*[a-z0-9])$

In practice, it is unlikely any BSR will ever have owner lengths of 1 character, but if you wanted the semantically correct regex for that case, this should be it. (Using a look-behind would be unnecessarily complex.)

@emcfarlane emcfarlane merged commit 371f64e into main Nov 30, 2023
6 checks passed
@emcfarlane emcfarlane deleted the ed/username-regex branch November 30, 2023 22:18
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.

4 participants