Skip to content

Commit

Permalink
Merge pull request ajv-validator#1463 from ajv-validator/revert-1455-…
Browse files Browse the repository at this point in the history
…master

Revert "add unknown => empty mapping in JTDSchemaType"
  • Loading branch information
epoberezkin authored Feb 23, 2021
2 parents ec68e7b + 7d759e6 commit 19e52da
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 41 deletions.
2 changes: 0 additions & 2 deletions lib/types/jtd-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ export type JTDSchemaType<T, D extends Record<string, unknown> = Record<string,
// NOTE there should only be one type that extends Record<string, never> so unions
// shouldn't be a worry
| (T extends Record<string, never> ? unknown : never)
// unknown also allows empty schema
| (unknown extends T ? unknown : never)
// null
// NOTE we have to check this too because null as an exclusive type also
// qualifies for the empty schema
Expand Down
53 changes: 14 additions & 39 deletions spec/types/jtd-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,17 @@ describe("JTDSchemaType", () => {
nullable: true,
}

void [properties, optionalProperties, mixedProperties, fewerProperties, propertiesNull]
// can't use properties for any object (e.g. keyof = never)
const noProperties: TypeEquality<JTDSchemaType<unknown>, never> = true

void [
properties,
optionalProperties,
mixedProperties,
fewerProperties,
propertiesNull,
noProperties,
]
})

it("should typecheck discriminator schemas", () => {
Expand Down Expand Up @@ -227,15 +237,6 @@ describe("JTDSchemaType", () => {
},
},
}
const unionWithDiscriminator: JTDSchemaType<A | B> = {
discriminator: "type",
mapping: {
a: {properties: {type: {enum: ["a"]}, a: {type: "float64"}}},
b: {
optionalProperties: {b: {type: "string"}},
},
},
}
const unionNull: JTDSchemaType<A | B | null> = {
discriminator: "type",
mapping: {
Expand All @@ -253,29 +254,18 @@ describe("JTDSchemaType", () => {
never
> = true

void [
union,
unionDuplicate,
unionMissing,
multOne,
multTwo,
unionWithDiscriminator,
unionNull,
noCommon,
]
void [union, unionDuplicate, unionMissing, multOne, multTwo, unionNull, noCommon]
})

it("should typecheck empty schemas", () => {
const empty: JTDSchemaType<Record<string, never>> = {}
// unknown can be null
const emptyUnknown: JTDSchemaType<unknown> = {nullable: true}
// can only use empty for empty and null
// @ts-expect-error
const emptyButFull: JTDSchemaType<{a: string}> = {}
const emptyNull: JTDSchemaType<null> = {nullable: true}
const emptyMeta: JTDSchemaType<Record<string, never>> = {metadata: {}}

void [empty, emptyUnknown, emptyButFull, emptyNull, emptyMeta]
void [empty, emptyButFull, emptyNull, emptyMeta]
})

it("should typecheck ref schemas", () => {
Expand Down Expand Up @@ -316,22 +306,7 @@ describe("JTDSchemaType", () => {
nullable: true,
}

// can type recursive schemas
type LinkedList = {val: number; next: LinkedList} | null
const linkedListSchema: JTDSchemaType<LinkedList, {node: LinkedList}> = {
definitions: {
node: {
properties: {
val: {type: "float64"},
next: {ref: "node"},
},
nullable: true,
},
},
ref: "node",
}

void [refs, missingDef, missingType, nullRefs, refsNullOne, refsNullTwo, linkedListSchema]
void [refs, missingDef, missingType, nullRefs, refsNullOne, refsNullTwo]
})

it("should typecheck metadata schemas", () => {
Expand Down

0 comments on commit 19e52da

Please sign in to comment.