Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request #85 from braydonf/email-validation
Browse files Browse the repository at this point in the history
Relax email validation to only care about lengths
  • Loading branch information
bryanchriswhite committed Mar 17, 2017
2 parents 227e563 + 598a467 commit bcc8fe2
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 26 deletions.
4 changes: 4 additions & 0 deletions lib/models/debit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const Debit = new mongoose.Schema({
type: String,
required: true,
ref: 'User',
validate: {
validator: value => utils.isValidEmail(value),
message: 'Invalid user email address'
}
},
created: {
type: Date,
Expand Down
6 changes: 5 additions & 1 deletion lib/models/payment-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ const PaymentProcessorSchema = new mongoose.Schema({
user: {
type: String,
ref: 'User',
required: true
required: true,
validate: {
validator: value => utils.isValidEmail(value),
message: 'Invalid user email address'
}
},
name: {
type: String,
Expand Down
8 changes: 7 additions & 1 deletion lib/models/pubkey.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ var PublicKey = new mongoose.Schema({
},
user: {
type: String,
ref: 'User'
ref: 'User',
validate: {
validator: function(v) {
return utils.isValidEmail(v);
},
message: 'Invalid user email address'
},
},
label: {
type: String
Expand Down
6 changes: 5 additions & 1 deletion lib/models/storage-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ var StorageEvent = new mongoose.Schema({
user: {
type: String,
ref: 'User',
required: true
required: true,
validate: {
validator: value => utils.isValidEmail(value),
message: 'Invalid user email address'
}
},
timestamp: {
type: Date,
Expand Down
6 changes: 5 additions & 1 deletion lib/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ const { isValidEmail } = require('../utils');
var UserSchema = new mongoose.Schema({
_id: {
type: String,
required: true
required: true,
validate: {
validator: value => isValidEmail(value),
message: 'Invalid user email address'
}
},
hashpass: {
type: String,
Expand Down
6 changes: 5 additions & 1 deletion lib/models/usernonce.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ var UserNonce = new mongoose.Schema({
user: {
type: String,
ref: 'User',
required: true
required: true,
validate: {
validator: value => utils.isValidEmail(value),
message: 'Invalid user email address'
}
},
nonce: {
type: String,
Expand Down
7 changes: 1 addition & 6 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
const moment = require('moment');

module.exports.isValidEmail = function (email) {
const tester = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@(([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})|(([a-zA-Z\-0-9]+)(\.[a-zA-Z\-0-9]+){1,}))$/; // jshint ignore:line

if (email.length > 254) {
return false;
}

const tester = /^.{1,64}@.{1,255}$/;
if (!tester.test(email)) {
return false;
}
Expand Down
4 changes: 3 additions & 1 deletion test/contact.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ describe('Storage/models/Contact', function() {
lastSeen: Date.now()
}, next);
}, function(err) {
expect(err).to.not.be.instanceOf(Error);
if (err) {
return done(err);
}
Contact.count({}, function(err, count) {
expect(count).to.equal(3);
done();
Expand Down
8 changes: 2 additions & 6 deletions test/data/email.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
"invalid": [
"me@",
"@example.com",
"me.@example.com",
".me@example.com",
"me@example..com",
"me.example@com",
"me\\@example.com",
"wrong@domain"
"c4e34e270ebbe228b2543b6bd2c148e582659ccbad061ab9dc67518e6f0275f700@domain.com",
"user@614c22107dbc2ad8bee0e7b4bec8818ff52181be56233009903d8a4a1e74a80cd1fcf34707132c25a7cda0fc70ab572ca1d39b6935e254ddcfa2b2efe58cbf371bd3ef610dfc6822b03420a922358ab3fc7b120f48289c341fbd9a30137f5b05db8ad95d7ce8758346ebb6589f6f6a20ecc204c0d219eac61c9bc43b499eae11"
]
}
2 changes: 1 addition & 1 deletion test/debit.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('Storage/models/Debit', function() {
describe('@constructor', function() {
it('should fail validation', function(done) {
const debit = new Debit({
user: 'nobody@nowhere',
user: 'nobody@',
amount: 10000,
type: DEBIT_TYPES.STORAGE
});
Expand Down
2 changes: 1 addition & 1 deletion test/frame.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('Storage/models/Frame', function() {
describe('@constructor', function() {
it('should fail validation', function(done) {
const frame = new Frame({
user: 'nobody@nowhere'
user: 'nobody@'
});
frame.save((err) => {
expect(err).to.be.instanceOf(Error);
Expand Down
2 changes: 1 addition & 1 deletion test/payment-processor.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Storage/models/PaymentProcessor', function() {
describe('@constructor', function() {
it('should fail validation', function(done) {
const pp = new PaymentProcessor({
user: 'nobody@nowhere',
user: 'nobody@',
name: 'stripe'
});
pp.save((err) => {
Expand Down
8 changes: 4 additions & 4 deletions test/user.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ describe('Storage/models/User', function() {
});
});

it('should not create a invalid email (no tld)', function(done) {
User.create('wrong@domain', sha256('password'), function(err) {
it('should not create a invalid email (no domain)', function(done) {
User.create('wrong@', sha256('password'), function(err) {
expect(err).to.be.instanceOf(Error);
expect(err.message).to.equal('Invalid email');
done();
Expand Down Expand Up @@ -106,9 +106,9 @@ describe('Storage/models/User', function() {
});
});

it('should not create email with invalid symbols', function(done) {
it('should not create email with no user', function(done) {
User.create(
'test()test@gmail.com',
'@gmail.com',
sha256('password'),
function(err) {
expect(err).to.be.instanceOf(Error);
Expand Down
2 changes: 1 addition & 1 deletion test/usernonce.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('Storage/models/UserNonce', function() {
describe('@constructor', function() {
it('should fail validation', function(done) {
const nonce = new UserNonce({
user: 'nobody@nowhere',
user: 'nobody@',
nonce: n
});
nonce.save((err) => {
Expand Down

0 comments on commit bcc8fe2

Please sign in to comment.