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 ".."; { const compiler = AjvCompiler({}); expectType(compiler); } { const compiler = AjvCompiler(); expectType(compiler); } { const compiler = AjvCompiler({ jtdSerializer: false}); expectType(compiler); } { const factory = AjvCompiler({ jtdSerializer: false }); expectType(factory); factory({}, { onCreate(ajv) { expectType(ajv) } }); } { const compiler = AjvCompiler({ jtdSerializer: true}); expectType(compiler); } const reader = StandaloneValidator({ readMode: true, restoreFunction: (route: RouteDefinition) => { expectAssignable(route) return {} as ValidateFunction }, }); expectAssignable(reader); const writer = StandaloneValidator({ readMode: false, storeFunction: (route: RouteDefinition, code: string) => { expectAssignable(route) expectAssignable(code) }, }); expectAssignable(writer); expectType(({} as ErrorObject).data) expectType(({} as ErrorObject).instancePath) expectType(({} as ErrorObject).keyword) expectType(({} as ErrorObject).message) expectType>(({} as ErrorObject).params) expectType(({} as ErrorObject).parentSchema) expectType(({} as ErrorObject).propertyName) expectType(({} as ErrorObject).schema) expectType(({} as ErrorObject).schemaPath) expectType(AjvReference) { const jtdSchema = { discriminator: 'version', mapping: { 1: { properties: { foo: { type: 'uint8' } } }, 2: { properties: { foo: { type: 'string' } } } } } const externalSchemas1 = { foo: { definitions: { coordinates: { properties: { lat: { type: 'float32' }, lng: { type: 'float32' } } } } } } const factory = AjvCompiler({ jtdSerializer: true }) expectType(factory) const compiler = factory(externalSchemas1, {}) expectAssignable(compiler) const serializeFunc = compiler({ schema: jtdSchema }) expectType<(data: unknown) => string>(serializeFunc) expectType(serializeFunc({ version: '1', foo: 42 })) } // JTD { const factory = AjvCompiler() expectType(factory) const jtdSchema = { discriminator: 'version', mapping: { 1: { properties: { foo: { type: 'uint8' } } }, 2: { properties: { foo: { type: 'string' } } } } } const compiler = factory({}, { customOptions: {}, mode: 'JTD' }) expectAssignable(compiler) const validatorFunc = compiler({ schema: jtdSchema }) expectAssignable(validatorFunc) expectType>(validatorFunc({ version: '2', foo: [] })) } // generate standalone code { const base = { $id: 'urn:schema:base', definitions: { hello: { type: 'string' } }, type: 'object', properties: { hello: { $ref: '#/definitions/hello' } } } const refSchema = { $id: 'urn:schema:ref', type: 'object', properties: { hello: { $ref: 'urn:schema:base#/definitions/hello' } } } const endpointSchema = { schema: { $id: 'urn:schema:endpoint', $ref: 'urn:schema:ref' } } const schemaMap = { [base.$id]: base, [refSchema.$id]: refSchema } const factory = StandaloneValidator({ readMode: false, storeFunction(routeOpts, schemaValidationCode) { expectType(routeOpts) expectType(schemaValidationCode) } }) expectAssignable(factory) const compiler = factory(schemaMap) expectAssignable(compiler) expectAssignable(compiler(endpointSchema)) } { const base = { $id: 'urn:schema:base', definitions: { hello: { type: 'string' } }, type: 'object', properties: { hello: { $ref: '#/definitions/hello' } } } const refSchema = { $id: 'urn:schema:ref', type: 'object', properties: { hello: { $ref: 'urn:schema:base#/definitions/hello' } } } const endpointSchema = { schema: { $id: 'urn:schema:endpoint', $ref: 'urn:schema:ref' } } const schemaMap = { [base.$id]: base, [refSchema.$id]: refSchema } const factory = StandaloneValidator({ readMode: true, restoreFunction(routeOpts) { expectType(routeOpts) return {} as ValidateFunction } }) expectAssignable(factory) const compiler = factory(schemaMap) expectAssignable(compiler) expectType>(compiler(endpointSchema)) }