Skip to content

Commit

Permalink
use ValidatorFactory and SerializerFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Feb 8, 2023
1 parent 0cbe0fd commit 4847d54
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 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 @@ -127,8 +127,8 @@ declare namespace fastify {
getSchemas(): Record<string, unknown>;
};
compilersFactory?: {
buildValidator?: ValidatorCompiler;
buildSerializer?: SerializerCompiler;
buildValidator?: ValidatorFactory;
buildSerializer?: SerializerFactory;
};
};
return503OnClosing?: boolean,
Expand Down
23 changes: 20 additions & 3 deletions test/types/schema.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 @@ -66,15 +67,31 @@ expectAssignable<FastifyInstance>(server.setSerializerCompiler<FastifySchema & {
{
const factory = StandaloneValidator({
readMode: false,
storeFunction(routeOpts, schemaValidationCode) {}
storeFunction (routeOpts, schemaValidationCode) { }
})

const app = fastify({
jsonShorthand: false,
schemaController: {
compilersFactory: {
buildValidator: factory
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,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;
};
}

0 comments on commit 4847d54

Please sign in to comment.