UNPKG

1.23 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.IsJSON = exports.isJSON = exports.IS_JSON = void 0;
7const ValidateBy_1 = require("../common/ValidateBy");
8const isJSON_1 = __importDefault(require("validator/lib/isJSON"));
9exports.IS_JSON = 'isJson';
10/**
11 * Checks if the string is valid JSON (note: uses JSON.parse).
12 * If given value is not a string, then it returns false.
13 */
14function isJSON(value) {
15 return typeof value === 'string' && (0, isJSON_1.default)(value);
16}
17exports.isJSON = isJSON;
18/**
19 * Checks if the string is valid JSON (note: uses JSON.parse).
20 * If given value is not a string, then it returns false.
21 */
22function IsJSON(validationOptions) {
23 return (0, ValidateBy_1.ValidateBy)({
24 name: exports.IS_JSON,
25 validator: {
26 validate: (value, args) => isJSON(value),
27 defaultMessage: (0, ValidateBy_1.buildMessage)(eachPrefix => eachPrefix + '$property must be a json string', validationOptions),
28 },
29 }, validationOptions);
30}
31exports.IsJSON = IsJSON;
32//# sourceMappingURL=IsJSON.js.map
\No newline at end of file