Skip to content

Commit b1797a6

Browse files
CLOUDP-272000: Boolean field names avoid "is" prefix
1 parent 0f087ce commit b1797a6

File tree

4 files changed

+235
-0
lines changed

4 files changed

+235
-0
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import testRule from './__helpers__/testRule';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
testRule('xgen-IPA-112-boolean-field-names-avoid-is-prefix', [
5+
{
6+
name: 'valid schema - no "is" prefix in boolean fields',
7+
document: {
8+
components: {
9+
schemas: {
10+
SchemaName: {
11+
properties: {
12+
enabled: { type: 'boolean' },
13+
active: { type: 'boolean' },
14+
disabled: { type: 'boolean' },
15+
isString: { type: 'string' },
16+
visible: { type: 'boolean' },
17+
},
18+
},
19+
},
20+
},
21+
paths: {
22+
'/users': {
23+
post: {
24+
requestBody: {
25+
content: {
26+
'application/vnd.atlas.2024-01-01+json': {
27+
schema: {
28+
type: 'object',
29+
properties: {
30+
available: { type: 'boolean' },
31+
paused: { type: 'boolean' },
32+
},
33+
},
34+
},
35+
},
36+
},
37+
responses: {
38+
201: {
39+
content: {
40+
'application/vnd.atlas.2024-01-01+json': {
41+
schema: {
42+
type: 'object',
43+
properties: {
44+
valid: { type: 'boolean' },
45+
hidden: { type: 'boolean' },
46+
},
47+
},
48+
},
49+
},
50+
},
51+
},
52+
},
53+
},
54+
},
55+
},
56+
errors: [],
57+
},
58+
{
59+
name: 'invalid schema - with "is" prefix in boolean fields',
60+
document: {
61+
components: {
62+
schemas: {
63+
SchemaName: {
64+
properties: {
65+
isEnabled: { type: 'boolean' },
66+
isActive: { type: 'boolean' },
67+
isError: { type: 'boolean' },
68+
isString: { type: 'string' },
69+
},
70+
},
71+
},
72+
},
73+
paths: {
74+
'/users': {
75+
post: {
76+
requestBody: {
77+
content: {
78+
'application/vnd.atlas.2024-01-01+json': {
79+
schema: {
80+
type: 'object',
81+
properties: {
82+
isAvailable: { type: 'boolean' },
83+
},
84+
},
85+
},
86+
},
87+
},
88+
responses: {
89+
201: {
90+
content: {
91+
'application/vnd.atlas.2024-01-01+json': {
92+
schema: {
93+
type: 'object',
94+
properties: {
95+
isValid: { type: 'boolean' },
96+
},
97+
},
98+
},
99+
},
100+
},
101+
},
102+
},
103+
},
104+
},
105+
},
106+
errors: [
107+
{
108+
code: 'xgen-IPA-112-boolean-field-names-avoid-is-prefix',
109+
message: 'Boolean field "isEnabled" should not use the "is" prefix. Use "enabled" instead.',
110+
path: ['components', 'schemas', 'SchemaName', 'properties', 'isEnabled'],
111+
severity: DiagnosticSeverity.Warning,
112+
},
113+
{
114+
code: 'xgen-IPA-112-boolean-field-names-avoid-is-prefix',
115+
message: 'Boolean field "isActive" should not use the "is" prefix. Use "active" instead.',
116+
path: ['components', 'schemas', 'SchemaName', 'properties', 'isActive'],
117+
severity: DiagnosticSeverity.Warning,
118+
},
119+
{
120+
code: 'xgen-IPA-112-boolean-field-names-avoid-is-prefix',
121+
message: 'Boolean field "isError" should not use the "is" prefix. Use "error" instead.',
122+
path: ['components', 'schemas', 'SchemaName', 'properties', 'isError'],
123+
severity: DiagnosticSeverity.Warning,
124+
},
125+
{
126+
code: 'xgen-IPA-112-boolean-field-names-avoid-is-prefix',
127+
message: 'Boolean field "isAvailable" should not use the "is" prefix. Use "available" instead.',
128+
path: [
129+
'paths',
130+
'/users',
131+
'post',
132+
'requestBody',
133+
'content',
134+
'application/vnd.atlas.2024-01-01+json',
135+
'schema',
136+
'properties',
137+
'isAvailable',
138+
],
139+
severity: DiagnosticSeverity.Warning,
140+
},
141+
{
142+
code: 'xgen-IPA-112-boolean-field-names-avoid-is-prefix',
143+
message: 'Boolean field "isValid" should not use the "is" prefix. Use "valid" instead.',
144+
path: [
145+
'paths',
146+
'/users',
147+
'post',
148+
'responses',
149+
'201',
150+
'content',
151+
'application/vnd.atlas.2024-01-01+json',
152+
'schema',
153+
'properties',
154+
'isValid',
155+
],
156+
severity: DiagnosticSeverity.Warning,
157+
},
158+
],
159+
},
160+
{
161+
name: 'schema with exception - "is" prefix with exception',
162+
document: {
163+
components: {
164+
schemas: {
165+
SchemaName: {
166+
properties: {
167+
isEnabled: {
168+
type: 'boolean',
169+
'x-xgen-IPA-exception': {
170+
'xgen-IPA-112-boolean-field-names-avoid-is-prefix': 'Reason',
171+
},
172+
},
173+
},
174+
},
175+
},
176+
},
177+
},
178+
errors: [],
179+
},
180+
]);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
functions:
55
- IPA112AvoidProjectFieldNames
66
- IPA112FieldNamesAreCamelCase
7+
- IPA112BooleanFieldNamesAvoidIsPrefix
78

89
rules:
910
xgen-IPA-112-avoid-project-field-names:
@@ -50,3 +51,20 @@ rules:
5051
- '$.paths..responses..content[?(@property.match(/json$/i))].schema..properties[*]~'
5152
then:
5253
function: 'IPA112FieldNamesAreCamelCase'
54+
xgen-IPA-112-boolean-field-names-avoid-is-prefix:
55+
description: |
56+
Boolean field names should avoid the "is" prefix.
57+
58+
##### Implementation details
59+
Rule checks for the following conditions:
60+
- Applies only to properties with type 'boolean'
61+
- Identifies property names that start with "is" followed by an uppercase letter
62+
- Suggests using the direct adjective form instead (e.g., "disabled" instead of "isDisabled")
63+
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-112-boolean-field-names-avoid-is-prefix'
64+
severity: warn
65+
given:
66+
- '$.components.schemas..properties[*]~'
67+
- '$.paths..requestBody.content[?(@property.match(/json$/i))].schema..properties[*]~'
68+
- '$.paths..responses..content[?(@property.match(/json$/i))].schema..properties[*]~'
69+
then:
70+
function: 'IPA112BooleanFieldNamesAvoidIsPrefix'

tools/spectral/ipa/rulesets/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,17 @@ Rule checks for the following conditions:
475475
- Identifies property names that are not in camelCase format
476476
- Reports any instances where these field names are not in camelCase format
477477

478+
#### xgen-IPA-112-boolean-field-names-avoid-is-prefix
479+
480+
![warn](https://img.shields.io/badge/warning-yellow)
481+
Boolean field names should avoid the "is" prefix.
482+
483+
##### Implementation details
484+
Rule checks for the following conditions:
485+
- Applies only to properties with type 'boolean'
486+
- Identifies property names that start with "is" followed by an uppercase letter
487+
- Suggests using the direct adjective form instead (e.g., "disabled" instead of "isDisabled")
488+
478489

479490

480491
### IPA-113
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
2+
import { hasException } from './utils/exceptions.js';
3+
import { resolveObject } from './utils/componentUtils.js';
4+
5+
const RULE_NAME = 'xgen-IPA-112-boolean-field-names-avoid-is-prefix';
6+
const IS_PREFIX_REGEX = /^is[A-Z]/;
7+
8+
export default (input, options, { path, documentInventory }) => {
9+
const oas = documentInventory.resolved;
10+
const property = resolveObject(oas, path);
11+
12+
if (hasException(property, RULE_NAME)) {
13+
collectException(property, RULE_NAME, path);
14+
return;
15+
}
16+
17+
if (property.type === 'boolean') {
18+
if (IS_PREFIX_REGEX.test(input)) {
19+
const suggestedName = input.charAt(2).toLowerCase() + input.slice(3);
20+
const errorMessage = `Boolean field "${input}" should not use the "is" prefix. Use "${suggestedName}" instead.`;
21+
return collectAndReturnViolation(path, RULE_NAME, errorMessage);
22+
}
23+
}
24+
25+
collectAdoption(path, RULE_NAME);
26+
};

0 commit comments

Comments
 (0)