fastify-type-provider-zod
Version:
Zod Type Provider for Fastify@5
61 lines (60 loc) • 2.47 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const createError = require("@fastify/error");
const InvalidSchemaError = createError("FST_ERR_INVALID_SCHEMA", "Invalid schema passed: %s", 500);
const ZodFastifySchemaValidationErrorSymbol = Symbol.for("ZodFastifySchemaValidationError");
const ResponseSerializationBase = createError(
"FST_ERR_RESPONSE_SERIALIZATION",
"Response doesn't match the schema",
500
);
class ResponseSerializationError extends ResponseSerializationBase {
constructor(method, url, options) {
super({ cause: options.cause });
__publicField(this, "cause");
this.method = method;
this.url = url;
this.cause = options.cause;
}
}
function isResponseSerializationError(value) {
return "method" in value;
}
function isZodFastifySchemaValidationError(error) {
return typeof error === "object" && error !== null && error[ZodFastifySchemaValidationErrorSymbol] === true;
}
function hasZodFastifySchemaValidationErrors(error) {
return typeof error === "object" && error !== null && "validation" in error && Array.isArray(error.validation) && error.validation.length > 0 && isZodFastifySchemaValidationError(error.validation[0]);
}
function omit(obj, keys) {
const result = {};
for (const key of Object.keys(obj)) {
if (!keys.includes(key)) {
result[key] = obj[key];
}
}
return result;
}
function createValidationError(error) {
return error.issues.map((issue) => {
return {
[ZodFastifySchemaValidationErrorSymbol]: true,
keyword: issue.code,
instancePath: `/${issue.path.join("/")}`,
schemaPath: `#/${issue.path.join("/")}/${issue.code}`,
message: issue.message,
params: {
...omit(issue, ["path", "code", "message"])
}
};
});
}
exports.InvalidSchemaError = InvalidSchemaError;
exports.ResponseSerializationError = ResponseSerializationError;
exports.createValidationError = createValidationError;
exports.hasZodFastifySchemaValidationErrors = hasZodFastifySchemaValidationErrors;
exports.isResponseSerializationError = isResponseSerializationError;
//# sourceMappingURL=errors.cjs.map
;