| 1 |
/* eslint-disable @typescript-eslint/no-empty-interface,no-void,@typescript-eslint/ban-types */ |
| 2 |
import _Ajv from "../ajv_jtd" |
| 3 |
import type {JTDSchemaType, SomeJTDSchemaType, JTDDataType} from "../../dist/jtd" |
| 4 |
import chai from "../chai" |
| 5 |
const should = chai.should() |
| 6 |
|
| 7 |
/** type is true if T is identically E */ |
| 8 |
type TypeEquality<T, E> = [T] extends [E] ? ([E] extends [T] ? true : false) : false |
| 9 |
|
| 10 |
interface A { |
| 11 |
type: "a" |
| 12 |
a: number |
| 13 |
} |
| 14 |
|
| 15 |
interface B { |
| 16 |
type: "b" |
| 17 |
b?: string |
| 18 |
} |
| 19 |
|
| 20 |
interface C { |
| 21 |
type: "c" |
| 22 |
} |
| 23 |
|
| 24 |
type MyData = A | B |
| 25 |
|
| 26 |
type Missing = A | C |
| 27 |
|
| 28 |
interface LinkedList { |
| 29 |
val: number |
| 30 |
next?: LinkedList |
| 31 |
} |
| 32 |
|
| 33 |
const mySchema: JTDSchemaType<MyData> = { |
| 34 |
discriminator: "type", |
| 35 |
mapping: { |
| 36 |
a: {properties: {a: {type: "float64"}}}, |
| 37 |
b: {optionalProperties: {b: {type: "string"}}}, |
| 38 |
}, |
| 39 |
} |
| 40 |
|
| 41 |
const missingSchema: JTDSchemaType<Missing> = { |
| 42 |
discriminator: "type", |
| 43 |
mapping: { |
| 44 |
a: {properties: {a: {type: "float64"}}}, |
| 45 |
c: {properties: {}}, |
| 46 |
}, |
| 47 |
} |
| 48 |
|
| 49 |
describe("JTDSchemaType", () => { |
| 50 |
it("validation should prove the data type", () => { |
| 51 |
const ajv = new _Ajv() |
| 52 |
const validate = ajv.compile(mySchema) |
| 53 |
const validData: unknown = {type: "a", a: 1} |
| 54 |
if (validate(validData) && validData.type === "a") { |
| •55 |
// @ts-ignore |
| 56 |
validData.a.should.equal(1) |
| 57 |
} |
| 58 |
should.not.exist(validate.errors) |
| 59 |
|
| 60 |
if (ajv.validate(mySchema, validData) && validData.type === "a") { |
| •61 |
// @ts-ignore |
| 62 |
validData.a.should.equal(1) |
| 63 |
} |
| 64 |
should.not.exist(ajv.errors) |
| 65 |
}) |
| 66 |
|
| 67 |
it("parser should return correct data type", () => { |
| 68 |
const ajv = new _Ajv() |
| 69 |
const parse = ajv.compileParser(mySchema) |
| 70 |
const validJson = '{"type": "a", "a": 1}' |
| 71 |
const data = parse(validJson) |
| 72 |
if (data !== undefined && data.type === "a") { |
| •73 |
// @ts-ignore |
| 74 |
data.a.should.equal(1) |
| 75 |
} |
| 76 |
should.not.exist(parse.message) |
| 77 |
}) |
| 78 |
|
| 79 |
it("serializer should only accept correct data type", () => { |
| 80 |
const ajv = new _Ajv() |
| 81 |
const serialize = ajv.compileSerializer(mySchema) |
| 82 |
const validData = {type: "a" as const, a: 1} |
| •83 |
// @ts-ignore |
| 84 |
serialize(validData).should.equal('{"type":"a","a":1}') |
| 85 |
const invalidData = {type: "a" as const, b: "test"} |
| 86 |
// @ts-expect-error |
| 87 |
serialize(invalidData) |
| 88 |
}) |
| 89 |
|
| 90 |
it("validation should prove the data type for missingSchema", () => { |
| 91 |
const ajv = new _Ajv() |
| 92 |
const validate = ajv.compile(missingSchema) |
| 93 |
const validData: unknown = {type: "c"} |
| 94 |
|
| 95 |
if (validate(validData)) { |
| •96 |
// @ts-ignore |
| 97 |
validData.type.should.equal("c") |
| 98 |
} |
| 99 |
should.not.exist(validate.errors) |
| 100 |
|
| 101 |
if (ajv.validate(missingSchema, validData)) { |
| •102 |
// @ts-ignore |
| 103 |
validData.type.should.equal("c") |
| 104 |
} |
| 105 |
should.not.exist(validate.errors) |
| 106 |
}) |
| 107 |
|
| 108 |
it("should typecheck number schemas", () => { |
| 109 |
const numf: JTDSchemaType<number> = {type: "float64"} |
| 110 |
const numi: JTDSchemaType<number> = {type: "int32"} |
| 111 |
// @ts-expect-error |
| 112 |
const numl: JTDSchemaType<number> = {type: "int64"} |
| 113 |
// number literals don't work |
| 114 |
// @ts-expect-error |
| 115 |
const nums: JTDSchemaType<1 | 2 | 3> = {type: "int32"} |
| 116 |
const numNull: JTDSchemaType<number | null> = {type: "int32", nullable: true} |
| 117 |
// @ts-expect-error |
| 118 |
const numNotNull: JTDSchemaType<number | null> = {type: "float32"} |
| 119 |
|
| 120 |
void [numf, numi, numl, nums, numNull, numNotNull] |
| 121 |
}) |
| 122 |
|
| 123 |
it("should typecheck boolean schemas", () => { |
| 124 |
const bool: JTDSchemaType<boolean> = {type: "boolean"} |
| 125 |
// boolean literals don't |
| 126 |
// @ts-expect-error |
| 127 |
const boolTrue: JTDSchemaType<true> = {type: "boolean"} |
| 128 |
const boolNull: JTDSchemaType<boolean | null> = {type: "boolean", nullable: true} |
| 129 |
|
| 130 |
void [bool, boolTrue, boolNull] |
| 131 |
}) |
| 132 |
|
| 133 |
it("should typecheck string schemas", () => { |
| 134 |
const str: JTDSchemaType<string> = {type: "string"} |
| 135 |
const time: JTDSchemaType<string> = {type: "timestamp"} |
| 136 |
const strNull: JTDSchemaType<string | null> = {type: "string", nullable: true} |
| 137 |
|
| 138 |
void [str, time, strNull] |
| 139 |
}) |
| 140 |
|
| 141 |
it("should typecheck dates", () => { |
| 142 |
const time: JTDSchemaType<Date> = {type: "timestamp"} |
| 143 |
const timeNull: JTDSchemaType<Date | null> = {type: "timestamp", nullable: true} |
| 144 |
|
| 145 |
void [time, timeNull] |
| 146 |
}) |
| 147 |
|
| 148 |
it("should typecheck enumeration schemas", () => { |
| 149 |
const enumerate: JTDSchemaType<"a" | "b"> = {enum: ["a", "b"]} |
| 150 |
// don't need to specify everything |
| 151 |
const enumerateMissing: JTDSchemaType<"a" | "b" | "c"> = {enum: ["a", "b"]} |
| 152 |
// must all be string literals |
| 153 |
// @ts-expect-error |
| 154 |
const enumerateNumber: JTDSchemaType<"a" | "b" | 5> = {enum: ["a", "b"]} |
| 155 |
// can't overgeneralize in schema |
| 156 |
// @ts-expect-error |
| 157 |
const enumerateString: JTDSchemaType<"a" | "b"> = {type: "string"} |
| 158 |
const enumerateNull: JTDSchemaType<"a" | "b" | null> = {enum: ["a", "b"], nullable: true} |
| 159 |
|
| 160 |
void [enumerate, enumerateMissing, enumerateNumber, enumerateString, enumerateNull] |
| 161 |
}) |
| 162 |
|
| 163 |
it("should typecheck elements schemas", () => { |
| 164 |
const elements: JTDSchemaType<number[]> = {elements: {type: "float64"}} |
| 165 |
const readonlyElements: JTDSchemaType<readonly number[]> = {elements: {type: "float64"}} |
| 166 |
// tuples don't work |
| 167 |
// @ts-expect-error |
| 168 |
const tupleHomo: JTDSchemaType<[number, number]> = {elements: {type: "float64"}} |
| 169 |
// @ts-expect-error |
| 170 |
const tupleHeteroNum: JTDSchemaType<[number, string]> = { |
| 171 |
elements: {type: "float64"}, |
| 172 |
} |
| 173 |
// @ts-expect-error |
| 174 |
const tupleHeteroString: JTDSchemaType<[number, string]> = { |
| 175 |
elements: {type: "string"}, |
| 176 |
} |
| 177 |
const elemNull: JTDSchemaType<number[] | null> = {elements: {type: "float64"}, nullable: true} |
| 178 |
|
| 179 |
// can typecheck an array of unions |
| 180 |
const unionElem: TypeEquality<JTDSchemaType<(A | B)[]>, never> = false |
| 181 |
// can't typecheck a union of arrays |
| 182 |
const elemUnion: TypeEquality<JTDSchemaType<A[] | B[]>, never> = true |
| 183 |
|
| 184 |
void [ |
| 185 |
elements, |
| 186 |
readonlyElements, |
| 187 |
tupleHomo, |
| 188 |
tupleHeteroNum, |
| 189 |
tupleHeteroString, |
| 190 |
elemNull, |
| 191 |
unionElem, |
| 192 |
elemUnion, |
| 193 |
] |
| 194 |
}) |
| 195 |
|
| 196 |
it("should typecheck values schemas", () => { |
| 197 |
const values: JTDSchemaType<Record<string, number>> = {values: {type: "float64"}} |
| 198 |
const readonlyValues: JTDSchemaType<Readonly<Record<string, number>>> = { |
| 199 |
values: {type: "float64"}, |
| 200 |
} |
| 201 |
// values must be a whole mapping |
| 202 |
// @ts-expect-error |
| 203 |
const valuesDefined: JTDSchemaType<{prop: number}> = {values: {type: "float64"}} |
| 204 |
const valuesNull: JTDSchemaType<Record<string, number> | null> = { |
| 205 |
values: {type: "float64"}, |
| 206 |
nullable: true, |
| 207 |
} |
| 208 |
|
| 209 |
// can typecheck a values of unions |
| 210 |
const unionValues: TypeEquality<JTDSchemaType<Record<string, A | B>>, never> = false |
| 211 |
// can't typecheck a union of values |
| 212 |
const valuesUnion: TypeEquality< |
| 213 |
JTDSchemaType<Record<string, A> | Record<string, B>>, |
| 214 |
never |
| 215 |
> = true |
| 216 |
|
| 217 |
void [values, readonlyValues, valuesDefined, valuesNull, unionValues, valuesUnion] |
| 218 |
}) |
| 219 |
|
| 220 |
it("should typecheck properties schemas", () => { |
| 221 |
const properties: JTDSchemaType<{a: number; b: string}> = { |
| 222 |
properties: {a: {type: "float64"}, b: {type: "string"}}, |
| 223 |
} |
| 224 |
const optionalProperties: JTDSchemaType<{a?: number; b?: string}> = { |
| 225 |
optionalProperties: {a: {type: "float64"}, b: {type: "string"}}, |
| 226 |
additionalProperties: false, |
| 227 |
} |
| 228 |
const mixedProperties: JTDSchemaType<{a: number; b?: string}> = { |
| 229 |
properties: {a: {type: "float64"}}, |
| 230 |
optionalProperties: {b: {type: "string"}}, |
| 231 |
additionalProperties: true, |
| 232 |
} |
| 233 |
const fewerProperties: JTDSchemaType<{a: number; b: string}> = { |
| 234 |
// @ts-expect-error |
| 235 |
properties: {a: {type: "float64"}}, |
| 236 |
} |
| 237 |
const propertiesNull: JTDSchemaType<{a: number; b: string} | null> = { |
| 238 |
properties: {a: {type: "float64"}, b: {type: "string"}}, |
| 239 |
nullable: true, |
| 240 |
} |
| 241 |
|
| 242 |
void [properties, optionalProperties, mixedProperties, fewerProperties, propertiesNull] |
| 243 |
}) |
| 244 |
|
| 245 |
it("should typecheck discriminator schemas", () => { |
| 246 |
const union: JTDSchemaType<A | B> = { |
| 247 |
discriminator: "type", |
| 248 |
mapping: { |
| 249 |
a: {properties: {a: {type: "float64"}}}, |
| 250 |
b: { |
| 251 |
optionalProperties: {b: {type: "string"}}, |
| 252 |
}, |
| 253 |
}, |
| 254 |
} |
| 255 |
// can't mess up, e.g. value type isn't a union |
| 256 |
const unionDuplicate: JTDSchemaType<A | B> = { |
| 257 |
discriminator: "type", |
| 258 |
mapping: { |
| 259 |
a: {properties: {a: {type: "float64"}}}, |
| 260 |
// @ts-expect-error |
| 261 |
b: {properties: {a: {type: "float64"}}}, |
| 262 |
}, |
| 263 |
} |
| 264 |
// must specify everything |
| 265 |
const unionMissing: JTDSchemaType<A | B> = { |
| 266 |
discriminator: "type", |
| 267 |
// @ts-expect-error |
| 268 |
mapping: { |
| 269 |
a: {properties: {a: {type: "float64"}}}, |
| 270 |
}, |
| 271 |
} |
| 272 |
// can use any valid discrinimator |
| 273 |
type Mult = JTDSchemaType<(A & {typ: "alpha"}) | (B & {typ: "beta"})> |
| 274 |
const multOne: Mult = { |
| 275 |
discriminator: "type", |
| 276 |
mapping: { |
| 277 |
a: {properties: {a: {type: "float64"}, typ: {enum: ["alpha"]}}}, |
| 278 |
b: { |
| 279 |
properties: {typ: {enum: ["beta"]}}, |
| 280 |
optionalProperties: {b: {type: "string"}}, |
| 281 |
}, |
| 282 |
}, |
| 283 |
} |
| 284 |
const multTwo: Mult = { |
| 285 |
discriminator: "typ", |
| 286 |
mapping: { |
| 287 |
alpha: {properties: {a: {type: "float64"}, type: {enum: ["a"]}}}, |
| 288 |
beta: { |
| 289 |
properties: {type: {enum: ["b"]}}, |
| 290 |
optionalProperties: {b: {type: "string"}}, |
| 291 |
}, |
| 292 |
}, |
| 293 |
} |
| 294 |
const unionNull: JTDSchemaType<A | B | null> = { |
| 295 |
discriminator: "type", |
| 296 |
mapping: { |
| 297 |
a: {properties: {a: {type: "float64"}}}, |
| 298 |
b: { |
| 299 |
optionalProperties: {b: {type: "string"}}, |
| 300 |
}, |
| 301 |
}, |
| 302 |
nullable: true, |
| 303 |
} |
| 304 |
|
| 305 |
// properties must have common key |
| 306 |
const noCommon: TypeEquality< |
| 307 |
JTDSchemaType<{key1: "a"; a: number} | {key2: "b"; b: string}>, |
| 308 |
never |
| 309 |
> = true |
| 310 |
|
| 311 |
void [union, unionDuplicate, unionMissing, multOne, multTwo, unionNull, noCommon] |
| 312 |
}) |
| 313 |
|
| 314 |
it("should typecheck empty schemas", () => { |
| 315 |
const empty: JTDSchemaType<unknown> = {} |
| 316 |
// unknown can be null |
| 317 |
const emptyUnknown: JTDSchemaType<unknown> = {nullable: true} |
| 318 |
// somewhat unintuitively, it can still have nullable: false even though it can be null |
| 319 |
const falseUnknown: JTDSchemaType<unknown> = {nullable: false} |
| 320 |
// can only use empty for empty and null |
| 321 |
// @ts-expect-error |
| 322 |
const emptyButFull: JTDSchemaType<{a: string}> = {} |
| 323 |
const emptyMeta: JTDSchemaType<unknown> = {metadata: {}} |
| 324 |
|
| 325 |
// constant null representable as nullable empty object |
| 326 |
const emptyNull: TypeEquality<JTDSchemaType<null>, never> = true |
| 327 |
|
| 328 |
void [empty, emptyUnknown, falseUnknown, emptyButFull, emptyMeta, emptyNull] |
| 329 |
}) |
| 330 |
|
| 331 |
it("should typecheck empty records", () => { |
| 332 |
// empty record variants |
| 333 |
const emptyPro: JTDSchemaType<{}> = {properties: {}} |
| 334 |
const emptyOpt: JTDSchemaType<{}> = {optionalProperties: {}} |
| 335 |
const emptyBoth: JTDSchemaType<{}> = {properties: {}, optionalProperties: {}} |
| 336 |
const emptyRecord: JTDSchemaType<Record<string, never>> = {properties: {}} |
| 337 |
const notNullable: JTDSchemaType<{}> = {properties: {}, nullable: false} |
| 338 |
|
| 339 |
// can't be null |
| 340 |
// @ts-expect-error |
| 341 |
const nullable: JTDSchemaType<{}> = {properties: {}, nullable: true} |
| 342 |
|
| 343 |
const emptyNullUnion: JTDSchemaType<null | {}> = {properties: {}, nullable: true} |
| 344 |
const emptyNullRecord: JTDSchemaType<null | Record<string, never>> = { |
| 345 |
properties: {}, |
| 346 |
nullable: true, |
| 347 |
} |
| 348 |
|
| 349 |
void [ |
| 350 |
emptyPro, |
| 351 |
emptyOpt, |
| 352 |
emptyBoth, |
| 353 |
emptyRecord, |
| 354 |
notNullable, |
| 355 |
nullable, |
| 356 |
emptyNullUnion, |
| 357 |
emptyNullRecord, |
| 358 |
] |
| 359 |
}) |
| 360 |
|
| 361 |
it("should typecheck ref schemas", () => { |
| 362 |
const refs: JTDSchemaType<number[], {num: number}> = { |
| 363 |
definitions: { |
| 364 |
num: {type: "float64"}, |
| 365 |
}, |
| 366 |
elements: {ref: "num"}, |
| 367 |
} |
| 368 |
const missingDef: JTDSchemaType<number[], {num: number}> = { |
| 369 |
// @ts-expect-error |
| 370 |
definitions: {}, |
| 371 |
elements: {ref: "num"}, |
| 372 |
} |
| 373 |
const missingType: JTDSchemaType<number[]> = { |
| 374 |
definitions: {}, |
| 375 |
// @ts-expect-error |
| 376 |
elements: {ref: "num"}, |
| 377 |
} |
| 378 |
const nullRefs: JTDSchemaType<number | null, {num: number}> = { |
| 379 |
definitions: { |
| 380 |
num: {type: "float64"}, |
| 381 |
}, |
| 382 |
ref: "num", |
| 383 |
nullable: true, |
| 384 |
} |
| 385 |
const refsNullOne: JTDSchemaType<number | null, {num: number | null}> = { |
| 386 |
definitions: { |
| 387 |
num: {type: "float64", nullable: true}, |
| 388 |
}, |
| 389 |
ref: "num", |
| 390 |
} |
| 391 |
const refsNullTwo: JTDSchemaType<number | null, {num: number | null}> = { |
| 392 |
definitions: { |
| 393 |
num: {type: "float64", nullable: true}, |
| 394 |
}, |
| 395 |
ref: "num", |
| 396 |
nullable: true, |
| 397 |
} |
| 398 |
|
| 399 |
void [refs, missingDef, missingType, nullRefs, refsNullOne, refsNullTwo] |
| 400 |
}) |
| 401 |
|
| 402 |
it("should typecheck metadata schemas", () => { |
| 403 |
const meta: JTDSchemaType<number> = {type: "float32", metadata: {key: "val"}} |
| 404 |
const emptyMeta: JTDSchemaType<unknown> = {metadata: {key: "val"}} |
| 405 |
const unknownMeta: JTDSchemaType<unknown> = {nullable: true, metadata: {key: "val"}} |
| 406 |
|
| 407 |
void [meta, emptyMeta, unknownMeta] |
| 408 |
}) |
| 409 |
}) |
| 410 |
|
| 411 |
describe("JTDDataType", () => { |
| 412 |
it("validation should prove the data type", () => { |
| 413 |
const ajv = new _Ajv() |
| 414 |
const mySchema1 = { |
| 415 |
discriminator: "type", |
| 416 |
mapping: { |
| 417 |
a: {properties: {a: {type: "float64"}}}, |
| 418 |
b: {optionalProperties: {b: {type: "string"}}}, |
| 419 |
}, |
| 420 |
} as const |
| 421 |
|
| 422 |
const validate = ajv.compile(mySchema1) |
| 423 |
const validData: unknown = {type: "a", a: 1} |
| 424 |
if (validate(validData) && validData.type === "a") { |
| •425 |
// @ts-ignore |
| 426 |
validData.a.should.equal(1) |
| 427 |
} |
| 428 |
should.not.exist(validate.errors) |
| 429 |
|
| 430 |
if (ajv.validate(mySchema1, validData) && validData.type === "a") { |
| •431 |
// @ts-ignore |
| 432 |
validData.a.should.equal(1) |
| 433 |
} |
| 434 |
should.not.exist(ajv.errors) |
| 435 |
}) |
| 436 |
|
| 437 |
it("should typecheck number schemas", () => { |
| 438 |
const numSchema = {type: "float64"} as const |
| 439 |
const num: TypeEquality<JTDDataType<typeof numSchema>, number> = true |
| 440 |
|
| 441 |
void [num] |
| 442 |
}) |
| 443 |
|
| 444 |
it("should typecheck boolean schemas", () => { |
| 445 |
const booleanSchema = {type: "boolean"} as const |
| 446 |
const bool: TypeEquality<JTDDataType<typeof booleanSchema>, boolean> = true |
| 447 |
|
| 448 |
void [bool] |
| 449 |
}) |
| 450 |
|
| 451 |
it("should typecheck string schemas", () => { |
| 452 |
const strSchema = {type: "string"} as const |
| 453 |
const str: TypeEquality<JTDDataType<typeof strSchema>, string> = true |
| 454 |
|
| 455 |
void [str] |
| 456 |
}) |
| 457 |
|
| 458 |
it("should typecheck timestamp schemas", () => { |
| 459 |
const timeSchema = {type: "timestamp"} as const |
| 460 |
const time: TypeEquality<JTDDataType<typeof timeSchema>, string | Date> = true |
| 461 |
|
| 462 |
void [time] |
| 463 |
}) |
| 464 |
|
| 465 |
it("should typecheck enum schemas", () => { |
| 466 |
const enumSchema = {enum: ["a", "b"]} as const |
| 467 |
const enumerated: TypeEquality<JTDDataType<typeof enumSchema>, "a" | "b"> = true |
| 468 |
|
| 469 |
// if you forget const on an enum it will error |
| 470 |
const enumStringSchema = {enum: ["a", "b"]} |
| 471 |
const enumString: TypeEquality<JTDDataType<typeof enumStringSchema>, never> = true |
| 472 |
// also if not a string |
| 473 |
const enumNumSchema = {enum: [3]} as const |
| 474 |
const enumNum: TypeEquality<JTDDataType<typeof enumNumSchema>, never> = true |
| 475 |
|
| 476 |
void [enumerated, enumString, enumNum] |
| 477 |
}) |
| 478 |
|
| 479 |
it("should typecheck elements schemas", () => { |
| 480 |
const elementsSchema = {elements: {type: "float64"}} as const |
| 481 |
const elem: TypeEquality<JTDDataType<typeof elementsSchema>, number[]> = true |
| 482 |
|
| 483 |
void [elem] |
| 484 |
}) |
| 485 |
|
| 486 |
it("should typecheck properties schemas", () => { |
| 487 |
const bothPropsSchema = { |
| 488 |
properties: {a: {type: "float64"}}, |
| 489 |
optionalProperties: {b: {type: "string"}}, |
| 490 |
} as const |
| 491 |
const both: TypeEquality<JTDDataType<typeof bothPropsSchema>, {a: number; b?: string}> = true |
| 492 |
|
| 493 |
const reqPropsSchema = {properties: {a: {type: "float64"}}} as const |
| 494 |
const req: TypeEquality<JTDDataType<typeof reqPropsSchema>, {a: number}> = true |
| 495 |
|
| 496 |
const optPropsSchema = {optionalProperties: {b: {type: "string"}}} as const |
| 497 |
const opt: TypeEquality<JTDDataType<typeof optPropsSchema>, {b?: string}> = true |
| 498 |
|
| 499 |
const noAddSchema = { |
| 500 |
optionalProperties: {b: {type: "string"}}, |
| 501 |
additionalProperties: false, |
| 502 |
} as const |
| 503 |
const noAdd: TypeEquality<JTDDataType<typeof noAddSchema>, {b?: string}> = true |
| 504 |
|
| 505 |
const addSchema = { |
| 506 |
optionalProperties: {b: {type: "string"}}, |
| 507 |
additionalProperties: true, |
| 508 |
} as const |
| 509 |
const add: TypeEquality< |
| 510 |
JTDDataType<typeof addSchema>, |
| 511 |
{b?: string; [key: string]: unknown} |
| 512 |
> = true |
| 513 |
const addVal: JTDDataType<typeof addSchema> = {b: "b", additional: 6} |
| 514 |
|
| 515 |
void [both, req, opt, noAdd, add, addVal] |
| 516 |
}) |
| 517 |
|
| 518 |
it("should typecheck values schemas", () => { |
| 519 |
const valuesSchema = {values: {type: "float64"}} as const |
| 520 |
const values: TypeEquality<JTDDataType<typeof valuesSchema>, Record<string, number>> = true |
| 521 |
|
| 522 |
void [values] |
| 523 |
}) |
| 524 |
|
| 525 |
it("should typecheck discriminator schemas", () => { |
| 526 |
const discriminatorSchema = { |
| 527 |
discriminator: "type", |
| 528 |
mapping: { |
| 529 |
a: {properties: {a: {type: "float64"}}}, |
| 530 |
b: {optionalProperties: {b: {type: "string"}}}, |
| 531 |
}, |
| 532 |
} as const |
| 533 |
const disc: TypeEquality<JTDDataType<typeof discriminatorSchema>, A | B> = true |
| 534 |
|
| 535 |
void [disc] |
| 536 |
}) |
| 537 |
|
| 538 |
it("should typecheck ref schemas", () => { |
| 539 |
const refSchema = { |
| 540 |
definitions: {num: {type: "float64", nullable: true}}, |
| 541 |
ref: "num", |
| 542 |
nullable: true, |
| 543 |
} as const |
| 544 |
const ref: TypeEquality<JTDDataType<typeof refSchema>, number | null> = true |
| 545 |
|
| 546 |
// works for recursive schemas |
| 547 |
const llSchema = { |
| 548 |
definitions: { |
| 549 |
node: { |
| 550 |
properties: {val: {type: "float64"}}, |
| 551 |
optionalProperties: {next: {ref: "node"}}, |
| 552 |
}, |
| 553 |
}, |
| 554 |
ref: "node", |
| 555 |
} as const |
| 556 |
const list: TypeEquality<JTDDataType<typeof llSchema>, LinkedList> = true |
| 557 |
|
| 558 |
void [ref, list] |
| 559 |
}) |
| 560 |
|
| 561 |
it("should typecheck empty schemas", () => { |
| 562 |
const emptySchema = {metadata: {}} as const |
| 563 |
const empty: TypeEquality<JTDDataType<typeof emptySchema>, unknown> = true |
| 564 |
|
| 565 |
void [empty] |
| 566 |
}) |
| 567 |
}) |
| 568 |
|
| 569 |
describe("SomeJTDSchemaType", () => { |
| 570 |
it("should allow setting unknowns", () => { |
| 571 |
// This test is basically here to assert that we should be using `{}` in |
| 572 |
// SomeJTDSchemaType |
| 573 |
const schema: SomeJTDSchemaType = {} |
| 574 |
|
| 575 |
void [schema] |
| 576 |
}) |
| 577 |
}) |