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