Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
SamyPesse committed Oct 6, 2024
1 parent 4860c2e commit 3dc7ea3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
27 changes: 14 additions & 13 deletions src/compileComponentSchemas.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { builders } from "ast-types";
import { Compiler } from "./compiler";
import { compileValueSchema } from "./compileValueSchema";
import { OpenAPIValueSchema } from "./types";
import { annotateWithJSDocComment } from "./comments";
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.
Expand All @@ -11,9 +11,12 @@ Map of all components defined in the spec to their validation functions.
/**
* Compile all component schemas to be expoerted as `components['Name']`.
*/
export function compileComponentSchemas(compiler: Compiler, schemas: {
[key: string]: OpenAPIValueSchema;
}) {
export function compileComponentSchemas(
compiler: Compiler,
schemas: {
[key: string]: OpenAPIValueSchema;
},
) {
const properties = Object.entries(schemas).map(([name]) => {
return builders.property(
'init',
Expand All @@ -28,13 +31,11 @@ export function compileComponentSchemas(compiler: Compiler, schemas: {
builders.variableDeclaration('const', [
builders.variableDeclarator(
builders.identifier('componentSchemas'),
builders.objectExpression(
properties,
),
)
builders.objectExpression(properties),
),
]),
),
COMMENT,
),
];
}
}
16 changes: 10 additions & 6 deletions tests/gitbook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,19 +292,23 @@ 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({
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');
expect(error instanceof ValidationError ? error.message : null).toEqual(
'expected "build" to be defined',
);
});
});

0 comments on commit 3dc7ea3

Please sign in to comment.