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