Skip to content

Commit

Permalink
update username regex (#87)
Browse files Browse the repository at this point in the history
* update regex and error messaging

* update affected length test

* add test for single character usernames

* version bump
  • Loading branch information
tbremer authored Sep 16, 2024
1 parent c505b67 commit d175500
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zeit/schemas",
"version": "2.36.0",
"version": "2.37.0",
"description": "All schemas used for validation that are shared between our projects",
"scripts": {
"test": "yarn run lint && best --verbose",
Expand Down
20 changes: 14 additions & 6 deletions test/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exports.test_username_invalid_pattern = () => {
assert.equal(ajv.errors[0].dataPath, '.username');
assert.equal(
ajv.errors[0].message,
'should match pattern "^[a-z0-9][a-z0-9-]*[a-z0-9]$"'
'should match pattern "^(?!-)(?:[a-z0-9-]{1,48})(?<!-)$"'
);
};

Expand All @@ -43,28 +43,36 @@ exports.test_username_too_short = () => {
assert.equal(ajv.errors[1].dataPath, '.username');
assert.equal(
ajv.errors[1].message,
'should match pattern "^[a-z0-9][a-z0-9-]*[a-z0-9]$"'
'should match pattern "^(?!-)(?:[a-z0-9-]{1,48})(?<!-)$"'
);
};

exports.test_username_too_long = () => {
const isValid = ajv.validate(User, {
username: 'a'.repeat(50)
});
const username = 'a'.repeat(50);
const isValid = ajv.validate(User, { username });
assert.equal(isValid, false);
assert.equal(ajv.errors.length, 1);
assert.equal(ajv.errors.length, 2);
assert.equal(ajv.errors[0].dataPath, '.username');
assert.equal(
ajv.errors[0].message,
'should NOT be longer than 48 characters'
);
assert.equal(
ajv.errors[1].message,
'should match pattern "^(?!-)(?:[a-z0-9-]{1,48})(?<!-)$"'
);
};

exports.test_username_valid = () => {
assert(ajv.validate(User, { username: 'n8' }));
assert(ajv.validate(User, { username: 'rauchg' }));
};

exports.test_username_one_char = () => {
assert(ajv.validate(User, { username: 'a' }));
assert(ajv.validate(User, { username: '1' }));
};

// Name
exports.test_name_too_short = () => {
const isValid = ajv.validate(User, {
Expand Down
2 changes: 1 addition & 1 deletion user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Username = {
type: 'string',
minLength: 1,
maxLength: 48,
pattern: '^[a-z0-9][a-z0-9-]*[a-z0-9]$'
pattern: '^(?!-)(?:[a-z0-9-]{1,48})(?<!-)$'
};

const Name = {
Expand Down

0 comments on commit d175500

Please sign in to comment.