Skip to content

Commit 014e415

Browse files
committed
chore: ensmarten types for example prop
1 parent dc80fe3 commit 014e415

1 file changed

Lines changed: 32 additions & 5 deletions

File tree

lib/schema.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
import { Construct } from 'constructs';
22
import type { oas31 } from 'openapi3-ts';
33

4-
interface SchemaOptions<T extends oas31.SchemaObject> {
5-
schema: T;
6-
}
4+
type InferExample<T> = T extends oas31.ReferenceObject
5+
? any
6+
: T extends {
7+
type: 'string';
8+
}
9+
? string
10+
: T extends {
11+
type: 'number' | 'integer';
12+
}
13+
? number
14+
: T extends {
15+
type: 'boolean';
16+
}
17+
? boolean
18+
: T extends oas31.SchemaObject & { type: 'object' }
19+
? {
20+
[K in keyof T['properties']]: InferExample<T['properties'][K]>;
21+
}
22+
: T extends oas31.SchemaObject & { type: 'array' }
23+
? InferExample<T['items']>[]
24+
: any;
25+
26+
export type SchemaOptions<T extends oas31.SchemaObject> = {
27+
schema: T & {
28+
examples?: InferExample<T>[];
29+
};
30+
};
731

832
export class Schema<
9-
T extends oas31.SchemaObject = oas31.SchemaObject,
33+
const T extends oas31.SchemaObject = oas31.SchemaObject,
1034
> extends Construct {
1135
private options: SchemaOptions<T>;
1236

@@ -40,6 +64,9 @@ export class Schema<
4064
}
4165

4266
public synth() {
43-
return this.options.schema;
67+
return {
68+
additionalProperties: false,
69+
...this.options.schema,
70+
};
4471
}
4572
}

0 commit comments

Comments
 (0)