Skip to content

Commit

Permalink
Fix typescript integration bug with ajv-compiler (#4555)
Browse files Browse the repository at this point in the history
* add test

* use ValidatorFactory and SerializerFactory
  • Loading branch information
Uzlopak committed Feb 11, 2023
1 parent 9dd9c6a commit c6a40eb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
8 changes: 4 additions & 4 deletions fastify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import * as http2 from 'http2'
import * as https from 'https'
import { Socket } from 'net'

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

Expand Down Expand Up @@ -134,8 +134,8 @@ declare namespace fastify {
getSchemas(): Record<string, unknown>;
};
compilersFactory?: {
buildValidator?: ValidatorCompiler;
buildSerializer?: SerializerCompiler;
buildValidator?: ValidatorFactory;
buildSerializer?: SerializerFactory;
};
};
return503OnClosing?: boolean,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
"yup": "^0.32.11"
},
"dependencies": {
"@fastify/ajv-compiler": "^3.3.1",
"@fastify/ajv-compiler": "^3.5.0",
"@fastify/error": "^3.0.0",
"@fastify/fast-json-stringify-compiler": "^4.1.0",
"abstract-logging": "^2.0.1",
Expand Down
42 changes: 37 additions & 5 deletions test/types/schema.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { expectAssignable, expectError } from 'tsd'
import fastify, { FastifyInstance, FastifyRequest, FastifySchema } from '../../fastify'
import { RouteGenericInterface } from '../../types/route'
import { ContextConfigDefault } from '../../types/utils'
import { FastifyReply } from '../../types/reply'
import { expectAssignable } from 'tsd'
import fastify, { FastifyInstance, FastifySchema } from '../../fastify'
import Ajv from 'ajv'
import { StandaloneValidator } from '@fastify/ajv-compiler'
import { StandaloneSerializer } from '@fastify/fast-json-stringify-compiler'

const server = fastify()

Expand Down Expand Up @@ -63,3 +62,36 @@ expectAssignable<FastifyInstance>(server.setValidatorCompiler<FastifySchema & {
expectAssignable<FastifyInstance>(server.setSerializerCompiler<FastifySchema & { validate: string }>(
() => data => JSON.stringify(data)
))

// https://github.com/fastify/ajv-compiler/issues/95
{
const factory = StandaloneValidator({
readMode: false,
storeFunction (routeOpts, schemaValidationCode) { }
})

const app = fastify({
jsonShorthand: false,
schemaController: {
compilersFactory: {
buildValidator: factory
}
}
})
}

{
const factory = StandaloneSerializer({
readMode: false,
storeFunction (routeOpts, schemaValidationCode) { }
})

const app = fastify({
jsonShorthand: false,
schemaController: {
compilersFactory: {
buildSerializer: factory
}
}
})
}
9 changes: 5 additions & 4 deletions types/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ValidatorCompiler } from '@fastify/ajv-compiler'
import { FastifyInstance, FastifyServerOptions } from '../fastify'
import { ValidatorFactory } from '@fastify/ajv-compiler'
import { SerializerFactory } from '@fastify/fast-json-stringify-compiler'
import { FastifyInstance } from '../fastify'
/**
* Schemas in Fastify follow the JSON-Schema standard. For this reason
* we have opted to not ship strict schema based types. Instead we provide
Expand Down Expand Up @@ -50,8 +51,8 @@ export interface FastifySchemaControllerOptions{
getSchemas(): Record<string, unknown>;
};
compilersFactory?: {
buildValidator?: ValidatorCompiler;
buildSerializer?: (externalSchemas: unknown, serializerOptsServerOption: FastifyServerOptions['serializerOpts']) => FastifySerializerCompiler<unknown>;
buildValidator?: ValidatorFactory;
buildSerializer?: SerializerFactory;
};
}

Expand Down

0 comments on commit c6a40eb

Please sign in to comment.