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

update username regex #87

Merged
merged 4 commits into from
Sep 16, 2024
Merged
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
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
Loading