Skip to content

Commit

Permalink
fix types and better colocation
Browse files Browse the repository at this point in the history
  • Loading branch information
jloleysens committed Jun 28, 2024
1 parent 75ee9f9 commit d2ccdb1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* Side Public License, v 1.
*/

import { z } from 'zod';
import { schema } from '@kbn/config-schema';

export const sharedOas = {
components: {
schemas: {},
Expand Down Expand Up @@ -415,3 +418,43 @@ export const sharedOas = {
},
],
};

export function createSharedConfigSchema() {
return schema.object({
string: schema.string({ maxLength: 10, minLength: 1 }),
maybeNumber: schema.maybe(schema.number({ max: 1000, min: 1 })),
booleanDefault: schema.boolean({
defaultValue: true,
meta: {
description: 'defaults to to true',
},
}),
ipType: schema.ip({ versions: ['ipv4'] }),
literalType: schema.literal('literallythis'),
record: schema.recordOf(schema.string(), schema.string()),
union: schema.oneOf([
schema.string({ maxLength: 1, meta: { description: 'Union string' } }),
schema.number({ min: 0, meta: { description: 'Union number' } }),
]),
uri: schema.uri({
scheme: ['prototest'],
defaultValue: () => 'prototest://something',
}),
});
}

export function createZodSharedSchema() {
return z.object({
string: z.string().max(10).min(1),
maybeNumber: z.number().max(1000).min(1).optional(),
booleanDefault: z.boolean({ description: 'defaults to to true' }).default(true),
ipType: z.string().ip({ version: 'v4' }),
literalType: z.literal('literallythis'),
record: z.record(z.string(), z.string()),
union: z.union([
z.string({ description: 'Union string' }).max(1),
z.number({ description: 'Union number' }).min(0),
]),
uri: z.string().url().default('prototest://something'),
});
}
10 changes: 6 additions & 4 deletions packages/kbn-router-to-openapispec/src/generate_oas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import { z } from 'zod';
import { schema, Type } from '@kbn/config-schema';
import { generateOpenApiDocument } from './generate_oas';
import { createSharedSchema as createConfigSchemaSharedSchema } from './oas_converter/kbn_config_schema/lib.test.util';
import { createSharedSchema as createZodSharedSchema } from './oas_converter/zod/lib.test.util';
import { createTestRouters, createRouter, createVersionedRouter } from './generate_oas.test.util';
import { sharedOas } from './generate_oas.test.shared.fixture';
import {
sharedOas,
createZodSharedSchema,
createSharedConfigSchema,
} from './generate_oas.test.shared.fixture';

interface RecursiveType {
name: string;
Expand All @@ -31,7 +33,7 @@ describe('generateOpenApiDocument', () => {
const [routers, versionedRouters] = createTestRouters({
routers: { testRouter: { routes: [{ method: 'get' }, { method: 'post' }] } },
versionedRouters: { testVersionedRouter: { routes: [{}] } },
bodySchema: createConfigSchemaSharedSchema(),
bodySchema: createSharedConfigSchema(),
});
expect(
generateOpenApiDocument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const getRouterDefaults = (bodySchema?: RuntimeSchema) => ({
handler: jest.fn(),
});

export const getVersionedRouterDefaults = (bodySchema?: any) => ({
export const getVersionedRouterDefaults = (bodySchema?: RuntimeSchema) => ({
method: 'get',
path: '/bar',
options: {
Expand Down

0 comments on commit d2ccdb1

Please sign in to comment.