Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
Also updated README.md
  • Loading branch information
Michael D. Stemle, Jr committed Oct 17, 2016
2 parents f6cc1aa + 61104b4 commit e25c149
Show file tree
Hide file tree
Showing 14 changed files with 2,955 additions and 15 deletions.
14 changes: 11 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
#### HEAD

- New locales
([#585](https://github.com/chriso/validator.js/pull/585))
- Added support for greater or less than in `isFloat()`
([#544](https://github.com/chriso/validator.js/issues/544))
- Added support for `lt` and `gt` into `isInt()`
([#588](https://github.com/chriso/validator.js/pull/588))

#### 6.1.0

- Added support for greater or less than in `isFloat()`
([#544](https://github.com/chriso/validator.js/issues/544))
- Added support for ISSN validation via `isISSN()`
([#593](https://github.com/chriso/validator.js/pull/593))
- Fixed a bug in `normalizeEmail()`
([#594](https://github.com/chriso/validator.js/issues/594))
- New locales
([#585](https://github.com/chriso/validator.js/pull/585))

#### 6.0.0

- Renamed `isNull()` to `isEmpty()`
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ Passing anything other than a string is an error.
- **isHexadecimal(str)** - check if the string is a hexadecimal number.
- **isIP(str [, version])** - check if the string is an IP (version 4 or 6).
- **isISBN(str [, version])** - check if the string is an ISBN (version 10 or 13).
- **isISSN(str [, options])** - check if the string is an [ISSN](https://en.wikipedia.org/wiki/International_Standard_Serial_Number). `options` is an object which defaults to `{ case_sensitive: false, require_hyphen: false }`. If `case_sensitive` is true, ISSNs with a lowercase `'x'` as the check digit are rejected.
- **isISIN(str)** - check if the string is an [ISIN][ISIN] (stock/security identifier).
- **isISO8601(str)** - check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date.
- **isIn(str, values)** - check if the string is in a array of allowed values.
- **isInt(str [, options])** - check if the string is an integer. `options` is an object which can contain the keys `min` and/or `max` to check the integer is within boundaries (e.g. `{ min: 10, max: 99 }`). `options` can also contain the key `allow_leading_zeroes`, which when set to false will disallow integer values with leading zeroes (e.g. `{ allow_leading_zeroes: false }`).
- **isInt(str [, options])** - check if the string is an integer. `options` is an object which can contain the keys `min` and/or `max` to check the integer is within boundaries (e.g. `{ min: 10, max: 99 }`). `options` can also contain the key `allow_leading_zeroes`, which when set to false will disallow integer values with leading zeroes (e.g. `{ allow_leading_zeroes: false }`). Finally, `options` can contain the keys `gt` and/or `lt` which will enforce integers being greater than or less than, respectively, the value provided (e.g. `{gt: 1, lt: 4}` for a number between 1 and 4).
- **isJSON(str)** - check if the string is valid JSON (note: uses JSON.parse).
- **isLength(str, options)** - check if the string's length falls in a range. `options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs.
- **isLowercase(str)** - check if the string is lowercase.
Expand Down
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ var _isISBN = require('./lib/isISBN');

var _isISBN2 = _interopRequireDefault(_isISBN);

var _isISSN = require('./lib/isISSN');

var _isISSN2 = _interopRequireDefault(_isISSN);

var _isMobilePhone = require('./lib/isMobilePhone');

var _isMobilePhone2 = _interopRequireDefault(_isMobilePhone);
Expand Down Expand Up @@ -246,7 +250,7 @@ var _toString2 = _interopRequireDefault(_toString);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var version = '6.0.0';
var version = '6.1.0';

var validator = {
version: version,
Expand All @@ -269,7 +273,7 @@ var validator = {
isDate: _isDate2.default, isAfter: _isAfter2.default, isBefore: _isBefore2.default,
isIn: _isIn2.default,
isCreditCard: _isCreditCard2.default,
isISIN: _isISIN2.default, isISBN: _isISBN2.default,
isISIN: _isISIN2.default, isISBN: _isISBN2.default, isISSN: _isISSN2.default,
isMobilePhone: _isMobilePhone2.default,
isCurrency: _isCurrency2.default,
isISO8601: _isISO2.default,
Expand Down
58 changes: 58 additions & 0 deletions lib/isISSN.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isISSN;

var _assertString = require('./util/assertString');

var _assertString2 = _interopRequireDefault(_assertString);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var issn = '^\\d{4}-?\\d{3}[\\dX]$';

function isISSN(str) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

(0, _assertString2.default)(str);
var testIssn = issn;
testIssn = options.require_hyphen ? testIssn.replace('?', '') : testIssn;
testIssn = options.case_sensitive ? new RegExp(testIssn) : new RegExp(testIssn, 'i');
if (!testIssn.test(str)) {
return false;
}
var issnDigits = str.replace('-', '');
var position = 8;
var checksum = 0;
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
for (var _iterator = issnDigits[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var digit = _step.value;

var digitValue = digit.toUpperCase() === 'X' ? 10 : +digit;
checksum += digitValue * position;
--position;
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}

return checksum % 11 === 0;
}
module.exports = exports['default'];
6 changes: 5 additions & 1 deletion lib/normalizeEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ function normalizeEmail(email, options) {
if (!(0, _isEmail2.default)(email)) {
return false;
}
var parts = email.split('@', 2);

var raw_parts = email.split('@');
var domain = raw_parts.pop();
var user = raw_parts.join('@');
var parts = [user, domain];

// The domain is always lowercased, as it's case-insensitive per RFC 1035
parts[1] = parts[1].toLowerCase();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "validator",
"description": "String validation and sanitization",
"version": "6.0.0",
"version": "6.1.0",
"homepage": "http://github.com/chriso/validator.js",
"files": [
"index.js",
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const version = '6.0.0';
const version = '6.1.0';

import toDate from './lib/toDate';
import toFloat from './lib/toFloat';
Expand Down Expand Up @@ -58,6 +58,7 @@ import isCreditCard from './lib/isCreditCard';

import isISIN from './lib/isISIN';
import isISBN from './lib/isISBN';
import isISSN from './lib/isISSN';

import isMobilePhone from './lib/isMobilePhone';

Expand Down Expand Up @@ -103,7 +104,7 @@ const validator = {
isDate, isAfter, isBefore,
isIn,
isCreditCard,
isISIN, isISBN,
isISIN, isISBN, isISSN,
isMobilePhone,
isCurrency,
isISO8601,
Expand Down
22 changes: 22 additions & 0 deletions src/lib/isISSN.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import assertString from './util/assertString';

const issn = '^\\d{4}-?\\d{3}[\\dX]$';

export default function isISSN(str, options = {}) {
assertString(str);
let testIssn = issn;
testIssn = options.require_hyphen ? testIssn.replace('?', '') : testIssn;
testIssn = options.case_sensitive ? new RegExp(testIssn) : new RegExp(testIssn, 'i');
if (!testIssn.test(str)) {
return false;
}
const issnDigits = str.replace('-', '');
let position = 8;
let checksum = 0;
for (const digit of issnDigits) {
const digitValue = digit.toUpperCase() === 'X' ? 10 : +digit;
checksum += digitValue * position;
--position;
}
return checksum % 11 === 0;
}
6 changes: 5 additions & 1 deletion src/lib/normalizeEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ export default function normalizeEmail(email, options) {
if (!isEmail(email)) {
return false;
}
const parts = email.split('@', 2);

const raw_parts = email.split('@');
const domain = raw_parts.pop();
const user = raw_parts.join('@');
const parts = [user, domain];

// The domain is always lowercased, as it's case-insensitive per RFC 1035
parts[1] = parts[1].toLowerCase();
Expand Down
1 change: 1 addition & 0 deletions test/sanitizers.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ describe('Sanitizers', function () {
'[email protected]': false,
'[email protected]': false,
'[email protected]': '[email protected]',
'"foo@bar"@baz.com': '"foo@bar"@baz.com',
},
});

Expand Down
67 changes: 67 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,73 @@ describe('Validators', function () {
});
});

it('should validate ISSNs', function () {
test({
validator: 'isISSN',
valid: [
'0378-5955',
'0000-0000',
'2434-561X',
'2434-561x',
'01896016',
'20905076',
],
invalid: [
'0378-5954',
'0000-0001',
'0378-123',
'037-1234',
'0',
'2434-561c',
'1684-5370',
'19960791',
'',
],
});
test({
validator: 'isISSN',
args: [{ case_sensitive: true }],
valid: [
'2434-561X',
'2434561X',
'0378-5955',
'03785955',
],
invalid: [
'2434-561x',
'2434561x',
],
});
test({
validator: 'isISSN',
args: [{ require_hyphen: true }],
valid: [
'2434-561X',
'2434-561x',
'0378-5955',
],
invalid: [
'2434561X',
'2434561x',
'03785955',
],
});
test({
validator: 'isISSN',
args: [{ case_sensitive: true, require_hyphen: true }],
valid: [
'2434-561X',
'0378-5955',
],
invalid: [
'2434-561x',
'2434561X',
'2434561x',
'03785955',
],
});
});

it('should validate JSON', function () {
test({
validator: 'isJSON',
Expand Down
55 changes: 52 additions & 3 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,51 @@
return false;
}

var issn = '^\\d{4}-?\\d{3}[\\dX]$';

function isISSN(str) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

assertString(str);
var testIssn = issn;
testIssn = options.require_hyphen ? testIssn.replace('?', '') : testIssn;
testIssn = options.case_sensitive ? new RegExp(testIssn) : new RegExp(testIssn, 'i');
if (!testIssn.test(str)) {
return false;
}
var issnDigits = str.replace('-', '');
var position = 8;
var checksum = 0;
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
for (var _iterator = issnDigits[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var digit = _step.value;

var digitValue = digit.toUpperCase() === 'X' ? 10 : +digit;
checksum += digitValue * position;
--position;
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}

return checksum % 11 === 0;
}

/* eslint-disable max-len */
var phones = {
'ar-DZ': /^(\+?213|0)(5|6|7)\d{8}$/,
Expand Down Expand Up @@ -1283,7 +1328,11 @@
if (!isEmail(email)) {
return false;
}
var parts = email.split('@', 2);

var raw_parts = email.split('@');
var domain = raw_parts.pop();
var user = raw_parts.join('@');
var parts = [user, domain];

// The domain is always lowercased, as it's case-insensitive per RFC 1035
parts[1] = parts[1].toLowerCase();
Expand Down Expand Up @@ -1346,7 +1395,7 @@
return parts.join('@');
}

var version = '6.0.0';
var version = '6.1.0';

var validator = {
version: version,
Expand All @@ -1369,7 +1418,7 @@
isDate: isDate, isAfter: isAfter, isBefore: isBefore,
isIn: isIn,
isCreditCard: isCreditCard,
isISIN: isISIN, isISBN: isISBN,
isISIN: isISIN, isISBN: isISBN, isISSN: isISSN,
isMobilePhone: isMobilePhone,
isCurrency: isCurrency,
isISO8601: isISO8601,
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit e25c149

Please sign in to comment.