Skip to content

Commit 9f534c9

Browse files
committed
Add common field name variants to AEP-143 detection
1 parent bbe3ed5 commit 9f534c9

File tree

3 files changed

+136
-7
lines changed

3 files changed

+136
-7
lines changed

docs/0143.md

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,40 @@ for common concepts like languages, currencies, regions, and media types.
1818
This rule checks schema property names and flags incorrect variants, suggesting
1919
the correct standardized field name according to AEP-143.
2020

21+
#### Limitations
22+
23+
This rule uses a **map-based detection approach** that only catches explicitly
24+
listed field name variants. While it covers the most common incorrect naming
25+
patterns, it cannot detect all possible variations developers might use.
26+
27+
**What this means:**
28+
29+
- ✅ Catches common mistakes like `lang`, `country`, `mime_type`, etc.
30+
- ❌ Won't catch creative variations like `user_locale`, `iso_country`, or
31+
`document_mime`
32+
- ✅ Low false positive rate - only flags known incorrect patterns
33+
- ❌ May miss some non-standard field names that should use standardized codes
34+
35+
**Why this approach:**
36+
37+
Pattern-based detection (e.g., flagging any field containing "language" or
38+
"country") would produce too many false positives. The map-based approach is
39+
intentionally conservative to maintain high precision.
40+
41+
**Recommendation:** Use this rule as a first-pass check, but rely on human code
42+
review to catch edge cases and uncommon field name variations. If you encounter
43+
a common variant that should be flagged, consider contributing it to the
44+
linter.
45+
2146
### Field Name Standards
2247

23-
| Concept | Correct Field Name | Incorrect Variants | Standard |
24-
| ------------------ | ------------------ | ------------------------------- | ---------------- |
25-
| Language | `language_code` | `lang`, `language` | IETF BCP-47 |
26-
| Country/Region | `region_code` | `country`, `country_code` | Unicode CLDR |
27-
| Currency | `currency_code` | `currency` | ISO-4217 |
28-
| Content/Media Type | `content_type` | `mime`, `mimetype`, `mime_type` | IANA media types |
29-
| Time Zone | `time_zone` | `tz`, `timezone` | IANA TZ |
48+
| Concept | Correct Field Name | Incorrect Variants | Standard |
49+
| ------------------ | ------------------ | ---------------------------------------------------------- | ---------------- |
50+
| Language | `language_code` | `lang`, `language`, `locale` | IETF BCP-47 |
51+
| Country/Region | `region_code` | `country`, `country_code`, `region` | Unicode CLDR |
52+
| Currency | `currency_code` | `currency` | ISO-4217 |
53+
| Content/Media Type | `content_type` | `mime`, `mimetype`, `mime_type`, `media_type`, `mediatype` | IANA media types |
54+
| Time Zone | `time_zone` | `tz`, `timezone` | IANA TZ |
3055

3156
### Examples
3257

functions/standardized-codes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ const fieldNameVariants = {
77
mime: 'content_type',
88
mimetype: 'content_type',
99
mime_type: 'content_type',
10+
media_type: 'content_type',
11+
mediatype: 'content_type',
1012
// Countries/Regions
1113
country: 'region_code',
1214
country_code: 'region_code',
15+
region: 'region_code',
1316
// Currency
1417
currency: 'currency_code',
1518
// Language
1619
lang: 'language_code',
1720
language: 'language_code',
21+
locale: 'language_code',
1822
// Time zones
1923
tz: 'time_zone',
2024
timezone: 'time_zone',

test/0143/standardized-codes.test.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,106 @@ describe('aep-143-standardized-codes', () => {
205205
});
206206
});
207207

208+
test('should flag "region" and suggest "region_code"', () => {
209+
const oasDoc = {
210+
openapi: '3.0.3',
211+
components: {
212+
schemas: {
213+
Address: {
214+
type: 'object',
215+
properties: {
216+
region: {
217+
type: 'string',
218+
},
219+
},
220+
},
221+
},
222+
},
223+
};
224+
return linter.run(oasDoc).then((results) => {
225+
expect(results.length).toBe(1);
226+
expect(results).toContainMatch({
227+
path: ['components', 'schemas', 'Address', 'properties', 'region'],
228+
message: 'Use "region_code" instead of "region" for standardized code fields.',
229+
});
230+
});
231+
});
232+
233+
test('should flag "locale" and suggest "language_code"', () => {
234+
const oasDoc = {
235+
openapi: '3.0.3',
236+
components: {
237+
schemas: {
238+
Book: {
239+
type: 'object',
240+
properties: {
241+
locale: {
242+
type: 'string',
243+
},
244+
},
245+
},
246+
},
247+
},
248+
};
249+
return linter.run(oasDoc).then((results) => {
250+
expect(results.length).toBe(1);
251+
expect(results).toContainMatch({
252+
path: ['components', 'schemas', 'Book', 'properties', 'locale'],
253+
message: 'Use "language_code" instead of "locale" for standardized code fields.',
254+
});
255+
});
256+
});
257+
258+
test('should flag "media_type" and suggest "content_type"', () => {
259+
const oasDoc = {
260+
openapi: '3.0.3',
261+
components: {
262+
schemas: {
263+
File: {
264+
type: 'object',
265+
properties: {
266+
media_type: {
267+
type: 'string',
268+
},
269+
},
270+
},
271+
},
272+
},
273+
};
274+
return linter.run(oasDoc).then((results) => {
275+
expect(results.length).toBe(1);
276+
expect(results).toContainMatch({
277+
path: ['components', 'schemas', 'File', 'properties', 'media_type'],
278+
message: 'Use "content_type" instead of "media_type" for standardized code fields.',
279+
});
280+
});
281+
});
282+
283+
test('should flag "mediatype" and suggest "content_type"', () => {
284+
const oasDoc = {
285+
openapi: '3.0.3',
286+
components: {
287+
schemas: {
288+
File: {
289+
type: 'object',
290+
properties: {
291+
mediatype: {
292+
type: 'string',
293+
},
294+
},
295+
},
296+
},
297+
},
298+
};
299+
return linter.run(oasDoc).then((results) => {
300+
expect(results.length).toBe(1);
301+
expect(results).toContainMatch({
302+
path: ['components', 'schemas', 'File', 'properties', 'mediatype'],
303+
message: 'Use "content_type" instead of "mediatype" for standardized code fields.',
304+
});
305+
});
306+
});
307+
208308
test('should not flag correct standardized field names', () => {
209309
const oasDoc = {
210310
openapi: '3.0.3',

0 commit comments

Comments
 (0)