Skip to content

Commit e1b66c0

Browse files
committed
Attempt custom implementations of some Superforms functions
1 parent 5fc9792 commit e1b66c0

22 files changed

Lines changed: 71 additions & 38 deletions

File tree

Site/src/lib/server/formError.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Returns a custom error for a superForm
22

33
import { fail } from "@sveltejs/kit"
4-
import type { SuperValidated } from "$lib/server/validate"
4+
import type { Form } from "$lib/validate"
55

66
/**
77
* Returns a custom error for a superForm.
@@ -12,13 +12,9 @@ import type { SuperValidated } from "$lib/server/validate"
1212
* @example
1313
* return formError(form, ["password"], ["Username or password is incorrect"])
1414
*/
15-
export default (
16-
form: SuperValidated<
17-
{ [_: string]: unknown },
18-
unknown,
19-
{ [_: string]: unknown }
20-
>,
21-
fields?: string[],
15+
export default <T>(
16+
form: Form<T>,
17+
fields?: (keyof T)[],
2218
messages?: string[]
2319
) => {
2420
form.valid = false

Site/src/lib/server/validate.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// I know barrel files suck but I'd like to wrap these functions in the near future
22

33
import type { ActionFailure } from "@sveltejs/kit"
4+
import type { Type } from "arktype"
45
import { message as ogMessage, type SuperValidated } from "sveltekit-superforms"
6+
import type { Form } from "$lib/validate"
57

6-
export { arktype } from "sveltekit-superforms/adapters"
8+
// export { arktype } from "sveltekit-superforms/adapters"
79
export {
810
message,
911
type SuperValidated,
10-
superValidate,
12+
// superValidate,
1113
} from "sveltekit-superforms/server"
1214

1315
export function errMessage<
@@ -23,3 +25,26 @@ export function errMessage<
2325
form.valid = false
2426
return ogMessage(form, message)
2527
}
28+
29+
export function arktype<T>(schema: T) {
30+
return schema
31+
}
32+
33+
export async function superValidate<T>(
34+
request: Request | null,
35+
schema: Type<T>
36+
): Promise<Form<T>> {
37+
if (!request) {
38+
return {
39+
valid: true,
40+
errors: {},
41+
data: schema as T,
42+
}
43+
}
44+
45+
return {
46+
valid: false,
47+
errors: {},
48+
data: {} as T,
49+
}
50+
}

Site/src/lib/validate.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
// client version of $lib/server/validate
22

33
export { type SuperForm, superForm } from "sveltekit-superforms/client"
4+
5+
type FormErrors<T> = { [K in keyof T]?: string[] }
6+
7+
export type Form<T> = {
8+
valid: boolean
9+
errors: FormErrors<T>
10+
data: T
11+
}

Site/src/routes/(legal)/report/+page.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export async function load({ locals, url }) {
4040
return {
4141
reportee,
4242
url: reportedUrl,
43-
form: await superValidate(arktype(schema)),
43+
form: await superValidate(null, arktype(schema)),
4444
}
4545
}
4646

Site/src/routes/(main)/admin/accounts/+page.server.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export async function load({ locals }) {
2525
const [users] = await db.query<BasicUser[][]>(usersQuery)
2626

2727
return {
28-
form: await superValidate(arktype(schema)),
28+
form: await superValidate(null, arktype(schema)),
2929
users,
3030
}
3131
}
@@ -47,8 +47,7 @@ actions.changePassword = async ({ locals, request, getClientAddress }) => {
4747
npassword: Bun.password.hashSync(password),
4848
username: username,
4949
})
50-
if (!ok)
51-
return errMessage(form, "No user found with given username")
50+
if (!ok) return errMessage(form, "No user found with given username")
5251
} catch (e) {
5352
console.error(e)
5453
return errMessage(form, "An error occurred")

Site/src/routes/(main)/admin/banners/+page.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export async function load({ locals }) {
3434
await authorise(locals, 5)
3535

3636
const [banners] = await db.query<BannerType[][]>(bannersQuery)
37-
return { banners, form: await superValidate(arktype(schema)) }
37+
return { banners, form: await superValidate(null, arktype(schema)) }
3838
}
3939

4040
async function bannerActiveCount() {

Site/src/routes/(main)/admin/create/+page.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export async function load({ locals }) {
3030
await authorise(locals, 3)
3131

3232
return {
33-
form: await superValidate(arktype(schema)),
33+
form: await superValidate(null, arktype(schema)),
3434
}
3535
}
3636

Site/src/routes/(main)/admin/moderation/+page.server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export async function load({ locals, url }) {
3030
await authorise(locals, 4)
3131

3232
const associatedReport = url.searchParams.get("report")
33-
if (!associatedReport) return { form: await superValidate(arktype(schema)) }
33+
if (!associatedReport)
34+
return { form: await superValidate(null, arktype(schema)) }
3435

3536
const [report] = await db.query<
3637
({ note: string; reportee: string } | undefined)[]

Site/src/routes/(main)/admin/regkeys/+page.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export async function load({ locals }) {
3939

4040
const [regKeys] = await db.query<RegKey[][]>(regkeysQuery)
4141
return {
42-
form: await superValidate(arktype(schema)),
42+
form: await superValidate(null, arktype(schema)),
4343
regKeys,
4444
prefix: config.Registration.Keys.Prefix,
4545
}

Site/src/routes/(main)/catalog/[id=asset]/[name]/+page.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export async function load({ fetch: f, locals, params }) {
7070
return {
7171
noText: noTexts[Math.floor(Math.random() * noTexts.length)],
7272
failText: failTexts[Math.floor(Math.random() * failTexts.length)],
73-
form: await superValidate(arktype(schema)),
73+
form: await superValidate(null, arktype(schema)),
7474
slug,
7575
asset,
7676
balance: balance.value,

0 commit comments

Comments
 (0)