From ed787c015d5c193e9e4b0d9a5a0e90b7224b14a0 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Wed, 20 Jul 2022 14:54:26 +0200 Subject: [PATCH] Feat: add validator tests and fix some errors --- .../openapi/spec/playground-feature-schema.ts | 8 +++---- .../openapi/spec/playground-request-schema.ts | 4 ++-- src/lib/openapi/spec/sdk-context-schema.ts | 22 +++++++++---------- src/test/e2e/api/openapi/openapi.e2e.test.ts | 19 +++++++++++++--- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/lib/openapi/spec/playground-feature-schema.ts b/src/lib/openapi/spec/playground-feature-schema.ts index d65004ad2f..f658534670 100644 --- a/src/lib/openapi/spec/playground-feature-schema.ts +++ b/src/lib/openapi/spec/playground-feature-schema.ts @@ -10,9 +10,9 @@ export const playgroundFeatureSchema = { additionalProperties: false, required: ['name', 'projectId', 'isEnabled', 'variant', 'variants'], properties: { - name: { type: 'string', examples: ['my-feature'] }, - projectId: { type: 'string', examples: ['my-project'] }, - isEnabled: { type: 'boolean', examples: [true] }, + name: { type: 'string', example: 'my-feature' }, + projectId: { type: 'string', example: 'my-project' }, + isEnabled: { type: 'boolean', example: true }, variant: { type: 'object', additionalProperties: false, @@ -34,7 +34,7 @@ export const playgroundFeatureSchema = { }, }, nullable: true, - examples: ['green'], + example: { name: 'green', enabled: true }, }, variants: { type: 'array', items: { $ref: variantSchema.$id } }, }, diff --git a/src/lib/openapi/spec/playground-request-schema.ts b/src/lib/openapi/spec/playground-request-schema.ts index def85ca996..4e61b74ea9 100644 --- a/src/lib/openapi/spec/playground-request-schema.ts +++ b/src/lib/openapi/spec/playground-request-schema.ts @@ -8,13 +8,13 @@ export const playgroundRequestSchema = { type: 'object', required: ['environment', 'context'], properties: { - environment: { type: 'string', examples: ['development'] }, + environment: { type: 'string', example: 'development' }, projects: { oneOf: [ { type: 'array', items: { type: 'string' }, - examples: ['my-project', 'my-other-project'], + example: ['my-project'], description: 'A list of projects to check for toggles in.', }, { diff --git a/src/lib/openapi/spec/sdk-context-schema.ts b/src/lib/openapi/spec/sdk-context-schema.ts index de390df859..49ac25666f 100644 --- a/src/lib/openapi/spec/sdk-context-schema.ts +++ b/src/lib/openapi/spec/sdk-context-schema.ts @@ -6,40 +6,38 @@ export const sdkContextSchema = { type: 'object', additionalProperties: { type: 'string', - examples: ['top-level custom context value'], + example: 'top-level custom context value', }, required: ['appName'], properties: { appName: { type: 'string', minLength: 1, - examples: ['My cool application.'], + example: 'My cool application.', }, currentTime: { type: 'string', format: 'date-time', - examples: ['2022-07-05T12:56:41+02:00'], + example: '2022-07-05T12:56:41+02:00', }, environment: { type: 'string', deprecated: true }, properties: { type: 'object', additionalProperties: { type: 'string' }, - examples: [ - { - customContextField: 'this is one!', - otherCustomField: 3, - }, - ], + example: { + customContextField: 'this is one!', + otherCustomField: '3', + }, }, remoteAddress: { type: 'string', - examples: ['192.168.1.1'], + example: '192.168.1.1', }, sessionId: { type: 'string', - examples: ['b65e7b23-fec0-4814-a129-0e9861ef18fc'], + example: 'b65e7b23-fec0-4814-a129-0e9861ef18fc', }, - userId: { type: 'string', examples: ['username@provider.com'] }, + userId: { type: 'string', example: 'username@provider.com' }, }, components: {}, } as const; diff --git a/src/test/e2e/api/openapi/openapi.e2e.test.ts b/src/test/e2e/api/openapi/openapi.e2e.test.ts index a3440668f7..a38012016a 100644 --- a/src/test/e2e/api/openapi/openapi.e2e.test.ts +++ b/src/test/e2e/api/openapi/openapi.e2e.test.ts @@ -38,19 +38,32 @@ test('should serve the OpenAPI spec', async () => { }); }); +// test('the generated OpenAPI should not have any warnings', async () => { +// const { body } = await app.request +// .get('/docs/openapi.json') +// .expect('Content-Type', /json/) +// .expect(200); + +// const [_openapi, _error, warning] = await enforcer(body, { +// fullResult: true, +// }); + +// if (warning !== undefined) console.warn(warning); + +// expect(warning).toBeFalsy(); +// }); + test('the generated OpenAPI spec is valid', async () => { const { body } = await app.request .get('/docs/openapi.json') .expect('Content-Type', /json/) .expect(200); - const [openapi, error, warning] = await enforcer(body, { + const [openapi, error] = await enforcer(body, { fullResult: true, }); if (error !== undefined) console.error(error); - if (warning !== undefined) console.warn(warning); - if (openapi !== undefined) console.log('Document is valid'); expect(openapi).toBeTruthy(); });