Skip to content

Commit fcc4818

Browse files
committed
Add ComposedJsonSchema
1 parent aa91133 commit fcc4818

File tree

2 files changed

+20
-57
lines changed

2 files changed

+20
-57
lines changed

src/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
export {
2-
Reference, JsonSchema, AnyJsonSchema, ObjectJsonSchema, ArrayJsonSchema, IntegerJsonSchema, NumberJsonSchema, StringJsonSchema, BooleanJsonSchema, NullJsonSchema,
3-
isReference, isAnyJsonSchema, isObjectJsonSchema, isArrayJsonSchema, isIntegerJsonSchema, isNumberJsonSchema, isStringJsonSchema, isBooleanJsonSchema, isNullJsonSchema,
4-
} from './jsonSchema';
1+
export * from './jsonSchema';

src/jsonSchema.ts

Lines changed: 19 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,22 @@ export interface JsonSchema {
1414
writeOnly?: boolean
1515
}
1616

17-
export interface NullJsonSchema extends JsonSchema {
18-
allOf?: Array<NullJsonSchema>,
19-
anyOf?: Array<NullJsonSchema>,
17+
export interface ComposedJsonSchema<T = JsonSchema> {
18+
allOf?: Array<T>,
19+
anyOf?: Array<T>,
20+
not?: T,
21+
oneOf?: Array<T>
22+
}
23+
24+
export function isComposedJsonSchema(schema: any): schema is ComposedJsonSchema {
25+
return Array.isArray(schema.allOf) || Array.isArray(schema.anyOf) || Array.isArray(schema.oneOf) || schema.not !== undefined;
26+
}
27+
28+
export interface NullJsonSchema extends JsonSchema, ComposedJsonSchema<NullJsonSchema> {
2029
const?: null,
2130
default?: null,
2231
enum?: Array<null>,
2332
examples?: Array<null>,
24-
not?: NullJsonSchema,
25-
oneOf?: Array<NullJsonSchema>,
2633
type?: 'null' | Array<'null' | 'boolean' | 'string' | 'integer' | 'number' | 'array' | 'object'>,
2734
}
2835

@@ -38,15 +45,11 @@ export function isNullJsonSchema(schema: any): schema is NullJsonSchema {
3845
return false;
3946
}
4047

41-
export interface BooleanJsonSchema extends JsonSchema {
42-
allOf?: Array<BooleanJsonSchema>,
43-
anyOf?: Array<BooleanJsonSchema>,
48+
export interface BooleanJsonSchema extends JsonSchema, ComposedJsonSchema<BooleanJsonSchema> {
4449
const?: boolean,
4550
default?: boolean,
4651
enum?: Array<boolean>,
4752
examples?: Array<boolean>,
48-
not?: BooleanJsonSchema,
49-
oneOf?: Array<BooleanJsonSchema>,
5053
type?: 'boolean' | Array<'null' | 'boolean' | 'string' | 'integer' | 'number' | 'array' | 'object'>,
5154
}
5255

@@ -62,17 +65,13 @@ export function isBooleanJsonSchema(schema: any): schema is BooleanJsonSchema {
6265
return false;
6366
}
6467

65-
export interface StringJsonSchema extends JsonSchema {
66-
allOf?: Array<StringJsonSchema>,
67-
anyOf?: Array<StringJsonSchema>,
68+
export interface StringJsonSchema extends JsonSchema, ComposedJsonSchema<StringJsonSchema> {
6869
const?: string,
6970
default?: string,
7071
enum?: Array<string>,
7172
examples?: Array<string>,
7273
maxLength?: number,
7374
minLength?: number,
74-
not?: StringJsonSchema,
75-
oneOf?: Array<StringJsonSchema>,
7675
pattern?: string,
7776
type?: 'string' | Array<'null' | 'boolean' | 'string' | 'integer' | 'number' | 'array' | 'object'>,
7877
}
@@ -97,9 +96,7 @@ export function isStringJsonSchema(schema: any): schema is StringJsonSchema {
9796
return false;
9897
}
9998

100-
export interface NumberJsonSchema extends JsonSchema {
101-
allOf?: Array<NumberJsonSchema>,
102-
anyOf?: Array<NumberJsonSchema>,
99+
export interface NumberJsonSchema extends JsonSchema, ComposedJsonSchema<NumberJsonSchema> {
103100
const?: number,
104101
default?: number,
105102
enum?: Array<number>,
@@ -109,8 +106,6 @@ export interface NumberJsonSchema extends JsonSchema {
109106
maximum?: number,
110107
minimum?: number,
111108
multipleOf?: number,
112-
not?: NumberJsonSchema,
113-
oneOf?: Array<NumberJsonSchema>,
114109
type?: 'number' | Array<'null' | 'boolean' | 'string' | 'integer' | 'number' | 'array' | 'object'>,
115110
}
116111

@@ -136,9 +131,7 @@ export function isNumberJsonSchema(schema: any): schema is NumberJsonSchema {
136131
return false;
137132
}
138133

139-
export interface IntegerJsonSchema extends JsonSchema {
140-
allOf?: Array<IntegerJsonSchema>,
141-
anyOf?: Array<IntegerJsonSchema>,
134+
export interface IntegerJsonSchema extends JsonSchema, ComposedJsonSchema<IntegerJsonSchema> {
142135
const?: number,
143136
default?: number,
144137
enum?: Array<number>,
@@ -148,8 +141,6 @@ export interface IntegerJsonSchema extends JsonSchema {
148141
maximum?: number,
149142
minimum?: number,
150143
multipleOf?: number,
151-
not?: IntegerJsonSchema,
152-
oneOf?: Array<IntegerJsonSchema>,
153144
type?: 'integer' | Array<'null' | 'boolean' | 'string' | 'integer' | 'number' | 'array' | 'object'>,
154145
}
155146

@@ -175,9 +166,7 @@ export function isIntegerJsonSchema(schema: any): schema is IntegerJsonSchema {
175166
return false;
176167
}
177168

178-
export interface ArrayJsonSchema extends JsonSchema {
179-
allOf?: Array<ArrayJsonSchema>,
180-
anyOf?: Array<ArrayJsonSchema>,
169+
export interface ArrayJsonSchema extends JsonSchema, ComposedJsonSchema<ArrayJsonSchema> {
181170
const?: Array<any>,
182171
contains?: JsonSchema,
183172
default?: Array<any>,
@@ -188,8 +177,6 @@ export interface ArrayJsonSchema extends JsonSchema {
188177
maxItems?: number,
189178
minContains?: number,
190179
minItems?: number,
191-
not?: ArrayJsonSchema,
192-
oneOf?: Array<ArrayJsonSchema>,
193180
prefixItems?: Array<JsonSchema>,
194181
type?: 'array' | Array<'null' | 'boolean' | 'string' | 'integer' | 'number' | 'array' | 'object'>,
195182
uniqueItems?: boolean,
@@ -220,25 +207,20 @@ export function isArrayJsonSchema(schema: any): schema is ArrayJsonSchema {
220207
return false;
221208
}
222209

223-
export interface ObjectJsonSchema extends JsonSchema {
210+
export interface ObjectJsonSchema extends JsonSchema, ComposedJsonSchema<ObjectJsonSchema> {
224211
additionalProperties?: JsonSchema,
225-
allOf?: Array<ObjectJsonSchema>,
226-
anyOf?: Array<ObjectJsonSchema>,
227212
const?: Record<string, any>,
228213
default?: Record<string, any>,
229214
dependentRequired?: Record<string, Array<string>>,
230215
enum?: Array<Record<string, any>>,
231216
examples?: Array<Record<string, any>>,
232217
maxProperties?: number,
233218
minProperties?: number,
234-
not?: ObjectJsonSchema,
235-
oneOf?: Array<ObjectJsonSchema>,
236219
patternProperties?: { [propertyNameRegex: string]: JsonSchema },
237220
properties?: { [propertyName: string]: JsonSchema },
238221
propertyNames?: JsonSchema,
239222
required?: Array<string>,
240223
type?: 'object' | Array<'null' | 'boolean' | 'string' | 'integer' | 'number' | 'array' | 'object'>,
241-
writeOnly?: boolean
242224
}
243225

244226
export function isObjectJsonSchema(schema: any): schema is ObjectJsonSchema {
@@ -266,10 +248,8 @@ export function isObjectJsonSchema(schema: any): schema is ObjectJsonSchema {
266248
return false;
267249
}
268250

269-
export interface AnyJsonSchema extends JsonSchema {
251+
export interface AnyJsonSchema extends JsonSchema, ComposedJsonSchema {
270252
additionalProperties?: never,
271-
allOf?: Array<JsonSchema>,
272-
anyOf?: Array<JsonSchema>,
273253
const?: any,
274254
contains?: never,
275255
default?: any,
@@ -290,8 +270,6 @@ export interface AnyJsonSchema extends JsonSchema {
290270
minProperties?: never,
291271
minimum?: never,
292272
multipleOf?: never,
293-
not?: JsonSchema,
294-
oneOf?: Array<JsonSchema>,
295273
pattern?: never,
296274
patternProperties?: never,
297275
prefixItems?: never,
@@ -300,16 +278,4 @@ export interface AnyJsonSchema extends JsonSchema {
300278
required?: never,
301279
type?: never,
302280
uniqueItems?: never,
303-
writeOnly?: boolean
304-
}
305-
306-
export function isAnyJsonSchema(schema: any): schema is AnyJsonSchema {
307-
return !isReference(schema)
308-
&& !isNullJsonSchema(schema)
309-
&& !isBooleanJsonSchema(schema)
310-
&& !isStringJsonSchema(schema)
311-
&& !isNumberJsonSchema(schema)
312-
&& !isIntegerJsonSchema(schema)
313-
&& !isArrayJsonSchema(schema)
314-
&& !isObjectJsonSchema(schema);
315281
}

0 commit comments

Comments
 (0)