Skip to content

Commit 900c47b

Browse files
fix(ipa): Validate both inline and reusable enums (#890)
1 parent c56a617 commit 900c47b

File tree

5 files changed

+96
-16
lines changed

5 files changed

+96
-16
lines changed

tools/spectral/ipa/__tests__/IPA123EnumValuesShouldNotExceed20.test.js

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ testRule('xgen-IPA-123-allowable-enum-values-should-not-exceed-20', [
100100
errors: [
101101
{
102102
code: 'xgen-IPA-123-allowable-enum-values-should-not-exceed-20',
103-
message: 'Inline enum arrays should not exceed 20 values. Current count: 21',
103+
message: 'The number of allowable enum values should not exceed 20 values. Current count: 21',
104104
path: ['components', 'schemas', 'TestSchema', 'properties', 'status', 'enum'],
105105
severity: DiagnosticSeverity.Error,
106106
},
@@ -172,7 +172,7 @@ testRule('xgen-IPA-123-allowable-enum-values-should-not-exceed-20', [
172172
errors: [
173173
{
174174
code: 'xgen-IPA-123-allowable-enum-values-should-not-exceed-20',
175-
message: 'Inline enum arrays should not exceed 20 values. Current count: 21',
175+
message: 'The number of allowable enum values should not exceed 20 values. Current count: 21',
176176
path: ['components', 'schemas', 'TestSchema', 'properties', 'priority', 'enum'],
177177
severity: DiagnosticSeverity.Error,
178178
},
@@ -245,7 +245,7 @@ testRule('xgen-IPA-123-allowable-enum-values-should-not-exceed-20', [
245245
errors: [
246246
{
247247
code: 'xgen-IPA-123-allowable-enum-values-should-not-exceed-20',
248-
message: 'Inline enum arrays should not exceed 20 values. Current count: 21',
248+
message: 'The number of allowable enum values should not exceed 20 values. Current count: 21',
249249
path: ['paths', '/resources', 'get', 'parameters', '0', 'schema', 'enum'],
250250
severity: DiagnosticSeverity.Error,
251251
},
@@ -303,7 +303,7 @@ testRule('xgen-IPA-123-allowable-enum-values-should-not-exceed-20', [
303303
errors: [],
304304
},
305305
{
306-
name: 'valid on with reusable schemas',
306+
name: 'invalid on with reusable schemas',
307307
document: {
308308
paths: {
309309
'/resources': {
@@ -362,6 +362,84 @@ testRule('xgen-IPA-123-allowable-enum-values-should-not-exceed-20', [
362362
},
363363
},
364364
},
365+
errors: [
366+
{
367+
code: 'xgen-IPA-123-allowable-enum-values-should-not-exceed-20',
368+
message: 'The number of allowable enum values should not exceed 20 values. Current count: 25',
369+
path: ['components', 'schemas', 'StatusEnum', 'enum'],
370+
severity: DiagnosticSeverity.Error,
371+
},
372+
],
373+
},
374+
{
375+
name: 'valid enum array in items schema',
376+
document: {
377+
components: {
378+
schemas: {
379+
TestSchema: {
380+
properties: {
381+
statusList: {
382+
type: 'array',
383+
items: {
384+
type: 'string',
385+
enum: ['PENDING', 'ACTIVE', 'COMPLETE', 'FAILED', 'CANCELLED'],
386+
},
387+
},
388+
},
389+
},
390+
},
391+
},
392+
},
365393
errors: [],
366394
},
395+
{
396+
name: 'invalid enum array in items schema exceeding limit',
397+
document: {
398+
components: {
399+
schemas: {
400+
TestSchema: {
401+
properties: {
402+
statusList: {
403+
type: 'array',
404+
items: {
405+
type: 'string',
406+
enum: [
407+
'VAL_1',
408+
'VAL_2',
409+
'VAL_3',
410+
'VAL_4',
411+
'VAL_5',
412+
'VAL_6',
413+
'VAL_7',
414+
'VAL_8',
415+
'VAL_9',
416+
'VAL_10',
417+
'VAL_11',
418+
'VAL_12',
419+
'VAL_13',
420+
'VAL_14',
421+
'VAL_15',
422+
'VAL_16',
423+
'VAL_17',
424+
'VAL_18',
425+
'VAL_19',
426+
'VAL_20',
427+
'VAL_21',
428+
],
429+
},
430+
},
431+
},
432+
},
433+
},
434+
},
435+
},
436+
errors: [
437+
{
438+
code: 'xgen-IPA-123-allowable-enum-values-should-not-exceed-20',
439+
message: 'The number of allowable enum values should not exceed 20 values. Current count: 21',
440+
path: ['components', 'schemas', 'TestSchema', 'properties', 'statusList', 'items', 'enum'],
441+
severity: DiagnosticSeverity.Error,
442+
},
443+
],
444+
},
367445
]);

tools/spectral/ipa/ipa-spectral.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ overrides:
102102
- '**#/components/schemas/CloudRegionConfig/properties/regionName/oneOf/2' # reference to support future investigation - CLOUDP-310775
103103
- '**#/components/schemas/InvoiceLineItem/properties/sku' # reference to support future investigation - CLOUDP-310775
104104
- '**#/components/schemas/Collation/properties/locale' # reference to support future investigation - CLOUDP-310775
105+
- '**#/components/schemas/BaseCloudProviderInstanceSize/oneOf/0' # reference to support future investigation - CLOUDP-310775
106+
- '**#/components/schemas/BaseCloudProviderInstanceSize/oneOf/1' # reference to support future investigation - CLOUDP-310775
107+
- '**#/components/schemas/BillingEventTypeViewForOrg' # reference to support future investigation - CLOUDP-310775
108+
- '**#/components/schemas/EventTypeForNdsGroup/oneOf/24' # reference to support future investigation - CLOUDP-310775
109+
- '**#/components/schemas/EventTypeForNdsGroup/oneOf/27' # reference to support future investigation - CLOUDP-310775
110+
- '**#/components/schemas/EventTypeForOrg/oneOf/4' # reference to support future investigation - CLOUDP-310775
111+
- '**#/components/schemas/EventTypeForOrg/oneOf/5' # reference to support future investigation - CLOUDP-310775
112+
- '**#/components/schemas/EventTypeForOrg/oneOf/9' # reference to support future investigation - CLOUDP-310775
113+
- '**#/components/schemas/HostEventTypeViewForNdsGroup' # reference to support future investigation - CLOUDP-310775
114+
- '**#/components/schemas/NDSAuditTypeViewForNdsGroup' # reference to support future investigation - CLOUDP-310775
115+
- '**#/components/schemas/OrgEventTypeViewForOrg' # reference to support future investigation - CLOUDP-310775
105116
- '**#/components/schemas/tokenFiltersnowballStemming/properties/stemmerName' # external field, to be covered by CLOUDP-293178
106117
- '**#/components/schemas/DataLakeS3StoreSettings/allOf/1/properties/region' # external field, to be covered by CLOUDP-293178
107118
- '**#/components/schemas/DataLakeDLSAWSStore/allOf/1/properties/region' # external field, to be covered by CLOUDP-293178

tools/spectral/ipa/rulesets/IPA-123.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ rules:
2828
2929
##### Implementation details
3030
Rule checks for the following conditions:
31-
- Applies to inline enum values
32-
- Validates that each enum array has 20 or fewer values
33-
- Reusable enum schemas will be ignored
31+
- Validates that each enum set has 20 or fewer values
3432
- Skips validation if the schema has an exception defined for this rule
3533
- This validation threshold can be adjusted by changing the functionOptions.maxEnumValues parameter
3634
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-123-allowable-enum-values-should-not-exceed-20'

tools/spectral/ipa/rulesets/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -910,9 +910,7 @@ Allowable enum values should not exceed 20 entries.
910910

911911
##### Implementation details
912912
Rule checks for the following conditions:
913-
- Applies to inline enum values
914-
- Validates that each enum array has 20 or fewer values
915-
- Reusable enum schemas will be ignored
913+
- Validates that each enum set has 20 or fewer values
916914
- Skips validation if the schema has an exception defined for this rule
917915
- This validation threshold can be adjusted by changing the functionOptions.maxEnumValues parameter
918916

tools/spectral/ipa/rulesets/functions/IPA123EnumValuesShouldNotExceed20.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@ import { getSchemaPathFromEnumPath } from './utils/schemaUtils.js';
33
import { resolveObject } from './utils/componentUtils.js';
44

55
const RULE_NAME = 'xgen-IPA-123-allowable-enum-values-should-not-exceed-20';
6-
const ERROR_MESSAGE = 'Inline enum arrays should not exceed 20 values. Current count: ';
6+
const ERROR_MESSAGE = 'The number of allowable enum values should not exceed 20 values. Current count: ';
77

88
export default (input, { maxEnumValues }, { path, documentInventory }) => {
99
const oas = documentInventory.resolved;
1010
const schemaPath = getSchemaPathFromEnumPath(path);
1111
const schemaObject = resolveObject(oas, schemaPath);
1212

13-
//Ignore if the schemaObject belongs to a reusable enum
14-
if (!schemaPath.includes('properties') && !schemaPath.includes('parameters')) {
15-
return;
16-
}
17-
1813
if (!Array.isArray(input)) {
1914
return;
2015
}

0 commit comments

Comments
 (0)