1 | type StrictNullChecksWrapper<Name extends string, Type> = undefined extends null ? `strictNullChecks must be true in tsconfig to use ${Name}` : Type;
|
2 | type UnionToIntersection<U> = (U extends any ? (_: U) => void : never) extends (_: infer I) => void ? I : never;
|
3 | export type SomeJSONSchema = UncheckedJSONSchemaType<Known, true>;
|
4 | type UncheckedPartialSchema<T> = Partial<UncheckedJSONSchemaType<T, true>>;
|
5 | export type PartialSchema<T> = StrictNullChecksWrapper<"PartialSchema", UncheckedPartialSchema<T>>;
|
6 | type JSONType<T extends string, IsPartial extends boolean> = IsPartial extends true ? T | undefined : T;
|
7 | interface NumberKeywords {
|
8 | minimum?: number;
|
9 | maximum?: number;
|
10 | exclusiveMinimum?: number;
|
11 | exclusiveMaximum?: number;
|
12 | multipleOf?: number;
|
13 | format?: string;
|
14 | }
|
15 | interface StringKeywords {
|
16 | minLength?: number;
|
17 | maxLength?: number;
|
18 | pattern?: string;
|
19 | format?: string;
|
20 | }
|
21 | type UncheckedJSONSchemaType<T, IsPartial extends boolean> = (
|
22 | {
|
23 | anyOf: readonly UncheckedJSONSchemaType<T, IsPartial>[];
|
24 | } | {
|
25 | oneOf: readonly UncheckedJSONSchemaType<T, IsPartial>[];
|
26 | } | ({
|
27 | type: readonly (T extends number ? JSONType<"number" | "integer", IsPartial> : T extends string ? JSONType<"string", IsPartial> : T extends boolean ? JSONType<"boolean", IsPartial> : never)[];
|
28 | } & UnionToIntersection<T extends number ? NumberKeywords : T extends string ? StringKeywords : T extends boolean ? {} : never>) | ((T extends number ? {
|
29 | type: JSONType<"number" | "integer", IsPartial>;
|
30 | } & NumberKeywords : T extends string ? {
|
31 | type: JSONType<"string", IsPartial>;
|
32 | } & StringKeywords : T extends boolean ? {
|
33 | type: JSONType<"boolean", IsPartial>;
|
34 | } : T extends readonly [any, ...any[]] ? {
|
35 | type: JSONType<"array", IsPartial>;
|
36 | items: {
|
37 | readonly [K in keyof T]-?: UncheckedJSONSchemaType<T[K], false> & Nullable<T[K]>;
|
38 | } & {
|
39 | length: T["length"];
|
40 | };
|
41 | minItems: T["length"];
|
42 | } & ({
|
43 | maxItems: T["length"];
|
44 | } | {
|
45 | additionalItems: false;
|
46 | }) : T extends readonly any[] ? {
|
47 | type: JSONType<"array", IsPartial>;
|
48 | items: UncheckedJSONSchemaType<T[0], false>;
|
49 | contains?: UncheckedPartialSchema<T[0]>;
|
50 | minItems?: number;
|
51 | maxItems?: number;
|
52 | minContains?: number;
|
53 | maxContains?: number;
|
54 | uniqueItems?: true;
|
55 | additionalItems?: never;
|
56 | } : T extends Record<string, any> ? {
|
57 | type: JSONType<"object", IsPartial>;
|
58 | additionalProperties?: boolean | UncheckedJSONSchemaType<T[string], false>;
|
59 | unevaluatedProperties?: boolean | UncheckedJSONSchemaType<T[string], false>;
|
60 | properties?: IsPartial extends true ? Partial<UncheckedPropertiesSchema<T>> : UncheckedPropertiesSchema<T>;
|
61 | patternProperties?: Record<string, UncheckedJSONSchemaType<T[string], false>>;
|
62 | propertyNames?: Omit<UncheckedJSONSchemaType<string, false>, "type"> & {
|
63 | type?: "string";
|
64 | };
|
65 | dependencies?: {
|
66 | [K in keyof T]?: Readonly<(keyof T)[]> | UncheckedPartialSchema<T>;
|
67 | };
|
68 | dependentRequired?: {
|
69 | [K in keyof T]?: Readonly<(keyof T)[]>;
|
70 | };
|
71 | dependentSchemas?: {
|
72 | [K in keyof T]?: UncheckedPartialSchema<T>;
|
73 | };
|
74 | minProperties?: number;
|
75 | maxProperties?: number;
|
76 | } & (IsPartial extends true ? {
|
77 | required: Readonly<(keyof T)[]>;
|
78 | } : [UncheckedRequiredMembers<T>] extends [never] ? {
|
79 | required?: Readonly<UncheckedRequiredMembers<T>[]>;
|
80 | } : {
|
81 | required: Readonly<UncheckedRequiredMembers<T>[]>;
|
82 | }) : T extends null ? {
|
83 | type: JSONType<"null", IsPartial>;
|
84 | nullable: true;
|
85 | } : never) & {
|
86 | allOf?: Readonly<UncheckedPartialSchema<T>[]>;
|
87 | anyOf?: Readonly<UncheckedPartialSchema<T>[]>;
|
88 | oneOf?: Readonly<UncheckedPartialSchema<T>[]>;
|
89 | if?: UncheckedPartialSchema<T>;
|
90 | then?: UncheckedPartialSchema<T>;
|
91 | else?: UncheckedPartialSchema<T>;
|
92 | not?: UncheckedPartialSchema<T>;
|
93 | })) & {
|
94 | [keyword: string]: any;
|
95 | $id?: string;
|
96 | $ref?: string;
|
97 | $defs?: Record<string, UncheckedJSONSchemaType<Known, true>>;
|
98 | definitions?: Record<string, UncheckedJSONSchemaType<Known, true>>;
|
99 | };
|
100 | export type JSONSchemaType<T> = StrictNullChecksWrapper<"JSONSchemaType", UncheckedJSONSchemaType<T, false>>;
|
101 | type Known = {
|
102 | [key: string]: Known;
|
103 | } | [Known, ...Known[]] | Known[] | number | string | boolean | null;
|
104 | type UncheckedPropertiesSchema<T> = {
|
105 | [K in keyof T]-?: (UncheckedJSONSchemaType<T[K], false> & Nullable<T[K]>) | {
|
106 | $ref: string;
|
107 | };
|
108 | };
|
109 | export type PropertiesSchema<T> = StrictNullChecksWrapper<"PropertiesSchema", UncheckedPropertiesSchema<T>>;
|
110 | type UncheckedRequiredMembers<T> = {
|
111 | [K in keyof T]-?: undefined extends T[K] ? never : K;
|
112 | }[keyof T];
|
113 | export type RequiredMembers<T> = StrictNullChecksWrapper<"RequiredMembers", UncheckedRequiredMembers<T>>;
|
114 | type Nullable<T> = undefined extends T ? {
|
115 | nullable: true;
|
116 | const?: null;
|
117 | enum?: Readonly<(T | null)[]>;
|
118 | default?: T | null;
|
119 | } : {
|
120 | nullable?: false;
|
121 | const?: T;
|
122 | enum?: Readonly<T[]>;
|
123 | default?: T;
|
124 | };
|
125 | export {};
|