Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export all "schema/components" as componentSchemas #4

Merged
merged 3 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/compileComponentSchemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { builders } from 'ast-types';
import { Compiler } from './compiler';
import { compileValueSchema } from './compileValueSchema';
import { OpenAPIValueSchema } from './types';
import { annotateWithJSDocComment } from './comments';

const COMMENT = `
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
`;

/**
* Compile all component schemas to be expoerted as `components['Name']`.
*/
export function compileComponentSchemas(
compiler: Compiler,
schemas: {
[key: string]: OpenAPIValueSchema;
},
) {
const properties = Object.entries(schemas).map(([name]) => {
return builders.property(
'init',
builders.literal(name),
compileValueSchema(compiler, schemas[name]),
);
});

return [
annotateWithJSDocComment(
builders.exportNamedDeclaration(
builders.variableDeclaration('const', [
builders.variableDeclarator(
builders.identifier('componentSchemas'),
builders.objectExpression(properties),
),
]),
),
COMMENT,
),
];
}
13 changes: 2 additions & 11 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { OpenAPIRef, OpenAPISpec } from './types';
import { compileValueSchema } from './compileValueSchema';
import { hash } from './hash';
import { compileValidateRequest } from './compileValidateRequest';
import { compileComponentSchemas } from './compileComponentSchemas';

/**
* Compiler for OpenAPI specs.
Expand Down Expand Up @@ -117,23 +118,13 @@ export class Compiler {
});
}

/**
* Build the AST from the entire spec.
*/
public indexAllComponents() {
// Index all the schema components.
const schemas = this.input.components?.schemas ?? {};
Object.values(schemas).forEach((schema) => {
compileValueSchema(this, schema);
});
}

/**
* Return the AST for the program.
*/
public ast() {
return builders.program([
...compileValidateRequest(this, this.input),
...compileComponentSchemas(this, this.input.components?.schemas ?? {}),
...this.globalDeclarations,
]);
}
Expand Down
105 changes: 105 additions & 0 deletions src/tests/__snapshots__/compileValueSchema.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -49,6 +54,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -91,6 +101,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -133,6 +148,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -175,6 +195,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -217,6 +242,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -256,6 +286,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -298,6 +333,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -334,6 +374,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -374,6 +419,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -414,6 +464,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -492,6 +547,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -552,6 +612,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -589,6 +654,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -649,6 +719,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -725,6 +800,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -768,6 +848,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -826,6 +911,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -883,6 +973,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -939,6 +1034,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -1002,6 +1102,11 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down
16 changes: 16 additions & 0 deletions src/tests/__snapshots__/compiler.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {
'A': obj0,
'B': obj1
};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down Expand Up @@ -86,6 +94,14 @@ Validate a request against the OpenAPI spec
export function validateRequest(request, context) {
return new RequestError(404, 'no operation match path');
}
/**
Map of all components defined in the spec to their validation functions.
{Object.<string, <T>(path: string[], value: T, context: any) => (T | ValidationError)>}
*/
export const componentSchemas = {
'A': obj0,
'B': obj1
};
export class RequestError extends Error {
/** @param {number} code HTTP code for the error
@param {string} message The error message*/
Expand Down
2 changes: 0 additions & 2 deletions src/tests/compiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ test('components ref', () => {
},
},
});
compiler.indexAllComponents();
expect(compiler.compile()).toMatchSnapshot();
});

Expand Down Expand Up @@ -50,6 +49,5 @@ test('recursive refs', () => {
},
},
});
compiler.indexAllComponents();
expect(compiler.compile()).toMatchSnapshot();
});
Loading
Loading