================ CODE SNIPPETS ================ TITLE: Update Validation with Adapters in SvelteKit Superforms DESCRIPTION: Validation now requires explicit adapters. Import from `sveltekit-superforms/adapters` and wrap schemas with `zod()` for `superValidate`. Type parameters for schemas must be wrapped with `Infer`. SOURCE: https://github.com/ciscoheat/sveltekit-superforms/blob/main/CHANGELOG.md#_snippet_0 LANGUAGE: TypeScript CODE: ``` import { superValidate } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; import { Infer } from 'sveltekit-superforms'; // Old: // superValidate(schema) // New: // superValidate(zod(schema)) // For schemas with type parameters: // type MySchema = Infer; ``` -------------------------------- TITLE: Update Superform Validator Functions with superformClient Adapter DESCRIPTION: Superform validator functions now require the `superformClient` adapter. Additionally, the input to these validator functions can now be `undefined`. SOURCE: https://github.com/ciscoheat/sveltekit-superforms/blob/main/CHANGELOG.md#_snippet_6 LANGUAGE: TypeScript CODE: ``` import { superForm } from 'sveltekit-superforms'; import { superformClient } from 'sveltekit-superforms/adapters'; // Old: // const { form } = superForm(data, { // validators: { /* ... */ } // }); // New: const { form } = superForm(data, { validators: superformClient({ // Validator function can now receive undefined myField: (value?: string) => { if (value === undefined) return 'Value is undefined'; return null; } }) }); ``` -------------------------------- TITLE: Adjust Default superForm Options and Legacy Mode DESCRIPTION: Default `superForm` options are now `resetForm: true` and `taintedMessage: false`. To revert to old defaults, add `define: { SUPERFORMS_LEGACY: true }` in `vite.config.ts`. SOURCE: https://github.com/ciscoheat/sveltekit-superforms/blob/main/CHANGELOG.md#_snippet_1 LANGUAGE: TypeScript CODE: ``` // vite.config.ts import { sveltekit } from '@sveltejs/kit/vite'; import { defineConfig } from 'vite'; export default defineConfig({ plugins: [sveltekit()], define: { SUPERFORMS_LEGACY: true } }); // superForm usage (defaults changed): // const { form } = superForm(data, { // resetForm: true, // Now default // taintedMessage: false // Now default // }); ``` -------------------------------- TITLE: Rename superValidateSync to defaults and Change Behavior DESCRIPTION: `superValidateSync` is now `defaults` and only returns schema default values, no longer performing validation. For initial validation, use `+page.ts`. SOURCE: https://github.com/ciscoheat/sveltekit-superforms/blob/main/CHANGELOG.md#_snippet_2 LANGUAGE: TypeScript CODE: ``` import { defaults } from 'sveltekit-superforms'; // Old: // const data = superValidateSync(schema); // New: const defaultValues = defaults(schema); // For initial validation in SvelteKit: // // +page.ts // import { superValidate } from 'sveltekit-superforms'; // import { zod } from 'sveltekit-superforms/adapters'; // import { schema } from './schema'; // export const load = async () => { // const form = await superValidate(zod(schema)); // return { form }; // }; ``` -------------------------------- TITLE: Styling Input Elements with CSS DESCRIPTION: This CSS snippet sets the line-height property for all input elements to 1. This is a common practice to standardize vertical alignment and spacing within forms, ensuring consistent rendering across different browsers and form controls. SOURCE: https://github.com/ciscoheat/sveltekit-superforms/blob/main/src/app.html#_snippet_0 LANGUAGE: CSS CODE: ``` input { line-height: 1; } ``` -------------------------------- TITLE: Require String Type for Form ID DESCRIPTION: The form ID can no longer be `undefined` and must be a `string`. It is now automatically set by default. SOURCE: https://github.com/ciscoheat/sveltekit-superforms/blob/main/CHANGELOG.md#_snippet_9 LANGUAGE: TypeScript CODE: ``` import { superForm } from 'sveltekit-superforms'; // Old (form ``` -------------------------------- TITLE: Rename fieldErrors to valueErrors in arrayProxy DESCRIPTION: The `fieldErrors` property within `arrayProxy` has been renamed to `valueErrors`. SOURCE: https://github.com/ciscoheat/sveltekit-superforms/blob/main/CHANGELOG.md#_snippet_3 LANGUAGE: TypeScript CODE: ``` // Old: // arrayProxy.fieldErrors // New: // arrayProxy.valueErrors ``` -------------------------------- TITLE: Remove 'required' Constraint for Properties with Default Values DESCRIPTION: Properties in schemas that have a default value will no longer include the `required` constraint in their generated constraints. SOURCE: https://github.com/ciscoheat/sveltekit-superforms/blob/main/CHANGELOG.md#_snippet_8 LANGUAGE: TypeScript CODE: ``` import { z } from 'zod'; const schema = z.object({ optionalField: z.string().optional(), fieldWithDefault: z.string().default('default') }); // Constraints for fieldWithDefault will not include 'required'. ``` -------------------------------- TITLE: Change superValidate Default Value Validation Behavior DESCRIPTION: When `superValidate` is called with only a schema, default values are no longer validated by default. To enable validation of default values, the `errors` option must be explicitly set to `true`. SOURCE: https://github.com/ciscoheat/sveltekit-superforms/blob/main/CHANGELOG.md#_snippet_7 LANGUAGE: TypeScript CODE: ``` import { superValidate } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; import { z } from 'zod'; const schema = z.object({ name: z.string().default('Default Name') }); // Old (default values were validated): // const form = await superValidate(zod(schema)); // New (default values not validated unless errors: true): const formWithoutDefaultValidation = await superValidate(zod(schema)); const formWithDefaultValidation = await superValidate(zod(schema), { errors: true }); ``` -------------------------------- TITLE: Require Explicit Default Values for Enums in Schemas DESCRIPTION: Enums used in schemas must now be defined with an explicit default value. SOURCE: https://github.com/ciscoheat/sveltekit-superforms/blob/main/CHANGELOG.md#_snippet_4 LANGUAGE: TypeScript CODE: ``` import { z } from 'zod'; // Old (might work, but not recommended): // const MyEnumSchema = z.enum(['A', 'B']); // New (explicit default required): const MyEnumSchema = z.enum(['A', 'B']).default('A'); ``` -------------------------------- TITLE: Remove String Parsing for Numeric Enums DESCRIPTION: Numeric enums can no longer be parsed using their string representations. SOURCE: https://github.com/ciscoheat/sveltekit-superforms/blob/main/CHANGELOG.md#_snippet_5 LANGUAGE: TypeScript CODE: ``` import { z } from 'zod'; enum NumericEnum { A = 0, B = 1 } const Schema = z.object({ value: z.nativeEnum(NumericEnum) }); // This will no longer work: // Schema.parse({ value: '0' }); // Must use numeric value: // Schema.parse({ value: 0 }); ```