diff --git a/src/lib/openapi/index.test.ts b/src/lib/openapi/index.test.ts index f30709d0f3..37a1659ec0 100644 --- a/src/lib/openapi/index.test.ts +++ b/src/lib/openapi/index.test.ts @@ -3,7 +3,34 @@ import { createRequestSchema, createResponseSchema, removeJsonSchemaProps, + schemas, } from './index'; +import fs from 'fs'; +import path from 'path'; + +test('all schema files should be added to the schemas object', () => { + const schemaFileNames = fs + .readdirSync(path.join(__dirname, 'spec')) + .filter((fileName) => fileName.endsWith('-schema.ts')); + + const expectedSchemaNames = schemaFileNames.map((fileName) => { + return fileName + .replace(/\.ts$/, '') + .replace(/-./g, (x) => x[1].toUpperCase()); + }); + + const addedSchemaNames = Object.keys(schemas); + expect(expectedSchemaNames.sort()).toEqual(addedSchemaNames.sort()); +}); + +test('all schema $id attributes should have the expected format', () => { + const schemaIds = Object.values(schemas).map((schema) => schema.$id); + const schemaIdRegExp = new RegExp(`^#/components/schemas/[a-z][a-zA-Z]+$`); + + schemaIds.forEach((schemaId) => { + expect(schemaId).toMatch(schemaIdRegExp); + }); +}); test('createRequestSchema', () => { expect(createRequestSchema('schemaName')).toMatchInlineSnapshot(` diff --git a/src/lib/openapi/index.ts b/src/lib/openapi/index.ts index 59f0e20be2..3a15694692 100644 --- a/src/lib/openapi/index.ts +++ b/src/lib/openapi/index.ts @@ -4,6 +4,7 @@ import { constraintSchema } from './spec/constraint-schema'; import { createFeatureSchema } from './spec/create-feature-schema'; import { createStrategySchema } from './spec/create-strategy-schema'; import { environmentSchema } from './spec/environment-schema'; +import { environmentsSchema } from './spec/environments-schema'; import { featureEnvironmentSchema } from './spec/feature-environment-schema'; import { featureSchema } from './spec/feature-schema'; import { featureStrategySchema } from './spec/feature-strategy-schema'; @@ -22,6 +23,8 @@ import { patchesSchema } from './spec/patches-schema'; import { projectEnvironmentSchema } from './spec/project-environment-schema'; import { projectSchema } from './spec/project-schema'; import { projectsSchema } from './spec/projects-schema'; +import { sortOrderSchema } from './spec/sort-order-schema'; +import { splashSchema } from './spec/splash-schema'; import { strategySchema } from './spec/strategy-schema'; import { tagSchema } from './spec/tag-schema'; import { tagsSchema } from './spec/tags-schema'; @@ -31,34 +34,8 @@ import { updateStrategySchema } from './spec/update-strategy-schema'; import { variantSchema } from './spec/variant-schema'; import { variantsSchema } from './spec/variants-schema'; import { versionSchema } from './spec/version-schema'; -import { environmentsSchema } from './spec/environments-schema'; -import { sortOrderSchema } from './spec/sort-order-schema'; -import { splashSchema } from './spec/splash-schema'; - -// Schemas must have $id property on the form "#/components/schemas/mySchema". -export type SchemaId = typeof schemas[keyof typeof schemas]['$id']; - -// Schemas must list all $ref schemas in "components", including nested schemas. -export type SchemaRef = typeof schemas[keyof typeof schemas]['components']; - -// JSON schema properties that should not be included in the OpenAPI spec. -export interface JsonSchemaProps { - $id: string; - components: object; -} - -export interface AdminApiOperation - extends Omit { - operationId: string; - tags: ['admin']; -} - -export interface ClientApiOperation - extends Omit { - operationId: string; - tags: ['client']; -} +// All schemas in `openapi/spec` should be listed here. export const schemas = { cloneFeatureSchema, constraintSchema, @@ -95,6 +72,30 @@ export const schemas = { versionSchema, }; +// Schemas must have an $id property on the form "#/components/schemas/mySchema". +export type SchemaId = typeof schemas[keyof typeof schemas]['$id']; + +// Schemas must list all their $refs in `components`, including nested schemas. +export type SchemaRef = typeof schemas[keyof typeof schemas]['components']; + +// JSON schema properties that should not be included in the OpenAPI spec. +export interface JsonSchemaProps { + $id: string; + components: object; +} + +export interface AdminApiOperation + extends Omit { + operationId: string; + tags: ['admin']; +} + +export interface ClientApiOperation + extends Omit { + operationId: string; + tags: ['client']; +} + export const createRequestSchema = ( schemaName: string, ): OpenAPIV3.RequestBodyObject => {