1 | /* eslint-disable @typescript-eslint/no-empty-interface */
|
---|
2 | type StrictNullChecksWrapper<Name extends string, Type> = undefined extends null
|
---|
3 | ? `strictNullChecks must be true in tsconfig to use ${Name}`
|
---|
4 | : Type
|
---|
5 |
|
---|
6 | export type SomeJSONSchema = UncheckedJSONSchemaType<Known, true>
|
---|
7 |
|
---|
8 | type UncheckedPartialSchema<T> = Partial<UncheckedJSONSchemaType<T, true>>
|
---|
9 |
|
---|
10 | export type PartialSchema<T> = StrictNullChecksWrapper<"PartialSchema", UncheckedPartialSchema<T>>
|
---|
11 |
|
---|
12 | type JSONType<T extends string, IsPartial extends boolean> = IsPartial extends true
|
---|
13 | ? T | undefined
|
---|
14 | : T
|
---|
15 |
|
---|
16 | interface NumberKeywords {
|
---|
17 | minimum?: number
|
---|
18 | maximum?: number
|
---|
19 | exclusiveMinimum?: number
|
---|
20 | exclusiveMaximum?: number
|
---|
21 | multipleOf?: number
|
---|
22 | format?: string
|
---|
23 | }
|
---|
24 |
|
---|
25 | interface StringKeywords {
|
---|
26 | minLength?: number
|
---|
27 | maxLength?: number
|
---|
28 | pattern?: string
|
---|
29 | format?: string
|
---|
30 | }
|
---|
31 |
|
---|
32 | type UncheckedJSONSchemaType<T, IsPartial extends boolean> = (
|
---|
33 | | // these two unions allow arbitrary unions of types
|
---|
34 | {
|
---|
35 | anyOf: readonly UncheckedJSONSchemaType<T, IsPartial>[]
|
---|
36 | }
|
---|
37 | | {
|
---|
38 | oneOf: readonly UncheckedJSONSchemaType<T, IsPartial>[]
|
---|
39 | }
|
---|
40 | // this union allows for { type: (primitive)[] } style schemas
|
---|
41 | | ({
|
---|
42 | type: (T extends number
|
---|
43 | ? JSONType<"number" | "integer", IsPartial>
|
---|
44 | : T extends string
|
---|
45 | ? JSONType<"string", IsPartial>
|
---|
46 | : T extends boolean
|
---|
47 | ? JSONType<"boolean", IsPartial>
|
---|
48 | : never)[]
|
---|
49 | } & (T extends number
|
---|
50 | ? NumberKeywords
|
---|
51 | : T extends string
|
---|
52 | ? StringKeywords
|
---|
53 | : T extends boolean
|
---|
54 | ? unknown
|
---|
55 | : never))
|
---|
56 | // this covers "normal" types; it's last so typescript looks to it first for errors
|
---|
57 | | ((T extends number
|
---|
58 | ? {
|
---|
59 | type: JSONType<"number" | "integer", IsPartial>
|
---|
60 | } & NumberKeywords
|
---|
61 | : T extends string
|
---|
62 | ? {
|
---|
63 | type: JSONType<"string", IsPartial>
|
---|
64 | } & StringKeywords
|
---|
65 | : T extends boolean
|
---|
66 | ? {
|
---|
67 | type: "boolean"
|
---|
68 | }
|
---|
69 | : T extends [any, ...any[]]
|
---|
70 | ? {
|
---|
71 | // JSON AnySchema for tuple
|
---|
72 | type: JSONType<"array", IsPartial>
|
---|
73 | items: {
|
---|
74 | readonly [K in keyof T]-?: UncheckedJSONSchemaType<T[K], false> & Nullable<T[K]>
|
---|
75 | } & {length: T["length"]}
|
---|
76 | minItems: T["length"]
|
---|
77 | } & ({maxItems: T["length"]} | {additionalItems: false})
|
---|
78 | : T extends readonly any[]
|
---|
79 | ? {
|
---|
80 | type: JSONType<"array", IsPartial>
|
---|
81 | items: UncheckedJSONSchemaType<T[0], false>
|
---|
82 | contains?: UncheckedPartialSchema<T[0]>
|
---|
83 | minItems?: number
|
---|
84 | maxItems?: number
|
---|
85 | minContains?: number
|
---|
86 | maxContains?: number
|
---|
87 | uniqueItems?: true
|
---|
88 | additionalItems?: never
|
---|
89 | }
|
---|
90 | : T extends Record<string, any>
|
---|
91 | ? {
|
---|
92 | // JSON AnySchema for records and dictionaries
|
---|
93 | // "required" is not optional because it is often forgotten
|
---|
94 | // "properties" are optional for more concise dictionary schemas
|
---|
95 | // "patternProperties" and can be only used with interfaces that have string index
|
---|
96 | type: JSONType<"object", IsPartial>
|
---|
97 | additionalProperties?: boolean | UncheckedJSONSchemaType<T[string], false>
|
---|
98 | unevaluatedProperties?: boolean | UncheckedJSONSchemaType<T[string], false>
|
---|
99 | properties?: IsPartial extends true
|
---|
100 | ? Partial<UncheckedPropertiesSchema<T>>
|
---|
101 | : UncheckedPropertiesSchema<T>
|
---|
102 | patternProperties?: {[Pattern in string]?: UncheckedJSONSchemaType<T[string], false>}
|
---|
103 | propertyNames?: Omit<UncheckedJSONSchemaType<string, false>, "type"> & {type?: "string"}
|
---|
104 | dependencies?: {[K in keyof T]?: Readonly<(keyof T)[]> | UncheckedPartialSchema<T>}
|
---|
105 | dependentRequired?: {[K in keyof T]?: Readonly<(keyof T)[]>}
|
---|
106 | dependentSchemas?: {[K in keyof T]?: UncheckedPartialSchema<T>}
|
---|
107 | minProperties?: number
|
---|
108 | maxProperties?: number
|
---|
109 | } & (// "required" type does not guarantee that all required properties
|
---|
110 | // are listed it only asserts that optional cannot be listed.
|
---|
111 | // "required" is not necessary if it's a non-partial type with no required keys
|
---|
112 | IsPartial extends true
|
---|
113 | ? {required: Readonly<(keyof T)[]>}
|
---|
114 | : [UncheckedRequiredMembers<T>] extends [never]
|
---|
115 | ? {required?: Readonly<UncheckedRequiredMembers<T>[]>}
|
---|
116 | : {required: Readonly<UncheckedRequiredMembers<T>[]>})
|
---|
117 | : T extends null
|
---|
118 | ? {
|
---|
119 | nullable: true
|
---|
120 | }
|
---|
121 | : never) & {
|
---|
122 | allOf?: Readonly<UncheckedPartialSchema<T>[]>
|
---|
123 | anyOf?: Readonly<UncheckedPartialSchema<T>[]>
|
---|
124 | oneOf?: Readonly<UncheckedPartialSchema<T>[]>
|
---|
125 | if?: UncheckedPartialSchema<T>
|
---|
126 | then?: UncheckedPartialSchema<T>
|
---|
127 | else?: UncheckedPartialSchema<T>
|
---|
128 | not?: UncheckedPartialSchema<T>
|
---|
129 | })
|
---|
130 | ) & {
|
---|
131 | [keyword: string]: any
|
---|
132 | $id?: string
|
---|
133 | $ref?: string
|
---|
134 | $defs?: {
|
---|
135 | [Key in string]?: UncheckedJSONSchemaType<Known, true>
|
---|
136 | }
|
---|
137 | definitions?: {
|
---|
138 | [Key in string]?: UncheckedJSONSchemaType<Known, true>
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | export type JSONSchemaType<T> = StrictNullChecksWrapper<
|
---|
143 | "JSONSchemaType",
|
---|
144 | UncheckedJSONSchemaType<T, false>
|
---|
145 | >
|
---|
146 |
|
---|
147 | type Known = KnownRecord | [Known, ...Known[]] | Known[] | number | string | boolean | null
|
---|
148 |
|
---|
149 | interface KnownRecord extends Record<string, Known> {}
|
---|
150 |
|
---|
151 | type UncheckedPropertiesSchema<T> = {
|
---|
152 | [K in keyof T]-?: (UncheckedJSONSchemaType<T[K], false> & Nullable<T[K]>) | {$ref: string}
|
---|
153 | }
|
---|
154 |
|
---|
155 | export type PropertiesSchema<T> = StrictNullChecksWrapper<
|
---|
156 | "PropertiesSchema",
|
---|
157 | UncheckedPropertiesSchema<T>
|
---|
158 | >
|
---|
159 |
|
---|
160 | type UncheckedRequiredMembers<T> = {
|
---|
161 | [K in keyof T]-?: undefined extends T[K] ? never : K
|
---|
162 | }[keyof T]
|
---|
163 |
|
---|
164 | export type RequiredMembers<T> = StrictNullChecksWrapper<
|
---|
165 | "RequiredMembers",
|
---|
166 | UncheckedRequiredMembers<T>
|
---|
167 | >
|
---|
168 |
|
---|
169 | type Nullable<T> = undefined extends T
|
---|
170 | ? {
|
---|
171 | nullable: true
|
---|
172 | const?: never // any non-null value would fail `const: null`, `null` would fail any other value in const
|
---|
173 | enum?: Readonly<(T | null)[]> // `null` must be explicitly included in "enum" for `null` to pass
|
---|
174 | default?: T | null
|
---|
175 | }
|
---|
176 | : {
|
---|
177 | const?: T
|
---|
178 | enum?: Readonly<T[]>
|
---|
179 | default?: T
|
---|
180 | }
|
---|