Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fastify-type-provider-zod

Package Overview
Dependencies
Maintainers
0
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-type-provider-zod - npm Package Compare versions

Comparing version 3.0.0 to 4.0.0

dist/ResponseValidationError.d.ts

2

dist/index.d.ts
export { type ZodTypeProvider, type FastifyPluginAsyncZod, type FastifyPluginCallbackZod, type ZodSerializerCompilerOptions, jsonSchemaTransform, createJsonSchemaTransform, createJsonSchemaTransformObject, serializerCompiler, validatorCompiler, createSerializerCompiler, } from './src/core';
export { ResponseSerializationError, InvalidSchemaError } from './src/errors';
export { type ZodFastifySchemaValidationError, ResponseSerializationError, InvalidSchemaError, hasZodFastifySchemaValidationErrors, isResponseSerializationError, } from './src/errors';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InvalidSchemaError = exports.ResponseSerializationError = exports.createSerializerCompiler = exports.validatorCompiler = exports.serializerCompiler = exports.createJsonSchemaTransformObject = exports.createJsonSchemaTransform = exports.jsonSchemaTransform = void 0;
exports.isResponseSerializationError = exports.hasZodFastifySchemaValidationErrors = exports.InvalidSchemaError = exports.ResponseSerializationError = exports.createSerializerCompiler = exports.validatorCompiler = exports.serializerCompiler = exports.createJsonSchemaTransformObject = exports.createJsonSchemaTransform = exports.jsonSchemaTransform = void 0;
var core_1 = require("./src/core");

@@ -14,1 +14,3 @@ Object.defineProperty(exports, "jsonSchemaTransform", { enumerable: true, get: function () { return core_1.jsonSchemaTransform; } });

Object.defineProperty(exports, "InvalidSchemaError", { enumerable: true, get: function () { return errors_1.InvalidSchemaError; } });
Object.defineProperty(exports, "hasZodFastifySchemaValidationErrors", { enumerable: true, get: function () { return errors_1.hasZodFastifySchemaValidationErrors; } });
Object.defineProperty(exports, "isResponseSerializationError", { enumerable: true, get: function () { return errors_1.isResponseSerializationError; } });

@@ -68,6 +68,6 @@ "use strict";

exports.createJsonSchemaTransformObject = createJsonSchemaTransformObject;
const validatorCompiler = ({ schema, method, url }) => (data) => {
const validatorCompiler = ({ schema }) => (data) => {
const result = schema.safeParse(data);
if (result.error) {
return { error: (0, errors_1.createValidationError)(result.error, method, url) };
return { error: (0, errors_1.createValidationError)(result.error) };
}

@@ -90,3 +90,3 @@ return { value: result.data };

if (result.error) {
throw new errors_1.ResponseSerializationError(result.error, method, url);
throw new errors_1.ResponseSerializationError(method, url, { cause: result.error });
}

@@ -93,0 +93,0 @@ return JSON.stringify(result.data, options?.replacer);

@@ -1,3 +0,3 @@

import type { FastifySchemaValidationError } from 'fastify/types/schema';
import type { ZodError } from 'zod';
import type { FastifyError } from 'fastify';
import type { ZodError, ZodIssue, ZodIssueCode } from 'zod';
declare const ResponseSerializationError_base: import("@fastify/error").FastifyErrorConstructor<{

@@ -9,11 +9,28 @@ code: string;

export declare class ResponseSerializationError extends ResponseSerializationError_base {
cause: ZodError;
method: string;
url: string;
constructor(cause: ZodError, method: string, url: string);
cause: ZodError;
constructor(method: string, url: string, options: {
cause: ZodError;
});
}
export declare function isResponseSerializationError(value: unknown): value is ResponseSerializationError;
export declare const InvalidSchemaError: import("@fastify/error").FastifyErrorConstructor<{
code: string;
}, [string]>;
export declare const createValidationError: (error: ZodError, method: string, url: string) => FastifySchemaValidationError[];
export type ZodFastifySchemaValidationError = {
name: 'ZodFastifySchemaValidationError';
keyword: ZodIssueCode;
instancePath: string;
schemaPath: string;
params: {
issue: ZodIssue;
zodError: ZodError;
};
message: string;
};
export declare const hasZodFastifySchemaValidationErrors: (error: unknown) => error is FastifyError & {
validation: ZodFastifySchemaValidationError[];
};
export declare const createValidationError: (error: ZodError) => ZodFastifySchemaValidationError[];
export {};

@@ -6,8 +6,8 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.createValidationError = exports.InvalidSchemaError = exports.ResponseSerializationError = void 0;
exports.createValidationError = exports.hasZodFastifySchemaValidationErrors = exports.InvalidSchemaError = exports.ResponseSerializationError = void 0;
exports.isResponseSerializationError = isResponseSerializationError;
const error_1 = __importDefault(require("@fastify/error"));
class ResponseSerializationError extends (0, error_1.default)('FST_ERR_RESPONSE_SERIALIZATION', "Response doesn't match the schema", 500) {
constructor(cause, method, url) {
super({ cause });
this.cause = cause;
constructor(method, url, options) {
super({ cause: options.cause });
this.method = method;

@@ -18,4 +18,19 @@ this.url = url;

exports.ResponseSerializationError = ResponseSerializationError;
function isResponseSerializationError(value) {
return 'method' in value;
}
exports.InvalidSchemaError = (0, error_1.default)('FST_ERR_INVALID_SCHEMA', 'Invalid schema passed: %s', 500);
const createValidationError = (error, method, url) => error.errors.map((issue) => ({
const isZodFastifySchemaValidationError = (error) => typeof error === 'object' &&
error !== null &&
'name' in error &&
error.name === 'ZodFastifySchemaValidationError';
const hasZodFastifySchemaValidationErrors = (error) => typeof error === 'object' &&
error !== null &&
'validation' in error &&
Array.isArray(error.validation) &&
error.validation.length > 0 &&
isZodFastifySchemaValidationError(error.validation[0]);
exports.hasZodFastifySchemaValidationErrors = hasZodFastifySchemaValidationErrors;
const createValidationError = (error) => error.errors.map((issue) => ({
name: 'ZodFastifySchemaValidationError',
keyword: issue.code,

@@ -27,7 +42,5 @@ instancePath: `/${issue.path.join('/')}`,

zodError: error,
method,
url,
},
message: error.message,
message: issue.message,
}));
exports.createValidationError = createValidationError;

@@ -7,3 +7,5 @@ "use strict";

const map = new Map();
Object.entries(schemas).forEach(([key, value]) => map.set(JSON.stringify(value), key));
for (const [key, value] of Object.entries(schemas)) {
map.set(JSON.stringify(value), key);
}
return map;

@@ -10,0 +12,0 @@ };

{
"name": "fastify-type-provider-zod",
"version": "3.0.0",
"version": "4.0.0",
"description": "Zod Type Provider for Fastify@5",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"README.md",
"LICENSE",
"dist"
],
"files": ["README.md", "LICENSE", "dist"],
"scripts": {

@@ -16,4 +12,4 @@ "build": "tsc",

"test:coverage": "vitest --coverage",
"lint": "eslint .",
"lint:fix": "eslint --fix . && prettier --write .",
"lint": "biome check . && tsc --project tsconfig.lint.json --noEmit",
"lint:fix": "biome check --write .",
"typescript": "tsd",

@@ -29,8 +25,3 @@ "prepublishOnly": "npm run build"

},
"keywords": [
"fastify",
"zod",
"type",
"provider"
],
"keywords": ["fastify", "zod", "type", "provider"],
"author": "turkerd",

@@ -46,10 +37,8 @@ "license": "MIT",

"devDependencies": {
"@fastify/swagger": "^9.0.0",
"@biomejs/biome": "^1.9.2",
"@fastify/swagger": "^9.1.0",
"@fastify/swagger-ui": "^5.0.1",
"@types/node": "^20.12.12",
"@typescript-eslint/eslint-plugin": "^7.9.0",
"@typescript-eslint/parser": "^7.9.0",
"@kibertoad/biome-config": "^1.2.1",
"@types/node": "^20.16.10",
"@vitest/coverage-v8": "^2.1.1",
"eslint": "^8.57.0",
"eslint-plugin-import": "^2.29.1",
"fastify": "^5.0.0",

@@ -59,5 +48,4 @@ "fastify-plugin": "^5.0.1",

"openapi-types": "^12.1.3",
"prettier": "^3.2.5",
"tsd": "^0.31.0",
"typescript": "^5.4.5",
"tsd": "^0.31.2",
"typescript": "^5.6.2",
"vitest": "^2.1.1",

@@ -64,0 +52,0 @@ "zod": "^3.23.8"

@@ -145,2 +145,40 @@ # Fastify Type Provider Zod

## Customizing error responses
You can add custom handling of request and response validation errors to your fastify error handler like this:
```ts
import { hasZodFastifySchemaValidationErrors } from 'fastify-type-provider-zod'
fastifyApp.setErrorHandler((err, req, reply) => {
if (hasZodFastifySchemaValidationErrors(err)) {
return reply.code(400).send({
error: 'Response Validation Error',
message: "Request doesn't match the schema",
statusCode: 400,
details: {
issues: err.validation,
method: req.method,
url: req.url,
},
})
}
if (isResponseSerializationError(err)) {
return reply.code(500).send({
error: 'Internal Server Error',
message: "Response doesn't match the schema",
statusCode: 500,
details: {
issues: err.cause.issues,
method: err.method,
url: err.url,
},
})
}
// the rest of the error handler
})
```
## How to create refs to the schemas?

@@ -147,0 +185,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc