Skip to content

Commit e5c8507

Browse files
committed
Null and undefined cannot be sent to superForm anymore.
1 parent dd110da commit e5c8507

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Removed
1111

12+
- For type safety, you cannot send `null` or `undefined` to `superForm` anymore. Use `superValidate`, or pass a complete data object to `superForm`. Default values can be added with the `defaultValues` function.
1213
- The `valid` option is removed from `message`, any status >= 400 will return a fail.
1314

1415
## [1.0.0-rc.2]

src/lib/client/index.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export type FormOptions<T extends ZodValidation<AnyZodObject>, M> = Partial<{
143143
};
144144
warnings: {
145145
duplicateId?: boolean;
146+
noValidationAndConstraints?: boolean;
146147
};
147148
}>;
148149

@@ -260,11 +261,7 @@ export function superForm<
260261
// eslint-disable-next-line @typescript-eslint/no-explicit-any
261262
M = any
262263
>(
263-
form:
264-
| z.infer<UnwrapEffects<T>>
265-
| SuperValidated<UnwrapEffects<T>, M>
266-
| null
267-
| undefined,
264+
form: z.infer<UnwrapEffects<T>> | SuperValidated<T, M>,
268265
options: FormOptions<UnwrapEffects<T>, M> = {}
269266
): SuperForm<UnwrapEffects<T>, M> {
270267
type UnwrappedT = UnwrapEffects<T>;
@@ -278,18 +275,32 @@ export function superForm<
278275

279276
if (options.SPA && options.validators === undefined) {
280277
console.warn(
281-
'No validators set for Superform in SPA mode. Add them to the validators option, or set it to false to disable this warning.'
278+
'No validators set for Superform in SPA mode. ' +
279+
'Add them to the validators option, or set it to false to disable this warning.'
282280
);
283281
}
284282
}
285283

286284
let _formId: string | undefined = options.id;
287285

288286
// Normalize form argument to SuperValidated<T, M>
289-
if (!form) {
290-
form = Context_newEmptyForm(); // Takes care of null | undefined
291-
} else if (Context_isValidationObject(form) === false) {
292-
form = Context_newEmptyForm(form); // Takes care of Partial<z.infer<T>>
287+
if (!form || Context_isValidationObject(form) === false) {
288+
if (options.warnings?.noValidationAndConstraints !== false) {
289+
console.warn(
290+
(form
291+
? 'Form data sent directly to superForm instead of through superValidate. '
292+
: 'No form data sent to superForm. Schema type safety cannot be guaranteed. ') +
293+
'No initial validation is made and no constraints will exist for the form. ' +
294+
'Set the warnings.noValidationAndConstraints option to false to disable this warning.'
295+
);
296+
}
297+
form = {
298+
valid: false,
299+
posted: false,
300+
errors: {},
301+
data: form ?? {},
302+
constraints: {} as SuperValidated<T, M>['constraints']
303+
};
293304
} else {
294305
if (_formId === undefined) _formId = form.id;
295306
}
@@ -303,7 +314,7 @@ export function superForm<
303314
if (postedForm.id === _formId) {
304315
const pageDataForm = form as SuperValidated<T, M>;
305316
form = postedForm as SuperValidated<T, M>;
306-
// Do the non-use:enhance stuff
317+
// Reset the form if option set and form is valid.
307318
if (
308319
form.valid &&
309320
options.resetForm &&
@@ -351,18 +362,6 @@ export function superForm<
351362
Context.taintedFormState = clone(data);
352363
}
353364

354-
function Context_newEmptyForm(
355-
data?: Partial<z.infer<T>>
356-
): SuperValidated<T, M> {
357-
return {
358-
valid: false,
359-
posted: false,
360-
errors: {},
361-
data: data ?? {},
362-
constraints: {} as SuperValidated<T, M>['constraints']
363-
};
364-
}
365-
366365
function Context_findValidationForms(data: Record<string, unknown>) {
367366
const forms = Object.values(data).filter(
368367
(v) => Context_isValidationObject(v) !== false

0 commit comments

Comments
 (0)