From 59b7aafcfa2f51c370e5f942ff128c58016c45c0 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 2 Apr 2025 16:57:19 +0100 Subject: [PATCH 1/8] CLOUDP-306578: IPA-123: Enums --- .../IPA123EnumValuesShouldNotExceed20.test.js | 254 ++++++++++++++++++ tools/spectral/ipa/rulesets/IPA-123.yaml | 17 ++ tools/spectral/ipa/rulesets/README.md | 11 + .../IPA123EnumValuesShouldNotExceed20.js | 35 +++ 4 files changed, 317 insertions(+) create mode 100644 tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js create mode 100644 tools/spectral/ipa/rulesets/functions/IPA123EnumValuesShouldNotExceed20.js diff --git a/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js b/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js new file mode 100644 index 0000000000..568afe9ab9 --- /dev/null +++ b/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js @@ -0,0 +1,254 @@ +import testRule from './__helpers__/testRule.js'; +import { DiagnosticSeverity } from '@stoplight/types'; + +testRule('xgen-IPA-123-enum-values-should-not-exceed-20', [ + { + name: 'valid when enum has exactly 20 values', + document: { + components: { + schemas: { + TestSchema: { + properties: { + status: { + type: 'string', + enum: [ + 'VALUE_1', + 'VALUE_2', + 'VALUE_3', + 'VALUE_4', + 'VALUE_5', + 'VALUE_6', + 'VALUE_7', + 'VALUE_8', + 'VALUE_9', + 'VALUE_10', + 'VALUE_11', + 'VALUE_12', + 'VALUE_13', + 'VALUE_14', + 'VALUE_15', + 'VALUE_16', + 'VALUE_17', + 'VALUE_18', + 'VALUE_19', + 'VALUE_20', + ], + }, + }, + }, + }, + }, + }, + errors: [], + }, + { + name: 'valid when enum has fewer than 20 values', + document: { + components: { + schemas: { + TestSchema: { + properties: { + status: { + type: 'string', + enum: ['PENDING', 'ACTIVE', 'COMPLETE', 'FAILED'], + }, + }, + }, + }, + }, + }, + errors: [], + }, + { + name: 'invalid when enum has more than 20 values', + document: { + components: { + schemas: { + TestSchema: { + properties: { + status: { + type: 'string', + enum: [ + 'VALUE_1', + 'VALUE_2', + 'VALUE_3', + 'VALUE_4', + 'VALUE_5', + 'VALUE_6', + 'VALUE_7', + 'VALUE_8', + 'VALUE_9', + 'VALUE_10', + 'VALUE_11', + 'VALUE_12', + 'VALUE_13', + 'VALUE_14', + 'VALUE_15', + 'VALUE_16', + 'VALUE_17', + 'VALUE_18', + 'VALUE_19', + 'VALUE_20', + 'VALUE_21', + ], + }, + }, + }, + }, + }, + }, + errors: [ + { + code: 'xgen-IPA-123-enum-values-should-not-exceed-20', + message: 'Enum arrays should not exceed 20 values. Current count: 21', + path: ['components', 'schemas', 'TestSchema', 'properties', 'status', 'enum'], + severity: DiagnosticSeverity.Warning, + }, + ], + }, + { + name: 'valid when exception is defined', + document: { + components: { + schemas: { + TestSchema: { + properties: { + status: { + type: 'string', + enum: [ + 'VALUE_1', + 'VALUE_2', + 'VALUE_3', + 'VALUE_4', + 'VALUE_5', + 'VALUE_6', + 'VALUE_7', + 'VALUE_8', + 'VALUE_9', + 'VALUE_10', + 'VALUE_11', + 'VALUE_12', + 'VALUE_13', + 'VALUE_14', + 'VALUE_15', + 'VALUE_16', + 'VALUE_17', + 'VALUE_18', + 'VALUE_19', + 'VALUE_20', + 'VALUE_21', + 'VALUE_22', + 'VALUE_23', + 'VALUE_24', + 'VALUE_25', + ], + 'x-xgen-IPA-exception': { + 'xgen-IPA-123-enum-values-should-not-exceed-20': 'Legacy enum with more than 20 values', + }, + }, + }, + }, + }, + }, + }, + errors: [], + }, + { + name: 'invalid with integer enum values exceeding limit', + document: { + components: { + schemas: { + TestSchema: { + properties: { + priority: { + type: 'integer', + enum: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21], + }, + }, + }, + }, + }, + }, + errors: [ + { + code: 'xgen-IPA-123-enum-values-should-not-exceed-20', + message: 'Enum arrays should not exceed 20 values. Current count: 21', + path: ['components', 'schemas', 'TestSchema', 'properties', 'priority', 'enum'], + severity: DiagnosticSeverity.Warning, + }, + ], + }, + { + name: 'valid for parameters in path operation', + document: { + paths: { + '/resources': { + get: { + parameters: [ + { + name: 'status', + in: 'query', + schema: { + type: 'string', + enum: ['ACTIVE', 'INACTIVE', 'PENDING'], + }, + }, + ], + }, + }, + }, + }, + errors: [], + }, + { + name: 'invalid for parameters in path operation exceeding limit', + document: { + paths: { + '/resources': { + get: { + parameters: [ + { + name: 'status', + in: 'query', + schema: { + type: 'string', + enum: [ + 'VAL_1', + 'VAL_2', + 'VAL_3', + 'VAL_4', + 'VAL_5', + 'VAL_6', + 'VAL_7', + 'VAL_8', + 'VAL_9', + 'VAL_10', + 'VAL_11', + 'VAL_12', + 'VAL_13', + 'VAL_14', + 'VAL_15', + 'VAL_16', + 'VAL_17', + 'VAL_18', + 'VAL_19', + 'VAL_20', + 'VAL_21', + ], + }, + }, + ], + }, + }, + }, + }, + errors: [ + { + code: 'xgen-IPA-123-enum-values-should-not-exceed-20', + message: 'Enum arrays should not exceed 20 values. Current count: 21', + path: ['paths', '/resources', 'get', 'parameters', '0', 'schema', 'enum'], + severity: DiagnosticSeverity.Warning, + }, + ], + }, +]); diff --git a/tools/spectral/ipa/rulesets/IPA-123.yaml b/tools/spectral/ipa/rulesets/IPA-123.yaml index e8635ed9ab..f9dbaec99f 100644 --- a/tools/spectral/ipa/rulesets/IPA-123.yaml +++ b/tools/spectral/ipa/rulesets/IPA-123.yaml @@ -3,6 +3,7 @@ functions: - IPA123EachEnumValueMustBeUpperSnakeCase + - IPA123EnumValuesShouldNotExceed20 rules: xgen-IPA-123-enum-values-must-be-upper-snake-case: @@ -17,6 +18,22 @@ rules: - Skips validation if the schema has an exception defined for this rule message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-123-enum-values-must-be-upper-snake-case' severity: error + resolved: false given: '$..enum' then: function: 'IPA123EachEnumValueMustBeUpperSnakeCase' + xgen-IPA-123-enum-values-should-not-exceed-20: + description: | + Enum values should not exceed 20 entries. + + ##### Implementation details + Rule checks for the following conditions: + - Applies to all enum value arrays defined in the OpenAPI schema + - Validates that each enum array has 20 or fewer values + - Skips validation if the schema has an exception defined for this rule + message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-123-enum-values-should-not-exceed-20' + severity: warn + resolved: false + given: '$..enum' + then: + function: 'IPA123EnumValuesShouldNotExceed20' diff --git a/tools/spectral/ipa/rulesets/README.md b/tools/spectral/ipa/rulesets/README.md index ff9890bda9..3416d9e264 100644 --- a/tools/spectral/ipa/rulesets/README.md +++ b/tools/spectral/ipa/rulesets/README.md @@ -830,6 +830,17 @@ Rule checks for the following conditions: - Validates each enum value individually against the UPPER_SNAKE_CASE pattern - Skips validation if the schema has an exception defined for this rule +#### xgen-IPA-123-enum-values-should-not-exceed-20 + + ![warn](https://img.shields.io/badge/warning-yellow) +Enum values should not exceed 20 entries. + +##### Implementation details +Rule checks for the following conditions: + - Applies to all enum value arrays defined in the OpenAPI schema + - Validates that each enum array has 20 or fewer values + - Skips validation if the schema has an exception defined for this rule + ### IPA-125 diff --git a/tools/spectral/ipa/rulesets/functions/IPA123EnumValuesShouldNotExceed20.js b/tools/spectral/ipa/rulesets/functions/IPA123EnumValuesShouldNotExceed20.js new file mode 100644 index 0000000000..318ae82dcc --- /dev/null +++ b/tools/spectral/ipa/rulesets/functions/IPA123EnumValuesShouldNotExceed20.js @@ -0,0 +1,35 @@ +import { hasException } from './utils/exceptions.js'; +import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js'; +import { getSchemaPathFromEnumPath } from './utils/schemaUtils.js'; +import { resolveObject } from './utils/componentUtils.js'; + +const RULE_NAME = 'xgen-IPA-123-enum-values-should-not-exceed-20'; +const ERROR_MESSAGE = 'Enum arrays should not exceed 20 values. Current count: '; +const MAX_ENUM_VALUES = 20; + +export default (input, options, { path, documentInventory }) => { + const oas = documentInventory.resolved; + const schemaPath = getSchemaPathFromEnumPath(path); + const schemaObject = resolveObject(oas, schemaPath); + + // Check for exceptions + if (hasException(schemaObject, RULE_NAME)) { + collectException(schemaObject, RULE_NAME, path); + return; + } + + if (!Array.isArray(input)) { + return; + } + + if (input.length > MAX_ENUM_VALUES) { + return collectAndReturnViolation(path, RULE_NAME, [ + { + path, + message: `${ERROR_MESSAGE}${input.length}`, + }, + ]); + } + + collectAdoption(path, RULE_NAME); +}; From e062ddcbb4e6fce8313304a23cb7d97e03d6791e Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 2 Apr 2025 17:13:52 +0100 Subject: [PATCH 2/8] fix on tests --- .../IPA123EnumValuesShouldNotExceed20.test.js | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js b/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js index 568afe9ab9..4e5404f231 100644 --- a/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js +++ b/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js @@ -107,7 +107,7 @@ testRule('xgen-IPA-123-enum-values-should-not-exceed-20', [ ], }, { - name: 'valid when exception is defined', + name: 'exception on schema', document: { components: { schemas: { @@ -251,4 +251,35 @@ testRule('xgen-IPA-123-enum-values-should-not-exceed-20', [ }, ], }, + { + name: 'exception on parameter schema', + document: { + paths: { + '/resources': { + get: { + parameters: [ + { + name: 'status', + in: 'query', + schema: { + type: 'string', + enum: [ + 'VAL_1', 'VAL_2', 'VAL_3', 'VAL_4', 'VAL_5', + 'VAL_6', 'VAL_7', 'VAL_8', 'VAL_9', 'VAL_10', + 'VAL_11', 'VAL_12', 'VAL_13', 'VAL_14', 'VAL_15', + 'VAL_16', 'VAL_17', 'VAL_18', 'VAL_19', 'VAL_20', + 'VAL_21', 'VAL_22', 'VAL_23', 'VAL_24', 'VAL_25', + ], + 'x-xgen-IPA-exception': { + 'xgen-IPA-123-enum-values-should-not-exceed-20': 'Parameter with many possible values', + }, + }, + }, + ], + }, + }, + }, + }, + errors: [], + }, ]); From f3f7087cd00d23f7c8bff0bcfaad3b08fa0f0766 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Wed, 2 Apr 2025 17:15:07 +0100 Subject: [PATCH 3/8] prettier fix --- .../IPA123EnumValuesShouldNotExceed20.test.js | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js b/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js index 4e5404f231..127c4b4b7a 100644 --- a/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js +++ b/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js @@ -264,11 +264,31 @@ testRule('xgen-IPA-123-enum-values-should-not-exceed-20', [ schema: { type: 'string', enum: [ - 'VAL_1', 'VAL_2', 'VAL_3', 'VAL_4', 'VAL_5', - 'VAL_6', 'VAL_7', 'VAL_8', 'VAL_9', 'VAL_10', - 'VAL_11', 'VAL_12', 'VAL_13', 'VAL_14', 'VAL_15', - 'VAL_16', 'VAL_17', 'VAL_18', 'VAL_19', 'VAL_20', - 'VAL_21', 'VAL_22', 'VAL_23', 'VAL_24', 'VAL_25', + 'VAL_1', + 'VAL_2', + 'VAL_3', + 'VAL_4', + 'VAL_5', + 'VAL_6', + 'VAL_7', + 'VAL_8', + 'VAL_9', + 'VAL_10', + 'VAL_11', + 'VAL_12', + 'VAL_13', + 'VAL_14', + 'VAL_15', + 'VAL_16', + 'VAL_17', + 'VAL_18', + 'VAL_19', + 'VAL_20', + 'VAL_21', + 'VAL_22', + 'VAL_23', + 'VAL_24', + 'VAL_25', ], 'x-xgen-IPA-exception': { 'xgen-IPA-123-enum-values-should-not-exceed-20': 'Parameter with many possible values', From ab837aa7ce8f132578a30180686a0dcf9e33420c Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Thu, 3 Apr 2025 11:20:34 +0100 Subject: [PATCH 4/8] reusable enum schemas --- .../IPA123EnumValuesShouldNotExceed20.test.js | 67 +++++++++++++++++-- tools/spectral/ipa/rulesets/IPA-123.yaml | 9 +-- tools/spectral/ipa/rulesets/README.md | 7 +- .../IPA123EnumValuesShouldNotExceed20.js | 7 +- 4 files changed, 76 insertions(+), 14 deletions(-) diff --git a/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js b/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js index 127c4b4b7a..e88df02f81 100644 --- a/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js +++ b/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js @@ -1,7 +1,7 @@ import testRule from './__helpers__/testRule.js'; import { DiagnosticSeverity } from '@stoplight/types'; -testRule('xgen-IPA-123-enum-values-should-not-exceed-20', [ +testRule('xgen-IPA-123-allowable-enum-values-should-not-exceed-20', [ { name: 'valid when enum has exactly 20 values', document: { @@ -99,7 +99,7 @@ testRule('xgen-IPA-123-enum-values-should-not-exceed-20', [ }, errors: [ { - code: 'xgen-IPA-123-enum-values-should-not-exceed-20', + code: 'xgen-IPA-123-allowable-enum-values-should-not-exceed-20', message: 'Enum arrays should not exceed 20 values. Current count: 21', path: ['components', 'schemas', 'TestSchema', 'properties', 'status', 'enum'], severity: DiagnosticSeverity.Warning, @@ -143,7 +143,7 @@ testRule('xgen-IPA-123-enum-values-should-not-exceed-20', [ 'VALUE_25', ], 'x-xgen-IPA-exception': { - 'xgen-IPA-123-enum-values-should-not-exceed-20': 'Legacy enum with more than 20 values', + 'xgen-IPA-123-allowable-enum-values-should-not-exceed-20': 'Legacy enum with more than 20 values', }, }, }, @@ -171,7 +171,7 @@ testRule('xgen-IPA-123-enum-values-should-not-exceed-20', [ }, errors: [ { - code: 'xgen-IPA-123-enum-values-should-not-exceed-20', + code: 'xgen-IPA-123-allowable-enum-values-should-not-exceed-20', message: 'Enum arrays should not exceed 20 values. Current count: 21', path: ['components', 'schemas', 'TestSchema', 'properties', 'priority', 'enum'], severity: DiagnosticSeverity.Warning, @@ -244,7 +244,7 @@ testRule('xgen-IPA-123-enum-values-should-not-exceed-20', [ }, errors: [ { - code: 'xgen-IPA-123-enum-values-should-not-exceed-20', + code: 'xgen-IPA-123-allowable-enum-values-should-not-exceed-20', message: 'Enum arrays should not exceed 20 values. Current count: 21', path: ['paths', '/resources', 'get', 'parameters', '0', 'schema', 'enum'], severity: DiagnosticSeverity.Warning, @@ -291,7 +291,7 @@ testRule('xgen-IPA-123-enum-values-should-not-exceed-20', [ 'VAL_25', ], 'x-xgen-IPA-exception': { - 'xgen-IPA-123-enum-values-should-not-exceed-20': 'Parameter with many possible values', + 'xgen-IPA-123-allowable-enum-values-should-not-exceed-20': 'Parameter with many possible values', }, }, }, @@ -302,4 +302,59 @@ testRule('xgen-IPA-123-enum-values-should-not-exceed-20', [ }, errors: [], }, + { + name: 'valid on with reusable schemas', + document: { + paths: { + '/resources': { + get: { + parameters: [ + { + name: 'status', + in: 'query', + schema: { + $ref: '#/components/schemas/StatusEnum', + }, + }, + ], + }, + }, + }, + components: { + schemas: { + StatusEnum: { + type: 'string', + enum: [ + 'VAL_1', + 'VAL_2', + 'VAL_3', + 'VAL_4', + 'VAL_5', + 'VAL_6', + 'VAL_7', + 'VAL_8', + 'VAL_9', + 'VAL_10', + 'VAL_11', + 'VAL_12', + 'VAL_13', + 'VAL_14', + 'VAL_15', + 'VAL_16', + 'VAL_17', + 'VAL_18', + 'VAL_19', + 'VAL_20', + 'VAL_21', + 'VAL_22', + 'VAL_23', + 'VAL_24', + 'VAL_25', + ], + }, + }, + }, + }, + errors: [], + }, ]); diff --git a/tools/spectral/ipa/rulesets/IPA-123.yaml b/tools/spectral/ipa/rulesets/IPA-123.yaml index f9dbaec99f..6b206e0f14 100644 --- a/tools/spectral/ipa/rulesets/IPA-123.yaml +++ b/tools/spectral/ipa/rulesets/IPA-123.yaml @@ -22,16 +22,17 @@ rules: given: '$..enum' then: function: 'IPA123EachEnumValueMustBeUpperSnakeCase' - xgen-IPA-123-enum-values-should-not-exceed-20: + xgen-IPA-123-allowable-enum-values-should-not-exceed-20: description: | - Enum values should not exceed 20 entries. + Allowable enum values should not exceed 20 entries. ##### Implementation details Rule checks for the following conditions: - - Applies to all enum value arrays defined in the OpenAPI schema + - Applies to enum value arrays defined in the parameters and schema properties - Validates that each enum array has 20 or fewer values + - Reusable enum schemas will be ignored - Skips validation if the schema has an exception defined for this rule - message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-123-enum-values-should-not-exceed-20' + message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-123-allowable-enum-values-should-not-exceed-20' severity: warn resolved: false given: '$..enum' diff --git a/tools/spectral/ipa/rulesets/README.md b/tools/spectral/ipa/rulesets/README.md index 3416d9e264..883d9e0404 100644 --- a/tools/spectral/ipa/rulesets/README.md +++ b/tools/spectral/ipa/rulesets/README.md @@ -830,15 +830,16 @@ Rule checks for the following conditions: - Validates each enum value individually against the UPPER_SNAKE_CASE pattern - Skips validation if the schema has an exception defined for this rule -#### xgen-IPA-123-enum-values-should-not-exceed-20 +#### xgen-IPA-123-allowable-enum-values-should-not-exceed-20 ![warn](https://img.shields.io/badge/warning-yellow) -Enum values should not exceed 20 entries. +Allowable enum values should not exceed 20 entries. ##### Implementation details Rule checks for the following conditions: - - Applies to all enum value arrays defined in the OpenAPI schema + - Applies to enum value arrays defined in the parameters and schema properties - Validates that each enum array has 20 or fewer values + - Reusable enum schemas will be ignored - Skips validation if the schema has an exception defined for this rule diff --git a/tools/spectral/ipa/rulesets/functions/IPA123EnumValuesShouldNotExceed20.js b/tools/spectral/ipa/rulesets/functions/IPA123EnumValuesShouldNotExceed20.js index 318ae82dcc..97a6dfc980 100644 --- a/tools/spectral/ipa/rulesets/functions/IPA123EnumValuesShouldNotExceed20.js +++ b/tools/spectral/ipa/rulesets/functions/IPA123EnumValuesShouldNotExceed20.js @@ -3,7 +3,7 @@ import { collectAdoption, collectAndReturnViolation, collectException } from './ import { getSchemaPathFromEnumPath } from './utils/schemaUtils.js'; import { resolveObject } from './utils/componentUtils.js'; -const RULE_NAME = 'xgen-IPA-123-enum-values-should-not-exceed-20'; +const RULE_NAME = 'xgen-IPA-123-allowable-enum-values-should-not-exceed-20'; const ERROR_MESSAGE = 'Enum arrays should not exceed 20 values. Current count: '; const MAX_ENUM_VALUES = 20; @@ -12,6 +12,11 @@ export default (input, options, { path, documentInventory }) => { const schemaPath = getSchemaPathFromEnumPath(path); const schemaObject = resolveObject(oas, schemaPath); + //Ignore if the schemaObject belongs to a reusable enum + if (!schemaPath.includes('properties') && !schemaPath.includes('parameters')) { + return; + } + // Check for exceptions if (hasException(schemaObject, RULE_NAME)) { collectException(schemaObject, RULE_NAME, path); From 6bff9a4e4c0da7c462d8ceabadfcd00cd759ac0f Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Thu, 3 Apr 2025 11:23:53 +0100 Subject: [PATCH 5/8] reusable enum schemas --- .../__tests__/IPA123EnumValuesShouldNotExceed20.test.js | 7 +++++++ tools/spectral/ipa/rulesets/IPA-123.yaml | 2 +- tools/spectral/ipa/rulesets/README.md | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js b/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js index e88df02f81..41f23429eb 100644 --- a/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js +++ b/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js @@ -352,6 +352,13 @@ testRule('xgen-IPA-123-allowable-enum-values-should-not-exceed-20', [ 'VAL_25', ], }, + TestSchema: { + properties: { + status: { + $ref: '#/components/schemas/StatusEnum', + }, + }, + }, }, }, }, diff --git a/tools/spectral/ipa/rulesets/IPA-123.yaml b/tools/spectral/ipa/rulesets/IPA-123.yaml index 6b206e0f14..ff430bb0fc 100644 --- a/tools/spectral/ipa/rulesets/IPA-123.yaml +++ b/tools/spectral/ipa/rulesets/IPA-123.yaml @@ -28,7 +28,7 @@ rules: ##### Implementation details Rule checks for the following conditions: - - Applies to enum value arrays defined in the parameters and schema properties + - Applies to inline enum values - Validates that each enum array has 20 or fewer values - Reusable enum schemas will be ignored - Skips validation if the schema has an exception defined for this rule diff --git a/tools/spectral/ipa/rulesets/README.md b/tools/spectral/ipa/rulesets/README.md index 883d9e0404..e828145239 100644 --- a/tools/spectral/ipa/rulesets/README.md +++ b/tools/spectral/ipa/rulesets/README.md @@ -837,7 +837,7 @@ Allowable enum values should not exceed 20 entries. ##### Implementation details Rule checks for the following conditions: - - Applies to enum value arrays defined in the parameters and schema properties + - Applies to inline enum values - Validates that each enum array has 20 or fewer values - Reusable enum schemas will be ignored - Skips validation if the schema has an exception defined for this rule From 0b1f8fac24de327d50639c1d80a10a1ed268575a Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Thu, 3 Apr 2025 11:31:13 +0100 Subject: [PATCH 6/8] reusable enum schemas --- .../ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js | 6 +++--- .../rulesets/functions/IPA123EnumValuesShouldNotExceed20.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js b/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js index 41f23429eb..7669c78f40 100644 --- a/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js +++ b/tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js @@ -100,7 +100,7 @@ testRule('xgen-IPA-123-allowable-enum-values-should-not-exceed-20', [ errors: [ { code: 'xgen-IPA-123-allowable-enum-values-should-not-exceed-20', - message: 'Enum arrays should not exceed 20 values. Current count: 21', + message: 'Inline enum arrays should not exceed 20 values. Current count: 21', path: ['components', 'schemas', 'TestSchema', 'properties', 'status', 'enum'], severity: DiagnosticSeverity.Warning, }, @@ -172,7 +172,7 @@ testRule('xgen-IPA-123-allowable-enum-values-should-not-exceed-20', [ errors: [ { code: 'xgen-IPA-123-allowable-enum-values-should-not-exceed-20', - message: 'Enum arrays should not exceed 20 values. Current count: 21', + message: 'Inline enum arrays should not exceed 20 values. Current count: 21', path: ['components', 'schemas', 'TestSchema', 'properties', 'priority', 'enum'], severity: DiagnosticSeverity.Warning, }, @@ -245,7 +245,7 @@ testRule('xgen-IPA-123-allowable-enum-values-should-not-exceed-20', [ errors: [ { code: 'xgen-IPA-123-allowable-enum-values-should-not-exceed-20', - message: 'Enum arrays should not exceed 20 values. Current count: 21', + message: 'Inline enum arrays should not exceed 20 values. Current count: 21', path: ['paths', '/resources', 'get', 'parameters', '0', 'schema', 'enum'], severity: DiagnosticSeverity.Warning, }, diff --git a/tools/spectral/ipa/rulesets/functions/IPA123EnumValuesShouldNotExceed20.js b/tools/spectral/ipa/rulesets/functions/IPA123EnumValuesShouldNotExceed20.js index 97a6dfc980..10acd88aa8 100644 --- a/tools/spectral/ipa/rulesets/functions/IPA123EnumValuesShouldNotExceed20.js +++ b/tools/spectral/ipa/rulesets/functions/IPA123EnumValuesShouldNotExceed20.js @@ -4,7 +4,7 @@ import { getSchemaPathFromEnumPath } from './utils/schemaUtils.js'; import { resolveObject } from './utils/componentUtils.js'; const RULE_NAME = 'xgen-IPA-123-allowable-enum-values-should-not-exceed-20'; -const ERROR_MESSAGE = 'Enum arrays should not exceed 20 values. Current count: '; +const ERROR_MESSAGE = 'Inline enum arrays should not exceed 20 values. Current count: '; const MAX_ENUM_VALUES = 20; export default (input, options, { path, documentInventory }) => { From 4d9bf07a8fd10c73f38147ddb16e6dbef94fe2f1 Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Thu, 3 Apr 2025 11:43:17 +0100 Subject: [PATCH 7/8] maxEnumValues parameter --- tools/spectral/ipa/rulesets/IPA-123.yaml | 2 ++ .../rulesets/functions/IPA123EnumValuesShouldNotExceed20.js | 5 ++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/spectral/ipa/rulesets/IPA-123.yaml b/tools/spectral/ipa/rulesets/IPA-123.yaml index ff430bb0fc..1eb311b1c6 100644 --- a/tools/spectral/ipa/rulesets/IPA-123.yaml +++ b/tools/spectral/ipa/rulesets/IPA-123.yaml @@ -38,3 +38,5 @@ rules: given: '$..enum' then: function: 'IPA123EnumValuesShouldNotExceed20' + functionOptions: + maxEnumValues: 20 \ No newline at end of file diff --git a/tools/spectral/ipa/rulesets/functions/IPA123EnumValuesShouldNotExceed20.js b/tools/spectral/ipa/rulesets/functions/IPA123EnumValuesShouldNotExceed20.js index 10acd88aa8..06aa15bf1a 100644 --- a/tools/spectral/ipa/rulesets/functions/IPA123EnumValuesShouldNotExceed20.js +++ b/tools/spectral/ipa/rulesets/functions/IPA123EnumValuesShouldNotExceed20.js @@ -5,9 +5,8 @@ import { resolveObject } from './utils/componentUtils.js'; const RULE_NAME = 'xgen-IPA-123-allowable-enum-values-should-not-exceed-20'; const ERROR_MESSAGE = 'Inline enum arrays should not exceed 20 values. Current count: '; -const MAX_ENUM_VALUES = 20; -export default (input, options, { path, documentInventory }) => { +export default (input, { maxEnumValues }, { path, documentInventory }) => { const oas = documentInventory.resolved; const schemaPath = getSchemaPathFromEnumPath(path); const schemaObject = resolveObject(oas, schemaPath); @@ -27,7 +26,7 @@ export default (input, options, { path, documentInventory }) => { return; } - if (input.length > MAX_ENUM_VALUES) { + if (input.length > maxEnumValues) { return collectAndReturnViolation(path, RULE_NAME, [ { path, From 2c5c2cc5bdb7f4d220a778ffef892ce93498d11b Mon Sep 17 00:00:00 2001 From: Yeliz Henden Date: Thu, 3 Apr 2025 11:44:28 +0100 Subject: [PATCH 8/8] prettier fix --- tools/spectral/ipa/rulesets/IPA-123.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/spectral/ipa/rulesets/IPA-123.yaml b/tools/spectral/ipa/rulesets/IPA-123.yaml index 1eb311b1c6..61b0fc8bef 100644 --- a/tools/spectral/ipa/rulesets/IPA-123.yaml +++ b/tools/spectral/ipa/rulesets/IPA-123.yaml @@ -39,4 +39,4 @@ rules: then: function: 'IPA123EnumValuesShouldNotExceed20' functionOptions: - maxEnumValues: 20 \ No newline at end of file + maxEnumValues: 20