|
| 1 | +# Rules for AEP-143: Standardized codes |
| 2 | + |
| 3 | +[aep-143]: https://aep.dev/143 |
| 4 | + |
| 5 | +This document covers the linting rules for [AEP-143][], which provides guidance |
| 6 | +on using standardized codes for common concepts like languages, regions, |
| 7 | +currencies, and time zones. |
| 8 | + |
| 9 | +## aep-143-standardized-codes |
| 10 | + |
| 11 | +**Rule**: Field names for standardized codes must follow AEP-143 conventions. |
| 12 | + |
| 13 | +This rule enforces that field names use the correct standardized terminology |
| 14 | +for common concepts like languages, currencies, regions, and media types. |
| 15 | + |
| 16 | +### Details |
| 17 | + |
| 18 | +This rule checks schema property names and flags incorrect variants, suggesting |
| 19 | +the correct standardized field name according to AEP-143. |
| 20 | + |
| 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 | + |
| 46 | +### Field Name Standards |
| 47 | + |
| 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 | |
| 55 | + |
| 56 | +### Examples |
| 57 | + |
| 58 | +**Incorrect** code for this rule: |
| 59 | + |
| 60 | +```yaml |
| 61 | +components: |
| 62 | + schemas: |
| 63 | + book: |
| 64 | + type: object |
| 65 | + properties: |
| 66 | + title: |
| 67 | + type: string |
| 68 | + lang: # Should be language_code |
| 69 | + type: string |
| 70 | + country: # Should be region_code |
| 71 | + type: string |
| 72 | +``` |
| 73 | +
|
| 74 | +**Correct** code for this rule: |
| 75 | +
|
| 76 | +```yaml |
| 77 | +components: |
| 78 | + schemas: |
| 79 | + book: |
| 80 | + type: object |
| 81 | + properties: |
| 82 | + title: |
| 83 | + type: string |
| 84 | + language_code: |
| 85 | + type: string |
| 86 | + description: IETF BCP-47 language code (e.g., en-US, de-CH) |
| 87 | + region_code: |
| 88 | + type: string |
| 89 | + description: Unicode CLDR region code (e.g., US, CH) |
| 90 | +``` |
| 91 | +
|
| 92 | +### Disabling |
| 93 | +
|
| 94 | +If you need to violate this rule, add an override to your Spectral |
| 95 | +configuration: |
| 96 | +
|
| 97 | +```yaml |
| 98 | +overrides: |
| 99 | + - files: |
| 100 | + - 'openapi.json#/components/schemas/book' |
| 101 | + rules: |
| 102 | + aep-143-standardized-codes: 'off' |
| 103 | +``` |
| 104 | +
|
| 105 | +## aep-143-standardized-codes-string-type |
| 106 | +
|
| 107 | +**Rule**: Standardized code fields must be of type string. |
| 108 | +
|
| 109 | +This rule enforces that all standardized code fields (language_code, |
| 110 | +region_code, currency_code, content_type, time_zone, utc_offset) use type |
| 111 | +`string`. |
| 112 | + |
| 113 | +### Details |
| 114 | + |
| 115 | +According to AEP-143, standardized code fields must use the appropriate data |
| 116 | +type for standard codes (usually `string`), and should not use enums even if |
| 117 | +they only allow a small subset of possible values. |
| 118 | + |
| 119 | +This rule checks the following fields: |
| 120 | + |
| 121 | +- `language_code` |
| 122 | +- `region_code` |
| 123 | +- `currency_code` |
| 124 | +- `content_type` |
| 125 | +- `time_zone` |
| 126 | +- `utc_offset` |
| 127 | + |
| 128 | +### Examples |
| 129 | + |
| 130 | +**Incorrect** code for this rule: |
| 131 | + |
| 132 | +```yaml |
| 133 | +components: |
| 134 | + schemas: |
| 135 | + book: |
| 136 | + type: object |
| 137 | + properties: |
| 138 | + language_code: |
| 139 | + type: integer # Wrong type |
| 140 | + description: Language code |
| 141 | +``` |
| 142 | + |
| 143 | +**Correct** code for this rule: |
| 144 | + |
| 145 | +```yaml |
| 146 | +components: |
| 147 | + schemas: |
| 148 | + book: |
| 149 | + type: object |
| 150 | + properties: |
| 151 | + language_code: |
| 152 | + type: string |
| 153 | + description: IETF BCP-47 language code (e.g., en-US, de-CH) |
| 154 | +``` |
| 155 | + |
| 156 | +### Disabling |
| 157 | + |
| 158 | +If you need to violate this rule, add an override to your Spectral |
| 159 | +configuration: |
| 160 | + |
| 161 | +```yaml |
| 162 | +overrides: |
| 163 | + - files: |
| 164 | + - 'openapi.json#/components/schemas/book/properties/language_code' |
| 165 | + rules: |
| 166 | + aep-143-standardized-codes-string-type: 'off' |
| 167 | +``` |
| 168 | + |
| 169 | +## References |
| 170 | + |
| 171 | +- [AEP-143: Standardized codes][aep-143] |
| 172 | +- [IETF BCP-47 Language Tags](https://en.wikipedia.org/wiki/IETF_language_tag) |
| 173 | +- [Unicode CLDR Region Codes](http://cldr.unicode.org/) |
| 174 | +- [ISO-4217 Currency Codes](https://en.wikipedia.org/wiki/ISO_4217) |
| 175 | +- [IANA Media Types](https://www.iana.org/assignments/media-types/) |
| 176 | +- [IANA Time Zones](http://www.iana.org/time-zones) |
0 commit comments