Skip to content

Commit fcfbff5

Browse files
authored
feat(isJson): allow any valid JSON value to pass (#2690)
1 parent f06caee commit fcfbff5

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Validator | Description
137137
**isISO4217(str)** | check if the string is a valid [ISO 4217][ISO 4217] officially assigned currency code.
138138
**isISRC(str)** | check if the string is an [ISRC][ISRC].
139139
**isISSN(str [, options])** | check if the string is an [ISSN][ISSN].<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.
140-
**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.
140+
**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, allow_any_value: false }`. If `allow_primitives` is true, the primitives 'true', 'false' and 'null' are accepted as valid JSON values. If `allow_any_value` is true, any string that passes JSON.parse is considered valid.
141141
**isJWT(str)** | check if the string is valid JWT token.
142142
**isLatLong(str [, options])** | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`.<br/><br/>`options` is an object that defaults to `{ checkDMS: false }`. Pass `checkDMS` as `true` to validate DMS(degrees, minutes, and seconds) latitude-longitude format.
143143
**isLength(str [, options])** | check if the string's length falls in a range and equal to any of the integers of the `discreteLengths` array if provided.<br/><br/>`options` is an object which defaults to `{ min: 0, max: undefined, discreteLengths: undefined }`. Note: this function takes into account surrogate pairs.

src/lib/isJSON.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@ import merge from './util/merge';
44

55
const default_json_options = {
66
allow_primitives: false,
7+
allow_any_value: false,
78
};
89

910
export default function isJSON(str, options) {
1011
assertString(str);
1112
try {
1213
options = merge(options, default_json_options);
14+
const obj = JSON.parse(str);
15+
16+
// When allow_any_value is true, accept anything that JSON.parse successfully parses
17+
if (options.allow_any_value) {
18+
return true;
19+
}
20+
1321
let primitives = [];
1422
if (options.allow_primitives) {
1523
primitives = [null, false, true];
1624
}
1725

18-
const obj = JSON.parse(str);
1926
return includes(primitives, obj) || (!!obj && typeof obj === 'object');
2027
} catch (e) { /* ignore */ }
2128
return false;

test/validators.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7238,6 +7238,30 @@ describe('Validators', () => {
72387238
});
72397239
});
72407240

7241+
it('should validate JSON with any value', () => {
7242+
test({
7243+
validator: 'isJSON',
7244+
args: [{ allow_any_value: true }],
7245+
valid: [
7246+
'{ "key": "value" }',
7247+
'{}',
7248+
'null',
7249+
'false',
7250+
'true',
7251+
'"RFC8259"',
7252+
'42',
7253+
'0',
7254+
],
7255+
invalid: [
7256+
'{ key: "value" }',
7257+
'{ \'key\': \'value\' }',
7258+
'{ "key": value }',
7259+
'01234',
7260+
"'nope'",
7261+
],
7262+
});
7263+
});
7264+
72417265
it('should validate multibyte strings', () => {
72427266
test({
72437267
validator: 'isMultibyte',

0 commit comments

Comments
 (0)