Skip to content

Commit

Permalink
fix(registration): Make Username optional (danny-avila#831)
Browse files Browse the repository at this point in the history
* fix(User.js): update validation schema for username field, allow empty string as a valid value
fix(validators.js): update validation schema for username field, allow empty string as a valid value
fix(Registration.tsx, validators.js): update validation rules for name and username fields, change minimum length to 2 and maximum length to 80, assure they match and allow empty string as a valid value
fix(Eng.tsx): update localization string for com_auth_username, indicate that it is optional

* fix(User.js): update regex pattern for username validation to allow special characters @#$%&*()
fix(validators.js): update regex pattern for username validation to allow special characters @#$%&*()

* fix(Registration.spec.tsx): fix validation error message for username length requirement
  • Loading branch information
danny-avila authored Aug 23, 2023
1 parent d38e463 commit 37347d4
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
11 changes: 5 additions & 6 deletions api/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ const userSchema = mongoose.Schema(
username: {
type: String,
lowercase: true,
required: [true, 'can\'t be blank'],
match: [/^[a-zA-Z0-9_.-]+$/, 'is invalid'],
index: true,
default: '',
},
email: {
type: String,
Expand Down Expand Up @@ -173,12 +171,13 @@ module.exports.validateUser = (user) => {
});
const schema = {
avatar: Joi.any(),
name: Joi.string().min(2).max(80).required(),
name: Joi.string().min(3).max(80).required(),
username: Joi.string()
.trim()
.allow('')
.min(2)
.max(80)
.regex(/^[a-zA-Z0-9_.-]+$/)
.required(),
.regex(/^[a-zA-Z0-9_.-@#$%&*() ]+$/),
password: Joi.string().min(8).max(128).allow('').allow(null),
};

Expand Down
8 changes: 4 additions & 4 deletions api/strategies/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const loginSchema = Joi.object().keys({
});

const registerSchema = Joi.object().keys({
name: Joi.string().trim().min(2).max(30).required(),
name: Joi.string().trim().min(3).max(80).required(),
username: Joi.string()
.trim()
.allow('')
.min(2)
.max(20)
.regex(/^[a-zA-Z0-9_.-]+$/)
.required(),
.max(80)
.regex(/^[a-zA-Z0-9_.-@#$%&*() ]+$/),
email: Joi.string().trim().email().required(),
password: Joi.string().trim().min(8).max(128).required(),
confirm_password: Joi.string().trim().min(8).max(128).required(),
Expand Down
6 changes: 3 additions & 3 deletions client/src/components/Auth/Registration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ function Registration() {
id="username"
aria-label={localize('com_auth_username')}
{...register('username', {
required: localize('com_auth_username_required'),
// required: localize('com_auth_username_required'),
minLength: {
value: 3,
value: 2,
message: localize('com_auth_username_min_length'),
},
maxLength: {
value: 20,
value: 80,
message: localize('com_auth_username_max_length'),
},
})}
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/Auth/__tests__/Registration.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ test('shows validation error messages', async () => {
const alerts = getAllByRole('alert');
expect(alerts).toHaveLength(5);
expect(alerts[0]).toHaveTextContent(/Name must be at least 3 characters/i);
expect(alerts[1]).toHaveTextContent(/Username must be at least 3 characters/i);
expect(alerts[1]).toHaveTextContent(/Username must be at least 2 characters/i);
expect(alerts[2]).toHaveTextContent(/You must enter a valid email address/i);
expect(alerts[3]).toHaveTextContent(/Password must be at least 8 characters/i);
expect(alerts[4]).toHaveTextContent(/Passwords do not match/i);
Expand Down
4 changes: 2 additions & 2 deletions client/src/localization/languages/Eng.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ export default {
com_auth_name_required: 'Name is required',
com_auth_name_min_length: 'Name must be at least 3 characters',
com_auth_name_max_length: 'Name must be less than 80 characters',
com_auth_username: 'Username',
com_auth_username: 'Username (optional)',
com_auth_username_required: 'Username is required',
com_auth_username_min_length: 'Username must be at least 3 characters',
com_auth_username_min_length: 'Username must be at least 2 characters',
com_auth_username_max_length: 'Username must be less than 20 characters',
com_auth_already_have_account: 'Already have an account?',
com_auth_login: 'Login',
Expand Down

0 comments on commit 37347d4

Please sign in to comment.