Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -137,7 +137,7 @@ Validator | Description
**isISO4217(str)** | check if the string is a valid [ISO 4217][ISO 4217] officially assigned currency code.
**isISRC(str)** | check if the string is an [ISRC][ISRC].
**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.
**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.
**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 cosidered valid.
Comment thread
relu91 marked this conversation as resolved.
Outdated
**isJWT(str)** | check if the string is valid JWT token.
**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.
**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.
Expand Down
9 changes: 8 additions & 1 deletion src/lib/isJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@ import merge from './util/merge';

const default_json_options = {
allow_primitives: false,
allow_any_value: false,
};

export default function isJSON(str, options) {
assertString(str);
try {
options = merge(options, default_json_options);
const obj = JSON.parse(str);

// Strict RFC validation anything that parse is JSON
Comment thread
relu91 marked this conversation as resolved.
Outdated
if (options.allow_any_value) {
return true;
}

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

const obj = JSON.parse(str);
return includes(primitives, obj) || (!!obj && typeof obj === 'object');
} catch (e) { /* ignore */ }
return false;
Expand Down
23 changes: 23 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7238,6 +7238,29 @@ describe('Validators', () => {
});
});

it('should validate JSON with any value', () => {
test({
validator: 'isJSON',
args: [{ allow_any_value: true }],
valid: [
'{ "key": "value" }',
'{}',
'null',
'false',
'true',
'"RFC8259"',
'42',
Comment thread
relu91 marked this conversation as resolved.
],
invalid: [
'{ key: "value" }',
'{ \'key\': \'value\' }',
'{ "key": value }',
'01234',
"'nope'",
],
});
});

it('should validate multibyte strings', () => {
test({
validator: 'isMultibyte',
Expand Down
Loading