|
| 1 | +# ATProto Lexicon Style Guide |
| 2 | + |
| 3 | +This document summarizes the ATProto Lexicon Style Guide rules that are checked by our automated checker. For the complete guide, visit: https://atproto.com/guides/lexicon-style-guide |
| 4 | + |
| 5 | +## Key Style Rules |
| 6 | + |
| 7 | +### 1. Naming Conventions |
| 8 | + |
| 9 | +#### Lexicon IDs (NSIDs) |
| 10 | + |
| 11 | +- **Format**: Reverse-DNS notation (e.g., `org.hypercerts.claim.activity`) |
| 12 | +- **Case**: All lowercase |
| 13 | +- **Segments**: Use descriptive, concise names |
| 14 | +- **Avoid**: Numbers in authority segments, generic names like "item" or "thing" |
| 15 | + |
| 16 | +#### Record Keys |
| 17 | + |
| 18 | +- **Recommendation**: Use `any` for most record keys to allow flexible client-side naming |
| 19 | +- **TIDs**: When using TIDs (Timestamp IDs), ensure they're appropriate for the use case |
| 20 | +- **Literal keys**: Only use for singleton records or very specific use cases |
| 21 | + |
| 22 | +#### Property Names |
| 23 | + |
| 24 | +- **Format**: camelCase |
| 25 | +- **Clarity**: Use clear, descriptive names |
| 26 | +- **Avoid**: Abbreviations unless widely understood |
| 27 | +- **Consistency**: Use consistent terminology across related lexicons |
| 28 | + |
| 29 | +### 2. Descriptions |
| 30 | + |
| 31 | +#### Lexicon Descriptions |
| 32 | + |
| 33 | +- **Completeness**: Every lexicon should have a clear description |
| 34 | +- **Detail**: Explain the purpose and usage |
| 35 | +- **Context**: Provide enough information for developers to understand |
| 36 | + |
| 37 | +#### Property Descriptions |
| 38 | + |
| 39 | +- **Required**: All properties should have descriptions |
| 40 | +- **Clarity**: Explain what the property represents and how it should be used |
| 41 | +- **Format hints**: Include format expectations when relevant |
| 42 | + |
| 43 | +#### Definition Descriptions |
| 44 | + |
| 45 | +- **Coverage**: All definitions (objects, arrays, etc.) should have descriptions |
| 46 | +- **Purpose**: Explain the role of the definition in the schema |
| 47 | + |
| 48 | +### 3. Schema Design |
| 49 | + |
| 50 | +#### Type Selection |
| 51 | + |
| 52 | +- **Primitives**: Use appropriate primitive types (string, integer, boolean) |
| 53 | +- **Formats**: Apply format constraints (datetime, uri, cid, did, etc.) |
| 54 | +- **Refs**: Use refs for reusable types and relationships |
| 55 | +- **Unions**: Use unions when a property can be one of several types |
| 56 | + |
| 57 | +#### Constraints |
| 58 | + |
| 59 | +- **String lengths**: Always set maxLength for strings |
| 60 | +- **maxGraphemes**: Consider using maxGraphemes for user-visible text |
| 61 | +- **Array constraints**: Consider maxLength for arrays |
| 62 | +- **Required fields**: Mark truly required fields, keep optional what can be optional |
| 63 | + |
| 64 | +#### Blob Handling |
| 65 | + |
| 66 | +- **Size limits**: Always specify maxSize for blobs |
| 67 | +- **MIME types**: Specify accepted MIME types with the `accept` property |
| 68 | +- **Documentation**: Document blob size limits and types clearly |
| 69 | + |
| 70 | +### 4. Relationships and References |
| 71 | + |
| 72 | +#### Strong References |
| 73 | + |
| 74 | +- **Usage**: Use `com.atproto.repo.strongRef` for references to other records |
| 75 | +- **Documentation**: Clearly document what type of record is expected |
| 76 | +- **Validation**: Specify the expected lexicon type in the description |
| 77 | + |
| 78 | +#### Arrays of References |
| 79 | + |
| 80 | +- **Consistency**: Use consistent patterns for arrays of references |
| 81 | +- **Documentation**: Explain the relationship and cardinality |
| 82 | + |
| 83 | +### 5. Timestamps |
| 84 | + |
| 85 | +#### DateTime Fields |
| 86 | + |
| 87 | +- **Format**: Always use `format: "datetime"` for timestamp fields |
| 88 | +- **Naming**: Use clear names like `createdAt`, `updatedAt`, `publishedAt` |
| 89 | +- **Requirement**: Mark as required when appropriate |
| 90 | +- **Documentation**: Explain when the timestamp should be set |
| 91 | + |
| 92 | +### 6. Common Patterns |
| 93 | + |
| 94 | +#### Created At |
| 95 | + |
| 96 | +- **Standard**: Include a `createdAt` field for records |
| 97 | +- **Type**: `string` with `format: "datetime"` |
| 98 | +- **Description**: "Client-declared timestamp when this record was originally created" |
| 99 | + |
| 100 | +#### Updated At |
| 101 | + |
| 102 | +- **Usage**: Include `updatedAt` for mutable records |
| 103 | +- **Type**: `string` with `format: "datetime"` |
| 104 | +- **Description**: "Client-declared timestamp when this record was last updated" |
| 105 | + |
| 106 | +#### Versioning |
| 107 | + |
| 108 | +- **Consideration**: Think about versioning strategy for evolving schemas |
| 109 | +- **Migration**: Document breaking changes |
| 110 | + |
| 111 | +### 7. Validation and Constraints |
| 112 | + |
| 113 | +#### String Validation |
| 114 | + |
| 115 | +- **maxLength**: Always set for strings (prevents abuse) |
| 116 | +- **maxGraphemes**: Use for user-facing text (better UX for internationalization) |
| 117 | +- **Patterns**: Use regex patterns when format validation is needed |
| 118 | + |
| 119 | +#### Number Validation |
| 120 | + |
| 121 | +- **Range**: Consider minimum and maximum values |
| 122 | +- **Integers**: Use integer type when fractional values don't make sense |
| 123 | + |
| 124 | +#### Required vs Optional |
| 125 | + |
| 126 | +- **Conservative**: Only mark as required if truly necessary |
| 127 | +- **Flexibility**: Allow optional fields for future extensibility |
| 128 | +- **Documentation**: Explain when optional fields should be used |
| 129 | + |
| 130 | +### 8. Documentation Best Practices |
| 131 | + |
| 132 | +#### Inline Documentation |
| 133 | + |
| 134 | +- **Comprehensive**: Document all types, properties, and constraints |
| 135 | +- **Examples**: Include example values in descriptions when helpful |
| 136 | +- **Context**: Explain relationships and dependencies |
| 137 | + |
| 138 | +#### External Documentation |
| 139 | + |
| 140 | +- **README**: Maintain high-level documentation |
| 141 | +- **Migration guides**: Document schema changes and migrations |
| 142 | +- **Examples**: Provide usage examples |
| 143 | + |
| 144 | +### 9. Lexicon Organization |
| 145 | + |
| 146 | +#### File Structure |
| 147 | + |
| 148 | +- **Grouping**: Group related lexicons in namespaces |
| 149 | +- **Definitions**: Use `defs.json` for shared definitions |
| 150 | +- **Modularity**: Keep lexicons focused and modular |
| 151 | + |
| 152 | +#### Dependencies |
| 153 | + |
| 154 | +- **Explicit**: Clearly document dependencies on other lexicons |
| 155 | +- **Minimal**: Minimize cross-lexicon dependencies |
| 156 | +- **Standard**: Use standard ATProto lexicons when appropriate |
| 157 | + |
| 158 | +### 10. Security and Privacy |
| 159 | + |
| 160 | +#### Data Sensitivity |
| 161 | + |
| 162 | +- **PII**: Be careful with personally identifiable information |
| 163 | +- **Access control**: Consider who can read/write records |
| 164 | +- **Validation**: Validate all input data |
| 165 | + |
| 166 | +#### Size Limits |
| 167 | + |
| 168 | +- **DoS prevention**: Set appropriate size limits on all fields |
| 169 | +- **Blob limits**: Be conservative with blob size limits |
| 170 | +- **Array limits**: Limit array sizes to prevent abuse |
| 171 | + |
| 172 | +## Automated Checks |
| 173 | + |
| 174 | +The `style:check` script checks for: |
| 175 | + |
| 176 | +1. ✅ All lexicons have descriptions |
| 177 | +2. ✅ All properties have descriptions |
| 178 | +3. ✅ All definitions have descriptions |
| 179 | +4. ✅ String properties have maxLength constraints |
| 180 | +5. ✅ Blob properties have maxSize and accept properties |
| 181 | +6. ✅ DateTime fields use the correct format |
| 182 | +7. ✅ Property names use camelCase |
| 183 | +8. ✅ Required fields are properly marked |
| 184 | +9. ✅ StrongRef usage is documented |
| 185 | +10. ✅ Lexicon IDs follow naming conventions |
| 186 | + |
| 187 | +## Running the Checker |
| 188 | + |
| 189 | +```bash |
| 190 | +npm run style:check |
| 191 | +``` |
| 192 | + |
| 193 | +This will check all lexicons in the `lexicons/` directory and report any style guide violations. |
0 commit comments