Skip to content

Commit 4452d7f

Browse files
committed
isTainted now works with undefined values in the $tainted store.
1 parent 61f63ef commit 4452d7f

File tree

6 files changed

+95
-3
lines changed

6 files changed

+95
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ Headlines: Added, Changed, Deprecated, Removed, Fixed, Security
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Fixed
11+
12+
- `isTainted` now works with undefined values in the `$tainted` store.
13+
814
## [2.13.1] - 2024-05-07
915

1016
### Fixed

src/lib/client/superForm.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,8 +1209,7 @@ export function superForm<
12091209
): boolean {
12101210
if (typeof path === 'boolean') return path;
12111211
if (typeof path === 'object') return Tainted__isObjectTainted(path);
1212-
if (!Data.tainted) return false;
1213-
if (!path) return Tainted__isObjectTainted(Data.tainted);
1212+
if (!Data.tainted || path === undefined) return false;
12141213

12151214
const field = pathExists(Data.tainted, splitPath(path));
12161215
return Tainted__isObjectTainted(field?.value);

src/routes/(v2)/v2/Navigation.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
'nested-traverse',
6161
'reset-errors',
6262
'schemasafe-types',
63-
'discriminated-union'
63+
'discriminated-union',
64+
'simple-tainted'
6465
].sort();
6566
</script>
6667

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { message, superValidate } from '$lib/index.js';
2+
import { zod } from '$lib/adapters/zod.js';
3+
import { fail } from '@sveltejs/kit';
4+
import { schema } from './schema.js';
5+
6+
export const load = async () => {
7+
const form = await superValidate(zod(schema));
8+
9+
// Always return { form } in load functions
10+
return { form };
11+
};
12+
13+
export const actions = {
14+
default: async ({ request }) => {
15+
const form = await superValidate(request, zod(schema));
16+
console.log(form);
17+
18+
if (!form.valid) {
19+
// Again, return { form } and things will just work.
20+
return fail(400, { form });
21+
}
22+
23+
// TODO: Do something with the validated form.data
24+
25+
return message(form, 'Form posted successfully!');
26+
}
27+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/index.js';
3+
import { zodClient } from '$lib/adapters/zod.js';
4+
import type { PageData } from './$types.js';
5+
import { schema } from './schema.js';
6+
7+
export let data: PageData;
8+
9+
const { form, errors, constraints, message, enhance, tainted, isTainted } = superForm(data.form, {
10+
dataType: 'json',
11+
validators: zodClient(schema)
12+
});
13+
</script>
14+
15+
{#if $message}<h3>{$message}</h3>{/if}
16+
17+
<form method="POST" use:enhance>
18+
<label for="name">Name</label>
19+
<input
20+
type="text"
21+
name="name"
22+
aria-invalid={$errors.name?.value ? 'true' : undefined}
23+
bind:value={$form.name.value}
24+
{...$constraints.name?.value}
25+
/>
26+
{#if $errors.name?.value}<span class="invalid">{$errors.name.value}</span>{/if}
27+
28+
<label for="email">E-mail</label>
29+
<input
30+
type="email"
31+
name="email"
32+
aria-invalid={$errors.email?.value ? 'true' : undefined}
33+
bind:value={$form.email.value}
34+
{...$constraints.email?.value}
35+
/>
36+
{#if $errors.email?.value}<span class="invalid">{$errors.email.value}</span>{/if}
37+
38+
<div><button>Submit</button></div>
39+
</form>
40+
41+
<p id="tainted">Tainted: {isTainted($tainted)}</p>
42+
<p id="email-tainted">Tainted email: {isTainted($tainted?.email)}</p>
43+
44+
<style>
45+
.invalid {
46+
color: red;
47+
}
48+
</style>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { z } from 'zod';
2+
3+
// Define at the top-level so it stays in memory and the adapter can be cached
4+
export const schema = z.object({
5+
name: z.object({
6+
value: z.string().min(5)
7+
}),
8+
email: z.object({
9+
value: z.string().email()
10+
})
11+
});

0 commit comments

Comments
 (0)