Skip to content

Commit de1d79d

Browse files
committed
Support for any in FormPath and FormPathLeaves.
1 parent ca5bbc6 commit de1d79d

File tree

6 files changed

+57
-26
lines changed

6 files changed

+57
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
- `defaults` 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.
21-
- `FormPathLeaves` didn't handle fields with type `unknown`.
21+
- `FormPath` and `FormPathLeaves` didn't handle fields with type `unknown` and `any`.
2222

2323
## [2.5.0] - 2024-02-21
2424

src/lib/stringPath.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ export type FormPathLeavesWithErrors<T extends object, Type = any> = string &
5454
export type FormPathArrays<T extends object, Type = any> = string &
5555
StringPath<T, { filter: 'arrays'; objAppend: never; path: ''; type: Type }>;
5656

57+
// Thanks to https://stackoverflow.com/a/77451367/70894
58+
type IsAny<T> = boolean extends (T extends never ? true : false) ? true : false;
59+
5760
type Concat<
5861
Path extends string,
5962
Next extends string
@@ -148,24 +151,26 @@ type StringPath<
148151
}
149152
>
150153
: never)
151-
:
152-
| If<
153-
Options,
154-
'filter',
155-
'all',
156-
Concat<Options['path'], K>,
157-
unknown extends T[K] ? Concat<Options['path'], K> : never,
158-
T[K]
159-
>
160-
| StringPath<
161-
NonNullable<T[K]>,
162-
{
163-
filter: Options['filter'];
164-
objAppend: Options['objAppend'];
165-
path: Concat<Options['path'], K>;
166-
type: Options['type'];
167-
}
168-
>
154+
: IsAny<T[K]> extends true
155+
? Concat<Options['path'], K>
156+
:
157+
| If<
158+
Options,
159+
'filter',
160+
'all',
161+
Concat<Options['path'], K>,
162+
unknown extends T[K] ? Concat<Options['path'], K> : never,
163+
T[K]
164+
>
165+
| StringPath<
166+
NonNullable<T[K]>,
167+
{
168+
filter: Options['filter'];
169+
objAppend: Options['objAppend'];
170+
path: Concat<Options['path'], K>;
171+
type: Options['type'];
172+
}
173+
>
169174
: If<Options, 'filter', 'leaves' | 'all', Concat<Options['path'], K>, never, T[K]>;
170175
}[Extract<AllKeys<T>, string>];
171176

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

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

66
export const load = async () => {
77
const form = await superValidate(zod(schema));
8-
return { form };
8+
const anyForm = await superValidate(zod(anySchema));
9+
return { form, anyForm };
910
};
1011

1112
export const actions = {

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
77
export let data;
88
9-
const form = superForm(data.form, {
10-
taintedMessage: false
11-
//dataType: 'json',
12-
//validators: zod(schema)
13-
});
9+
const form = superForm(data.form, { taintedMessage: false });
10+
const anyForm = superForm(data.anyForm, { taintedMessage: false });
1411
</script>
1512

1613
<FormCmptAge {form} field="age" />
1714
<FormCmptAge {form} field="homePlanet" />
15+
<FormCmptAge form={anyForm} field="age" />

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ export const schema = z.object({
55
age: z.number(),
66
homePlanet: z.unknown()
77
});
8+
9+
export const anySchema = z.object({
10+
name: z.string(),
11+
age: z.any()
12+
});

src/tests/legacy/paths.test-d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,28 @@ test('FormPath with unknown type', () => {
339339
const t3: FPA = 'homePlanet';
340340
});
341341

342+
test('FormPath with any type', () => {
343+
const schema = z.object({
344+
name: z.string(),
345+
age: z.any()
346+
});
347+
348+
type FP = FormPath<z.infer<typeof schema>>;
349+
type FPL = FormPathLeaves<z.infer<typeof schema>>;
350+
type FPA = FormPathArrays<z.infer<typeof schema>>;
351+
352+
const t1: FP = 'age';
353+
const t11: FP = 'age[3]';
354+
const t2: FPL = 'age';
355+
const t21: FPL = 'age[3]';
356+
const t3: FPA = 'age';
357+
358+
// @ts-expect-error Invalid path
359+
const f1: FP = 'nope';
360+
// @ts-expect-error Invalid path
361+
const f2: FPA = 'age[3]';
362+
});
363+
342364
///////////////////////////////////////////////////
343365

344366
// Recursive schema test

0 commit comments

Comments
 (0)