sveltekit-superforms
Advanced tools
Comparing version 0.1.3 to 0.2.0
@@ -57,3 +57,3 @@ import { type MaybePromise, type SubmitFunction } from '$app/forms'; | ||
message: Writable<Validation<T>['message']>; | ||
success: Readable<boolean>; | ||
validated: Readable<boolean>; | ||
empty: Readable<boolean>; | ||
@@ -60,0 +60,0 @@ submitting: Readable<boolean>; |
@@ -138,3 +138,3 @@ import { enhance, applyAction } from '$app/forms'; | ||
return { | ||
success: true, | ||
validated: true, | ||
errors: {}, | ||
@@ -158,3 +158,3 @@ data: {}, | ||
// Stores for the properties of Validation<T> | ||
const Success = writable(form.success); | ||
const Validated = writable(form.validated); | ||
const Errors = writable(form.errors); | ||
@@ -187,3 +187,3 @@ const Data = writable(form.data); | ||
function rebind(validation, untaint) { | ||
Success.set(validation.success); | ||
Validated.set(validation.validated); | ||
Errors.set(validation.errors); | ||
@@ -207,3 +207,3 @@ Data.set(validation.data); | ||
} | ||
if (validation.success && options.resetForm) { | ||
if (validation.validated && options.resetForm) { | ||
_resetForm(); | ||
@@ -298,3 +298,3 @@ } | ||
message: Message, | ||
success: derived(Success, ($s) => $s), | ||
validated: derived(Validated, ($s) => $s), | ||
empty: derived(Empty, ($e) => $e), | ||
@@ -301,0 +301,0 @@ submitting: derived(Submitting, ($s) => $s), |
import type { z, AnyZodObject } from 'zod'; | ||
export type ValidationErrors<T extends AnyZodObject> = Partial<Record<keyof z.infer<T>, string[] | undefined>>; | ||
export type Validation<T extends AnyZodObject> = { | ||
success: boolean; | ||
validated: boolean; | ||
errors: ValidationErrors<T>; | ||
@@ -6,0 +6,0 @@ data: z.infer<T>; |
@@ -25,3 +25,2 @@ import { type RequestEvent } from '@sveltejs/kit'; | ||
* @param options.noErrors For load requests, this is usually set to prevent validation errors from showing directly on a GET request. | ||
* @returns An object with success, errors and data properties. | ||
*/ | ||
@@ -28,0 +27,0 @@ export declare function superValidate<T extends AnyZodObject>(data: RequestEvent | Request | FormData | Partial<Record<keyof z.infer<T>, unknown>> | null | undefined, schema: T, options?: { |
@@ -59,3 +59,3 @@ import { fail, json } from '@sveltejs/kit'; | ||
} | ||
form.success = false; | ||
form.validated = false; | ||
return fail(400, { form }); | ||
@@ -232,3 +232,2 @@ } | ||
* @param options.noErrors For load requests, this is usually set to prevent validation errors from showing directly on a GET request. | ||
* @returns An object with success, errors and data properties. | ||
*/ | ||
@@ -280,3 +279,3 @@ export async function superValidate(data, schema, options = {}) { | ||
return { | ||
success: true, | ||
validated: true, | ||
errors: {}, | ||
@@ -294,3 +293,3 @@ data: data, | ||
return { | ||
success: false, | ||
validated: false, | ||
errors, | ||
@@ -304,3 +303,3 @@ data: data, | ||
return { | ||
success: true, | ||
validated: true, | ||
errors: {}, | ||
@@ -307,0 +306,0 @@ data: status.data, |
{ | ||
"name": "sveltekit-superforms", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"author": "Andreas Söderlund <ciscoheat@gmail.com> (https://blog.encodeart.dev)", | ||
@@ -5,0 +5,0 @@ "description": "Supercharge your SvelteKit forms with this powerhouse of a library!", |
@@ -121,3 +121,3 @@ # sveltekit-superforms 💥 | ||
// Convenient validation check: | ||
if (!form.success) { | ||
if (!form.validated) { | ||
// Again, always return { form } and you'll be fine. | ||
@@ -137,3 +137,3 @@ return fail(400, { form }); | ||
POST { | ||
success: false, | ||
validated: false, | ||
errors: { email: [ 'Invalid email' ] }, | ||
@@ -148,3 +148,3 @@ data: { name: 'Hello world!', email: '' }, | ||
- `success` - A `boolean` which tells you whether the validation succeeded or not. | ||
- `validated` - A `boolean` which tells you whether the validation succeeded or not. | ||
- `errors` - A `Record<string, string[]>` of all validation errors. | ||
@@ -158,3 +158,3 @@ - `data` - The coerced posted data, in this case not valid, so it should be promptly returned to the client. | ||
```ts | ||
if (!form.success) { | ||
if (!form.validated) { | ||
return fail(400, { form }); | ||
@@ -295,3 +295,3 @@ } | ||
The `update(result, untaint?)` function takes an `ActionResult` which is **not** of type `error`, and an optional `untaint` parameter, which can be used to untaint the form, so the dialog won't appear when navigating away. If not specified, result types `success` and `redirect` will untaint the form. | ||
The `update(result, untaint?)` function takes an `ActionResult` of type `success` or `failure`, and an optional `untaint` parameter which can be used to untaint the form, so the dialog won't appear when navigating away. If `untaint` isn't specified, a result status between 200-299 will untaint the form. | ||
@@ -583,3 +583,3 @@ `formEl` is the `HTMLFormElement` of the form. | ||
const form = await superValidate(event, crudSchema); | ||
if (!form.success) return fail(400, { form }); | ||
if (!form.validated) return fail(400, { form }); | ||
@@ -634,3 +634,3 @@ if (!form.data.id) { | ||
export type Validation<T extends AnyZodObject> = { | ||
success: boolean; | ||
validated: boolean; | ||
errors: ValidationErrors<T>; | ||
@@ -677,3 +677,3 @@ data: z.infer<T>; | ||
{ | ||
success: true; | ||
validated: true; | ||
errors: {}; | ||
@@ -700,3 +700,3 @@ data: z.infer<T>; // See further down for default entity values. | ||
If you want to return a form with no validation errors. Only the `errors` property will be modified, so `success` still indicates the validation status. Useful for load functions where the entity is invalid, but as a initial state no errors should be displayed on the form. | ||
If you want to return a form with no validation errors. Only the `errors` property will be modified, so `validated` still indicates the validation status. Useful for load functions where the entity is invalid, but as a initial state no errors should be displayed on the form. | ||
@@ -725,3 +725,3 @@ ```ts | ||
const form = await superValidate(event, loginSchema); | ||
if (!form.success) return actionResult('failure', { form }); | ||
if (!form.validated) return actionResult('failure', { form }); | ||
@@ -728,0 +728,0 @@ // Verify login here // |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
77951
1092
1