summaryrefslogtreecommitdiff
path: root/src/misc/schema.ts
blob: 442c95ad988102685feacb084eca88cdefe10b79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
export const types = {
	boolean: 'boolean' as 'boolean',
	string: 'string' as 'string',
	number: 'number' as 'number',
	array: 'array' as 'array',
	object: 'object' as 'object',
	any: 'any' as 'any',
};

export const bool = {
	true: true as true,
	false: false as false,
};

export type Schema = {
	type: 'boolean' | 'number' | 'string' | 'array' | 'object' | 'any';
	nullable: boolean;
	optional: boolean;
	items?: Schema;
	properties?: Obj;
	description?: string;
	example?: any;
	format?: string;
	ref?: string;
	enum?: string[];
};

type NonUndefinedPropertyNames<T extends Obj> = {
	[K in keyof T]: T[K]['optional'] extends true ? never : K
}[keyof T];

type UndefinedPropertyNames<T extends Obj> = {
	[K in keyof T]: T[K]['optional'] extends true ? K : never
}[keyof T];

type OnlyRequired<T extends Obj> = Pick<T, NonUndefinedPropertyNames<T>>;
type OnlyOptional<T extends Obj> = Pick<T, UndefinedPropertyNames<T>>;

export type Obj = { [key: string]: Schema };

export type ObjType<s extends Obj> =
	{ [P in keyof OnlyOptional<s>]?: SchemaType<s[P]> } &
	{ [P in keyof OnlyRequired<s>]: SchemaType<s[P]> };

// https://qiita.com/hrsh7th@github/items/84e8968c3601009cdcf2
type MyType<T extends Schema> = {
	0: any;
	1: SchemaType<T>;
}[T extends Schema ? 1 : 0];

type NullOrUndefined<p extends Schema, T> =
	p['nullable'] extends true
		?	p['optional'] extends true
			? (T | null | undefined)
			: (T | null)
		: p['optional'] extends true
			? (T | undefined)
			: T;

export type SchemaType<p extends Schema> =
	p['type'] extends 'number' ? NullOrUndefined<p, number> :
	p['type'] extends 'string' ? NullOrUndefined<p, string> :
	p['type'] extends 'boolean' ? NullOrUndefined<p, boolean> :
	p['type'] extends 'array' ? NullOrUndefined<p, MyType<NonNullable<p['items']>>[]> :
	p['type'] extends 'object' ? NullOrUndefined<p, ObjType<NonNullable<p['properties']>>> :
	p['type'] extends 'any' ? NullOrUndefined<p, any> :
	any;