Skip to content

Commit

Permalink
Merge pull request #22 from jaebradley/use-email-validator
Browse files Browse the repository at this point in the history
fix(email-validator): use different email validation package
  • Loading branch information
jaebradley authored Mar 20, 2018
2 parents 7fafb66 + 775a0e1 commit a9528d4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 39 deletions.
20 changes: 5 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@
"semantic-release": "^15.0.0"
},
"dependencies": {
"isemail-es5": "^3.1.1"
"email-validator": "^1.1.1"
}
}
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import isEmail from 'isemail-es5';
import EmailValidator from 'email-validator';

const requiredEmailPropType = (props, propName, componentName) => {
const value = props[propName];

if (value == null || typeof value !== 'string' || !isEmail.validate(value)) {
if (value == null || typeof value !== 'string' || !EmailValidator.validate(value)) {
return new TypeError(`Invalid Email Prop Value: ${value} for ${propName} in ${componentName}`);
}

Expand Down
42 changes: 21 additions & 21 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import isEmail from 'isemail-es5';
import EmailValidator from 'email-validator';

import emailPropType from './index';

Expand All @@ -8,87 +8,87 @@ describe('Email Prop Type Validation', () => {
const email = 'foobar';

afterEach(() => {
isEmail.validate.mockClear();
isEmail.validate.mockRestore();
EmailValidator.validate.mockClear();
EmailValidator.validate.mockRestore();
});

describe('urlPropType', () => {
it('should throw an error if prop value is not a valid URL', () => {
isEmail.validate = jest.fn().mockImplementation(() => false);
EmailValidator.validate = jest.fn().mockImplementation(() => false);
const props = {};
props[propName] = email;

expect(emailPropType(props, propName, componentName))
.toEqual(new TypeError(`Invalid Email Prop Value: ${email} for ${propName} in ${componentName}`));
expect(isEmail.validate).toHaveBeenCalledTimes(1);
expect(isEmail.validate).toHaveBeenCalledWith(email);
expect(EmailValidator.validate).toHaveBeenCalledTimes(1);
expect(EmailValidator.validate).toHaveBeenCalledWith(email);
});

it('should throw an error if prop value is not a string', () => {
isEmail.validate = jest.fn().mockImplementation(() => false);
EmailValidator.validate = jest.fn().mockImplementation(() => false);
const props = {};
props[propName] = 1;

expect(emailPropType(props, propName, componentName))
.toEqual(new TypeError(`Invalid Email Prop Value: 1 for ${propName} in ${componentName}`));
expect(isEmail.validate).not.toHaveBeenCalled();
expect(EmailValidator.validate).not.toHaveBeenCalled();
});

it('should return null if prop is not defined', () => {
const props = {};

expect(emailPropType(props, propName, componentName)).toBeNull();
expect(isEmail.validate).not.toHaveBeenCalled();
expect(EmailValidator.validate).not.toHaveBeenCalled();
});

it('should return null if prop value is a valid email', () => {
isEmail.validate = jest.fn().mockImplementation(() => true);
EmailValidator.validate = jest.fn().mockImplementation(() => true);
const props = {};
props[propName] = email;

expect(emailPropType(props, propName, componentName)).toBeNull();
expect(isEmail.validate).toHaveBeenCalledTimes(1);
expect(isEmail.validate).toHaveBeenCalledWith(email);
expect(EmailValidator.validate).toHaveBeenCalledTimes(1);
expect(EmailValidator.validate).toHaveBeenCalledWith(email);
});
});

describe('requiredEmailPropType', () => {
it('should throw an error if prop value is not a valid email', () => {
isEmail.validate = jest.fn().mockImplementation(() => false);
EmailValidator.validate = jest.fn().mockImplementation(() => false);
const props = {};
props[propName] = email;

expect(emailPropType.isRequired(props, propName, componentName))
.toEqual(new TypeError(`Invalid Email Prop Value: ${email} for ${propName} in ${componentName}`));
expect(isEmail.validate).toHaveBeenCalledTimes(1);
expect(isEmail.validate).toHaveBeenCalledWith(email);
expect(EmailValidator.validate).toHaveBeenCalledTimes(1);
expect(EmailValidator.validate).toHaveBeenCalledWith(email);
});

it('should throw an error if prop value is not a string', () => {
isEmail.validate = jest.fn().mockImplementation(() => false);
EmailValidator.validate = jest.fn().mockImplementation(() => false);
const props = {};
props[propName] = 1;

expect(emailPropType(props, propName, componentName))
.toEqual(new TypeError(`Invalid Email Prop Value: 1 for ${propName} in ${componentName}`));
expect(isEmail.validate).not.toHaveBeenCalled();
expect(EmailValidator.validate).not.toHaveBeenCalled();
});

it('should throw an error if prop is not defined', () => {
const props = {};
expect(emailPropType.isRequired(props, propName, componentName))
.toEqual(new TypeError(`Invalid Email Prop Value: undefined for ${propName} in ${componentName}`));
expect(isEmail.validate).toHaveBeenCalledTimes(0);
expect(EmailValidator.validate).toHaveBeenCalledTimes(0);
});

it('should return null if prop value is a valid email', () => {
isEmail.validate = jest.fn().mockImplementation(() => true);
EmailValidator.validate = jest.fn().mockImplementation(() => true);
const props = {};
props[propName] = email;

expect(emailPropType.isRequired(props, propName, componentName)).toBeNull();
expect(isEmail.validate).toHaveBeenCalledTimes(1);
expect(isEmail.validate).toHaveBeenCalledWith(email);
expect(EmailValidator.validate).toHaveBeenCalledTimes(1);
expect(EmailValidator.validate).toHaveBeenCalledWith(email);
});
});
});

0 comments on commit a9528d4

Please sign in to comment.