Skip to content

Commit 88691c7

Browse files
committed
Validate email addresses.
1 parent ace39c2 commit 88691c7

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/service.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ function isUrl( str ) {
2121
return true;
2222
}
2323

24+
function isEmail( str ) {
25+
return (/^[a-zA-Z0-9.!#$%&'*+\/=?\^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/).test( str );
26+
}
27+
2428
// package.json
2529
extend( Repo.prototype, {
2630
getPackageJson: function( version, file, fn ) {
@@ -101,9 +105,13 @@ extend( Repo.prototype, {
101105
if ( typeof package.author.name !== "string" ) {
102106
errors.push( "Invalid data type for author.name; must be a string." );
103107
}
104-
// TODO: verify email address format
105-
if ( "email" in package.author && typeof package.author.email !== "string" ) {
106-
errors.push( "Invalid data type for author.email; must be a string." );
108+
109+
if ( "email" in package.author ) {
110+
if ( typeof package.author.email !== "string" ) {
111+
errors.push( "Invalid data type for author.email; must be a string." );
112+
} else if ( !isEmail( package.author.email ) ) {
113+
errors.push( "Invalid value for author.email." );
114+
}
107115
}
108116

109117
if ( "url" in package.author ) {
@@ -196,9 +204,13 @@ extend( Repo.prototype, {
196204
if ( typeof maintainer.name !== "string" ) {
197205
errors.push( "Invalid data type for maintainers[" + i + "].name; must be a string." );
198206
}
199-
// TODO: verify email address format
200-
if ( "email" in maintainer && typeof maintainer.email !== "string" ) {
201-
errors.push( "Invalid data type for maintainers[" + i + "].email; must be a string." );
207+
208+
if ( "email" in maintainer ) {
209+
if ( typeof maintainer.email !== "string" ) {
210+
errors.push( "Invalid data type for maintainers[" + i + "].email; must be a string." );
211+
} else if ( !isEmail( maintainer.email ) ) {
212+
errors.push( "Invalid value for maintainers[" + i + "].email." );
213+
}
202214
}
203215

204216
if ( "url" in maintainer ) {

0 commit comments

Comments
 (0)