Skip to content

Commit a5c0b91

Browse files
refactor: define type specifics so we can easily define union type
1 parent 24f9ea9 commit a5c0b91

File tree

1 file changed

+55
-69
lines changed

1 file changed

+55
-69
lines changed

src/schema.ts

Lines changed: 55 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -24,71 +24,63 @@ type Schema = {
2424
anyOf?: SchemaObject[],
2525
};
2626

27-
type ObjectSchema = {
28-
type: "object",
29-
/**
30-
* An object is a collection of property/value pairs. The properties keyword is used to define the object properties – you need to list the property names and specify a schema for each property.
31-
*/
32-
properties?: Record<string, SchemaObject>,
33-
/**
34-
* By default, all object properties are optional. You can specify the required properties in the required list
35-
*/
36-
required?: string[],
37-
additionalProperties?: SchemaObject | ReferenceObject | boolean,
38-
minProperties?: number,
39-
maxProperties?: number,
40-
};
41-
42-
type StringSchema = {
43-
type: "string",
44-
format?: "date" | "date-time" | "password" | "byte" | "binary" | "email" | "uuid" | "uri" | "hostname" | "ipv4" | "ipv6",
45-
contentMediaType?: string,
46-
contentEncoding?: "base16" | "base32" | "base64" | "base64url",
47-
pattern?: string,
48-
default?: string,
49-
enum?: string[],
50-
minLength?: number,
51-
maxLength?: number,
52-
};
27+
type TypeSpecifics = {
28+
object: {
29+
/**
30+
* An object is a collection of property/value pairs. The properties keyword is used to define the object properties – you need to list the property names and specify a schema for each property.
31+
*/
32+
properties?: Record<string, SchemaObject>,
33+
/**
34+
* By default, all object properties are optional. You can specify the required properties in the required list
35+
*/
36+
required?: string[],
37+
additionalProperties?: SchemaObject | ReferenceObject | boolean,
38+
minProperties?: number,
39+
maxProperties?: number,
40+
},
5341

54-
type NumberSchema = {
55-
type: "number",
56-
format?: "float" | "double",
57-
minimum?: number,
58-
exclusiveMinimum?: number,
59-
maximum?: number,
60-
exclusiveMaximum?: number,
61-
default?: number,
62-
multipleOf?: number,
63-
};
42+
string: {
43+
format?: "date" | "date-time" | "password" | "byte" | "binary" | "email" | "uuid" | "uri" | "hostname" | "ipv4" | "ipv6",
44+
contentMediaType?: string,
45+
contentEncoding?: "base16" | "base32" | "base64" | "base64url",
46+
pattern?: string,
47+
default?: string,
48+
enum?: string[],
49+
minLength?: number,
50+
maxLength?: number,
51+
},
6452

65-
type IntegerSchema = {
66-
type: "integer",
67-
format?: "int32" | "int64",
68-
minimum?: number,
69-
exclusiveMinimum?: number,
70-
maximum?: number,
71-
exclusiveMaximum?: number,
72-
default?: number,
73-
multipleOf?: number,
74-
};
53+
number: {
54+
format?: "float" | "double",
55+
minimum?: number,
56+
exclusiveMinimum?: number,
57+
maximum?: number,
58+
exclusiveMaximum?: number,
59+
default?: number,
60+
multipleOf?: number,
61+
},
7562

76-
type ArraySchema = {
77-
type: "array",
78-
items?: SchemaObject,
79-
prefixItems?: SchemaObject[],
80-
minItems?: number,
81-
maxItems?: number,
82-
uniqueItems?: boolean,
83-
};
63+
integer: {
64+
format?: "int32" | "int64",
65+
minimum?: number,
66+
exclusiveMinimum?: number,
67+
maximum?: number,
68+
exclusiveMaximum?: number,
69+
default?: number,
70+
multipleOf?: number,
71+
},
8472

85-
type NullSchema = {
86-
type: "null",
87-
};
73+
array: {
74+
items?: SchemaObject,
75+
prefixItems?: SchemaObject[],
76+
minItems?: number,
77+
maxItems?: number,
78+
uniqueItems?: boolean,
79+
},
8880

89-
type BooleanSchema = {
90-
type: "boolean",
91-
};
81+
null: object,
82+
boolean: object,
83+
}
9284

9385
type UnknownSchema = {
9486
type?: undefined,
@@ -97,12 +89,6 @@ type UnknownSchema = {
9789
/**
9890
* The Schema Object allows the definition of input and output data types. These types can be objects, but also primitives and arrays. This object is a superset of the JSON Schema Specification Draft 2020-12.
9991
*/
100-
export type SchemaObject = Schema & (
101-
ObjectSchema |
102-
StringSchema |
103-
NumberSchema |
104-
IntegerSchema |
105-
ArraySchema |
106-
NullSchema |
107-
BooleanSchema
108-
) | (ReferenceObject & UnknownSchema);
92+
export type SchemaObject = Schema & {
93+
[T in keyof TypeSpecifics]: { type: T } & TypeSpecifics[T];
94+
}[keyof TypeSpecifics] | (ReferenceObject & UnknownSchema);

0 commit comments

Comments
 (0)