Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(rulesets): simplify oasOpSecurityDefined #2537

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ testRule('oas3-operation-security-defined', [
apikey: [],
basic: [],
},
{},
],
},
},
Expand Down
4 changes: 2 additions & 2 deletions packages/rulesets/src/oas/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { default as oasDocumentSchema } from './oasDocumentSchema';
import { default as oasOpFormDataConsumeCheck } from './oasOpFormDataConsumeCheck';
import { default as oasOpSuccessResponse } from './oasOpSuccessResponse';
import { default as oasExample } from './oasExample';
import { default as oasOpSecurityDefined } from './oasOpSecurityDefined';
import { default as oasSecurityDefined } from './oasSecurityDefined';
import { default as typedEnum } from './typedEnum';
import { default as refSiblings } from './refSiblings';
import { default as oasPathParam } from './oasPathParam';
Expand All @@ -20,7 +20,7 @@ export {
oasOpFormDataConsumeCheck,
oasOpSuccessResponse,
oasExample,
oasOpSecurityDefined,
oasSecurityDefined,
typedEnum,
refSiblings,
oasPathParam,
Expand Down
110 changes: 0 additions & 110 deletions packages/rulesets/src/oas/functions/oasOpSecurityDefined.ts

This file was deleted.

53 changes: 53 additions & 0 deletions packages/rulesets/src/oas/functions/oasSecurityDefined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { createRulesetFunction } from '@stoplight/spectral-core';
import type { IFunctionResult } from '@stoplight/spectral-core';
import { isPlainObject } from '@stoplight/json';

type Options = {
oasVersion: 2 | 3;
};

export default createRulesetFunction<Record<string, unknown>, Options>(
{
input: {
type: 'object',
},
options: {
type: 'object',
properties: {
oasVersion: {
enum: [2, 3],
},
},
additionalProperties: false,
},
},
function oasSecurityDefined(input, { oasVersion }, { document, path }) {
const schemeNames = Object.keys(input);
if (schemeNames.length === 0) return;

if (!isPlainObject(document.data)) return;

const allDefs =
oasVersion === 2
? document.data.securityDefinitions
: isPlainObject(document.data.components)
? document.data.components.securitySchemes
: null;

let results: IFunctionResult[] | undefined;

for (const schemeName of schemeNames) {
if (!isPlainObject(allDefs) || !(schemeName in allDefs)) {
const object = path.length == 2 ? 'API' : 'Operation';
const location = oasVersion === 2 ? 'securityDefinitions' : 'components.securitySchemes';
results ??= [];
results.push({
message: `${object} "security" values must match a scheme defined in the "${location}" object.`,
path: [...path, schemeName],
});
}
}

return results;
},
);
15 changes: 8 additions & 7 deletions packages/rulesets/src/oas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
oasExample,
oasUnusedComponent,
oasDocumentSchema,
oasOpSecurityDefined,
oasSecurityDefined,
oasSchema,
oasDiscriminator,
} from './functions';
Expand All @@ -36,6 +36,7 @@ const ruleset = {
aliases: {
PathItem: ['$.paths[*]'],
OperationObject: ['#PathItem[get,put,post,delete,options,head,patch,trace]'],
SecurityRequirementObject: ['$.security[*]', '#OperationObject.security[*]'],
ResponseObject: {
targets: [
{
Expand Down Expand Up @@ -451,11 +452,11 @@ const ruleset = {
message: '{{error}}',
recommended: true,
formats: [oas2],
given: '$',
given: '#SecurityRequirementObject',
then: {
function: oasOpSecurityDefined,
function: oasSecurityDefined,
functionOptions: {
schemesPath: ['securityDefinitions'],
oasVersion: 2,
},
},
},
Expand Down Expand Up @@ -584,11 +585,11 @@ const ruleset = {
message: '{{error}}',
recommended: true,
formats: [oas3],
given: '$',
given: '#SecurityRequirementObject',
then: {
function: oasOpSecurityDefined,
function: oasSecurityDefined,
functionOptions: {
schemesPath: ['components', 'securitySchemes'],
oasVersion: 3,
},
},
},
Expand Down