|
| 1 | +# Field Name Validation Fix - Technical Summary |
| 2 | + |
| 3 | +## Issue Report (Chinese) |
| 4 | +按规则,字段名必须是 snake_case,这里的代码是怎么 test 通过的?为什么启动没有报错? |
| 5 | + |
| 6 | +**Translation:** |
| 7 | +According to the rules, field names must be snake_case. How did this code pass the tests? Why didn't it report an error at startup? |
| 8 | + |
| 9 | +## Root Cause Analysis |
| 10 | + |
| 11 | +### The Problem |
| 12 | +The ObjectStack Protocol specification mandates snake_case naming for field names, but this validation was not being enforced in practice. |
| 13 | + |
| 14 | +### Investigation Findings |
| 15 | + |
| 16 | +1. **Field Schema Validation EXISTS** (but wasn't running): |
| 17 | + - Location: `packages/spec/src/data/field.zod.ts:346` |
| 18 | + - Regex: `/^[a-z_][a-z0-9_]*$/` |
| 19 | + - Field `name` property is optional and validated when provided |
| 20 | + |
| 21 | +2. **Object Schema Validation EXISTS** (but wasn't running): |
| 22 | + - Location: `packages/spec/src/data/object.zod.ts:263-265` |
| 23 | + - Field **keys** in `fields` record validated with same regex |
| 24 | + - Custom error message: "Field names must be lowercase snake_case" |
| 25 | + |
| 26 | +3. **defineStack() Bypassed Validation**: |
| 27 | + - Location: `packages/spec/src/stack.zod.ts:335-337` |
| 28 | + - Default was `strict: false` |
| 29 | + - When `strict: false`, returned config as-is **without any validation** |
| 30 | + - Code: `if (!options?.strict) { return config as ObjectStackDefinition; }` |
| 31 | + |
| 32 | +### Why Tests Passed |
| 33 | + |
| 34 | +The test suite was passing because: |
| 35 | +- Tests that checked validation explicitly used `ObjectSchema.parse()` |
| 36 | +- Tests that used `defineStack()` relied on default `strict: false` behavior |
| 37 | +- The validation logic itself was correct, just never executed |
| 38 | +- Examples used `as any` casts to bypass TypeScript checking |
| 39 | + |
| 40 | +### Why No Runtime Errors |
| 41 | + |
| 42 | +Runtime didn't fail because: |
| 43 | +- `defineStack()` with default options skipped validation entirely |
| 44 | +- Invalid field names were accepted and passed through |
| 45 | +- Only when strict=true was explicitly set would validation run |
| 46 | + |
| 47 | +## Solution Implemented |
| 48 | + |
| 49 | +### Core Fix |
| 50 | +Changed `defineStack()` default from `strict: false` to `strict: true`: |
| 51 | + |
| 52 | +```typescript |
| 53 | +// Before (packages/spec/src/stack.zod.ts) |
| 54 | +/** |
| 55 | + * @default false // ❌ BAD: No validation by default |
| 56 | + */ |
| 57 | +strict?: boolean; |
| 58 | + |
| 59 | +if (!options?.strict) { |
| 60 | + return config as ObjectStackDefinition; // ❌ Skip validation |
| 61 | +} |
| 62 | + |
| 63 | +// After |
| 64 | +/** |
| 65 | + * @default true // ✅ GOOD: Validate by default |
| 66 | + */ |
| 67 | +strict?: boolean; |
| 68 | + |
| 69 | +const strict = options?.strict !== false; // ✅ Default to true |
| 70 | + |
| 71 | +if (!strict) { |
| 72 | + return config as ObjectStackDefinition; // Only skip if explicitly requested |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +### Additional Changes |
| 77 | + |
| 78 | +1. **Updated Tests** (`packages/spec/src/stack.test.ts`): |
| 79 | + - Fixed test expecting old behavior |
| 80 | + - Added 4 new field name validation tests |
| 81 | + - Verified camelCase/PascalCase rejection |
| 82 | + - Verified snake_case acceptance |
| 83 | + - Verified strict=false bypass |
| 84 | + |
| 85 | +2. **Added Validation Tests** (`packages/spec/src/data/field-name-validation.test.ts`): |
| 86 | + - Direct FieldSchema validation tests |
| 87 | + - Confirms regex works correctly |
| 88 | + |
| 89 | +3. **Created Changeset** (`.changeset/strict-validation-by-default.md`): |
| 90 | + - Documents breaking change |
| 91 | + - Migration guide for users |
| 92 | + - Explains benefits and impact |
| 93 | + |
| 94 | +## Validation Rules Reference |
| 95 | + |
| 96 | +### Valid Field Names (snake_case) |
| 97 | +```typescript |
| 98 | +✅ first_name |
| 99 | +✅ last_name |
| 100 | +✅ email |
| 101 | +✅ company_name |
| 102 | +✅ annual_revenue |
| 103 | +✅ _system_id |
| 104 | +``` |
| 105 | + |
| 106 | +### Invalid Field Names |
| 107 | +```typescript |
| 108 | +❌ firstName // camelCase |
| 109 | +❌ FirstName // PascalCase |
| 110 | +❌ first-name // kebab-case |
| 111 | +❌ first name // spaces |
| 112 | +❌ 123field // starts with number |
| 113 | +❌ first.name // dots |
| 114 | +``` |
| 115 | + |
| 116 | +## Validation Flow |
| 117 | + |
| 118 | +### With strict=true (NEW DEFAULT) |
| 119 | +``` |
| 120 | +defineStack(config) |
| 121 | + ↓ |
| 122 | +ObjectStackDefinitionSchema.safeParse(config) |
| 123 | + ↓ |
| 124 | +ObjectSchema validation |
| 125 | + ↓ |
| 126 | +fields: z.record(regex validation on keys) |
| 127 | + ↓ |
| 128 | +Reject if any field key is not snake_case |
| 129 | + ↓ |
| 130 | +Cross-reference validation |
| 131 | + ↓ |
| 132 | +Return validated data |
| 133 | +``` |
| 134 | + |
| 135 | +### With strict=false (OPT-OUT) |
| 136 | +``` |
| 137 | +defineStack(config, { strict: false }) |
| 138 | + ↓ |
| 139 | +Return config as-is (NO VALIDATION) |
| 140 | +``` |
| 141 | + |
| 142 | +## Impact Assessment |
| 143 | + |
| 144 | +### Breaking Change |
| 145 | +This is a **major version breaking change** because: |
| 146 | +- Existing code with invalid field names will now fail |
| 147 | +- Default behavior changed from permissive to strict |
| 148 | +- Migration required for affected projects |
| 149 | + |
| 150 | +### Benefits |
| 151 | +1. **Early Error Detection**: Catches naming violations during development |
| 152 | +2. **Convention Enforcement**: Ensures consistent snake_case naming |
| 153 | +3. **Database Compatibility**: Prevents case-sensitivity issues in SQL queries |
| 154 | +4. **AI Safety**: Prevents AI-generated objects from violating conventions |
| 155 | +5. **Better DX**: Clear error messages guide developers to fix issues |
| 156 | + |
| 157 | +### Migration Path |
| 158 | + |
| 159 | +**Option 1: Fix Field Names (Recommended)** |
| 160 | +```typescript |
| 161 | +// Change field definitions to use snake_case |
| 162 | +fields: { |
| 163 | + first_name: Field.text(), // ✅ Fixed |
| 164 | + last_name: Field.text(), // ✅ Fixed |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +**Option 2: Temporary Bypass (Not Recommended)** |
| 169 | +```typescript |
| 170 | +// Use strict: false to temporarily disable validation |
| 171 | +defineStack(config, { strict: false }); |
| 172 | +``` |
| 173 | + |
| 174 | +## Test Results |
| 175 | + |
| 176 | +✅ **All tests passing**: 195 test files, 5251 tests |
| 177 | +✅ **Examples verified**: All use correct snake_case naming |
| 178 | +✅ **Validation confirmed**: Correctly rejects camelCase/PascalCase |
| 179 | +✅ **Validation confirmed**: Correctly accepts snake_case |
| 180 | +✅ **Backward compatibility**: strict=false still works for edge cases |
| 181 | + |
| 182 | +## Files Modified |
| 183 | + |
| 184 | +1. `packages/spec/src/stack.zod.ts` - Changed default to strict=true |
| 185 | +2. `packages/spec/src/stack.test.ts` - Updated tests for new behavior |
| 186 | +3. `packages/spec/src/data/field-name-validation.test.ts` - New validation tests |
| 187 | +4. `.changeset/strict-validation-by-default.md` - Breaking change documentation |
| 188 | + |
| 189 | +## Conclusion |
| 190 | + |
| 191 | +The issue was that field name validation **existed but was disabled by default**. By changing `defineStack()` to validate by default, we now enforce the snake_case naming convention that was always part of the specification but wasn't being checked in practice. |
| 192 | + |
| 193 | +This fix ensures that the rule "字段名必须是 snake_case" (field names must be snake_case) is now actually enforced, preventing issues like the one reported by the user. |
0 commit comments