Skip to content

Commit 6199d10

Browse files
committed
fix(import): add some validation
1 parent 0e5680d commit 6199d10

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

components/Gestion/ImportForm.vue

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,24 @@
8585
const buffer = reader.result as ArrayBuffer;
8686
const string: string = new TextDecoder().decode(buffer);
8787
88-
this.$nuxt.$loading.start();
89-
let jsonInput: ImportExportFormat | any[];
88+
let jsonInput: ImportExportFormat | any;
89+
// since TypeScript doesn't check type at runtime ; better check that
90+
const isExportFormat = (obj: any) =>
91+
(obj as ImportExportFormat).categories &&
92+
(obj as ImportExportFormat).exercises &&
93+
Array.isArray(obj.exercises) &&
94+
typeof (obj as ImportExportFormat).categories == "object"
95+
;
9096
9197
try {
98+
this.$nuxt.$loading.start();
9299
// first validate the json before anything else
93100
jsonInput = JSON.parse(string);
94101
// at the end, both import type use the same endpoint
95102
let exercises;
96103
97104
// Type Guards to distinct the two types
98-
if ((jsonInput as ImportExportFormat).categories) {
105+
if (isExportFormat(jsonInput)) {
99106
100107
const exportFormat = jsonInput as ImportExportFormat;
101108
@@ -125,9 +132,9 @@
125132
state: ex.state,
126133
tags: ex.tags.map(tag => (
127134
{
128-
// TODO : tag state ???
129135
text: tag.text,
130-
category_id: tags_dictionary[exportFormat.categories[tag.category]][0].id
136+
category_id: tags_dictionary[exportFormat.categories[tag.category]][0].id,
137+
state: tag.state
131138
}
132139
))
133140
})
@@ -149,7 +156,7 @@
149156
})
150157
} catch (e) {
151158
152-
if (e instanceof SyntaxError) {
159+
if (e instanceof SyntaxError || e instanceof TypeError || e instanceof ReferenceError) {
153160
this.$displayError("Le contenu de votre fichier n'est pas correct.");
154161
} else {
155162
const error = e as AxiosError;

types/exercise.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ export interface ImportExportFormat {
165165
url: null | string,
166166
tags: {
167167
text: string,
168-
category: number
168+
category: number,
169+
state: TagState
169170
}[]
170171
}[]
171172
}

0 commit comments

Comments
 (0)