Skip to content

Commit 4d39e44

Browse files
authored
Export all "schema/components" as componentSchemas (#4)
* Export all componentSchemas * Format * Improve type
1 parent 7ff89f7 commit 4d39e44

6 files changed

+192
-15
lines changed

src/compileComponentSchemas.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { builders } from 'ast-types';
2+
import { Compiler } from './compiler';
3+
import { compileValueSchema } from './compileValueSchema';
4+
import { OpenAPIValueSchema } from './types';
5+
import { annotateWithJSDocComment } from './comments';
6+
7+
const COMMENT = `
8+
Map of all components defined in the spec to their validation functions.
9+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
10+
`;
11+
12+
/**
13+
* Compile all component schemas to be expoerted as `components['Name']`.
14+
*/
15+
export function compileComponentSchemas(
16+
compiler: Compiler,
17+
schemas: {
18+
[key: string]: OpenAPIValueSchema;
19+
},
20+
) {
21+
const properties = Object.entries(schemas).map(([name]) => {
22+
return builders.property(
23+
'init',
24+
builders.literal(name),
25+
compileValueSchema(compiler, schemas[name]),
26+
);
27+
});
28+
29+
return [
30+
annotateWithJSDocComment(
31+
builders.exportNamedDeclaration(
32+
builders.variableDeclaration('const', [
33+
builders.variableDeclarator(
34+
builders.identifier('componentSchemas'),
35+
builders.objectExpression(properties),
36+
),
37+
]),
38+
),
39+
COMMENT,
40+
),
41+
];
42+
}

src/compiler.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { OpenAPIRef, OpenAPISpec } from './types';
55
import { compileValueSchema } from './compileValueSchema';
66
import { hash } from './hash';
77
import { compileValidateRequest } from './compileValidateRequest';
8+
import { compileComponentSchemas } from './compileComponentSchemas';
89

910
/**
1011
* Compiler for OpenAPI specs.
@@ -117,23 +118,13 @@ export class Compiler {
117118
});
118119
}
119120

120-
/**
121-
* Build the AST from the entire spec.
122-
*/
123-
public indexAllComponents() {
124-
// Index all the schema components.
125-
const schemas = this.input.components?.schemas ?? {};
126-
Object.values(schemas).forEach((schema) => {
127-
compileValueSchema(this, schema);
128-
});
129-
}
130-
131121
/**
132122
* Return the AST for the program.
133123
*/
134124
public ast() {
135125
return builders.program([
136126
...compileValidateRequest(this, this.input),
127+
...compileComponentSchemas(this, this.input.components?.schemas ?? {}),
137128
...this.globalDeclarations,
138129
]);
139130
}

src/tests/__snapshots__/compileValueSchema.test.ts.snap

+105
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ Validate a request against the OpenAPI spec
1010
export function validateRequest(request, context) {
1111
return new RequestError(404, 'no operation match path');
1212
}
13+
/**
14+
Map of all components defined in the spec to their validation functions.
15+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
16+
*/
17+
export const componentSchemas = {};
1318
export class RequestError extends Error {
1419
/** @param {number} code HTTP code for the error
1520
@param {string} message The error message*/
@@ -49,6 +54,11 @@ Validate a request against the OpenAPI spec
4954
export function validateRequest(request, context) {
5055
return new RequestError(404, 'no operation match path');
5156
}
57+
/**
58+
Map of all components defined in the spec to their validation functions.
59+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
60+
*/
61+
export const componentSchemas = {};
5262
export class RequestError extends Error {
5363
/** @param {number} code HTTP code for the error
5464
@param {string} message The error message*/
@@ -91,6 +101,11 @@ Validate a request against the OpenAPI spec
91101
export function validateRequest(request, context) {
92102
return new RequestError(404, 'no operation match path');
93103
}
104+
/**
105+
Map of all components defined in the spec to their validation functions.
106+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
107+
*/
108+
export const componentSchemas = {};
94109
export class RequestError extends Error {
95110
/** @param {number} code HTTP code for the error
96111
@param {string} message The error message*/
@@ -133,6 +148,11 @@ Validate a request against the OpenAPI spec
133148
export function validateRequest(request, context) {
134149
return new RequestError(404, 'no operation match path');
135150
}
151+
/**
152+
Map of all components defined in the spec to their validation functions.
153+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
154+
*/
155+
export const componentSchemas = {};
136156
export class RequestError extends Error {
137157
/** @param {number} code HTTP code for the error
138158
@param {string} message The error message*/
@@ -175,6 +195,11 @@ Validate a request against the OpenAPI spec
175195
export function validateRequest(request, context) {
176196
return new RequestError(404, 'no operation match path');
177197
}
198+
/**
199+
Map of all components defined in the spec to their validation functions.
200+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
201+
*/
202+
export const componentSchemas = {};
178203
export class RequestError extends Error {
179204
/** @param {number} code HTTP code for the error
180205
@param {string} message The error message*/
@@ -217,6 +242,11 @@ Validate a request against the OpenAPI spec
217242
export function validateRequest(request, context) {
218243
return new RequestError(404, 'no operation match path');
219244
}
245+
/**
246+
Map of all components defined in the spec to their validation functions.
247+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
248+
*/
249+
export const componentSchemas = {};
220250
export class RequestError extends Error {
221251
/** @param {number} code HTTP code for the error
222252
@param {string} message The error message*/
@@ -256,6 +286,11 @@ Validate a request against the OpenAPI spec
256286
export function validateRequest(request, context) {
257287
return new RequestError(404, 'no operation match path');
258288
}
289+
/**
290+
Map of all components defined in the spec to their validation functions.
291+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
292+
*/
293+
export const componentSchemas = {};
259294
export class RequestError extends Error {
260295
/** @param {number} code HTTP code for the error
261296
@param {string} message The error message*/
@@ -298,6 +333,11 @@ Validate a request against the OpenAPI spec
298333
export function validateRequest(request, context) {
299334
return new RequestError(404, 'no operation match path');
300335
}
336+
/**
337+
Map of all components defined in the spec to their validation functions.
338+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
339+
*/
340+
export const componentSchemas = {};
301341
export class RequestError extends Error {
302342
/** @param {number} code HTTP code for the error
303343
@param {string} message The error message*/
@@ -334,6 +374,11 @@ Validate a request against the OpenAPI spec
334374
export function validateRequest(request, context) {
335375
return new RequestError(404, 'no operation match path');
336376
}
377+
/**
378+
Map of all components defined in the spec to their validation functions.
379+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
380+
*/
381+
export const componentSchemas = {};
337382
export class RequestError extends Error {
338383
/** @param {number} code HTTP code for the error
339384
@param {string} message The error message*/
@@ -374,6 +419,11 @@ Validate a request against the OpenAPI spec
374419
export function validateRequest(request, context) {
375420
return new RequestError(404, 'no operation match path');
376421
}
422+
/**
423+
Map of all components defined in the spec to their validation functions.
424+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
425+
*/
426+
export const componentSchemas = {};
377427
export class RequestError extends Error {
378428
/** @param {number} code HTTP code for the error
379429
@param {string} message The error message*/
@@ -414,6 +464,11 @@ Validate a request against the OpenAPI spec
414464
export function validateRequest(request, context) {
415465
return new RequestError(404, 'no operation match path');
416466
}
467+
/**
468+
Map of all components defined in the spec to their validation functions.
469+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
470+
*/
471+
export const componentSchemas = {};
417472
export class RequestError extends Error {
418473
/** @param {number} code HTTP code for the error
419474
@param {string} message The error message*/
@@ -492,6 +547,11 @@ Validate a request against the OpenAPI spec
492547
export function validateRequest(request, context) {
493548
return new RequestError(404, 'no operation match path');
494549
}
550+
/**
551+
Map of all components defined in the spec to their validation functions.
552+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
553+
*/
554+
export const componentSchemas = {};
495555
export class RequestError extends Error {
496556
/** @param {number} code HTTP code for the error
497557
@param {string} message The error message*/
@@ -552,6 +612,11 @@ Validate a request against the OpenAPI spec
552612
export function validateRequest(request, context) {
553613
return new RequestError(404, 'no operation match path');
554614
}
615+
/**
616+
Map of all components defined in the spec to their validation functions.
617+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
618+
*/
619+
export const componentSchemas = {};
555620
export class RequestError extends Error {
556621
/** @param {number} code HTTP code for the error
557622
@param {string} message The error message*/
@@ -589,6 +654,11 @@ Validate a request against the OpenAPI spec
589654
export function validateRequest(request, context) {
590655
return new RequestError(404, 'no operation match path');
591656
}
657+
/**
658+
Map of all components defined in the spec to their validation functions.
659+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
660+
*/
661+
export const componentSchemas = {};
592662
export class RequestError extends Error {
593663
/** @param {number} code HTTP code for the error
594664
@param {string} message The error message*/
@@ -649,6 +719,11 @@ Validate a request against the OpenAPI spec
649719
export function validateRequest(request, context) {
650720
return new RequestError(404, 'no operation match path');
651721
}
722+
/**
723+
Map of all components defined in the spec to their validation functions.
724+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
725+
*/
726+
export const componentSchemas = {};
652727
export class RequestError extends Error {
653728
/** @param {number} code HTTP code for the error
654729
@param {string} message The error message*/
@@ -725,6 +800,11 @@ Validate a request against the OpenAPI spec
725800
export function validateRequest(request, context) {
726801
return new RequestError(404, 'no operation match path');
727802
}
803+
/**
804+
Map of all components defined in the spec to their validation functions.
805+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
806+
*/
807+
export const componentSchemas = {};
728808
export class RequestError extends Error {
729809
/** @param {number} code HTTP code for the error
730810
@param {string} message The error message*/
@@ -768,6 +848,11 @@ Validate a request against the OpenAPI spec
768848
export function validateRequest(request, context) {
769849
return new RequestError(404, 'no operation match path');
770850
}
851+
/**
852+
Map of all components defined in the spec to their validation functions.
853+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
854+
*/
855+
export const componentSchemas = {};
771856
export class RequestError extends Error {
772857
/** @param {number} code HTTP code for the error
773858
@param {string} message The error message*/
@@ -826,6 +911,11 @@ Validate a request against the OpenAPI spec
826911
export function validateRequest(request, context) {
827912
return new RequestError(404, 'no operation match path');
828913
}
914+
/**
915+
Map of all components defined in the spec to their validation functions.
916+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
917+
*/
918+
export const componentSchemas = {};
829919
export class RequestError extends Error {
830920
/** @param {number} code HTTP code for the error
831921
@param {string} message The error message*/
@@ -883,6 +973,11 @@ Validate a request against the OpenAPI spec
883973
export function validateRequest(request, context) {
884974
return new RequestError(404, 'no operation match path');
885975
}
976+
/**
977+
Map of all components defined in the spec to their validation functions.
978+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
979+
*/
980+
export const componentSchemas = {};
886981
export class RequestError extends Error {
887982
/** @param {number} code HTTP code for the error
888983
@param {string} message The error message*/
@@ -939,6 +1034,11 @@ Validate a request against the OpenAPI spec
9391034
export function validateRequest(request, context) {
9401035
return new RequestError(404, 'no operation match path');
9411036
}
1037+
/**
1038+
Map of all components defined in the spec to their validation functions.
1039+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
1040+
*/
1041+
export const componentSchemas = {};
9421042
export class RequestError extends Error {
9431043
/** @param {number} code HTTP code for the error
9441044
@param {string} message The error message*/
@@ -1002,6 +1102,11 @@ Validate a request against the OpenAPI spec
10021102
export function validateRequest(request, context) {
10031103
return new RequestError(404, 'no operation match path');
10041104
}
1105+
/**
1106+
Map of all components defined in the spec to their validation functions.
1107+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
1108+
*/
1109+
export const componentSchemas = {};
10051110
export class RequestError extends Error {
10061111
/** @param {number} code HTTP code for the error
10071112
@param {string} message The error message*/

src/tests/__snapshots__/compiler.test.ts.snap

+16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ Validate a request against the OpenAPI spec
1010
export function validateRequest(request, context) {
1111
return new RequestError(404, 'no operation match path');
1212
}
13+
/**
14+
Map of all components defined in the spec to their validation functions.
15+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
16+
*/
17+
export const componentSchemas = {
18+
'A': obj0,
19+
'B': obj1
20+
};
1321
export class RequestError extends Error {
1422
/** @param {number} code HTTP code for the error
1523
@param {string} message The error message*/
@@ -86,6 +94,14 @@ Validate a request against the OpenAPI spec
8694
export function validateRequest(request, context) {
8795
return new RequestError(404, 'no operation match path');
8896
}
97+
/**
98+
Map of all components defined in the spec to their validation functions.
99+
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
100+
*/
101+
export const componentSchemas = {
102+
'A': obj0,
103+
'B': obj1
104+
};
89105
export class RequestError extends Error {
90106
/** @param {number} code HTTP code for the error
91107
@param {string} message The error message*/

src/tests/compiler.test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ test('components ref', () => {
2222
},
2323
},
2424
});
25-
compiler.indexAllComponents();
2625
expect(compiler.compile()).toMatchSnapshot();
2726
});
2827

@@ -50,6 +49,5 @@ test('recursive refs', () => {
5049
},
5150
},
5251
});
53-
compiler.indexAllComponents();
5452
expect(compiler.compile()).toMatchSnapshot();
5553
});

0 commit comments

Comments
 (0)