fork of hey-api/openapi-ts because I need some additional things
1import type { Logger } from '@hey-api/codegen-core';
2
3import { createOperationKey } from '../../../ir/operation';
4import { httpMethods } from '../../../openApi/shared/utils/operation';
5import type { ValidatorIssue, ValidatorResult } from '../../../openApi/shared/utils/validator';
6import type { OpenApiV2_0_X, PathItemObject, PathsObject } from '../types/spec';
7
8export const validateOpenApiSpec = (spec: OpenApiV2_0_X, logger: Logger): ValidatorResult => {
9 const eventValidate = logger.timeEvent('validate');
10 const issues: Array<ValidatorIssue> = [];
11 const operationIds = new Map();
12
13 if (spec.paths) {
14 for (const entry of Object.entries(spec.paths)) {
15 const path = entry[0] as keyof PathsObject;
16 const pathItem = entry[1] as PathItemObject;
17 for (const method of httpMethods) {
18 if (method === 'trace') {
19 continue;
20 }
21
22 const operation = pathItem[method];
23 if (!operation) {
24 continue;
25 }
26
27 const operationKey = createOperationKey({ method, path });
28
29 if (operation.operationId) {
30 if (!operationIds.has(operation.operationId)) {
31 operationIds.set(operation.operationId, operationKey);
32 } else {
33 issues.push({
34 code: 'duplicate_key',
35 context: {
36 key: 'operationId',
37 value: operation.operationId,
38 },
39 message: 'Duplicate `operationId` found. Each `operationId` must be unique.',
40 path: ['paths', path, method, 'operationId'],
41 severity: 'error',
42 });
43 }
44 }
45 }
46 }
47 }
48
49 eventValidate.timeEnd();
50 return {
51 issues,
52 valid: !issues.some((issue) => issue.severity === 'error'),
53 };
54};