Skip to content

Commit b3e8f77

Browse files
committed
Fixes #195
Boolean fields with a default value of true always returned true when validating.
1 parent 401c817 commit b3e8f77

File tree

5 files changed

+85
-7
lines changed

5 files changed

+85
-7
lines changed

CHANGELOG.md

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

88
## [Unreleased]
99

10-
- Fixed #143, infinite deep type instantiation on `message`. (Thanks to [thelinuxlich](https://github.com/thelinuxlich))
10+
### Fixed
11+
12+
- Boolean fields with a default value of `true` always returned `true` when validating.
13+
- Fixed infinite deep type instantiation on `message`. (#143, thanks to [Alisson Cavalcante Agiani](https://github.com/thelinuxlich))
14+
- Fixed typesVersions map that caused incorrect auto-import paths (#191, thanks to [CokaKoala](https://github.com/AdrianGonz97))
1115

1216
## [1.0.0]
1317

src/lib/superValidate.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,14 @@ function formDataToValidation<T extends AnyZodObject>(
137137
typeInfo: ZodTypeInfo
138138
): unknown {
139139
const newValue = valueOrDefault(value, false, true, typeInfo);
140+
const zodType = typeInfo.zodType;
140141

141142
// If the value was empty, it now contains the default value,
142-
// so it can be returned immediately
143-
if (!value) return newValue;
144-
145-
const zodType = typeInfo.zodType;
143+
// so it can be returned immediately, unless it's boolean, which
144+
// means it could have been posted as a checkbox.
145+
if (!value && zodType._def.typeName != 'ZodBoolean') {
146+
return newValue;
147+
}
146148

147149
if (zodType._def.typeName == 'ZodString') {
148150
return value;
@@ -182,14 +184,14 @@ function formDataToValidation<T extends AnyZodObject>(
182184
return value;
183185
} else if (zodType._def.typeName == 'ZodNativeEnum') {
184186
const zodEnum = zodType as ZodNativeEnum<EnumLike>;
185-
if (value in zodEnum.enum) {
187+
if (value !== null && value in zodEnum.enum) {
186188
const enumValue = zodEnum.enum[value];
187189
if (typeof enumValue === 'number') return enumValue;
188190
else if (enumValue in zodEnum.enum) return zodEnum.enum[enumValue];
189191
}
190192
return undefined;
191193
} else if (zodType._def.typeName == 'ZodSymbol') {
192-
return Symbol(value);
194+
return Symbol(String(value));
193195
}
194196

195197
throw new SuperFormError(
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { Actions, PageServerLoad } from './$types';
2+
import { message, superValidate } from '$lib/server';
3+
import { schema } from './schema';
4+
import { fail } from '@sveltejs/kit';
5+
6+
export const load = (async () => {
7+
const form = await superValidate(schema);
8+
return { form };
9+
}) satisfies PageServerLoad;
10+
11+
export const actions = {
12+
default: async ({ request }) => {
13+
const formData = await request.formData();
14+
console.log(
15+
'🚀 ~ file: +page.server.ts:14 ~ default: ~ formData:',
16+
formData
17+
);
18+
const form = await superValidate(formData, schema);
19+
20+
console.log('POST', form);
21+
22+
if (!form.valid) return fail(400, { form });
23+
24+
return message(form, 'Posted OK!');
25+
}
26+
} satisfies Actions;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 { schema } from './schema';
6+
7+
export let data: PageData;
8+
9+
const { form, errors, tainted, message, enhance } = superForm(data.form, {
10+
//dataType: 'json',
11+
//validators: schema
12+
});
13+
</script>
14+
15+
<SuperDebug data={{ $form, $errors, $tainted }} />
16+
17+
{#if $message}<h4>{$message}</h4>{/if}
18+
19+
<form method="POST">
20+
<label class="flex items-center space-x-2">
21+
<input
22+
type="checkbox"
23+
class="checkbox"
24+
id="emaillist"
25+
name="emaillist"
26+
bind:checked={$form.emaillist}
27+
/> Emaillist
28+
</label>
29+
30+
<button>Submit</button>
31+
</form>
32+
33+
<style lang="scss">
34+
form {
35+
margin: 2rem 0;
36+
37+
input {
38+
background-color: #dedede;
39+
}
40+
}
41+
</style>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { z } from 'zod';
2+
3+
export const schema = z.object({
4+
emaillist: z.boolean().optional().default(true)
5+
});

0 commit comments

Comments
 (0)