Skip to content

Commit

Permalink
Merge branch 'master' into isAlpha-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mum-never-proud authored May 30, 2020
2 parents 9cd236a + dbb54f5 commit a919d99
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 28 deletions.
22 changes: 11 additions & 11 deletions README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/isMobilePhone.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ var phones = {
'nb-NO': /^(\+?47)?[49]\d{7}$/,
'ne-NP': /^(\+?977)?9[78]\d{8}$/,
'nl-BE': /^(\+?32|0)4?\d{8}$/,
'nl-NL': /^(\+?31|0)6?\d{8}$/,
'nl-NL': /^(((\+|00)?31\(0\))|((\+|00)?31)|0)6{1}\d{8}$/,
'nn-NO': /^(\+?47)?[49]\d{7}$/,
'pl-PL': /^(\+?48)? ?[5-8]\d ?\d{3} ?\d{2} ?\d{2}$/,
'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}$)/,
Expand Down Expand Up @@ -151,4 +151,4 @@ function isMobilePhone(str, locale, options) {
}

var locales = Object.keys(phones);
exports.locales = locales;
exports.locales = locales;
15 changes: 13 additions & 2 deletions src/lib/isJSON.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import assertString from './util/assertString';
import merge from './util/merge';

export default function isJSON(str) {
const default_json_options = {
allow_primitives: false,
};

export default function isJSON(str, options) {
assertString(str);
try {
options = merge(options, default_json_options);
let primitives = [];
if (options.allow_primitives) {
primitives = [null, false, true];
}

const obj = JSON.parse(str);
return !!obj && typeof obj === 'object';
return primitives.includes(obj) || (!!obj && typeof obj === 'object');
} catch (e) { /* ignore */ }
return false;
}
2 changes: 1 addition & 1 deletion src/lib/isMobilePhone.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const phones = {
'nb-NO': /^(\+?47)?[49]\d{7}$/,
'ne-NP': /^(\+?977)?9[78]\d{8}$/,
'nl-BE': /^(\+?32|0)4?\d{8}$/,
'nl-NL': /^(\+?31|0)6?\d{8}$/,
'nl-NL': /^(((\+|00)?31\(0\))|((\+|00)?31)|0)6{1}\d{8}$/,
'nn-NO': /^(\+?47)?[49]\d{7}$/,
'pl-PL': /^(\+?48)? ?[5-8]\d ?\d{3} ?\d{2} ?\d{2}$/,
'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}$)/,
Expand Down
35 changes: 31 additions & 4 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4369,7 +4369,27 @@ describe('Validators', () => {
'{ \'key\': \'value\' }',
'null',
'1234',
'"nope"',
],
});
});

it('should validate JSON with primitives', () => {
test({
validator: 'isJSON',
args: [{ allow_primitives: true }],
valid: [
'{ "key": "value" }',
'{}',
'null',
'false',
'true',
],
invalid: [
'{ key: "value" }',
'{ \'key\': \'value\' }',
'{ "key": value }',
'1234',
'"nope"',
],
});
Expand Down Expand Up @@ -6195,20 +6215,27 @@ describe('Validators', () => {
locale: 'nl-NL',
valid: [
'0670123456',
'+31670123456',
'0612345678',
'31612345678',
'31670123456',
'021234567',
'+3121234567',
'3121234567',
'+31612345678',
'+31670123456',
'+31(0)612345678',
'0031612345678',
'0031(0)612345678',
],
invalid: [
'12345',
'+3112345',
'3112345',
'06701234567',
'012345678',
'+3104701234567',
'3104701234567',
'0212345678',
'021234567',
'+3121234567',
'3121234567',
'+310212345678',
'310212345678',
],
Expand Down
26 changes: 19 additions & 7 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,12 @@ function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}

function _createForOfIteratorHelper(o) {
function _createForOfIteratorHelper(o, allowArrayLike) {
var it;

if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
if (Array.isArray(o) || (o = _unsupportedIterableToArray(o))) {
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
if (it) o = it;
var i = 0;

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

var it,
normalCompletion = true,
var normalCompletion = true,
didErr = false,
err;
return {
Expand Down Expand Up @@ -1527,12 +1529,22 @@ function isJWT(str) {
}, true);
}

function isJSON(str) {
var default_json_options = {
allow_primitives: false
};
function isJSON(str, options) {
assertString(str);

try {
options = merge(options, default_json_options);
var primitives = [];

if (options.allow_primitives) {
primitives = [null, false, true];
}

var obj = JSON.parse(str);
return !!obj && _typeof(obj) === 'object';
return primitives.includes(obj) || !!obj && _typeof(obj) === 'object';
} catch (e) {
/* ignore */
}
Expand Down Expand Up @@ -2208,7 +2220,7 @@ var phones = {
'nb-NO': /^(\+?47)?[49]\d{7}$/,
'ne-NP': /^(\+?977)?9[78]\d{8}$/,
'nl-BE': /^(\+?32|0)4?\d{8}$/,
'nl-NL': /^(\+?31|0)6?\d{8}$/,
'nl-NL': /^(((\+|00)?31\(0\))|((\+|00)?31)|0)6{1}\d{8}$/,
'nn-NO': /^(\+?47)?[49]\d{7}$/,
'pl-PL': /^(\+?48)? ?[5-8]\d ?\d{3} ?\d{2} ?\d{2}$/,
'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}$)/,
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit a919d99

Please sign in to comment.