Skip to content

Commit

Permalink
Merge pull request #8 from jaebradley/string-check
Browse files Browse the repository at this point in the history
fix(validation): add string validation
  • Loading branch information
jaebradley authored Dec 7, 2017
2 parents faea5b4 + 4d8ae80 commit 27a6f4c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import isEmail from 'isemail';
const requiredEmailPropType = (props, propName, componentName) => {
const value = props[propName];

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

Expand Down
20 changes: 20 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ describe('Email Prop Type Validation', () => {
expect(isEmail.validate).toHaveBeenCalledWith(email);
});

it('should throw an error if prop value is not a string', () => {
isEmail.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();
});

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

Expand Down Expand Up @@ -54,6 +64,16 @@ describe('Email Prop Type Validation', () => {
expect(isEmail.validate).toHaveBeenCalledWith(email);
});

it('should throw an error if prop value is not a string', () => {
isEmail.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();
});

it('should throw an error if prop is not defined', () => {
const props = {};
expect(emailPropType.isRequired(props, propName, componentName))
Expand Down

0 comments on commit 27a6f4c

Please sign in to comment.