Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: npm/npm-user-validate
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 688552a47e6c22cd07206894dcd21b9d7a37ee56
Choose a base ref
...
head repository: npm/npm-user-validate
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 5c5471ceee0d9f5249157f5ae10d65a5b92605c9
Choose a head ref
  • 6 commits
  • 7 files changed
  • 4 contributors

Commits on Apr 21, 2017

  1. fix: update build environment

    Benjamin Coe authored and iarna committed Apr 21, 2017
    Copy the full SHA
    c800063 View commit details
  2. fix: added regex for blocking illegal characters in usernames

    Benjamin Coe authored and iarna committed Apr 21, 2017
    Copy the full SHA
    ac3b200 View commit details
  3. 1.0.0

    iarna committed Apr 21, 2017
    1
    Copy the full SHA
    df602d6 View commit details
  4. Copy the full SHA
    cd75393 View commit details

Commits on Oct 15, 2020

  1. fix: update email validation

    PR-URL: #15
    Credit: @
    Close: #15
    Reviewed-by: @isaacs
    darcyclarke authored and isaacs committed Oct 15, 2020
    1
    Copy the full SHA
    c8a87da View commit details
  2. 1.0.1

    isaacs committed Oct 15, 2020
    1
    Copy the full SHA
    5c5471c View commit details
Showing with 58 additions and 25 deletions.
  1. +2 −1 .gitignore
  2. +3 −4 .travis.yml
  3. +17 −4 npm-user-validate.js
  4. +10 −4 package.json
  5. +12 −5 test/email.test.js
  6. +5 −5 test/pw.test.js
  7. +9 −2 test/username.test.js
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -10,4 +10,5 @@ coverage.html
.idea
lib-cov

node_modules
node_modules
.nyc_output
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: node_js
before_install:
- npm install -g npm@latest
sudo: false
node_js:
- "0.8"
- "0.10"
- "6"
- "7"
- "4"
21 changes: 17 additions & 4 deletions npm-user-validate.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
exports.email = email
exports.pw = pw
exports.username = username

var requirements = exports.requirements = {
username: {
length: 'Name length must be less than or equal to 214 characters long',
lowerCase: 'Name must be lowercase',
urlSafe: 'Name may not contain non-url-safe chars',
dot: 'Name may not start with "."'
dot: 'Name may not start with "."',
illegal: 'Name may not contain illegal character'
},
password: {},
email: {
length: 'Email length must be less then or equal to 254 characters long',
valid: 'Email must be an email address'
}
};
}

var illegalCharacterRe = new RegExp('([' + [
"'"
].join() + '])')

function username (un) {
if (un !== un.toLowerCase()) {
@@ -32,11 +37,19 @@ function username (un) {
return new Error(requirements.username.length)
}

var illegal = un.match(illegalCharacterRe)
if (illegal) {
return new Error(requirements.username.illegal + ' "' + illegal[0] + '"')
}

return null
}

function email (em) {
if (!em.match(/^.+@.+\..+$/)) {
if (em.length > 254) {
return new Error(requirements.email.length)
}
if (!em.match(/^[^@]+@.+\..+$/)) {
return new Error(requirements.email.valid)
}

14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{
"name": "npm-user-validate",
"version": "0.1.5",
"version": "1.0.1",
"description": "User validations for npm",
"main": "npm-user-validate.js",
"devDependencies": {
"tap": "^1.2.0"
"standard": "^8.4.0",
"standard-version": "^3.0.0",
"tap": "^7.1.2"
},
"scripts": {
"test": "tap test/*.js"
"pretest": "standard",
"test": "tap --100 test/*.js"
},
"repository": {
"type": "git",
@@ -19,5 +22,8 @@
"registry"
],
"author": "Robert Kowalski <rok@kowalski.gd>",
"license": "BSD-2-Clause"
"license": "BSD-2-Clause",
"files": [
"npm-user-validate.js"
]
}
17 changes: 12 additions & 5 deletions test/email.test.js
Original file line number Diff line number Diff line change
@@ -2,25 +2,32 @@ var test = require('tap').test
var v = require('../npm-user-validate.js').email

test('email misses an @', function (t) {
err = v('namedomain')
var err = v('namedomain')
t.type(err, 'object')
t.end()
})

test('email is longer then 254 characters', function (t) {
var str = '@'.repeat(255)
var err = v(str)
t.type(err, 'object')
t.end()
})

test('email misses a dot', function (t) {
err = v('name@domain')
var err = v('name@domain')
t.type(err, 'object')
t.end()
})

test('email misses a string before the @', function (t) {
err = v('@domain')
var err = v('@domain')
t.type(err, 'object')
t.end()
})

test('email is ok', function (t) {
err = v('name@domain.com')
var err = v('name@domain.com')
t.type(err, 'null')
t.end()
})
})
10 changes: 5 additions & 5 deletions test/pw.test.js
Original file line number Diff line number Diff line change
@@ -2,31 +2,31 @@ var test = require('tap').test
var v = require('../npm-user-validate.js').pw

test('pw contains a \'', function (t) {
err = v('\'')
var err = v('\'')
t.type(err, 'null')
t.end()
})

test('pw contains a :', function (t) {
err = v(':')
var err = v(':')
t.type(err, 'null')
t.end()
})

test('pw contains a @', function (t) {
err = v('@')
var err = v('@')
t.notOk(err, 'null')
t.end()
})

test('pw contains a "', function (t) {
err = v('"')
var err = v('"')
t.type(err, 'null')
t.end()
})

test('pw is ok', function (t) {
err = v('duck')
var err = v('duck')
t.type(err, 'null')
t.end()
})
11 changes: 9 additions & 2 deletions test/username.test.js
Original file line number Diff line number Diff line change
@@ -15,6 +15,13 @@ test('username may not contain non-url-safe chars', function (t) {
t.end()
})

test('username may not contain illegal characters', function (t) {
var err = v("ben's")
t.type(err, 'object')
t.match(err.message, /illegal character "'"/)
t.end()
})

test('username may not start with "."', function (t) {
var err = v('.username')
t.type(err, 'object')
@@ -27,13 +34,13 @@ test('username may not be longer than 214 characters', function (t) {
t.type(err, 'object')
t.match(err.message, /less than or equal to 214/)
t.end()
});
})

test('username may be as long as 214 characters', function (t) {
var err = v('bacon-ipsum-dolor-amet-tongue-short-loin-landjaeger-tenderloin-ball-tip-pork-loin-porchetta-pig-pork-chop-beef-ribs-pork-belly--shankle-t-bone-turducken-tongue-landjaeger-pork-loin-beef-chicken-short-loin-porchetta')
t.type(err, 'null')
t.end()
});
})

test('username is ok', function (t) {
var err = v('ente')