From 3247097eb7d6213fddb8b8485a25609c007580d2 Mon Sep 17 00:00:00 2001 From: abhi byte-62 Date: Sat, 3 Oct 2026 17:35:06 +0530 Subject: [PATCH] fix(types): update validator and serializer compiler signatures to accept route definition Update AjvCompile and AjvJTDCompile to accept RouteDefinition matching the runtime implementation in validator-compiler.js and serializer-compiler.js. Add SerializerFactory and SerializerCompiler types. Fixes #188 --- types/index.d.ts | 19 +++++++++++-------- types/index.tst.ts | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index fe8bd27..4de0880 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,13 +1,13 @@ import _ajv, { AnySchema, Options as AjvOptions, ValidateFunction, Plugin } from 'ajv' -import AjvJTD, { JTDOptions } from 'ajv/dist/jtd' +import type { JTDOptions } from 'ajv/dist/jtd' import type { Options, ErrorObject } from 'ajv' import { AnyValidateFunction } from 'ajv/dist/core' type Ajv = _ajv type AjvSerializerGenerator = typeof AjvCompiler -type AjvJTDCompile = AjvJTD['compileSerializer'] -type AjvCompile = (schema: AnySchema, _meta?: boolean) => AnyValidateFunction +type AjvJTDCompile = (routeDefinition: AjvCompiler.RouteDefinition) => (data: unknown) => string +type AjvCompile = (routeDefinition: AjvCompiler.RouteDefinition) => AnyValidateFunction type SharedCompilerOptions = { onCreate?: (ajvInstance: Ajv) => void; @@ -52,10 +52,11 @@ declare namespace AjvCompiler { } export type RouteDefinition = { - method: string, - url: string, - httpPart: HttpParts, - schema?: unknown, + schema: AnySchema, + method?: string, + url?: string, + httpPart?: HttpParts | string, + httpStatus?: string, } export type StandaloneRestoreFunction = (opts: RouteDefinition) => ValidateFunction @@ -74,9 +75,11 @@ declare namespace AjvCompiler { export type StandaloneOptions = StandaloneOptionsReadModeOn | StandaloneOptionsReadModeOff - export type ValidatorFactory = BuildCompilerFromPool | BuildSerializerFromPool + export type ValidatorFactory = BuildCompilerFromPool + export type SerializerFactory = BuildSerializerFromPool export type ValidatorCompiler = ReturnType + export type SerializerCompiler = ReturnType export { StandaloneValidator } diff --git a/types/index.tst.ts b/types/index.tst.ts index 9f4ef75..31c0424 100644 --- a/types/index.tst.ts +++ b/types/index.tst.ts @@ -4,12 +4,14 @@ import { expect } from 'tstyche' import AjvCompiler, { AjvReference, ValidatorFactory, + SerializerFactory, StandaloneValidator, RouteDefinition, ErrorObject, BuildCompilerFromPool, BuildSerializerFromPool, - ValidatorCompiler + ValidatorCompiler, + SerializerCompiler } from '..' import type Ajv from 'ajv' @@ -342,3 +344,33 @@ expect(AjvReference).type.toBe() } ) } + +// ValidatorCompiler and SerializerCompiler types +{ + const validatorFactory = AjvCompiler() + expect(validatorFactory).type.toBeAssignableTo() + const validatorCompiler = validatorFactory({}, {}) + expect(validatorCompiler).type.toBeAssignableTo() + + const routeDef: RouteDefinition = { + schema: { type: 'string' }, + method: 'GET', + url: '/test', + httpPart: AjvCompiler.HttpParts.Body, + httpStatus: '200' + } + const validate = validatorCompiler(routeDef) + expect(validate).type.toBe() + + const minimalRouteDef: RouteDefinition = { + schema: { type: 'number' } + } + expect(validatorCompiler(minimalRouteDef)).type.toBe() + + const serializerFactory = AjvCompiler({ jtdSerializer: true }) + expect(serializerFactory).type.toBeAssignableTo() + const serializerCompiler = serializerFactory({}, {}) + expect(serializerCompiler).type.toBeAssignableTo() + const serialize = serializerCompiler(routeDef) + expect(serialize).type.toBe<(data: unknown) => string>() +}