Skip to content

Commit 0c8e6aa

Browse files
committed
Move common fields to JsonSchema
1 parent fcc4818 commit 0c8e6aa

File tree

1 file changed

+11
-39
lines changed

1 file changed

+11
-39
lines changed

src/jsonSchema.ts

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ export function isReference(schema: any): schema is Reference {
66
return typeof schema.$ref === 'string';
77
}
88

9-
export interface JsonSchema {
9+
export interface JsonSchema<T = any> {
10+
const?: T,
11+
default?: T,
1012
deprecated?: boolean,
1113
description?: string,
14+
enum?: Array<T>,
15+
examples?: Array<T>,
1216
readOnly?: boolean,
1317
title?: string,
1418
writeOnly?: boolean
@@ -26,10 +30,6 @@ export function isComposedJsonSchema(schema: any): schema is ComposedJsonSchema
2630
}
2731

2832
export interface NullJsonSchema extends JsonSchema, ComposedJsonSchema<NullJsonSchema> {
29-
const?: null,
30-
default?: null,
31-
enum?: Array<null>,
32-
examples?: Array<null>,
3333
type?: 'null' | Array<'null' | 'boolean' | 'string' | 'integer' | 'number' | 'array' | 'object'>,
3434
}
3535

@@ -45,11 +45,7 @@ export function isNullJsonSchema(schema: any): schema is NullJsonSchema {
4545
return false;
4646
}
4747

48-
export interface BooleanJsonSchema extends JsonSchema, ComposedJsonSchema<BooleanJsonSchema> {
49-
const?: boolean,
50-
default?: boolean,
51-
enum?: Array<boolean>,
52-
examples?: Array<boolean>,
48+
export interface BooleanJsonSchema extends JsonSchema<boolean>, ComposedJsonSchema<BooleanJsonSchema> {
5349
type?: 'boolean' | Array<'null' | 'boolean' | 'string' | 'integer' | 'number' | 'array' | 'object'>,
5450
}
5551

@@ -65,11 +61,7 @@ export function isBooleanJsonSchema(schema: any): schema is BooleanJsonSchema {
6561
return false;
6662
}
6763

68-
export interface StringJsonSchema extends JsonSchema, ComposedJsonSchema<StringJsonSchema> {
69-
const?: string,
70-
default?: string,
71-
enum?: Array<string>,
72-
examples?: Array<string>,
64+
export interface StringJsonSchema extends JsonSchema<string>, ComposedJsonSchema<StringJsonSchema> {
7365
maxLength?: number,
7466
minLength?: number,
7567
pattern?: string,
@@ -96,11 +88,7 @@ export function isStringJsonSchema(schema: any): schema is StringJsonSchema {
9688
return false;
9789
}
9890

99-
export interface NumberJsonSchema extends JsonSchema, ComposedJsonSchema<NumberJsonSchema> {
100-
const?: number,
101-
default?: number,
102-
enum?: Array<number>,
103-
examples?: Array<number>,
91+
export interface NumberJsonSchema extends JsonSchema<number>, ComposedJsonSchema<NumberJsonSchema> {
10492
exclusiveMaximum?: number,
10593
exclusiveMinimum?: number,
10694
maximum?: number,
@@ -131,11 +119,7 @@ export function isNumberJsonSchema(schema: any): schema is NumberJsonSchema {
131119
return false;
132120
}
133121

134-
export interface IntegerJsonSchema extends JsonSchema, ComposedJsonSchema<IntegerJsonSchema> {
135-
const?: number,
136-
default?: number,
137-
enum?: Array<number>,
138-
examples?: Array<number>,
122+
export interface IntegerJsonSchema extends JsonSchema<number>, ComposedJsonSchema<IntegerJsonSchema> {
139123
exclusiveMaximum?: number,
140124
exclusiveMinimum?: number,
141125
maximum?: number,
@@ -166,12 +150,8 @@ export function isIntegerJsonSchema(schema: any): schema is IntegerJsonSchema {
166150
return false;
167151
}
168152

169-
export interface ArrayJsonSchema extends JsonSchema, ComposedJsonSchema<ArrayJsonSchema> {
170-
const?: Array<any>,
153+
export interface ArrayJsonSchema extends JsonSchema<Array<any>>, ComposedJsonSchema<ArrayJsonSchema> {
171154
contains?: JsonSchema,
172-
default?: Array<any>,
173-
enum?: Array<Array<any>>,
174-
examples?: Array<Array<any>>,
175155
items?: JsonSchema,
176156
maxContains?: number,
177157
maxItems?: number,
@@ -207,13 +187,9 @@ export function isArrayJsonSchema(schema: any): schema is ArrayJsonSchema {
207187
return false;
208188
}
209189

210-
export interface ObjectJsonSchema extends JsonSchema, ComposedJsonSchema<ObjectJsonSchema> {
190+
export interface ObjectJsonSchema extends JsonSchema<Record<string, any>>, ComposedJsonSchema<ObjectJsonSchema> {
211191
additionalProperties?: JsonSchema,
212-
const?: Record<string, any>,
213-
default?: Record<string, any>,
214192
dependentRequired?: Record<string, Array<string>>,
215-
enum?: Array<Record<string, any>>,
216-
examples?: Array<Record<string, any>>,
217193
maxProperties?: number,
218194
minProperties?: number,
219195
patternProperties?: { [propertyNameRegex: string]: JsonSchema },
@@ -250,12 +226,8 @@ export function isObjectJsonSchema(schema: any): schema is ObjectJsonSchema {
250226

251227
export interface AnyJsonSchema extends JsonSchema, ComposedJsonSchema {
252228
additionalProperties?: never,
253-
const?: any,
254229
contains?: never,
255-
default?: any,
256230
dependentRequired?: never,
257-
enum?: Array<any>,
258-
examples?: Array<any>,
259231
exclusiveMaximum?: never,
260232
exclusiveMinimum?: never,
261233
items?: never,

0 commit comments

Comments
 (0)