Skip to content

Commit 1bb4ff7

Browse files
Copilotrajbos
andcommitted
Fix CSV header validation to use exact matching instead of substring matching
Co-authored-by: rajbos <6085745+rajbos@users.noreply.github.com>
1 parent 88a95cb commit 1bb4ff7

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

src/lib/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ export function parseCSV(csv: string): CopilotUsageData[] {
4343
: m.replace(/^"(.*)"$/, '$1')
4444
);
4545

46-
// Check if all expected headers are present (case-insensitive)
46+
// Check if all expected headers are present (case-insensitive exact match)
4747
const missingHeaders = expectedHeaders.filter(expected =>
48-
!headers.some(header => header.toLowerCase().includes(expected.toLowerCase()))
48+
!headers.some(header => header.toLowerCase() === expected.toLowerCase())
4949
);
5050

5151
if (missingHeaders.length > 0) {

src/test/csv-header-validation.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,14 @@ user1,2024-01-01T00:00:00Z,gpt-4,100,1.5,false`
100100
totalMonthlyQuota: '50'
101101
})
102102
})
103+
104+
it('should reject CSV with headers that contain required words but are not exact matches', () => {
105+
// This test ensures exact header matching, not substring matching
106+
const csvWithSimilarHeaders = `"Event Timestamp","System User","Model Type","Total Requests Used","User Exceeds Monthly Quota","Total Monthly Quota Limit"
107+
2024-01-01T00:00:00Z,user1,gpt-4,1.5,false,100`
108+
109+
expect(() => parseCSV(csvWithSimilarHeaders)).toThrow(
110+
'CSV is missing required columns: Timestamp, User, Model, Requests Used, Exceeds Monthly Quota, Total Monthly Quota'
111+
)
112+
})
103113
})

0 commit comments

Comments
 (0)