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

Add option allowNonTld for email format validator #330

Merged
merged 4 commits into from
Aug 29, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 10 additions & 2 deletions addon/validators/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ const {
* - `allowBlank` (**Boolean**): If true, skips validation if the value is empty
* - `type` (**String**): Can be the one of the following options [`email`, `phone`, `url`]
* - `regex` (**RegExp**): The regular expression to test against
* - `allowNonTld` (**Boolean**): If true, the predefined regular expression `email` allows non top-level domains
*
* ```javascript
* // Examples
* validator('format', {
* type: 'email'
* type: 'email',
* allowNonTld: true
* })
* validator('format', {
* allowBlank: true,
Expand Down Expand Up @@ -58,6 +60,7 @@ const {
export default Base.extend({
regularExpressions: {
email: /^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i,
emailNonTld: /^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.?)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i,
phone: /^([\+]?1\s*[-\/\.]?\s*)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT]?[\.]?|extension)\s*([#*\d]+))*$/,
url: /(?:([A-Za-z]+):)?(\/{0,3})[a-zA-Z0-9][a-zA-Z-0-9]*(\.[\w-]+)+([\w.,@?^=%&:\/~+#-{}]*[\w@?^=%&\/~+#-{}])??/
},
Expand All @@ -76,7 +79,12 @@ export default Base.extend({
const { regex, type } = options;

if (type && !isNone(regularExpressions[type]) && isNone(regex)) {
options.regex = regularExpressions[type];
if (type === 'email' && options.allowNonTld) {
options.regex = regularExpressions['emailNonTld'];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As emailNonTld is a literal string, please replace it with options.regex = regularExpressions.emailNonTld

}
else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use

if(condition) {

} else {

}

options.regex = regularExpressions[type];
}
}

return this._super(options, defaultOptions, globalOptions);
Expand Down
2 changes: 1 addition & 1 deletion addon/validators/presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const {
* })
*
* validator('presence', {
* presence: true,
* presence: true,
* ignoreBlank: true,
* message: 'should not be empty or consist only of spaces'
* })
Expand Down
62 changes: 61 additions & 1 deletion tests/unit/validators/format-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test('allow blank', function(assert) {
assert.equal(message, 'This field must be a valid email address');
});

test('email', function(assert) {
test('email no option', function(assert) {
const validAddresses = [
'email@domain.com',
'firstname.lastname@domain.com',
Expand All @@ -82,6 +82,14 @@ test('email', function(assert) {
'あいうえお@domain.com',
'email@domain.com (Joe Smith)',
'email@domain',
'email@domain.',
'email@domain.-',
'email@domain-',
'email@domain-.',
'email@domain.com.',
'email@domain.com.-',
'email@domain.com-',
'email@domain.com-.',
'email@-domain.com',
'email@domain..com'
];
Expand All @@ -98,6 +106,58 @@ test('email', function(assert) {
invalidAddresses.forEach((email) => assert.equal(validator.validate(email, options), 'This field must be a valid email address', `validation of ${email} must fail`));
});

test('email option allowNonTld', function(assert) {
const validAddresses = [
'email@domain.com',
'firstname.lastname@domain.com',
'email@subdomain.domain.com',
'firstname+lastname@domain.com',
'1234567890@domain.com',
'email@domain-one.com',
'_______@domain.com',
'email@domain.name',
'email@domain.co.jp',
'firstname-lastname@domain.com',
'EMAIL@DOMAIN.COM',
'email@domain'
];
const invalidAddresses = [
'plainaddress',
'#@%^%#$@#$@#.com',
'@domain.com',
'Joe Smith <email@domain.com>',
'email.domain.com',
'email@domain@domain.com',
'.email@domain.com',
'email.@domain.com',
'email..email@domain.com',
'あいうえお@domain.com',
'email@domain.com (Joe Smith)',
'email@domain.',
'email@domain.-',
'email@domain-',
'email@domain-.',
'email@domain.com.',
'email@domain.com.-',
'email@domain.com-',
'email@domain.com-.',
'email@-domain.com',
'email@domain..com'
];

assert.expect(validAddresses.length + invalidAddresses.length);

options = {
type: 'email',
allowNonTld: true
};

options = validator.buildOptions(options, {}).copy();

validAddresses.forEach((email) => assert.equal(validator.validate(email, options), true, `validation of ${email} must succeed`));
invalidAddresses.forEach((email) => assert.equal(validator.validate(email, options), 'This field must be a valid email address', `validation of ${email} must fail`));
});

test('phone', function(assert) {
assert.expect(2);

Expand Down