Skip to content

Commit f8bbccf

Browse files
committed
Fixed exception message when using nested data in normal posting.
1 parent ad66e8f commit f8bbccf

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ 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-
## [1.2.0]
8+
## [Unreleased]
9+
10+
### Fixed
11+
12+
- Fixed exception message when the `dataType` option isn't set to `'json'` and the schema contains a nested object.
13+
14+
## [1.2.0] - 2023-07-06
915

1016
### Added
1117

1218
- The `scrollToError` option can now take the same parameters as [scrollIntoView](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView), which makes it work for nested scrollbars.
13-
- Added signature to more conveniently set a form-level error with `setError`.
19+
- Added `setError` signature, to more conveniently set a form-level error: `setError(form, 'Form-level error message')`
1420

1521
### Fixed
1622

src/errors.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,32 @@ test('Refined errors on leaf node', async () => {
277277
flavours: { _errors: ["Can't order more flavours than scoops!"] }
278278
});
279279
});
280+
281+
test('Error on posting nested data in dataType form mode', async () => {
282+
const registrationSchema = z.object({
283+
username: z.string(),
284+
credential: z.object({
285+
id: z.string(),
286+
publicKey: z.string(),
287+
algorithm: z.string()
288+
}),
289+
authenticatorData: z.string(),
290+
clientData: z.string()
291+
});
292+
293+
const schema = z.object({
294+
email: z.string().email(),
295+
registration: registrationSchema.optional()
296+
});
297+
298+
const formData = new FormData();
299+
formData.set('email', '[email protected]');
300+
formData.set(
301+
'registration',
302+
'{"username":"[email protected]","credential":{"id":"6igsmeLIsd2jTAraOL1QX4qUWjdtvBX3gMeEHOR-QcU","publicKey":"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEOdtZo4FdtR2AAU6pR0u9d4qJcLsfnCM18No1lwTjx-7sJds6sr4SI721yzwDMYIB1L8frZkuUs1JK4Rq5C4fYg==","algorithm":"ES256"},"authenticatorData":"SZYN5YgOjGh0NBcPZHZgW4_krrmihjLHmVzzuoMdl2NFAAAAAQECAwQFBgcIAQIDBAUGBwgAIOooLJniyLHdo0wK2ji9UF-KlFo3bbwV94DHhBzkfkHFpQECAyYgASFYIDnbWaOBXbUdgAFOqUdLvXeKiXC7H5wjNfDaNZcE48fuIlgg7CXbOrK-EiO9tcs8AzGCAdS_H62ZLlLNSSuEauQuH2I=","clientData":"eyJ0eXBlIjoid2ViYXV0aG4uY3JlYXRlIiwiY2hhbGxlbmdlIjoiU1llTVR5aHpmaHFXaUx4ZyIsIm9yaWdpbiI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTE3MyIsImNyb3NzT3JpZ2luIjpmYWxzZX0="}'
303+
);
304+
305+
await expect(superValidate(formData, schema)).rejects.toThrow(
306+
/Object found in form field "registration"/
307+
);
308+
});

src/lib/client/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,8 @@ export function superForm<
374374
for (const form of forms) {
375375
if (duplicateId.has(form.id)) {
376376
console.warn(
377-
`Duplicate form id found: "${form.id}"` +
378-
'. Multiple forms will receive the same data. Use the id option to differentiate between them, ' +
377+
`Duplicate form id found: "${form.id}". ` +
378+
'Multiple forms will receive the same data. Use the id option to differentiate between them, ' +
379379
'or if this is intended, set warnings.duplicateId option to false to disable this message.'
380380
);
381381
} else {
@@ -472,7 +472,9 @@ export function superForm<
472472
if (value.length > 0) Form_checkForNestedData(key, value[0]);
473473
} else if (!(value instanceof Date)) {
474474
throw new SuperFormError(
475-
`Object found in form field "${key}". Set options.dataType = 'json' and use:enhance to use nested data structures.`
475+
`Object found in form field "${key}". ` +
476+
`Set the dataType option to "json" and add use:enhance to use nested data structures. ` +
477+
`More information: https://superforms.rocks/concepts/nested-data`
476478
);
477479
}
478480
}

src/lib/superValidate.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,14 @@ function formDataToValidation<T extends AnyZodObject>(
258258
return Symbol(String(value));
259259
}
260260

261+
if (zodType._def.typeName == 'ZodObject') {
262+
throw new SuperFormError(
263+
`Object found in form field "${field}". ` +
264+
`Set the dataType option to "json" and add use:enhance on the client to use nested data structures. ` +
265+
`More information: https://superforms.rocks/concepts/nested-data`
266+
);
267+
}
268+
261269
throw new SuperFormError(
262270
'Unsupported Zod default type: ' + zodType.constructor.name
263271
);

0 commit comments

Comments
 (0)