Skip to content

Export all "schema/components" as componentSchemas #4

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

Merged
merged 3 commits into from
Oct 6, 2024
Merged
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
@@ -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.
@@ -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,
]);
}
105 changes: 105 additions & 0 deletions src/tests/__snapshots__/compileValueSchema.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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*/
@@ -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*/
@@ -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*/
@@ -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*/
@@ -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*/
@@ -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*/
@@ -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*/
@@ -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*/
@@ -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*/
@@ -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*/
@@ -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*/
@@ -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*/
@@ -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*/
@@ -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*/
@@ -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*/
@@ -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*/
@@ -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*/
@@ -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*/
@@ -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*/
@@ -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*/
@@ -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*/
16 changes: 16 additions & 0 deletions src/tests/__snapshots__/compiler.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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*/
@@ -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*/
2 changes: 0 additions & 2 deletions src/tests/compiler.test.ts
Original file line number Diff line number Diff line change
@@ -22,7 +22,6 @@ test('components ref', () => {
},
},
});
compiler.indexAllComponents();
expect(compiler.compile()).toMatchSnapshot();
});

@@ -50,6 +49,5 @@ test('recursive refs', () => {
},
},
});
compiler.indexAllComponents();
expect(compiler.compile()).toMatchSnapshot();
});
29 changes: 27 additions & 2 deletions tests/gitbook.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'bun:test';
import { validateRequest, ValidationError } from './gitbook.validate';
import { describe, expect, test } from 'bun:test';
import { validateRequest, ValidationError, componentSchemas } from './gitbook.validate';

test('POST orgs/appleId/custom-fields', () => {
const result = validateRequest({
@@ -287,3 +287,28 @@ test('/orgs/xxx/synced-blocks?ids[]=foo allow string parameter as array too', ()
});
expect(result.query).toEqual({ ids: ['foo'] });
});

describe('componentSchemas', () => {
test('should export a function to validate a component', () => {
const validate = componentSchemas['ApiInformation'];
expect(validate).toBeInstanceOf(Function);
expect(
validate([], {
version: '1.0.0',
build: '123',
}),
).toEqual({
version: '1.0.0',
build: '123',
});

const error = validate([], {
version: '1.0.0',
// Missing property
});
expect(error instanceof ValidationError ? error.path : null).toEqual([]);
expect(error instanceof ValidationError ? error.message : null).toEqual(
'expected "build" to be defined',
);
});
});