Skip to content

Commit 5804bb2

Browse files
committed
Fixed coercion of boolean false.
1 parent 2690e81 commit 5804bb2

File tree

5 files changed

+136
-1
lines changed

5 files changed

+136
-1
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+
- A boolean schema field didn't accept a boolean `false` value when posted, it was coerced as `true`.
13+
814
## [1.4.0] - 2023-07-20
915

1016
### Fixed

src/lib/superValidate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ function formDataToValidation<T extends AnyZodObject>(
211211
? parseInt(value ?? '', 10)
212212
: parseFloat(value ?? '');
213213
} else if (zodType._def.typeName == 'ZodBoolean') {
214-
return Boolean(value).valueOf();
214+
return Boolean(value == 'false' ? '' : value).valueOf();
215215
} else if (zodType._def.typeName == 'ZodDate') {
216216
return new Date(value ?? '');
217217
} else if (zodType._def.typeName == 'ZodArray') {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { superValidate, message } from '$lib/server';
2+
import { fail } from '@sveltejs/kit';
3+
4+
import type { Actions, PageServerLoad } from './$types';
5+
import { exampleSchema } from './schema.js';
6+
7+
///// Load function /////
8+
9+
export const load: PageServerLoad = async () => {
10+
const form = await superValidate(exampleSchema);
11+
return { form };
12+
};
13+
14+
///// Form actions /////
15+
16+
export const actions: Actions = {
17+
default: async ({ request }) => {
18+
const form = await superValidate(request, exampleSchema);
19+
20+
console.log('POST', form);
21+
22+
if (!form.valid) return fail(400, { form });
23+
24+
return message(form, 'Form posted successfully!');
25+
}
26+
};
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/client';
3+
import type { PageData } from './$types';
4+
import SuperDebug from '$lib/client/SuperDebug.svelte';
5+
import { page } from '$app/stores';
6+
7+
export let data: PageData;
8+
9+
const { form, errors, message, constraints, enhance, validate } =
10+
superForm(data.form);
11+
</script>
12+
13+
<SuperDebug
14+
data={{
15+
$form: $form,
16+
$errors: $errors,
17+
validate: validate
18+
}}
19+
/>
20+
21+
{#if $message}
22+
<div
23+
class="status"
24+
class:error={$page.status >= 400}
25+
class:success={$page.status == 200}
26+
>
27+
{$message}
28+
</div>
29+
{/if}
30+
31+
<form method="POST" use:enhance>
32+
<fieldset role="radiogroup">
33+
<legend>Radio Group</legend>
34+
<label>
35+
<input
36+
type="radio"
37+
name="radioGroup"
38+
value={true}
39+
bind:group={$form.radioGroup}
40+
/>
41+
Yes
42+
</label>
43+
<label>
44+
<input
45+
type="radio"
46+
name="radioGroup"
47+
value={false}
48+
bind:group={$form.radioGroup}
49+
/>
50+
No
51+
</label>
52+
</fieldset>
53+
<hr />
54+
<label>
55+
<input type="checkbox" name="checkbox" bind:checked={$form.checkbox} /> Checkbox
56+
</label>
57+
<button>Submit</button>
58+
</form>
59+
60+
<style>
61+
.invalid {
62+
color: red;
63+
}
64+
65+
.status {
66+
color: white;
67+
padding: 4px;
68+
padding-left: 8px;
69+
border-radius: 2px;
70+
font-weight: 500;
71+
}
72+
73+
.status.success {
74+
background-color: seagreen;
75+
}
76+
77+
.status.error {
78+
background-color: #ff2a02;
79+
}
80+
81+
input {
82+
background-color: #ddd;
83+
}
84+
85+
a {
86+
text-decoration: underline;
87+
}
88+
89+
hr {
90+
margin-top: 4rem;
91+
}
92+
93+
form {
94+
padding-top: 1rem;
95+
padding-bottom: 1rem;
96+
}
97+
</style>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { z } from 'zod';
2+
3+
export const exampleSchema = z.object({
4+
radioGroup: z.boolean().default(false),
5+
checkbox: z.boolean()
6+
});

0 commit comments

Comments
 (0)