Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1327 : Adding option to allow primitives without BC break #1328

Merged
merged 3 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Validator | Description
**isISO31661Alpha3(str)** | check if the string is a valid [ISO 3166-1 alpha-3](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3) officially assigned country code.
**isISRC(str)** | check if the string is a [ISRC](https://en.wikipedia.org/wiki/International_Standard_Recording_Code).
**isISSN(str [, options])** | check if the string is an [ISSN](https://en.wikipedia.org/wiki/International_Standard_Serial_Number).<br/><br/>`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.
**isJSON(str)** | check if the string is valid JSON (note: uses JSON.parse).
**isJSON(str [, options])** | check if the string is valid JSON (note: uses JSON.parse).<br/><br/>`options` is an object which defaults to `{ allow_primitives: false }`. If `allow_primitives` is true, the primitives 'true', 'false' and 'null' are accepted as valid JSON values.
**isJWT(str)** | check if the string is valid JWT token.
**isLatLong(str)**                     | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`.
**isLength(str [, options])** | check if the string's length falls in a range.<br/><br/>`options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs.
Expand Down
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;
}
20 changes: 20 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4330,7 +4330,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
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 @@ -1514,12 +1516,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 @@ -2195,7 +2207,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.