-
-
Notifications
You must be signed in to change notification settings - Fork 13
nodenext compatibility #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { AnySchema, default as _ajv, Options as AjvOptions, ValidateFunction } from "ajv"; | ||
| import { default as AjvJTD, 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 | ||
|
|
||
| declare namespace AjvCompiler { | ||
| export type { Options, ErrorObject } | ||
| export { Ajv }; | ||
|
|
||
| export type BuildSerializerFromPool = typeof buildSerializerFromPool | ||
|
|
||
| export type BuildCompilerFromPool = typeof buildCompilerFromPool | ||
|
|
||
| export const AjvReference: Symbol | ||
|
|
||
| export enum HttpParts { | ||
| Body = "body", | ||
| Headers = "headers", | ||
| Params = "params", | ||
| Query = "querystring", | ||
| } | ||
|
|
||
| export type RouteDefinition = { | ||
| method: string, | ||
| url: string, | ||
| httpPart: HttpParts, | ||
| schema?: unknown, | ||
| } | ||
|
|
||
| export type StandaloneRestoreFunction = (opts: RouteDefinition) => ValidateFunction | ||
|
|
||
| export type StandaloneStoreFunction = (opts: RouteDefinition, schemaValidationCode: string) => void | ||
|
|
||
| export type StandaloneOptionsReadModeOn = { | ||
| readMode: true; | ||
| restoreFunction?: StandaloneRestoreFunction | ||
| } | ||
|
|
||
| export type StandaloneOptionsReadModeOff = { | ||
| readMode?: false | undefined; | ||
| storeFunction?: StandaloneStoreFunction; | ||
| } | ||
|
|
||
| export type StandaloneOptions = StandaloneOptionsReadModeOn | StandaloneOptionsReadModeOff | ||
|
|
||
| export type ValidatorFactory = BuildCompilerFromPool | BuildSerializerFromPool | ||
|
|
||
| export type ValidatorCompiler = ReturnType<ValidatorFactory> | ||
|
|
||
| export { StandaloneValidator } | ||
|
|
||
| export const AjvCompiler: AjvSerializerGenerator | ||
| export { AjvCompiler as default } | ||
| } | ||
|
|
||
| declare function buildCompilerFromPool(externalSchemas: { [key: string]: AnySchema | AnySchema[] }, options?: { mode: 'JTD'; customOptions?: JTDOptions }): AjvCompile | ||
| declare function buildCompilerFromPool(externalSchemas: { [key: string]: AnySchema | AnySchema[] }, options?: { mode?: never; customOptions?: AjvOptions }): AjvCompile | ||
|
|
||
| declare function buildSerializerFromPool(externalSchemas: any, serializerOpts?: { mode?: never; } & JTDOptions): AjvJTDCompile | ||
|
|
||
| declare function AjvCompiler(opts: { jtdSerializer: true }): AjvCompiler.BuildSerializerFromPool | ||
| declare function AjvCompiler(opts?: { jtdSerializer?: false }): AjvCompiler.BuildCompilerFromPool | ||
|
|
||
| declare function StandaloneValidator(options: AjvCompiler.StandaloneOptions): AjvCompiler.BuildCompilerFromPool; | ||
|
|
||
| export = AjvCompiler |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| 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<BuildCompilerFromPool>(compiler); | ||
| } | ||
| { | ||
| const compiler = AjvCompiler(); | ||
| expectType<BuildCompilerFromPool>(compiler); | ||
| } | ||
| { | ||
| const compiler = AjvCompiler({ jtdSerializer: false}); | ||
| expectType<BuildCompilerFromPool>(compiler); | ||
| } | ||
| { | ||
| const compiler = AjvCompiler({ jtdSerializer: true}); | ||
| expectType<BuildSerializerFromPool>(compiler); | ||
| } | ||
| const reader = StandaloneValidator({ | ||
| readMode: true, | ||
| restoreFunction: (route: RouteDefinition) => { | ||
| expectAssignable<RouteDefinition>(route) | ||
| return {} as ValidateFunction | ||
| }, | ||
| }); | ||
| expectAssignable<ValidatorFactory>(reader); | ||
|
|
||
| const writer = StandaloneValidator({ | ||
| readMode: false, | ||
| storeFunction: (route: RouteDefinition, code: string) => { | ||
| expectAssignable<RouteDefinition>(route) | ||
| expectAssignable<string>(code) | ||
| }, | ||
| }); | ||
| expectAssignable<ValidatorFactory>(writer); | ||
|
|
||
| expectType<unknown>(({} as ErrorObject).data) | ||
| expectType<string>(({} as ErrorObject).instancePath) | ||
| expectType<string>(({} as ErrorObject).keyword) | ||
| expectType<string | undefined>(({} as ErrorObject).message) | ||
| expectType<Record<string, any>>(({} as ErrorObject).params) | ||
| expectType<AnySchemaObject | undefined>(({} as ErrorObject).parentSchema) | ||
| expectType<string | undefined>(({} as ErrorObject).propertyName) | ||
| expectType<unknown>(({} as ErrorObject).schema) | ||
| expectType<string>(({} as ErrorObject).schemaPath) | ||
|
|
||
| expectType<Symbol>(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<BuildSerializerFromPool>(factory) | ||
| const compiler = factory(externalSchemas1, {}) | ||
| expectAssignable<Function>(compiler) | ||
| const serializeFunc = compiler({ schema: jtdSchema }) | ||
| expectType<(data: unknown) => string>(serializeFunc) | ||
| expectType<string>(serializeFunc({ version: '1', foo: 42 })) | ||
| } | ||
| // JTD | ||
| { | ||
|
|
||
| const factory = AjvCompiler() | ||
| expectType<BuildCompilerFromPool>(factory) | ||
|
|
||
| const jtdSchema = { | ||
| discriminator: 'version', | ||
| mapping: { | ||
| 1: { | ||
| properties: { | ||
| foo: { type: 'uint8' } | ||
| } | ||
| }, | ||
| 2: { | ||
| properties: { | ||
| foo: { type: 'string' } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const compiler = factory({}, { | ||
| customOptions: {}, | ||
| mode: 'JTD' | ||
| }) | ||
| expectAssignable<ValidatorCompiler>(compiler) | ||
| const validatorFunc = compiler({ schema: jtdSchema }) | ||
| expectAssignable<ValidateFunction>(validatorFunc) | ||
|
|
||
| expectType<boolean | Promise<any>>(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<RouteDefinition>(routeOpts) | ||
| expectType<string>(schemaValidationCode) | ||
| } | ||
| }) | ||
| expectAssignable<ValidatorFactory>(factory) | ||
|
|
||
| const compiler = factory(schemaMap) | ||
| expectAssignable<ValidatorCompiler>(compiler) | ||
| expectAssignable<Function>(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<RouteDefinition>(routeOpts) | ||
| return {} as ValidateFunction | ||
| } | ||
| }) | ||
| expectAssignable<ValidatorFactory>(factory) | ||
|
|
||
| const compiler = factory(schemaMap) | ||
| expectAssignable<ValidatorCompiler>(compiler) | ||
| expectType<AnyValidateFunction<any>>(compiler(endpointSchema)) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.