Skip to content

Commit 14b7e46

Browse files
UzlopakEomm
andauthored
nodenext compatibility (#90)
* move types to types folder * nodenext compatibility * initial * EOD * Apply suggestions from code review Co-authored-by: Manuel Spigolon <[email protected]> Co-authored-by: Manuel Spigolon <[email protected]>
1 parent da04ae5 commit 14b7e46

File tree

7 files changed

+293
-74
lines changed

7 files changed

+293
-74
lines changed

index.d.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const AjvReference = Symbol.for('fastify.ajv-compiler.reference')
44
const ValidatorCompiler = require('./lib/validator-compiler')
55
const SerializerCompiler = require('./lib/serializer-compiler')
66

7-
function ValidatorSelector (opts) {
7+
function AjvCompiler (opts) {
88
const validatorPool = new Map()
99
const serializerPool = new Map()
1010

@@ -46,6 +46,8 @@ function getPoolKey (externalSchemas, options) {
4646
const ajvConfig = JSON.stringify(options)
4747
return `${externals}${ajvConfig}`
4848
}
49-
module.exports = ValidatorSelector
49+
module.exports = AjvCompiler
50+
module.exports.default = AjvCompiler
51+
module.exports.AjvCompiler = AjvCompiler
5052
module.exports.AjvReference = AjvReference
5153
module.exports.StandaloneValidator = require('./standalone')

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "3.4.0",
44
"description": "Build and manage the AJV instances for the fastify framework",
55
"main": "index.js",
6-
"types": "index.d.ts",
6+
"types": "types/index.d.ts",
77
"directories": {
88
"test": "test"
99
},
@@ -51,8 +51,5 @@
5151
"ajv": "^8.11.0",
5252
"ajv-formats": "^2.1.1",
5353
"fast-uri": "^2.0.0"
54-
},
55-
"tsd": {
56-
"directory": "test/types"
5754
}
5855
}

test/types/index.test.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

types/.gitkeep

Whitespace-only changes.

types/index.d.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { AnySchema, default as _ajv, Options as AjvOptions, ValidateFunction } from "ajv";
2+
import { default as AjvJTD, JTDOptions } from "ajv/dist/jtd";
3+
import type { Options, ErrorObject } from "ajv";
4+
import { AnyValidateFunction } from "ajv/dist/core";
5+
6+
type Ajv = _ajv;
7+
type AjvSerializerGenerator = typeof AjvCompiler
8+
9+
type AjvJTDCompile = AjvJTD['compileSerializer']
10+
type AjvCompile = (schema: AnySchema, _meta?: boolean) => AnyValidateFunction
11+
12+
declare namespace AjvCompiler {
13+
export type { Options, ErrorObject }
14+
export { Ajv };
15+
16+
export type BuildSerializerFromPool = typeof buildSerializerFromPool
17+
18+
export type BuildCompilerFromPool = typeof buildCompilerFromPool
19+
20+
export const AjvReference: Symbol
21+
22+
export enum HttpParts {
23+
Body = "body",
24+
Headers = "headers",
25+
Params = "params",
26+
Query = "querystring",
27+
}
28+
29+
export type RouteDefinition = {
30+
method: string,
31+
url: string,
32+
httpPart: HttpParts,
33+
schema?: unknown,
34+
}
35+
36+
export type StandaloneRestoreFunction = (opts: RouteDefinition) => ValidateFunction
37+
38+
export type StandaloneStoreFunction = (opts: RouteDefinition, schemaValidationCode: string) => void
39+
40+
export type StandaloneOptionsReadModeOn = {
41+
readMode: true;
42+
restoreFunction?: StandaloneRestoreFunction
43+
}
44+
45+
export type StandaloneOptionsReadModeOff = {
46+
readMode?: false | undefined;
47+
storeFunction?: StandaloneStoreFunction;
48+
}
49+
50+
export type StandaloneOptions = StandaloneOptionsReadModeOn | StandaloneOptionsReadModeOff
51+
52+
export type ValidatorFactory = BuildCompilerFromPool | BuildSerializerFromPool
53+
54+
export type ValidatorCompiler = ReturnType<ValidatorFactory>
55+
56+
export { StandaloneValidator }
57+
58+
export const AjvCompiler: AjvSerializerGenerator
59+
export { AjvCompiler as default }
60+
}
61+
62+
declare function buildCompilerFromPool(externalSchemas: { [key: string]: AnySchema | AnySchema[] }, options?: { mode: 'JTD'; customOptions?: JTDOptions }): AjvCompile
63+
declare function buildCompilerFromPool(externalSchemas: { [key: string]: AnySchema | AnySchema[] }, options?: { mode?: never; customOptions?: AjvOptions }): AjvCompile
64+
65+
declare function buildSerializerFromPool(externalSchemas: any, serializerOpts?: { mode?: never; } & JTDOptions): AjvJTDCompile
66+
67+
declare function AjvCompiler(opts: { jtdSerializer: true }): AjvCompiler.BuildSerializerFromPool
68+
declare function AjvCompiler(opts?: { jtdSerializer?: false }): AjvCompiler.BuildCompilerFromPool
69+
70+
declare function StandaloneValidator(options: AjvCompiler.StandaloneOptions): AjvCompiler.BuildCompilerFromPool;
71+
72+
export = AjvCompiler

types/index.test-d.ts

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
import { AnySchemaObject, ValidateFunction } from "ajv";
2+
import { AnyValidateFunction } from "ajv/dist/core";
3+
import { expectAssignable, expectType } from "tsd";
4+
import AjvCompiler, { AjvReference, ValidatorFactory, StandaloneValidator, RouteDefinition, ErrorObject, BuildCompilerFromPool, BuildSerializerFromPool, ValidatorCompiler } from "..";
5+
6+
{
7+
const compiler = AjvCompiler({});
8+
expectType<BuildCompilerFromPool>(compiler);
9+
}
10+
{
11+
const compiler = AjvCompiler();
12+
expectType<BuildCompilerFromPool>(compiler);
13+
}
14+
{
15+
const compiler = AjvCompiler({ jtdSerializer: false});
16+
expectType<BuildCompilerFromPool>(compiler);
17+
}
18+
{
19+
const compiler = AjvCompiler({ jtdSerializer: true});
20+
expectType<BuildSerializerFromPool>(compiler);
21+
}
22+
const reader = StandaloneValidator({
23+
readMode: true,
24+
restoreFunction: (route: RouteDefinition) => {
25+
expectAssignable<RouteDefinition>(route)
26+
return {} as ValidateFunction
27+
},
28+
});
29+
expectAssignable<ValidatorFactory>(reader);
30+
31+
const writer = StandaloneValidator({
32+
readMode: false,
33+
storeFunction: (route: RouteDefinition, code: string) => {
34+
expectAssignable<RouteDefinition>(route)
35+
expectAssignable<string>(code)
36+
},
37+
});
38+
expectAssignable<ValidatorFactory>(writer);
39+
40+
expectType<unknown>(({} as ErrorObject).data)
41+
expectType<string>(({} as ErrorObject).instancePath)
42+
expectType<string>(({} as ErrorObject).keyword)
43+
expectType<string | undefined>(({} as ErrorObject).message)
44+
expectType<Record<string, any>>(({} as ErrorObject).params)
45+
expectType<AnySchemaObject | undefined>(({} as ErrorObject).parentSchema)
46+
expectType<string | undefined>(({} as ErrorObject).propertyName)
47+
expectType<unknown>(({} as ErrorObject).schema)
48+
expectType<string>(({} as ErrorObject).schemaPath)
49+
50+
expectType<Symbol>(AjvReference)
51+
52+
{
53+
const jtdSchema = {
54+
discriminator: 'version',
55+
mapping: {
56+
1: {
57+
properties: {
58+
foo: { type: 'uint8' }
59+
}
60+
},
61+
2: {
62+
properties: {
63+
foo: { type: 'string' }
64+
}
65+
}
66+
}
67+
}
68+
69+
const externalSchemas1 = {
70+
foo: {
71+
definitions: {
72+
coordinates: {
73+
properties: {
74+
lat: { type: 'float32' },
75+
lng: { type: 'float32' }
76+
}
77+
}
78+
}
79+
}
80+
}
81+
82+
const factory = AjvCompiler({ jtdSerializer: true })
83+
expectType<BuildSerializerFromPool>(factory)
84+
const compiler = factory(externalSchemas1, {})
85+
expectAssignable<Function>(compiler)
86+
const serializeFunc = compiler({ schema: jtdSchema })
87+
expectType<(data: unknown) => string>(serializeFunc)
88+
expectType<string>(serializeFunc({ version: '1', foo: 42 }))
89+
}
90+
// JTD
91+
{
92+
93+
const factory = AjvCompiler()
94+
expectType<BuildCompilerFromPool>(factory)
95+
96+
const jtdSchema = {
97+
discriminator: 'version',
98+
mapping: {
99+
1: {
100+
properties: {
101+
foo: { type: 'uint8' }
102+
}
103+
},
104+
2: {
105+
properties: {
106+
foo: { type: 'string' }
107+
}
108+
}
109+
}
110+
}
111+
112+
const compiler = factory({}, {
113+
customOptions: {},
114+
mode: 'JTD'
115+
})
116+
expectAssignable<ValidatorCompiler>(compiler)
117+
const validatorFunc = compiler({ schema: jtdSchema })
118+
expectAssignable<ValidateFunction>(validatorFunc)
119+
120+
expectType<boolean | Promise<any>>(validatorFunc({
121+
version: '2',
122+
foo: []
123+
}))
124+
}
125+
126+
// generate standalone code
127+
{
128+
const base = {
129+
$id: 'urn:schema:base',
130+
definitions: {
131+
hello: { type: 'string' }
132+
},
133+
type: 'object',
134+
properties: {
135+
hello: { $ref: '#/definitions/hello' }
136+
}
137+
}
138+
139+
const refSchema = {
140+
$id: 'urn:schema:ref',
141+
type: 'object',
142+
properties: {
143+
hello: { $ref: 'urn:schema:base#/definitions/hello' }
144+
}
145+
}
146+
147+
const endpointSchema = {
148+
schema: {
149+
$id: 'urn:schema:endpoint',
150+
$ref: 'urn:schema:ref'
151+
}
152+
}
153+
154+
const schemaMap = {
155+
[base.$id]: base,
156+
[refSchema.$id]: refSchema
157+
}
158+
159+
const factory = StandaloneValidator({
160+
readMode: false,
161+
storeFunction(routeOpts, schemaValidationCode) {
162+
expectType<RouteDefinition>(routeOpts)
163+
expectType<string>(schemaValidationCode)
164+
}
165+
})
166+
expectAssignable<ValidatorFactory>(factory)
167+
168+
const compiler = factory(schemaMap)
169+
expectAssignable<ValidatorCompiler>(compiler)
170+
expectAssignable<Function>(compiler(endpointSchema))
171+
}
172+
173+
{
174+
const base = {
175+
$id: 'urn:schema:base',
176+
definitions: {
177+
hello: { type: 'string' }
178+
},
179+
type: 'object',
180+
properties: {
181+
hello: { $ref: '#/definitions/hello' }
182+
}
183+
}
184+
185+
const refSchema = {
186+
$id: 'urn:schema:ref',
187+
type: 'object',
188+
properties: {
189+
hello: { $ref: 'urn:schema:base#/definitions/hello' }
190+
}
191+
}
192+
193+
const endpointSchema = {
194+
schema: {
195+
$id: 'urn:schema:endpoint',
196+
$ref: 'urn:schema:ref'
197+
}
198+
}
199+
200+
const schemaMap = {
201+
[base.$id]: base,
202+
[refSchema.$id]: refSchema
203+
}
204+
const factory = StandaloneValidator({
205+
readMode: true,
206+
restoreFunction(routeOpts) {
207+
expectType<RouteDefinition>(routeOpts)
208+
return {} as ValidateFunction
209+
}
210+
})
211+
expectAssignable<ValidatorFactory>(factory)
212+
213+
const compiler = factory(schemaMap)
214+
expectAssignable<ValidatorCompiler>(compiler)
215+
expectType<AnyValidateFunction<any>>(compiler(endpointSchema))
216+
}

0 commit comments

Comments
 (0)