Skip to content

Commit a689bf4

Browse files
committed
Added file validation
1 parent bda4db8 commit a689bf4

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/lib/layout/Home.svelte

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,25 @@
99
1010
async function filesDropped(files: FileList) {
1111
const file = files[0];
12+
let data: any;
1213
13-
// TODO: file validation
14+
try {
15+
data = JSON.parse(await file.text()) as StatsData;
16+
} catch(e) {
17+
return `Invalid file format: ${e}`;
18+
}
19+
20+
if(typeof(data) == "object" && Array.isArray(data)) {
21+
return "Invalid file format: Expected Object got Array for root.";
22+
}
1423
15-
const data = JSON.parse(await file.text()) as StatsData;
24+
if(!data["songs"]) {
25+
return "Invalid file format: missing 'songs' field";
26+
}
27+
28+
if(!Array.isArray(data["songs"])) {
29+
return "Invalid file format: 'songs' must be an array";
30+
}
1631
1732
setStats(new Stats(data));
1833
}

0 commit comments

Comments
 (0)