Skip to content

Commit ef107f2

Browse files
fix(ipa): Validate both inline and reusable enums
1 parent 5772234 commit ef107f2

File tree

2 files changed

+67
-10
lines changed

2 files changed

+67
-10
lines changed

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

Lines changed: 66 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,68 @@ 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', 'VAL_2', 'VAL_3', 'VAL_4', 'VAL_5',
408+
'VAL_6', 'VAL_7', 'VAL_8', 'VAL_9', 'VAL_10',
409+
'VAL_11', 'VAL_12', 'VAL_13', 'VAL_14', 'VAL_15',
410+
'VAL_16', 'VAL_17', 'VAL_18', 'VAL_19', 'VAL_20',
411+
'VAL_21',
412+
],
413+
},
414+
},
415+
},
416+
},
417+
},
418+
},
419+
},
420+
errors: [
421+
{
422+
code: 'xgen-IPA-123-allowable-enum-values-should-not-exceed-20',
423+
message: 'The number of allowable enum values should not exceed 20 values. Current count: 21',
424+
path: ['components', 'schemas', 'TestSchema', 'properties', 'statusList', 'items', 'enum'],
425+
severity: DiagnosticSeverity.Error,
426+
},
427+
],
428+
},
367429
]);

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)