count unicode characters as single characters

This commit is contained in:
Ben Wheeler 2019-10-29 13:48:05 -04:00
parent 9221b6f6b5
commit 6bf27b7e8a
2 changed files with 11 additions and 1 deletions

View file

@ -50,7 +50,9 @@ module.exports.validateUsernameRemotely = username => (
module.exports.validatePassword = (password, username) => {
if (!password) {
return {valid: false, errMsgId: 'general.required'};
} else if (password.length < 6) {
// get length of password, considering unicode symbols as single chars.
// see discussion at https://stackoverflow.com/a/54370584/2308190
} else if (Array.from(password).length < 6) {
return {valid: false, errMsgId: 'registration.validationPasswordLength'};
} else if (password === 'password') {
return {valid: false, errMsgId: 'registration.validationPasswordNotEquals'};

View file

@ -39,6 +39,14 @@ describe('unit test lib/validate.js', () => {
expect(response).toEqual({valid: false, errMsgId: 'registration.validationPasswordLength'});
response = validate.validatePassword('password');
expect(response).toEqual({valid: false, errMsgId: 'registration.validationPasswordNotEquals'});
response = validate.validatePassword('😺');
expect(response).toEqual({valid: false, errMsgId: 'registration.validationPasswordLength'});
response = validate.validatePassword('😺🦆🐝');
expect(response).toEqual({valid: false, errMsgId: 'registration.validationPasswordLength'});
response = validate.validatePassword('😺🦆🐝🐮🐠');
expect(response).toEqual({valid: false, errMsgId: 'registration.validationPasswordLength'});
response = validate.validatePassword('😺🦆🐝🐮🐠🐻');
expect(response).toEqual({valid: true});
response = validate.validatePassword('abcdefg', 'abcdefg');
expect(response).toEqual({valid: false, errMsgId: 'registration.validationPasswordNotUsername'});
response = validate.validatePassword('abcdefg', 'abcdefG');