|
| 1 | +# AppSheet Field Type Support and Validation Enhancement |
| 2 | + |
| 3 | +## Overview |
| 4 | +Extend the AppSheet library to support all AppSheet-specific field types in schema definitions and implement comprehensive validation for required fields and type-specific constraints during add/update operations. |
| 5 | + |
| 6 | +## Problem Statement |
| 7 | +Currently, the library uses generic TypeScript types (`string`, `number`, `boolean`, `date`, `array`, `object`) which don't map directly to AppSheet's rich type system. This leads to: |
| 8 | + |
| 9 | +1. **Missing Type Information**: No way to specify AppSheet-specific types like `Enum`, `EnumList`, `Email`, `Phone`, `DateTime`, `Price`, etc. |
| 10 | +2. **Incomplete Enum Support**: Schema supports `enum` arrays but doesn't distinguish between `Enum` (single value) and `EnumList` (multiple values) |
| 11 | +3. **Limited Validation**: No validation for AppSheet-specific constraints (e.g., email format, phone format, price format) |
| 12 | +4. **Inconsistent Required Field Handling**: Required field validation only happens during `add()`, not consistently enforced |
| 13 | + |
| 14 | +## Current State |
| 15 | + |
| 16 | +### Current Schema Type System |
| 17 | +```typescript |
| 18 | +// src/types/schema.ts |
| 19 | +export type FieldType = 'string' | 'number' | 'boolean' | 'date' | 'array' | 'object'; |
| 20 | + |
| 21 | +export interface FieldDefinition { |
| 22 | + type: FieldType; |
| 23 | + required?: boolean; |
| 24 | + enum?: string[]; // Old enum property |
| 25 | + description?: string; |
| 26 | +} |
| 27 | + |
| 28 | +// Fields could be defined as: |
| 29 | +// - Simple string: "email": "string" |
| 30 | +// - Full object: "email": { type: "string", required: true } |
| 31 | +``` |
| 32 | + |
| 33 | +### Current Validation (DynamicTable.ts) |
| 34 | +- Type validation for basic types (string, number, boolean, date, array, object) |
| 35 | +- Required field validation only on `add()` operations |
| 36 | +- Generic enum validation (array membership check) |
| 37 | +- No AppSheet-specific type validation |
| 38 | + |
| 39 | +## Requirements |
| 40 | + |
| 41 | +### 1. AppSheet Field Type System |
| 42 | + |
| 43 | +Add support for all AppSheet column types: |
| 44 | + |
| 45 | +#### Core Types |
| 46 | +- `Text` - Base text type |
| 47 | +- `Number` - Numeric values |
| 48 | +- `Date` - Date only (YYYY-MM-DD) |
| 49 | +- `DateTime` - Date and time |
| 50 | +- `Time` - Time only |
| 51 | +- `Duration` - Time intervals |
| 52 | +- `YesNo` - Boolean values |
| 53 | + |
| 54 | +#### Specialized Text Types |
| 55 | +- `Name` - Person names |
| 56 | +- `Email` - Email addresses (with format validation) |
| 57 | +- `URL` - Web URLs (with format validation) |
| 58 | +- `Phone` - Phone numbers (with format validation) |
| 59 | +- `Address` - Physical addresses |
| 60 | + |
| 61 | +#### Specialized Number Types |
| 62 | +- `Decimal` - Decimal numbers |
| 63 | +- `Percent` - Percentage values (0.00 to 1.00) |
| 64 | +- `Price` - Currency values |
| 65 | + |
| 66 | +#### Selection Types |
| 67 | +- `Enum` - Single selection from fixed list |
| 68 | +- `EnumList` - Multiple selections from fixed list |
| 69 | + |
| 70 | +#### Media Types |
| 71 | +- `Image` - Image files |
| 72 | +- `File` - File attachments |
| 73 | +- `Drawing` - Drawing data |
| 74 | +- `Signature` - Signature data |
| 75 | + |
| 76 | +#### Tracking Types |
| 77 | +- `ChangeCounter` - Edit counter |
| 78 | +- `ChangeTimestamp` - Last edit timestamp |
| 79 | +- `ChangeLocation` - Edit location |
| 80 | + |
| 81 | +#### Reference Types |
| 82 | +- `Ref` - Reference to another table |
| 83 | +- `RefList` - Multiple references |
| 84 | + |
| 85 | +#### Special Types |
| 86 | +- `Color` - Color values |
| 87 | +- `Show` - Display-only columns |
| 88 | + |
| 89 | +### 2. Enhanced Field Definition |
| 90 | + |
| 91 | +```typescript |
| 92 | +export type AppSheetFieldType = |
| 93 | + // Core types |
| 94 | + | 'Text' | 'Number' | 'Date' | 'DateTime' | 'Time' | 'Duration' | 'YesNo' |
| 95 | + // Specialized text |
| 96 | + | 'Name' | 'Email' | 'URL' | 'Phone' | 'Address' |
| 97 | + // Specialized numbers |
| 98 | + | 'Decimal' | 'Percent' | 'Price' |
| 99 | + // Selection types |
| 100 | + | 'Enum' | 'EnumList' |
| 101 | + // Media types |
| 102 | + | 'Image' | 'File' | 'Drawing' | 'Signature' |
| 103 | + // Tracking types |
| 104 | + | 'ChangeCounter' | 'ChangeTimestamp' | 'ChangeLocation' |
| 105 | + // Reference types |
| 106 | + | 'Ref' | 'RefList' |
| 107 | + // Special types |
| 108 | + | 'Color' | 'Show'; |
| 109 | + |
| 110 | +export interface FieldDefinition { |
| 111 | + /** AppSheet field type (required) */ |
| 112 | + type: AppSheetFieldType; |
| 113 | + |
| 114 | + /** Whether the field is required (default: false) */ |
| 115 | + required?: boolean; |
| 116 | + |
| 117 | + /** Allowed values for Enum/EnumList fields */ |
| 118 | + allowedValues?: string[]; |
| 119 | + |
| 120 | + /** Referenced table name for Ref/RefList fields */ |
| 121 | + referencedTable?: string; |
| 122 | + |
| 123 | + /** Field description */ |
| 124 | + description?: string; |
| 125 | + |
| 126 | + /** Additional AppSheet-specific configuration */ |
| 127 | + appSheetConfig?: { |
| 128 | + /** Allow other values (for Enum) */ |
| 129 | + allowOtherValues?: boolean; |
| 130 | + |
| 131 | + /** Format hint for display */ |
| 132 | + format?: string; |
| 133 | + |
| 134 | + /** Default value */ |
| 135 | + defaultValue?: any; |
| 136 | + }; |
| 137 | +} |
| 138 | + |
| 139 | +export interface TableDefinition { |
| 140 | + /** Actual AppSheet table name */ |
| 141 | + tableName: string; |
| 142 | + |
| 143 | + /** Name of the key/primary field */ |
| 144 | + keyField: string; |
| 145 | + |
| 146 | + /** Field definitions (name -> definition object only) */ |
| 147 | + fields: Record<string, FieldDefinition>; |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +### 3. Enhanced Validation |
| 152 | + |
| 153 | +#### Required Field Validation |
| 154 | +- Validate required fields during both `add()` and `update()` operations |
| 155 | +- During `update()`: Only validate if field is present in the update payload |
| 156 | +- Clear error messages indicating which field is missing and in which row |
| 157 | + |
| 158 | +#### Type-Specific Validation |
| 159 | +- **Email**: Validate email format (basic RFC 5322 check) |
| 160 | +- **URL**: Validate URL format (http/https) |
| 161 | +- **Phone**: Validate phone format (flexible international format) |
| 162 | +- **Enum**: Validate single value against `allowedValues` |
| 163 | +- **EnumList**: Validate array of values against `allowedValues` |
| 164 | +- **Percent**: Validate range (0.00 to 1.00) |
| 165 | +- **Price**: Validate numeric format with optional decimals |
| 166 | +- **Date**: Validate ISO date format (YYYY-MM-DD) |
| 167 | +- **DateTime**: Validate ISO datetime format |
| 168 | +- **YesNo**: Validate boolean or "Yes"/"No" string |
| 169 | + |
| 170 | +#### Validation Behavior |
| 171 | +```typescript |
| 172 | +// Current behavior (keep): |
| 173 | +- add(): Validate required fields + type validation |
| 174 | +- update(): Skip required validation, only type validation for provided fields |
| 175 | + |
| 176 | +// Enhanced behavior (add): |
| 177 | +- add(): Required validation + AppSheet type validation + format validation |
| 178 | +- update(): Type validation + format validation for provided fields only |
| 179 | +``` |
| 180 | + |
| 181 | +### 4. SchemaInspector Enhancement |
| 182 | + |
| 183 | +Update CLI inspection to detect AppSheet field types: |
| 184 | +- Analyze actual data to infer AppSheet types (not just JS types) |
| 185 | +- Detect email patterns → `Email` type |
| 186 | +- Detect URL patterns → `URL` type |
| 187 | +- Detect phone patterns → `Phone` type |
| 188 | +- Detect percentage values → `Percent` type |
| 189 | +- Detect enum lists (comma-separated values) → `EnumList` type |
| 190 | +- Extract `allowedValues` from actual data for Enum/EnumList fields |
| 191 | + |
| 192 | +### 5. Error Messages |
| 193 | + |
| 194 | +Provide clear, actionable error messages: |
| 195 | +```typescript |
| 196 | +// Examples: |
| 197 | +"Row 0: Field 'email' is required in table 'users'" |
| 198 | +"Row 1: Field 'email' must be a valid email address, got: 'invalid-email'" |
| 199 | +"Row 2: Field 'status' must be one of: Active, Inactive, Pending. Got: 'Unknown'" |
| 200 | +"Row 3: Field 'tags' must be an array of values from: tag1, tag2, tag3. Got: ['invalid-tag']" |
| 201 | +"Row 4: Field 'discount' must be a percentage between 0.00 and 1.00, got: 1.5" |
| 202 | +``` |
| 203 | + |
| 204 | +## Implementation Plan |
| 205 | + |
| 206 | +### Phase 1: Type System Extension |
| 207 | +1. Add `AppSheetFieldType` enum to `src/types/schema.ts` |
| 208 | +2. Create `EnhancedFieldDefinition` interface |
| 209 | +3. Update `TableDefinition` to use new field definition format |
| 210 | +4. Remove old `FieldType` and replace with `AppSheetFieldType` |
| 211 | + |
| 212 | +### Phase 2: Validation Enhancement |
| 213 | +1. Extract validation logic to separate validator classes: |
| 214 | + - `BaseTypeValidator` - Basic type checks |
| 215 | + - `AppSheetTypeValidator` - AppSheet-specific validation |
| 216 | + - `FormatValidator` - Format validation (email, URL, phone) |
| 217 | +2. Update `DynamicTable.validateRows()` to use new validators |
| 218 | +3. Add format validation for specialized types |
| 219 | +4. Enhance error messages with detailed context |
| 220 | + |
| 221 | +### Phase 3: SchemaInspector Enhancement |
| 222 | +1. Add pattern detection for specialized types |
| 223 | +2. Implement `allowedValues` extraction for Enum/EnumList |
| 224 | +3. Add heuristics for type inference (e.g., email pattern detection) |
| 225 | +4. Update CLI output to show AppSheet types |
| 226 | + |
| 227 | +### Phase 4: Documentation |
| 228 | +1. Update CLAUDE.md with new type system |
| 229 | +2. Document all AppSheet types with examples |
| 230 | +3. Add validation examples to README |
| 231 | +4. Update migration guide for v2.0.0 (breaking changes) |
| 232 | + |
| 233 | +### Phase 5: Testing |
| 234 | +1. Unit tests for all AppSheet type validations |
| 235 | +2. Integration tests with real AppSheet data patterns |
| 236 | +3. CLI inspection tests with various data patterns |
| 237 | +4. Test error messages for all validation scenarios |
| 238 | + |
| 239 | +## Success Criteria |
| 240 | + |
| 241 | +1. ✅ All AppSheet field types are supported in schema definitions |
| 242 | +2. ✅ Type-specific validation works for all specialized types |
| 243 | +3. ✅ Enum and EnumList are properly distinguished and validated |
| 244 | +4. ✅ Required field validation works consistently |
| 245 | +5. ✅ Clear error messages for all validation failures |
| 246 | +6. ✅ CLI can detect and generate AppSheet-specific types |
| 247 | +7. ✅ Comprehensive test coverage (>90%) |
| 248 | +8. ✅ Documentation updated and complete |
| 249 | +9. ✅ Migration guide for v2.0.0 breaking changes |
| 250 | + |
| 251 | +## Technical Notes |
| 252 | + |
| 253 | +### Schema Format (v2.0.0) |
| 254 | +```typescript |
| 255 | +// Schema format (only supported format) |
| 256 | +{ |
| 257 | + "fields": { |
| 258 | + "email": { |
| 259 | + "type": "Email", |
| 260 | + "required": true |
| 261 | + }, |
| 262 | + "age": { |
| 263 | + "type": "Number", |
| 264 | + "required": false |
| 265 | + }, |
| 266 | + "status": { |
| 267 | + "type": "Enum", |
| 268 | + "allowedValues": ["Active", "Inactive", "Pending"], |
| 269 | + "required": true |
| 270 | + }, |
| 271 | + "tags": { |
| 272 | + "type": "EnumList", |
| 273 | + "allowedValues": ["TypeScript", "JavaScript", "Node.js"], |
| 274 | + "required": false |
| 275 | + } |
| 276 | + } |
| 277 | +} |
| 278 | +``` |
| 279 | + |
| 280 | +**Breaking Changes**: |
| 281 | +- Old generic types (`'string'`, `'number'`, `'boolean'`, `'date'`, `'array'`, `'object'`) are no longer supported |
| 282 | +- Shorthand string format (e.g., `"email": "string"`) is no longer supported |
| 283 | +- All fields must use the full object definition with `type` property |
| 284 | +- `enum` property renamed to `allowedValues` for clarity |
| 285 | + |
| 286 | +### Validation Order |
| 287 | +1. Check if field exists (for required validation) |
| 288 | +2. Check basic type (string, number, etc.) |
| 289 | +3. Check AppSheet type constraints |
| 290 | +4. Check format (for Email, URL, Phone) |
| 291 | +5. Check enum/enumList values |
| 292 | + |
| 293 | +### Performance Considerations |
| 294 | +- Keep validation fast (< 1ms per field) |
| 295 | +- Cache compiled regex patterns for format validation |
| 296 | +- Lazy validation: Only validate provided fields |
| 297 | +- No validation for undefined/null values (unless required) |
| 298 | + |
| 299 | +## Files to Modify |
| 300 | + |
| 301 | +1. `src/types/schema.ts` - Add AppSheet types |
| 302 | +2. `src/client/DynamicTable.ts` - Enhance validation |
| 303 | +3. `src/utils/validators/` - New validator classes (to be created) |
| 304 | +4. `src/cli/SchemaInspector.ts` - Add type detection |
| 305 | +5. `tests/client/DynamicTable.test.ts` - Add validation tests |
| 306 | +6. `tests/utils/validators/` - New validator tests |
| 307 | +7. `CLAUDE.md` - Update documentation |
| 308 | +8. `README.md` - Add examples |
| 309 | + |
| 310 | +## Dependencies |
| 311 | + |
| 312 | +No new external dependencies required. Use built-in Node.js capabilities for: |
| 313 | +- Email validation: RFC 5322 regex |
| 314 | +- URL validation: Built-in URL class |
| 315 | +- Phone validation: Flexible international format regex |
| 316 | + |
| 317 | +## Estimated Effort |
| 318 | + |
| 319 | +- Type System Extension: 4 hours |
| 320 | +- Validation Enhancement: 8 hours |
| 321 | +- SchemaInspector Enhancement: 6 hours |
| 322 | +- Documentation: 4 hours |
| 323 | +- Testing: 8 hours |
| 324 | +- **Total: ~30 hours (4-5 days)** |
| 325 | + |
| 326 | +## Related Issues |
| 327 | + |
| 328 | +- Schema validation improvements |
| 329 | +- CLI inspection enhancements |
| 330 | +- API response handling |
| 331 | + |
| 332 | +## References |
| 333 | + |
| 334 | +- [AppSheet Column Data Types](https://support.google.com/appsheet/answer/10106435) |
| 335 | +- [AppSheet Enum Documentation](https://support.google.com/appsheet/answer/10107878) |
| 336 | +- Current implementation: `src/client/DynamicTable.ts:285-411` |
0 commit comments