@graphql-hive/cli
Version:
A CLI util to manage and control your GraphQL Hive
59 lines • 1.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParseJson = exports.ParseJsonSafe = void 0;
const tslib_1 = require("tslib");
const value_1 = require("@sinclair/typebox/value");
tslib_1.__exportStar(require("@sinclair/typebox/value"), exports);
tslib_1.__exportStar(require("./materialized-value-error"), exports);
/**
* Non-throwing version of {@link ParseJson}.
*
* If JSON parsing fails then {@link TypeBoxError} is returned.
* Otherwise its just the regular {@link Value.Parse} error of type {@link AssertError}.
*/
const ParseJsonSafe = (type, jsonString) => {
try {
return (0, exports.ParseJson)(type, jsonString);
}
catch (e) {
return e;
}
};
exports.ParseJsonSafe = ParseJsonSafe;
/**
* Parses a JSON string and validates it against a TypeBox schema
*
* @returns The parsed and typed value
*
* @throwsError {@link TypeBoxError} If JSON parsing fails or if validation fails
*/
const ParseJson = (
/**
* The TypeBox schema to validate against
*/
type,
/**
* The JSON string to parse
*/
jsonString) => {
let rawData;
try {
rawData = JSON.parse(jsonString);
}
catch (e) {
const error = e;
const message = `Failed to parse contents of given JSON because the JSON itself was invalid: ${error.message}`;
const valueError = {
value: jsonString,
type: value_1.ValueErrorType.StringFormat,
message,
path: '',
errors: [],
schema: type,
};
throw new value_1.AssertError(new value_1.Value.ValueErrorIterator([valueError][Symbol.iterator]()));
}
return value_1.Value.Parse(type, rawData);
};
exports.ParseJson = ParseJson;
//# sourceMappingURL=parse-json.js.map