forked from validatorjs/validator.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisJSON.js
More file actions
29 lines (24 loc) · 729 Bytes
/
isJSON.js
File metadata and controls
29 lines (24 loc) · 729 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import assertString from './util/assertString';
import includes from './util/includesArray';
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
if (options.allow_any_value) {
return true;
}
let primitives = [];
if (options.allow_primitives) {
primitives = [null, false, true];
}
return includes(primitives, obj) || (!!obj && typeof obj === 'object');
} catch (e) { /* ignore */ }
return false;
}