Skip to content

Commit c6a40eb

Browse files
authored
Fix typescript integration bug with ajv-compiler (fastify#4555)
* add test * use ValidatorFactory and SerializerFactory
1 parent 9dd9c6a commit c6a40eb

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

fastify.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import * as http2 from 'http2'
33
import * as https from 'https'
44
import { Socket } from 'net'
55

6-
import { Options as AjvOptions, ValidatorCompiler } from '@fastify/ajv-compiler'
6+
import { Options as AjvOptions, ValidatorFactory } from '@fastify/ajv-compiler'
77
import { FastifyError } from '@fastify/error'
8-
import { Options as FJSOptions, SerializerCompiler } from '@fastify/fast-json-stringify-compiler'
8+
import { Options as FJSOptions, SerializerFactory } from '@fastify/fast-json-stringify-compiler'
99
import { ConstraintStrategy, HTTPVersion } from 'find-my-way'
1010
import { Chain as LightMyRequestChain, InjectOptions, Response as LightMyRequestResponse, CallbackFunc as LightMyRequestCallback } from 'light-my-request'
1111

@@ -134,8 +134,8 @@ declare namespace fastify {
134134
getSchemas(): Record<string, unknown>;
135135
};
136136
compilersFactory?: {
137-
buildValidator?: ValidatorCompiler;
138-
buildSerializer?: SerializerCompiler;
137+
buildValidator?: ValidatorFactory;
138+
buildSerializer?: SerializerFactory;
139139
};
140140
};
141141
return503OnClosing?: boolean,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
"yup": "^0.32.11"
172172
},
173173
"dependencies": {
174-
"@fastify/ajv-compiler": "^3.3.1",
174+
"@fastify/ajv-compiler": "^3.5.0",
175175
"@fastify/error": "^3.0.0",
176176
"@fastify/fast-json-stringify-compiler": "^4.1.0",
177177
"abstract-logging": "^2.0.1",

test/types/schema.test-d.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { expectAssignable, expectError } from 'tsd'
2-
import fastify, { FastifyInstance, FastifyRequest, FastifySchema } from '../../fastify'
3-
import { RouteGenericInterface } from '../../types/route'
4-
import { ContextConfigDefault } from '../../types/utils'
5-
import { FastifyReply } from '../../types/reply'
1+
import { expectAssignable } from 'tsd'
2+
import fastify, { FastifyInstance, FastifySchema } from '../../fastify'
63
import Ajv from 'ajv'
4+
import { StandaloneValidator } from '@fastify/ajv-compiler'
5+
import { StandaloneSerializer } from '@fastify/fast-json-stringify-compiler'
76

87
const server = fastify()
98

@@ -63,3 +62,36 @@ expectAssignable<FastifyInstance>(server.setValidatorCompiler<FastifySchema & {
6362
expectAssignable<FastifyInstance>(server.setSerializerCompiler<FastifySchema & { validate: string }>(
6463
() => data => JSON.stringify(data)
6564
))
65+
66+
// https://github.com/fastify/ajv-compiler/issues/95
67+
{
68+
const factory = StandaloneValidator({
69+
readMode: false,
70+
storeFunction (routeOpts, schemaValidationCode) { }
71+
})
72+
73+
const app = fastify({
74+
jsonShorthand: false,
75+
schemaController: {
76+
compilersFactory: {
77+
buildValidator: factory
78+
}
79+
}
80+
})
81+
}
82+
83+
{
84+
const factory = StandaloneSerializer({
85+
readMode: false,
86+
storeFunction (routeOpts, schemaValidationCode) { }
87+
})
88+
89+
const app = fastify({
90+
jsonShorthand: false,
91+
schemaController: {
92+
compilersFactory: {
93+
buildSerializer: factory
94+
}
95+
}
96+
})
97+
}

types/schema.d.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { ValidatorCompiler } from '@fastify/ajv-compiler'
2-
import { FastifyInstance, FastifyServerOptions } from '../fastify'
1+
import { ValidatorFactory } from '@fastify/ajv-compiler'
2+
import { SerializerFactory } from '@fastify/fast-json-stringify-compiler'
3+
import { FastifyInstance } from '../fastify'
34
/**
45
* Schemas in Fastify follow the JSON-Schema standard. For this reason
56
* we have opted to not ship strict schema based types. Instead we provide
@@ -50,8 +51,8 @@ export interface FastifySchemaControllerOptions{
5051
getSchemas(): Record<string, unknown>;
5152
};
5253
compilersFactory?: {
53-
buildValidator?: ValidatorCompiler;
54-
buildSerializer?: (externalSchemas: unknown, serializerOptsServerOption: FastifyServerOptions['serializerOpts']) => FastifySerializerCompiler<unknown>;
54+
buildValidator?: ValidatorFactory;
55+
buildSerializer?: SerializerFactory;
5556
};
5657
}
5758

0 commit comments

Comments
 (0)