UNPKG

1.94 kBTypeScriptView Raw
1import { ValidatorFactory } from '@fastify/ajv-compiler'
2import { SerializerFactory } from '@fastify/fast-json-stringify-compiler'
3import { FastifyInstance } from '../fastify'
4/**
5 * Schemas in Fastify follow the JSON-Schema standard. For this reason
6 * we have opted to not ship strict schema based types. Instead we provide
7 * an example in our documentation on how to solve this problem. Check it
8 * out here: https://github.com/fastify/fastify/blob/main/docs/Reference/TypeScript.md#json-schema
9 */
10export interface FastifySchema {
11 body?: unknown;
12 querystring?: unknown;
13 params?: unknown;
14 headers?: unknown;
15 response?: unknown;
16}
17
18export interface FastifyRouteSchemaDef<T> {
19 schema: T;
20 method: string;
21 url: string;
22 httpPart?: string;
23 httpStatus?: string;
24 contentType?: string;
25}
26
27export interface FastifySchemaValidationError {
28 keyword: string;
29 instancePath: string;
30 schemaPath: string;
31 params: Record<string, unknown>;
32 message?: string;
33}
34
35export interface FastifyValidationResult {
36 (data: any): boolean | PromiseLike<any> | { error?: Error, value?: any }
37 errors?: FastifySchemaValidationError[] | null;
38}
39
40/**
41 * Compiler for FastifySchema Type
42 */
43export type FastifySchemaCompiler<T> = (routeSchema: FastifyRouteSchemaDef<T>) => FastifyValidationResult
44
45export type FastifySerializerCompiler<T> = (routeSchema: FastifyRouteSchemaDef<T>) => (data: any) => string
46
47export interface FastifySchemaControllerOptions{
48 bucket?: (parentSchemas?: unknown) => {
49 add(schema: unknown): FastifyInstance;
50 getSchema(schemaId: string): unknown;
51 getSchemas(): Record<string, unknown>;
52 };
53 compilersFactory?: {
54 buildValidator?: ValidatorFactory;
55 buildSerializer?: SerializerFactory;
56 };
57}
58
59export type SchemaErrorDataVar = 'body' | 'headers' | 'params' | 'querystring'
60
61export type SchemaErrorFormatter = (errors: FastifySchemaValidationError[], dataVar: SchemaErrorDataVar) => Error