Skip to content

Commit 59b7aaf

Browse files
CLOUDP-306578: IPA-123: Enums
1 parent 0493c98 commit 59b7aaf

File tree

4 files changed

+317
-0
lines changed

4 files changed

+317
-0
lines changed
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
import testRule from './__helpers__/testRule.js';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
testRule('xgen-IPA-123-enum-values-should-not-exceed-20', [
5+
{
6+
name: 'valid when enum has exactly 20 values',
7+
document: {
8+
components: {
9+
schemas: {
10+
TestSchema: {
11+
properties: {
12+
status: {
13+
type: 'string',
14+
enum: [
15+
'VALUE_1',
16+
'VALUE_2',
17+
'VALUE_3',
18+
'VALUE_4',
19+
'VALUE_5',
20+
'VALUE_6',
21+
'VALUE_7',
22+
'VALUE_8',
23+
'VALUE_9',
24+
'VALUE_10',
25+
'VALUE_11',
26+
'VALUE_12',
27+
'VALUE_13',
28+
'VALUE_14',
29+
'VALUE_15',
30+
'VALUE_16',
31+
'VALUE_17',
32+
'VALUE_18',
33+
'VALUE_19',
34+
'VALUE_20',
35+
],
36+
},
37+
},
38+
},
39+
},
40+
},
41+
},
42+
errors: [],
43+
},
44+
{
45+
name: 'valid when enum has fewer than 20 values',
46+
document: {
47+
components: {
48+
schemas: {
49+
TestSchema: {
50+
properties: {
51+
status: {
52+
type: 'string',
53+
enum: ['PENDING', 'ACTIVE', 'COMPLETE', 'FAILED'],
54+
},
55+
},
56+
},
57+
},
58+
},
59+
},
60+
errors: [],
61+
},
62+
{
63+
name: 'invalid when enum has more than 20 values',
64+
document: {
65+
components: {
66+
schemas: {
67+
TestSchema: {
68+
properties: {
69+
status: {
70+
type: 'string',
71+
enum: [
72+
'VALUE_1',
73+
'VALUE_2',
74+
'VALUE_3',
75+
'VALUE_4',
76+
'VALUE_5',
77+
'VALUE_6',
78+
'VALUE_7',
79+
'VALUE_8',
80+
'VALUE_9',
81+
'VALUE_10',
82+
'VALUE_11',
83+
'VALUE_12',
84+
'VALUE_13',
85+
'VALUE_14',
86+
'VALUE_15',
87+
'VALUE_16',
88+
'VALUE_17',
89+
'VALUE_18',
90+
'VALUE_19',
91+
'VALUE_20',
92+
'VALUE_21',
93+
],
94+
},
95+
},
96+
},
97+
},
98+
},
99+
},
100+
errors: [
101+
{
102+
code: 'xgen-IPA-123-enum-values-should-not-exceed-20',
103+
message: 'Enum arrays should not exceed 20 values. Current count: 21',
104+
path: ['components', 'schemas', 'TestSchema', 'properties', 'status', 'enum'],
105+
severity: DiagnosticSeverity.Warning,
106+
},
107+
],
108+
},
109+
{
110+
name: 'valid when exception is defined',
111+
document: {
112+
components: {
113+
schemas: {
114+
TestSchema: {
115+
properties: {
116+
status: {
117+
type: 'string',
118+
enum: [
119+
'VALUE_1',
120+
'VALUE_2',
121+
'VALUE_3',
122+
'VALUE_4',
123+
'VALUE_5',
124+
'VALUE_6',
125+
'VALUE_7',
126+
'VALUE_8',
127+
'VALUE_9',
128+
'VALUE_10',
129+
'VALUE_11',
130+
'VALUE_12',
131+
'VALUE_13',
132+
'VALUE_14',
133+
'VALUE_15',
134+
'VALUE_16',
135+
'VALUE_17',
136+
'VALUE_18',
137+
'VALUE_19',
138+
'VALUE_20',
139+
'VALUE_21',
140+
'VALUE_22',
141+
'VALUE_23',
142+
'VALUE_24',
143+
'VALUE_25',
144+
],
145+
'x-xgen-IPA-exception': {
146+
'xgen-IPA-123-enum-values-should-not-exceed-20': 'Legacy enum with more than 20 values',
147+
},
148+
},
149+
},
150+
},
151+
},
152+
},
153+
},
154+
errors: [],
155+
},
156+
{
157+
name: 'invalid with integer enum values exceeding limit',
158+
document: {
159+
components: {
160+
schemas: {
161+
TestSchema: {
162+
properties: {
163+
priority: {
164+
type: 'integer',
165+
enum: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21],
166+
},
167+
},
168+
},
169+
},
170+
},
171+
},
172+
errors: [
173+
{
174+
code: 'xgen-IPA-123-enum-values-should-not-exceed-20',
175+
message: 'Enum arrays should not exceed 20 values. Current count: 21',
176+
path: ['components', 'schemas', 'TestSchema', 'properties', 'priority', 'enum'],
177+
severity: DiagnosticSeverity.Warning,
178+
},
179+
],
180+
},
181+
{
182+
name: 'valid for parameters in path operation',
183+
document: {
184+
paths: {
185+
'/resources': {
186+
get: {
187+
parameters: [
188+
{
189+
name: 'status',
190+
in: 'query',
191+
schema: {
192+
type: 'string',
193+
enum: ['ACTIVE', 'INACTIVE', 'PENDING'],
194+
},
195+
},
196+
],
197+
},
198+
},
199+
},
200+
},
201+
errors: [],
202+
},
203+
{
204+
name: 'invalid for parameters in path operation exceeding limit',
205+
document: {
206+
paths: {
207+
'/resources': {
208+
get: {
209+
parameters: [
210+
{
211+
name: 'status',
212+
in: 'query',
213+
schema: {
214+
type: 'string',
215+
enum: [
216+
'VAL_1',
217+
'VAL_2',
218+
'VAL_3',
219+
'VAL_4',
220+
'VAL_5',
221+
'VAL_6',
222+
'VAL_7',
223+
'VAL_8',
224+
'VAL_9',
225+
'VAL_10',
226+
'VAL_11',
227+
'VAL_12',
228+
'VAL_13',
229+
'VAL_14',
230+
'VAL_15',
231+
'VAL_16',
232+
'VAL_17',
233+
'VAL_18',
234+
'VAL_19',
235+
'VAL_20',
236+
'VAL_21',
237+
],
238+
},
239+
},
240+
],
241+
},
242+
},
243+
},
244+
},
245+
errors: [
246+
{
247+
code: 'xgen-IPA-123-enum-values-should-not-exceed-20',
248+
message: 'Enum arrays should not exceed 20 values. Current count: 21',
249+
path: ['paths', '/resources', 'get', 'parameters', '0', 'schema', 'enum'],
250+
severity: DiagnosticSeverity.Warning,
251+
},
252+
],
253+
},
254+
]);

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
functions:
55
- IPA123EachEnumValueMustBeUpperSnakeCase
6+
- IPA123EnumValuesShouldNotExceed20
67

78
rules:
89
xgen-IPA-123-enum-values-must-be-upper-snake-case:
@@ -17,6 +18,22 @@ rules:
1718
- Skips validation if the schema has an exception defined for this rule
1819
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-123-enum-values-must-be-upper-snake-case'
1920
severity: error
21+
resolved: false
2022
given: '$..enum'
2123
then:
2224
function: 'IPA123EachEnumValueMustBeUpperSnakeCase'
25+
xgen-IPA-123-enum-values-should-not-exceed-20:
26+
description: |
27+
Enum values should not exceed 20 entries.
28+
29+
##### Implementation details
30+
Rule checks for the following conditions:
31+
- Applies to all enum value arrays defined in the OpenAPI schema
32+
- Validates that each enum array has 20 or fewer values
33+
- Skips validation if the schema has an exception defined for this rule
34+
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-123-enum-values-should-not-exceed-20'
35+
severity: warn
36+
resolved: false
37+
given: '$..enum'
38+
then:
39+
function: 'IPA123EnumValuesShouldNotExceed20'

tools/spectral/ipa/rulesets/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,17 @@ Rule checks for the following conditions:
830830
- Validates each enum value individually against the UPPER_SNAKE_CASE pattern
831831
- Skips validation if the schema has an exception defined for this rule
832832

833+
#### xgen-IPA-123-enum-values-should-not-exceed-20
834+
835+
![warn](https://img.shields.io/badge/warning-yellow)
836+
Enum values should not exceed 20 entries.
837+
838+
##### Implementation details
839+
Rule checks for the following conditions:
840+
- Applies to all enum value arrays defined in the OpenAPI schema
841+
- Validates that each enum array has 20 or fewer values
842+
- Skips validation if the schema has an exception defined for this rule
843+
833844

834845

835846
### IPA-125
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { hasException } from './utils/exceptions.js';
2+
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
3+
import { getSchemaPathFromEnumPath } from './utils/schemaUtils.js';
4+
import { resolveObject } from './utils/componentUtils.js';
5+
6+
const RULE_NAME = 'xgen-IPA-123-enum-values-should-not-exceed-20';
7+
const ERROR_MESSAGE = 'Enum arrays should not exceed 20 values. Current count: ';
8+
const MAX_ENUM_VALUES = 20;
9+
10+
export default (input, options, { path, documentInventory }) => {
11+
const oas = documentInventory.resolved;
12+
const schemaPath = getSchemaPathFromEnumPath(path);
13+
const schemaObject = resolveObject(oas, schemaPath);
14+
15+
// Check for exceptions
16+
if (hasException(schemaObject, RULE_NAME)) {
17+
collectException(schemaObject, RULE_NAME, path);
18+
return;
19+
}
20+
21+
if (!Array.isArray(input)) {
22+
return;
23+
}
24+
25+
if (input.length > MAX_ENUM_VALUES) {
26+
return collectAndReturnViolation(path, RULE_NAME, [
27+
{
28+
path,
29+
message: `${ERROR_MESSAGE}${input.length}`,
30+
},
31+
]);
32+
}
33+
34+
collectAdoption(path, RULE_NAME);
35+
};

0 commit comments

Comments
 (0)