diff --git a/types/index.d.ts b/types/index.d.ts index 8b27386..fe8bd27 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -13,9 +13,19 @@ type SharedCompilerOptions = { onCreate?: (ajvInstance: Ajv) => void; plugins?: (Plugin | [Plugin, unknown])[]; } +type JdtCompilerOptions = SharedCompilerOptions & { + mode: 'JTD'; + customOptions?: JTDOptions +} +type AjvCompilerOptions = SharedCompilerOptions & { + mode?: never; + customOptions?: AjvOptions +} -type BuildAjvJtdCompilerFromPool = (externalSchemas: { [key: string]: AnySchema | AnySchema[] }, options?: SharedCompilerOptions & { mode: 'JTD'; customOptions?: JTDOptions }) => AjvCompile -type BuildAjvCompilerFromPool = (externalSchemas: { [key: string]: AnySchema | AnySchema[] }, options?: SharedCompilerOptions & { mode?: never; customOptions?: AjvOptions }) => AjvCompile +type BuildAjvOrJdtCompilerFromPool = ( + externalSchemas: { [key: string]: AnySchema | AnySchema[] }, + options?: JdtCompilerOptions | AjvCompilerOptions +) => AjvCompile type BuildJtdSerializerFromPool = (externalSchemas: any, serializerOpts?: { mode?: never; } & JTDOptions) => AjvJTDCompile @@ -30,7 +40,7 @@ declare namespace AjvCompiler { export type BuildSerializerFromPool = BuildJtdSerializerFromPool - export type BuildCompilerFromPool = BuildAjvCompilerFromPool & BuildAjvJtdCompilerFromPool + export type BuildCompilerFromPool = BuildAjvOrJdtCompilerFromPool export const AjvReference: Symbol diff --git a/types/index.test-d.ts b/types/index.test-d.ts index f29e3ad..5d9e5dc 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -2,6 +2,7 @@ import { AnySchemaObject, ValidateFunction } from 'ajv' import { AnyValidateFunction } from 'ajv/dist/core' import { expectAssignable, expectType } from 'tsd' import AjvCompiler, { AjvReference, ValidatorFactory, StandaloneValidator, RouteDefinition, ErrorObject, BuildCompilerFromPool, BuildSerializerFromPool, ValidatorCompiler } from '..' +import type Ajv from 'ajv' { const compiler = AjvCompiler({}) @@ -228,30 +229,74 @@ expectType(AjvReference) // Plugins { const factory = AjvCompiler() + const compilerFactoryParams = { + customOptions: {}, + plugins: [ + (ajv: Ajv) => { + expectType(ajv) + return ajv + }, + (ajv: Ajv, options: unknown) => { + expectType(ajv) + expectType(options) + return ajv + } + ] + } + expectAssignable>([{}, compilerFactoryParams]) + const compiler = factory({}, { + customOptions: {}, plugins: [ (ajv) => { - expectType(ajv) + expectType(ajv) return ajv }, (ajv, options) => { - expectType(ajv) + expectType(ajv) expectType(options) return ajv - }, - [ - (ajv) => { - expectType(ajv) - return ajv - }, ['keyword1', 'keyword2'] - ], - [ - (ajv) => { - expectType(ajv) - return ajv - }, [{ key: 'value' }] - ], + } ] }) expectAssignable(compiler) } +// Compiler factory should allow both signatures (mode: JTD and mode omitted) +{ + expectAssignable>([{}, {}]) + + const ajvPlugin = (ajv: Ajv): Ajv => { + expectType(ajv) + return ajv + } + expectAssignable>([{}, { plugins: [ajvPlugin] }]) + + expectAssignable>([{}, { + mode: 'JTD', + customOptions: { + removeAdditional: 'all' + }, + plugins: [ajvPlugin] + }]) + + expectAssignable>([{}, { + mode: 'JTD', + customOptions: { + removeAdditional: 'all' + }, + plugins: [[ajvPlugin, ['string1', 'string2']]] + }]) + + expectAssignable>([{}, { + plugins: [ + ajvPlugin, + (ajv: Ajv, options: unknown): Ajv => { + expectType(ajv) + expectType(options) + return ajv + }, + [ajvPlugin, ['keyword1', 'keyword2']], + [ajvPlugin, [{ key: 'value' }]], + ] + }]) +}