Skip to content

Commit 9dbad8e

Browse files
committed
Schema transforms weren't applied on the client.
1 parent 68c2af6 commit 9dbad8e

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Added `FormResult<T>`, which can be used in [onResult](https://superforms.rocks/concepts/events#onresult) to make the ActionResult strongly typed.
1414
- [SuperDebug](https://superforms.rocks/super-debug) now has a `collapsed` prop, to make it initially collapsed. Use together with `collapsible`. ([#279](https://github.com/ciscoheat/sveltekit-superforms/issues/279))
1515

16+
### Fixed
17+
18+
- Schema `transform` operations weren't applied in [SPA forms](https://superforms.rocks/concepts/spa) and when posting to the server with client-side [validators](https://superforms.rocks/concepts/client-validation#validators).
19+
1620
## [1.8.0]
1721

1822
### Fixed

src/lib/client/clientValidation.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ async function _clientValidation<T extends AnyZodObject, M = unknown>(
110110
errorShape(validator)
111111
// eslint-disable-next-line @typescript-eslint/no-explicit-any
112112
) as any;
113+
} else {
114+
checkData = result.data;
113115
}
114116
} else {
115117
// SuperForms validator

src/lib/client/formEnhance.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,10 @@ export function formEnhance<T extends AnyZodObject, M>(
165165
setCustomValidity(validityEl as HTMLInputElement, result.errors);
166166
}
167167

168-
if (result.data) {
169-
data.set(result.data);
170-
}
168+
// NOTE: Uncomment if Zod transformations should be immediately applied, not just when submitting.
169+
// Not enabled because it's not great UX, and it's rare to have transforms, which will just result in
170+
// redundant store updates.
171+
//if (result.data) data.set(result.data);
171172
}
172173

173174
/**
@@ -338,25 +339,21 @@ export function formEnhance<T extends AnyZodObject, M>(
338339
if (options.SPA) {
339340
cancel();
340341

341-
const validationResult: SuperValidated<T> = {
342-
valid: true,
343-
posted: true,
344-
errors: {},
345-
data: get(data),
346-
constraints: get(constraints),
347-
message: undefined,
348-
id: get(formId)
349-
};
342+
const validationResult = { ...validation, posted: true };
350343

351344
const result = {
352-
type: 'success' as const,
353-
status: 200,
345+
type: validationResult.valid ? 'success' : 'failure',
346+
status: validationResult.valid
347+
? 200
348+
: typeof options.SPA == 'object'
349+
? options.SPA?.failStatus
350+
: 400 ?? 400,
354351
data: { form: validationResult }
355-
};
352+
} as ActionResult;
356353

357354
setTimeout(() => validationResponse({ result }), 0);
358355
} else if (options.dataType === 'json') {
359-
const postData = get(data);
356+
const postData = validation.data;
360357
const chunks = chunkSubstr(
361358
stringify(postData),
362359
options.jsonChunkSize ?? 500000

0 commit comments

Comments
 (0)