Skip to content

Commit 6dc5050

Browse files
committed
defaults didn't infer the input type.
1 parent de1d79d commit 6dc5050

File tree

5 files changed

+73
-34
lines changed

5 files changed

+73
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
### Fixed
1717

18-
- `defaults` didn't generate correct `SuperValidated` data, making `superForm` confused. Also fixed type signature and removed the `jsonSchema` option that wasn't applicable.
18+
- `defaults` didn't infer the input type, and didn't generate correct `SuperValidated` data, making `superForm` confused. Also fixed type signature and removed the `jsonSchema` option that wasn't applicable.
1919
- A successful `PageData` result from `invalidateAll` was overwritten by the `ActionData` result.
2020
- Using `goto` in events didn't work when the target page redirected.
2121
- `FormPath` and `FormPathLeaves` didn't handle fields with type `unknown` and `any`.

src/lib/defaults.ts

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
12
import type { ValidationAdapter, ClientValidationAdapter } from './adapters/adapters.js';
23
import type { SuperValidateOptions, SuperValidated } from './superValidate.js';
34

@@ -9,47 +10,50 @@ type SuperSchemaOptions<T extends Record<string, unknown>> = Pick<
910
>;
1011

1112
export function defaults<
12-
T extends Record<string, unknown>,
13-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
14-
M = App.Superforms.Message extends never ? any : App.Superforms.Message
15-
>(adapter: ValidationAdapter<T>, options?: SuperSchemaOptions<T>): SuperValidated<T, M>;
13+
Out extends Record<string, unknown>,
14+
M = App.Superforms.Message extends never ? any : App.Superforms.Message,
15+
In extends Record<string, unknown> = Out
16+
>(
17+
adapter: ValidationAdapter<Out, In>,
18+
options?: SuperSchemaOptions<Out>
19+
): SuperValidated<Out, M, In>;
1620

1721
export function defaults<
18-
T extends Record<string, unknown>,
19-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
20-
M = App.Superforms.Message extends never ? any : App.Superforms.Message
22+
Out extends Record<string, unknown>,
23+
M = App.Superforms.Message extends never ? any : App.Superforms.Message,
24+
In extends Record<string, unknown> = Out
2125
>(
22-
defaults: SuperSchemaData<T>,
23-
adapter: ValidationAdapter<T>,
24-
options?: SuperSchemaOptions<T>
25-
): SuperValidated<T, M>;
26+
defaults: SuperSchemaData<Out>,
27+
adapter: ValidationAdapter<Out, In>,
28+
options?: SuperSchemaOptions<Out>
29+
): SuperValidated<Out, M, In>;
2630

2731
export function defaults<
28-
T extends Record<string, unknown>,
29-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
30-
M = App.Superforms.Message extends never ? any : App.Superforms.Message
32+
Out extends Record<string, unknown>,
33+
M = App.Superforms.Message extends never ? any : App.Superforms.Message,
34+
In extends Record<string, unknown> = Out
3135
>(
32-
defaults: T,
33-
adapter: ClientValidationAdapter<T>,
34-
options?: SuperSchemaOptions<T>
35-
): SuperValidated<T, M>;
36+
defaults: Out,
37+
adapter: ClientValidationAdapter<Out, In>,
38+
options?: SuperSchemaOptions<Out>
39+
): SuperValidated<Out, M, In>;
3640

3741
export function defaults<
38-
T extends Record<string, unknown>,
39-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
40-
M = App.Superforms.Message extends never ? any : App.Superforms.Message
42+
Out extends Record<string, unknown>,
43+
M = App.Superforms.Message extends never ? any : App.Superforms.Message,
44+
In extends Record<string, unknown> = Out
4145
>(
42-
data: SuperSchemaData<T> | ValidationAdapter<T> | T,
43-
adapter?: ValidationAdapter<T> | ClientValidationAdapter<T> | SuperSchemaOptions<T>,
44-
options?: SuperSchemaOptions<T>
45-
): SuperValidated<T, M> {
46+
data: SuperSchemaData<Out> | ValidationAdapter<Out, In> | Out,
47+
adapter?: ValidationAdapter<Out, In> | ClientValidationAdapter<Out, In> | SuperSchemaOptions<Out>,
48+
options?: SuperSchemaOptions<Out>
49+
): SuperValidated<Out, M, In> {
4650
if (data && 'superFormValidationLibrary' in data) {
47-
options = adapter as SuperSchemaOptions<T>;
51+
options = adapter as SuperSchemaOptions<Out>;
4852
adapter = data;
4953
data = null;
5054
}
5155

52-
const validator = adapter as ValidationAdapter<T>;
56+
const validator = adapter as ValidationAdapter<Out, In>;
5357
const optionDefaults = options?.defaults ?? validator.defaults;
5458

5559
return {
@@ -63,6 +67,8 @@ export function defaults<
6367
};
6468
}
6569

66-
export function defaultValues<T extends Record<string, unknown>>(adapter: ValidationAdapter<T>): T {
70+
export function defaultValues<T extends Record<string, unknown>>(
71+
adapter: ValidationAdapter<T, Record<string, unknown>>
72+
): T {
6773
return adapter.defaults;
6874
}

src/routes/(v2)/v2/unknown-in-schema/+page.server.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { zod } from '$lib/adapters/zod.js';
22
import { message, superValidate } from '$lib/server/index.js';
3-
import { anySchema, schema } from './schema.js';
3+
import { anySchema, schema, userSchema } from './schema.js';
44
import { fail } from '@sveltejs/kit';
55

66
export const load = async () => {
77
const form = await superValidate(zod(schema));
88
const anyForm = await superValidate(zod(anySchema));
9-
return { form, anyForm };
9+
const userForm = await superValidate(
10+
{ name: 'some name', options: [{ color: 'option-1-color', value: 'option-1' }] },
11+
zod(userSchema)
12+
);
13+
return { form, anyForm, userForm };
1014
};
1115

1216
export const actions = {

src/routes/(v2)/v2/unknown-in-schema/+page.svelte

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
<script lang="ts">
2-
import { superForm } from '$lib/client/index.js';
3-
//import { zod } from '$lib/adapters/zod.js'
4-
//import { schema } from './schema.js';
2+
import { defaults, superForm } from '$lib/client/index.js';
3+
import { zod } from '$lib/adapters/zod.js';
4+
import { userSchema } from './schema.js';
55
import FormCmptAge from './FormCmptAge.svelte';
66
77
export let data;
88
99
const form = superForm(data.form, { taintedMessage: false });
1010
const anyForm = superForm(data.anyForm, { taintedMessage: false });
11+
12+
const formValidated = defaults(zod(userSchema));
13+
const options: unknown[] = formValidated.data.options;
14+
console.log(options.length);
1115
</script>
1216

1317
<FormCmptAge {form} field="age" />

src/routes/(v2)/v2/unknown-in-schema/schema.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,28 @@ export const anySchema = z.object({
1010
name: z.string(),
1111
age: z.any()
1212
});
13+
14+
export const userSchema = z.object({
15+
name: z.string(),
16+
options: z
17+
.array(
18+
z.object({
19+
color: z.string().trim().nullable(),
20+
value: z.string().trim()
21+
})
22+
)
23+
.default([])
24+
});
25+
26+
/**
27+
*
28+
* Expected type
29+
* ----------------
30+
name: string;
31+
options: {
32+
value: string;
33+
color: string | null;
34+
}[];
35+
*
36+
*/
37+
export type UserSchemaInffered = z.infer<typeof userSchema>;

0 commit comments

Comments
 (0)