Skip to content

Commit 1f5c353

Browse files
authored
fix: minor bugfixes (#683)
* feat: using zod * feat: client-side validation * feat: changeset
1 parent c2e53f5 commit 1f5c353

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

.changeset/big-peaches-warn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"enspire": patch
3+
---
4+
5+
validation fixes and interface logic fixes

app/components/custom/club-file-upload.vue

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,35 @@ function readFileAsDataURL(file: File) {
7272
})
7373
}
7474
75+
const formSchema = z.object({
76+
clubId: z.number().int(),
77+
collectionId: z.string(),
78+
fileContent: z.string(),
79+
rawName: z.string().max(256),
80+
})
81+
7582
const onSubmit = form.handleSubmit(async (values) => {
7683
submitting.value = true
7784
const fileName = values.file.name
78-
const status = await $fetch('/api/files/new-record', {
79-
method: 'POST',
80-
body: {
81-
clubId: Number.parseInt(props.club),
82-
collectionId: props.collection,
83-
fileContent: await readFileAsDataURL(values.file),
84-
rawName: fileName,
85-
},
86-
})
87-
form.resetForm()
88-
inputKey.value = uuidv4()
89-
msg.value = (status && status.success) ? `${fileName} (提交成功)` : '提交失败'
85+
const data = {
86+
clubId: Number.parseInt(props.club),
87+
collectionId: props.collection,
88+
fileContent: await readFileAsDataURL(values.file),
89+
rawName: fileName,
90+
}
91+
if (formSchema.safeParse(data).success) {
92+
const status = await $fetch('/api/files/new-record', {
93+
method: 'POST',
94+
body: data,
95+
})
96+
form.resetForm()
97+
inputKey.value = uuidv4()
98+
await updateClub()
99+
msg.value = (status && status.success) ? `${fileName} (提交成功)` : '提交失败'
100+
}
101+
else {
102+
msg.value = '文件内容异常'
103+
}
90104
submitting.value = false
91105
})
92106
@@ -173,7 +187,7 @@ await updateClub()
173187
</FormItem>
174188
</FormField>
175189
<div class="mt-2">
176-
<Button type="submit" variant="secondary" :disabled="!form.values.file || submitting || clubUpdating">
190+
<Button type="submit" variant="secondary" :disabled="!form.values.file || submitting || clubUpdating || !props.club">
177191
上传
178192
</Button>
179193
<Button v-if="currentClubData" :disabled="downloading" variant="outline" class="ml-2" type="button" @click="download">

server/api/files/new-record.post.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { PrismaClient } from '@prisma/client'
22
import { randomUUID } from 'uncrypto'
3+
import { z } from 'zod'
34

45
interface Body {
56
clubId: number
@@ -8,6 +9,13 @@ interface Body {
89
rawName: string
910
}
1011

12+
const bodySchema = z.object({
13+
clubId: z.number().int(),
14+
collectionId: z.string(),
15+
fileContent: z.string(),
16+
rawName: z.string().max(256),
17+
})
18+
1119
const prisma = new PrismaClient()
1220

1321
export default eventHandler(async (event) => {
@@ -19,6 +27,12 @@ export default eventHandler(async (event) => {
1927

2028
return readBody(event)
2129
.then(async (body: Body) => {
30+
if (!bodySchema.safeParse(body).success) {
31+
return {
32+
success: false,
33+
error: 'Invalid file given',
34+
}
35+
}
2236
const { clubId, collectionId, fileContent, rawName } = body
2337
// TODO: this should be enabled after the ID mismatch is fixed
2438
// const collectionInfo = await prisma.fileCollection.findFirst({

0 commit comments

Comments
 (0)