Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typescript integration bug with ajv-compiler #4555

Merged
merged 2 commits into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -127,8 +127,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'
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
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,7 +51,7 @@ export interface FastifySchemaControllerOptions{
getSchemas(): Record<string, unknown>;
};
compilersFactory?: {
buildValidator?: ValidatorCompiler;
buildSerializer?: (externalSchemas: unknown, serializerOptsServerOption: FastifyServerOptions['serializerOpts']) => FastifySerializerCompiler<unknown>;
buildValidator?: ValidatorFactory;
buildSerializer?: SerializerFactory;
};
}