Skip to content

Commit a919d99

Browse files
Merge branch 'master' into isAlpha-refactor
2 parents 9cd236a + dbb54f5 commit a919d99

File tree

7 files changed

+78
-28
lines changed

7 files changed

+78
-28
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Large diffs are not rendered by default.

lib/isMobilePhone.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ var phones = {
8383
'nb-NO': /^(\+?47)?[49]\d{7}$/,
8484
'ne-NP': /^(\+?977)?9[78]\d{8}$/,
8585
'nl-BE': /^(\+?32|0)4?\d{8}$/,
86-
'nl-NL': /^(\+?31|0)6?\d{8}$/,
86+
'nl-NL': /^(((\+|00)?31\(0\))|((\+|00)?31)|0)6{1}\d{8}$/,
8787
'nn-NO': /^(\+?47)?[49]\d{7}$/,
8888
'pl-PL': /^(\+?48)? ?[5-8]\d ?\d{3} ?\d{2} ?\d{2}$/,
8989
'pt-BR': /(?=^(\+?5{2}\-?|0)[1-9]{2}\-?\d{4}\-?\d{4}$)(^(\+?5{2}\-?|0)[1-9]{2}\-?[6-9]{1}\d{3}\-?\d{4}$)|(^(\+?5{2}\-?|0)[1-9]{2}\-?9[6-9]{1}\d{3}\-?\d{4}$)/,
@@ -151,4 +151,4 @@ function isMobilePhone(str, locale, options) {
151151
}
152152

153153
var locales = Object.keys(phones);
154-
exports.locales = locales;
154+
exports.locales = locales;

src/lib/isJSON.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import assertString from './util/assertString';
2+
import merge from './util/merge';
23

3-
export default function isJSON(str) {
4+
const default_json_options = {
5+
allow_primitives: false,
6+
};
7+
8+
export default function isJSON(str, options) {
49
assertString(str);
510
try {
11+
options = merge(options, default_json_options);
12+
let primitives = [];
13+
if (options.allow_primitives) {
14+
primitives = [null, false, true];
15+
}
16+
617
const obj = JSON.parse(str);
7-
return !!obj && typeof obj === 'object';
18+
return primitives.includes(obj) || (!!obj && typeof obj === 'object');
819
} catch (e) { /* ignore */ }
920
return false;
1021
}

src/lib/isMobilePhone.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const phones = {
7777
'nb-NO': /^(\+?47)?[49]\d{7}$/,
7878
'ne-NP': /^(\+?977)?9[78]\d{8}$/,
7979
'nl-BE': /^(\+?32|0)4?\d{8}$/,
80-
'nl-NL': /^(\+?31|0)6?\d{8}$/,
80+
'nl-NL': /^(((\+|00)?31\(0\))|((\+|00)?31)|0)6{1}\d{8}$/,
8181
'nn-NO': /^(\+?47)?[49]\d{7}$/,
8282
'pl-PL': /^(\+?48)? ?[5-8]\d ?\d{3} ?\d{2} ?\d{2}$/,
8383
'pt-BR': /(?=^(\+?5{2}\-?|0)[1-9]{2}\-?\d{4}\-?\d{4}$)(^(\+?5{2}\-?|0)[1-9]{2}\-?[6-9]{1}\d{3}\-?\d{4}$)|(^(\+?5{2}\-?|0)[1-9]{2}\-?9[6-9]{1}\d{3}\-?\d{4}$)/,

test/validators.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4369,7 +4369,27 @@ describe('Validators', () => {
43694369
'{ \'key\': \'value\' }',
43704370
'null',
43714371
'1234',
4372+
'"nope"',
4373+
],
4374+
});
4375+
});
4376+
4377+
it('should validate JSON with primitives', () => {
4378+
test({
4379+
validator: 'isJSON',
4380+
args: [{ allow_primitives: true }],
4381+
valid: [
4382+
'{ "key": "value" }',
4383+
'{}',
4384+
'null',
43724385
'false',
4386+
'true',
4387+
],
4388+
invalid: [
4389+
'{ key: "value" }',
4390+
'{ \'key\': \'value\' }',
4391+
'{ "key": value }',
4392+
'1234',
43734393
'"nope"',
43744394
],
43754395
});
@@ -6195,20 +6215,27 @@ describe('Validators', () => {
61956215
locale: 'nl-NL',
61966216
valid: [
61976217
'0670123456',
6198-
'+31670123456',
6218+
'0612345678',
6219+
'31612345678',
61996220
'31670123456',
6200-
'021234567',
6201-
'+3121234567',
6202-
'3121234567',
6221+
'+31612345678',
6222+
'+31670123456',
6223+
'+31(0)612345678',
6224+
'0031612345678',
6225+
'0031(0)612345678',
62036226
],
62046227
invalid: [
62056228
'12345',
62066229
'+3112345',
62076230
'3112345',
62086231
'06701234567',
6232+
'012345678',
62096233
'+3104701234567',
62106234
'3104701234567',
62116235
'0212345678',
6236+
'021234567',
6237+
'+3121234567',
6238+
'3121234567',
62126239
'+310212345678',
62136240
'310212345678',
62146241
],

validator.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,12 @@ function _nonIterableRest() {
9898
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
9999
}
100100

101-
function _createForOfIteratorHelper(o) {
101+
function _createForOfIteratorHelper(o, allowArrayLike) {
102+
var it;
103+
102104
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
103-
if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) {
105+
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
106+
if (it) o = it;
104107
var i = 0;
105108

106109
var F = function () {};
@@ -126,8 +129,7 @@ function _createForOfIteratorHelper(o) {
126129
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
127130
}
128131

129-
var it,
130-
normalCompletion = true,
132+
var normalCompletion = true,
131133
didErr = false,
132134
err;
133135
return {
@@ -1527,12 +1529,22 @@ function isJWT(str) {
15271529
}, true);
15281530
}
15291531

1530-
function isJSON(str) {
1532+
var default_json_options = {
1533+
allow_primitives: false
1534+
};
1535+
function isJSON(str, options) {
15311536
assertString(str);
15321537

15331538
try {
1539+
options = merge(options, default_json_options);
1540+
var primitives = [];
1541+
1542+
if (options.allow_primitives) {
1543+
primitives = [null, false, true];
1544+
}
1545+
15341546
var obj = JSON.parse(str);
1535-
return !!obj && _typeof(obj) === 'object';
1547+
return primitives.includes(obj) || !!obj && _typeof(obj) === 'object';
15361548
} catch (e) {
15371549
/* ignore */
15381550
}
@@ -2208,7 +2220,7 @@ var phones = {
22082220
'nb-NO': /^(\+?47)?[49]\d{7}$/,
22092221
'ne-NP': /^(\+?977)?9[78]\d{8}$/,
22102222
'nl-BE': /^(\+?32|0)4?\d{8}$/,
2211-
'nl-NL': /^(\+?31|0)6?\d{8}$/,
2223+
'nl-NL': /^(((\+|00)?31\(0\))|((\+|00)?31)|0)6{1}\d{8}$/,
22122224
'nn-NO': /^(\+?47)?[49]\d{7}$/,
22132225
'pl-PL': /^(\+?48)? ?[5-8]\d ?\d{3} ?\d{2} ?\d{2}$/,
22142226
'pt-BR': /(?=^(\+?5{2}\-?|0)[1-9]{2}\-?\d{4}\-?\d{4}$)(^(\+?5{2}\-?|0)[1-9]{2}\-?[6-9]{1}\d{3}\-?\d{4}$)|(^(\+?5{2}\-?|0)[1-9]{2}\-?9[6-9]{1}\d{3}\-?\d{4}$)/,

validator.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)