persona-community-5/.pnpm-store/v3/files/00/1185deaed098d29d56a0968cf5cb9a8262685bb00c8da5fe614bbe9d27c4fa186cc87c0c3ced2533f87e5586b5c4f46c789869a2175c6b83e9bd72896d119d
rdev-worker a1d0d1bf1c
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
build: /implement-feature community-ui --requirements 'Build the React commu...
2026-02-24 08:22:30 +00:00

51 lines
1.5 KiB
Plaintext

import type { Oas3Rule, Oas2Rule } from '../../visitors';
import type { UserContext } from '../../walk';
import type { Oas3Paths } from '../../typings/openapi';
import type { Oas2Paths } from '../../typings/swagger';
export const NoAmbiguousPaths: Oas3Rule | Oas2Rule = () => {
return {
Paths(pathMap: Oas3Paths | Oas2Paths, { report, location }: UserContext) {
const seenPaths: string[] = [];
for (const currentPath of Object.keys(pathMap)) {
const ambiguousPath = seenPaths.find((seenPath) =>
arePathsAmbiguous(seenPath, currentPath)
);
if (ambiguousPath) {
report({
message: `Paths should resolve unambiguously. Found two ambiguous paths: \`${ambiguousPath}\` and \`${currentPath}\`.`,
location: location.child([currentPath]).key(),
});
}
seenPaths.push(currentPath);
}
},
};
};
function arePathsAmbiguous(a: string, b: string) {
const partsA = a.split('/');
const partsB = b.split('/');
if (partsA.length !== partsB.length) return false;
let aVars = 0;
let bVars = 0;
let ambiguous = true;
for (let i = 0; i < partsA.length; i++) {
const aIsVar = partsA[i].match(/^{.+?}$/);
const bIsVar = partsB[i].match(/^{.+?}$/);
if (aIsVar || bIsVar) {
if (aIsVar) aVars++;
if (bIsVar) bVars++;
continue;
} else if (partsA[i] !== partsB[i]) {
ambiguous = false;
}
}
return ambiguous && aVars === bVars;
}