Merge pull request #9290 from jakemcdermott/relax-yall

Relax client-side email validator

Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
softwarefactory-project-zuul[bot]
2021-02-11 02:27:58 +00:00
committed by GitHub
2 changed files with 27 additions and 3 deletions

View File

@@ -50,10 +50,23 @@ export function requiredEmail(i18n) {
if (!value) { if (!value) {
return i18n._(t`This field must not be blank`); return i18n._(t`This field must not be blank`);
} }
if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value)) {
return i18n._(t`Invalid email address`); // This isn't a perfect validator. It's likely to let a few
// invalid (though unlikely) email addresses through.
// This is ok, because the server will always do strict validation for us.
const splitVals = value.split('@');
if (splitVals.length >= 2) {
if (splitVals[0] && splitVals[1]) {
// We get here if the string has an '@' that is enclosed by
// non-empty substrings
return undefined;
}
} }
return undefined;
return i18n._(t`Invalid email address`);
}; };
} }

View File

@@ -8,6 +8,7 @@ import {
url, url,
combine, combine,
regExp, regExp,
requiredEmail,
} from './validators'; } from './validators';
const i18n = { _: val => val }; const i18n = { _: val => val };
@@ -187,4 +188,14 @@ describe('validators', () => {
expect(regExp(i18n)('ok')).toBeUndefined(); expect(regExp(i18n)('ok')).toBeUndefined();
expect(regExp(i18n)('[^a-zA-Z]')).toBeUndefined(); expect(regExp(i18n)('[^a-zA-Z]')).toBeUndefined();
}); });
test('email validator rejects obviously invalid email ', () => {
expect(requiredEmail(i18n)('foobar321')).toEqual({
id: 'Invalid email address',
});
});
test('bob has email', () => {
expect(requiredEmail(i18n)('bob@localhost')).toBeUndefined();
});
}); });