dockest
Version:
Dockest is an integration testing tool aimed at alleviating the process of evaluating unit tests whilst running multi-container Docker applications.
49 lines • 1.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.resetGlobalCustomZodErrorMap = exports.setGlobalCustomZodErrorMap = exports.customZodErrorMap = void 0;
const zod_1 = require("zod");
const DEFAULT_CUSTOM_ZOD_ERROR_MAP_OPTIONS = {
appendInputData: true,
};
/**
* Create a custom ZodErrorMap with options to append input data to the default error message.
*
* `customZodErrorMap` can also be used in individual parsings, for example:
* `Person.safeParse(person, { errorMap: customZodErrorMap() });`
*/
function customZodErrorMap(customZodErrorMapOptions = DEFAULT_CUSTOM_ZOD_ERROR_MAP_OPTIONS) {
return (_issue, ctx) => {
if (!customZodErrorMapOptions.appendInputData) {
return {
message: ctx.defaultError,
};
}
return {
message: `${ctx.defaultError} [DATA]<${JSON.stringify(ctx.data)}>`,
};
};
}
exports.customZodErrorMap = customZodErrorMap;
/**
* There can only be one global ZodErrorMap set at any one time.
*
* Using this function will set the global ZodErrorMap to `customZodErrorMap`.
*
* The default ZodErrorMap can still be used in individual parsings, for example:
* `Person.safeParse(person, { errorMap: z.defaultErrorMap });`
*/
function setGlobalCustomZodErrorMap(options = DEFAULT_CUSTOM_ZOD_ERROR_MAP_OPTIONS) {
zod_1.z.setErrorMap(customZodErrorMap(options));
}
exports.setGlobalCustomZodErrorMap = setGlobalCustomZodErrorMap;
/**
* There can only be one global ZodErrorMap set at any one time.
*
* Using this function will reset the global ZodErrorMap to `z.defaultErrorMap`,
* essentially overriding the previous global ZodErrorMap.
*/
function resetGlobalCustomZodErrorMap() {
zod_1.z.setErrorMap(zod_1.z.defaultErrorMap);
}
exports.resetGlobalCustomZodErrorMap = resetGlobalCustomZodErrorMap;
//# sourceMappingURL=custom-zod-error-map.js.map