persona-community-5/.pnpm-store/v3/files/ef/6419cec59ec5ac50f37eda2e4b4dfee870ffa5a4db9c5abe2fc49e01bbc27a19045c179cd1e36f985966fa0f88b9ca2eaed0bf2663e912e53c0836121a25eb
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

67 lines
2.1 KiB
Plaintext

import type { Oas3Server } from '../../typings/openapi';
import type { Oas3Rule } from '../../visitors';
enum enumError {
empty = 'empty',
invalidDefaultValue = 'invalidDefaultValue',
}
export const NoServerVariablesEmptyEnum: Oas3Rule = () => {
return {
Root(root, { report, location }) {
if (!root.servers || root.servers.length === 0) return;
const invalidVariables: enumError[] = [];
if (Array.isArray(root.servers)) {
for (const server of root.servers) {
const enumErrors = checkEnumVariables(server);
if (!enumErrors) continue;
invalidVariables.push(...enumErrors);
}
} else {
const enumErrors = checkEnumVariables(root.servers);
if (!enumErrors) return;
invalidVariables.push(...enumErrors);
}
for (const invalidVariable of invalidVariables) {
if (invalidVariable === enumError.empty) {
report({
message: 'Server variable with `enum` must be a non-empty array.',
location: location.child(['servers']).key(),
});
}
if (invalidVariable === enumError.invalidDefaultValue) {
report({
message:
'Server variable define `enum` and `default`. `enum` must include default value',
location: location.child(['servers']).key(),
});
}
}
},
};
};
function checkEnumVariables(server: Oas3Server): enumError[] | undefined {
if (server.variables && Object.keys(server.variables).length === 0) return;
const errors: enumError[] = [];
for (const variable in server.variables) {
const serverVariable = server.variables[variable];
if (!serverVariable.enum) continue;
if (Array.isArray(serverVariable.enum) && serverVariable.enum?.length === 0)
errors.push(enumError.empty);
if (!serverVariable.default) continue;
const defaultValue = server.variables[variable].default;
if (serverVariable.enum && !serverVariable.enum.includes(defaultValue))
errors.push(enumError.invalidDefaultValue);
}
if (errors.length) return errors;
return;
}