|
| 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