/* eslint-disable @typescript-eslint/no-empty-interface,no-void,@typescript-eslint/ban-types */ import _Ajv from "../ajv_jtd" import type {JTDSchemaType, SomeJTDSchemaType, JTDDataType} from "../../dist/jtd" import chai from "../chai" const should = chai.should() /** type is true if T is identically E */ type TypeEquality = [T] extends [E] ? ([E] extends [T] ? true : false) : false interface A { type: "a" a: number } interface B { type: "b" b?: string } interface C { type: "c" } type MyData = A | B type Missing = A | C interface LinkedList { val: number next?: LinkedList } const mySchema: JTDSchemaType = { discriminator: "type", mapping: { a: {properties: {a: {type: "float64"}}}, b: {optionalProperties: {b: {type: "string"}}}, }, } const missingSchema: JTDSchemaType = { discriminator: "type", mapping: { a: {properties: {a: {type: "float64"}}}, c: {properties: {}}, }, } describe("JTDSchemaType", () => { it("validation should prove the data type", () => { const ajv = new _Ajv() const validate = ajv.compile(mySchema) const validData: unknown = {type: "a", a: 1} if (validate(validData) && validData.type === "a") { // @ts-ignore validData.a.should.equal(1) } should.not.exist(validate.errors) if (ajv.validate(mySchema, validData) && validData.type === "a") { // @ts-ignore validData.a.should.equal(1) } should.not.exist(ajv.errors) }) it("parser should return correct data type", () => { const ajv = new _Ajv() const parse = ajv.compileParser(mySchema) const validJson = '{"type": "a", "a": 1}' const data = parse(validJson) if (data !== undefined && data.type === "a") { // @ts-ignore data.a.should.equal(1) } should.not.exist(parse.message) }) it("serializer should only accept correct data type", () => { const ajv = new _Ajv() const serialize = ajv.compileSerializer(mySchema) const validData = {type: "a" as const, a: 1} // @ts-ignore serialize(validData).should.equal('{"type":"a","a":1}') const invalidData = {type: "a" as const, b: "test"} // @ts-expect-error serialize(invalidData) }) it("validation should prove the data type for missingSchema", () => { const ajv = new _Ajv() const validate = ajv.compile(missingSchema) const validData: unknown = {type: "c"} if (validate(validData)) { // @ts-ignore validData.type.should.equal("c") } should.not.exist(validate.errors) if (ajv.validate(missingSchema, validData)) { // @ts-ignore validData.type.should.equal("c") } should.not.exist(validate.errors) }) it("should typecheck number schemas", () => { const numf: JTDSchemaType = {type: "float64"} const numi: JTDSchemaType = {type: "int32"} // @ts-expect-error const numl: JTDSchemaType = {type: "int64"} // number literals don't work // @ts-expect-error const nums: JTDSchemaType<1 | 2 | 3> = {type: "int32"} const numNull: JTDSchemaType = {type: "int32", nullable: true} // @ts-expect-error const numNotNull: JTDSchemaType = {type: "float32"} void [numf, numi, numl, nums, numNull, numNotNull] }) it("should typecheck boolean schemas", () => { const bool: JTDSchemaType = {type: "boolean"} // boolean literals don't // @ts-expect-error const boolTrue: JTDSchemaType = {type: "boolean"} const boolNull: JTDSchemaType = {type: "boolean", nullable: true} void [bool, boolTrue, boolNull] }) it("should typecheck string schemas", () => { const str: JTDSchemaType = {type: "string"} const time: JTDSchemaType = {type: "timestamp"} const strNull: JTDSchemaType = {type: "string", nullable: true} void [str, time, strNull] }) it("should typecheck dates", () => { const time: JTDSchemaType = {type: "timestamp"} const timeNull: JTDSchemaType = {type: "timestamp", nullable: true} void [time, timeNull] }) it("should typecheck enumeration schemas", () => { const enumerate: JTDSchemaType<"a" | "b"> = {enum: ["a", "b"]} // don't need to specify everything const enumerateMissing: JTDSchemaType<"a" | "b" | "c"> = {enum: ["a", "b"]} // must all be string literals // @ts-expect-error const enumerateNumber: JTDSchemaType<"a" | "b" | 5> = {enum: ["a", "b"]} // can't overgeneralize in schema // @ts-expect-error const enumerateString: JTDSchemaType<"a" | "b"> = {type: "string"} const enumerateNull: JTDSchemaType<"a" | "b" | null> = {enum: ["a", "b"], nullable: true} void [enumerate, enumerateMissing, enumerateNumber, enumerateString, enumerateNull] }) it("should typecheck elements schemas", () => { const elements: JTDSchemaType = {elements: {type: "float64"}} const readonlyElements: JTDSchemaType = {elements: {type: "float64"}} // tuples don't work // @ts-expect-error const tupleHomo: JTDSchemaType<[number, number]> = {elements: {type: "float64"}} // @ts-expect-error const tupleHeteroNum: JTDSchemaType<[number, string]> = { elements: {type: "float64"}, } // @ts-expect-error const tupleHeteroString: JTDSchemaType<[number, string]> = { elements: {type: "string"}, } const elemNull: JTDSchemaType = {elements: {type: "float64"}, nullable: true} // can typecheck an array of unions const unionElem: TypeEquality, never> = false // can't typecheck a union of arrays const elemUnion: TypeEquality, never> = true void [ elements, readonlyElements, tupleHomo, tupleHeteroNum, tupleHeteroString, elemNull, unionElem, elemUnion, ] }) it("should typecheck values schemas", () => { const values: JTDSchemaType> = {values: {type: "float64"}} const readonlyValues: JTDSchemaType>> = { values: {type: "float64"}, } // values must be a whole mapping // @ts-expect-error const valuesDefined: JTDSchemaType<{prop: number}> = {values: {type: "float64"}} const valuesNull: JTDSchemaType | null> = { values: {type: "float64"}, nullable: true, } // can typecheck a values of unions const unionValues: TypeEquality>, never> = false // can't typecheck a union of values const valuesUnion: TypeEquality< JTDSchemaType | Record>, never > = true void [values, readonlyValues, valuesDefined, valuesNull, unionValues, valuesUnion] }) it("should typecheck properties schemas", () => { const properties: JTDSchemaType<{a: number; b: string}> = { properties: {a: {type: "float64"}, b: {type: "string"}}, } const optionalProperties: JTDSchemaType<{a?: number; b?: string}> = { optionalProperties: {a: {type: "float64"}, b: {type: "string"}}, additionalProperties: false, } const mixedProperties: JTDSchemaType<{a: number; b?: string}> = { properties: {a: {type: "float64"}}, optionalProperties: {b: {type: "string"}}, additionalProperties: true, } const fewerProperties: JTDSchemaType<{a: number; b: string}> = { // @ts-expect-error properties: {a: {type: "float64"}}, } const propertiesNull: JTDSchemaType<{a: number; b: string} | null> = { properties: {a: {type: "float64"}, b: {type: "string"}}, nullable: true, } void [properties, optionalProperties, mixedProperties, fewerProperties, propertiesNull] }) it("should typecheck discriminator schemas", () => { const union: JTDSchemaType = { discriminator: "type", mapping: { a: {properties: {a: {type: "float64"}}}, b: { optionalProperties: {b: {type: "string"}}, }, }, } // can't mess up, e.g. value type isn't a union const unionDuplicate: JTDSchemaType = { discriminator: "type", mapping: { a: {properties: {a: {type: "float64"}}}, // @ts-expect-error b: {properties: {a: {type: "float64"}}}, }, } // must specify everything const unionMissing: JTDSchemaType = { discriminator: "type", // @ts-expect-error mapping: { a: {properties: {a: {type: "float64"}}}, }, } // can use any valid discrinimator type Mult = JTDSchemaType<(A & {typ: "alpha"}) | (B & {typ: "beta"})> const multOne: Mult = { discriminator: "type", mapping: { a: {properties: {a: {type: "float64"}, typ: {enum: ["alpha"]}}}, b: { properties: {typ: {enum: ["beta"]}}, optionalProperties: {b: {type: "string"}}, }, }, } const multTwo: Mult = { discriminator: "typ", mapping: { alpha: {properties: {a: {type: "float64"}, type: {enum: ["a"]}}}, beta: { properties: {type: {enum: ["b"]}}, optionalProperties: {b: {type: "string"}}, }, }, } const unionNull: JTDSchemaType = { discriminator: "type", mapping: { a: {properties: {a: {type: "float64"}}}, b: { optionalProperties: {b: {type: "string"}}, }, }, nullable: true, } // properties must have common key const noCommon: TypeEquality< JTDSchemaType<{key1: "a"; a: number} | {key2: "b"; b: string}>, never > = true void [union, unionDuplicate, unionMissing, multOne, multTwo, unionNull, noCommon] }) it("should typecheck empty schemas", () => { const empty: JTDSchemaType = {} // unknown can be null const emptyUnknown: JTDSchemaType = {nullable: true} // somewhat unintuitively, it can still have nullable: false even though it can be null const falseUnknown: JTDSchemaType = {nullable: false} // can only use empty for empty and null // @ts-expect-error const emptyButFull: JTDSchemaType<{a: string}> = {} const emptyMeta: JTDSchemaType = {metadata: {}} // constant null representable as nullable empty object const emptyNull: TypeEquality, never> = true void [empty, emptyUnknown, falseUnknown, emptyButFull, emptyMeta, emptyNull] }) it("should typecheck empty records", () => { // empty record variants const emptyPro: JTDSchemaType<{}> = {properties: {}} const emptyOpt: JTDSchemaType<{}> = {optionalProperties: {}} const emptyBoth: JTDSchemaType<{}> = {properties: {}, optionalProperties: {}} const emptyRecord: JTDSchemaType> = {properties: {}} const notNullable: JTDSchemaType<{}> = {properties: {}, nullable: false} // can't be null // @ts-expect-error const nullable: JTDSchemaType<{}> = {properties: {}, nullable: true} const emptyNullUnion: JTDSchemaType = {properties: {}, nullable: true} const emptyNullRecord: JTDSchemaType> = { properties: {}, nullable: true, } void [ emptyPro, emptyOpt, emptyBoth, emptyRecord, notNullable, nullable, emptyNullUnion, emptyNullRecord, ] }) it("should typecheck ref schemas", () => { const refs: JTDSchemaType = { definitions: { num: {type: "float64"}, }, elements: {ref: "num"}, } const missingDef: JTDSchemaType = { // @ts-expect-error definitions: {}, elements: {ref: "num"}, } const missingType: JTDSchemaType = { definitions: {}, // @ts-expect-error elements: {ref: "num"}, } const nullRefs: JTDSchemaType = { definitions: { num: {type: "float64"}, }, ref: "num", nullable: true, } const refsNullOne: JTDSchemaType = { definitions: { num: {type: "float64", nullable: true}, }, ref: "num", } const refsNullTwo: JTDSchemaType = { definitions: { num: {type: "float64", nullable: true}, }, ref: "num", nullable: true, } void [refs, missingDef, missingType, nullRefs, refsNullOne, refsNullTwo] }) it("should typecheck metadata schemas", () => { const meta: JTDSchemaType = {type: "float32", metadata: {key: "val"}} const emptyMeta: JTDSchemaType = {metadata: {key: "val"}} const unknownMeta: JTDSchemaType = {nullable: true, metadata: {key: "val"}} void [meta, emptyMeta, unknownMeta] }) }) describe("JTDDataType", () => { it("validation should prove the data type", () => { const ajv = new _Ajv() const mySchema1 = { discriminator: "type", mapping: { a: {properties: {a: {type: "float64"}}}, b: {optionalProperties: {b: {type: "string"}}}, }, } as const const validate = ajv.compile(mySchema1) const validData: unknown = {type: "a", a: 1} if (validate(validData) && validData.type === "a") { // @ts-ignore validData.a.should.equal(1) } should.not.exist(validate.errors) if (ajv.validate(mySchema1, validData) && validData.type === "a") { // @ts-ignore validData.a.should.equal(1) } should.not.exist(ajv.errors) }) it("should typecheck number schemas", () => { const numSchema = {type: "float64"} as const const num: TypeEquality, number> = true void [num] }) it("should typecheck boolean schemas", () => { const booleanSchema = {type: "boolean"} as const const bool: TypeEquality, boolean> = true void [bool] }) it("should typecheck string schemas", () => { const strSchema = {type: "string"} as const const str: TypeEquality, string> = true void [str] }) it("should typecheck timestamp schemas", () => { const timeSchema = {type: "timestamp"} as const const time: TypeEquality, string | Date> = true void [time] }) it("should typecheck enum schemas", () => { const enumSchema = {enum: ["a", "b"]} as const const enumerated: TypeEquality, "a" | "b"> = true // if you forget const on an enum it will error const enumStringSchema = {enum: ["a", "b"]} const enumString: TypeEquality, never> = true // also if not a string const enumNumSchema = {enum: [3]} as const const enumNum: TypeEquality, never> = true void [enumerated, enumString, enumNum] }) it("should typecheck elements schemas", () => { const elementsSchema = {elements: {type: "float64"}} as const const elem: TypeEquality, number[]> = true void [elem] }) it("should typecheck properties schemas", () => { const bothPropsSchema = { properties: {a: {type: "float64"}}, optionalProperties: {b: {type: "string"}}, } as const const both: TypeEquality, {a: number; b?: string}> = true const reqPropsSchema = {properties: {a: {type: "float64"}}} as const const req: TypeEquality, {a: number}> = true const optPropsSchema = {optionalProperties: {b: {type: "string"}}} as const const opt: TypeEquality, {b?: string}> = true const noAddSchema = { optionalProperties: {b: {type: "string"}}, additionalProperties: false, } as const const noAdd: TypeEquality, {b?: string}> = true const addSchema = { optionalProperties: {b: {type: "string"}}, additionalProperties: true, } as const const add: TypeEquality< JTDDataType, {b?: string; [key: string]: unknown} > = true const addVal: JTDDataType = {b: "b", additional: 6} void [both, req, opt, noAdd, add, addVal] }) it("should typecheck values schemas", () => { const valuesSchema = {values: {type: "float64"}} as const const values: TypeEquality, Record> = true void [values] }) it("should typecheck discriminator schemas", () => { const discriminatorSchema = { discriminator: "type", mapping: { a: {properties: {a: {type: "float64"}}}, b: {optionalProperties: {b: {type: "string"}}}, }, } as const const disc: TypeEquality, A | B> = true void [disc] }) it("should typecheck ref schemas", () => { const refSchema = { definitions: {num: {type: "float64", nullable: true}}, ref: "num", nullable: true, } as const const ref: TypeEquality, number | null> = true // works for recursive schemas const llSchema = { definitions: { node: { properties: {val: {type: "float64"}}, optionalProperties: {next: {ref: "node"}}, }, }, ref: "node", } as const const list: TypeEquality, LinkedList> = true void [ref, list] }) it("should typecheck empty schemas", () => { const emptySchema = {metadata: {}} as const const empty: TypeEquality, unknown> = true void [empty] }) }) describe("SomeJTDSchemaType", () => { it("should allow setting unknowns", () => { // This test is basically here to assert that we should be using `{}` in // SomeJTDSchemaType const schema: SomeJTDSchemaType = {} void [schema] }) })