sveltekit-superforms
Advanced tools
Comparing version 0.5.14 to 0.5.15
@@ -13,3 +13,3 @@ import { type MaybePromise, type SubmitFunction } from '$app/forms'; | ||
export type Validators<T extends AnyZodObject> = Partial<{ | ||
[Property in keyof z.infer<T>]: (value: z.infer<T>[Property]) => MaybePromise<string | null | undefined>; | ||
[Property in keyof z.infer<T>]: (value: z.infer<T>[Property]) => MaybePromise<string | string[] | null | undefined>; | ||
}>; | ||
@@ -72,5 +72,4 @@ export type FormOptions<T extends AnyZodObject> = { | ||
constraints: Writable<Validation<T>['constraints']>; | ||
fields: Readable<FormField<T>[]>; | ||
message: Writable<Validation<T>['message']>; | ||
validated: Readable<boolean>; | ||
valid: Readable<boolean>; | ||
empty: Readable<boolean>; | ||
@@ -80,2 +79,3 @@ submitting: Readable<boolean>; | ||
timeout: Readable<boolean>; | ||
fields: Readable<FormField<T>[]>; | ||
firstError: Readable<{ | ||
@@ -82,0 +82,0 @@ key: string; |
@@ -71,3 +71,3 @@ import { enhance, applyAction } from '$app/forms'; | ||
// Stores for the properties of Validation<T> | ||
const Validated = writable(initialForm.valid); | ||
const Valid = writable(initialForm.valid); | ||
const Errors = writable(initialForm.errors); | ||
@@ -94,4 +94,4 @@ const Data = writable(initialForm.data); | ||
} | ||
// Need to set this after use:enhance has run, to avoid showing the dialog | ||
// when a form doesn't use it. | ||
// Need to set this after use:enhance has run, to avoid showing the | ||
// tainted dialog when a form doesn't use it or the browser doesn't use JS. | ||
let savedForm; | ||
@@ -112,8 +112,6 @@ const _taintedMessage = options.taintedMessage; | ||
Data.set(form.data); | ||
// Set Errors AFTER Data so client-side validation doesn't | ||
// immediately overwrite the server errors! | ||
Errors.set(form.errors); | ||
Message.set(form.message); | ||
Empty.set(form.empty); | ||
Validated.set(form.valid); | ||
Valid.set(form.valid); | ||
Errors.set(form.errors); | ||
} | ||
@@ -173,26 +171,31 @@ async function _update(form, untaint) { | ||
}); | ||
// Check client validation on data change | ||
let previousForm = { ...initialForm.data }; | ||
Data.subscribe(async (f) => { | ||
// Validation check, must be run before Errors are updated | ||
// so they aren't immediately overwritten by this. | ||
if (get(Submitting)) | ||
return; | ||
for (const key of Object.keys(f)) { | ||
if (f[key] !== previousForm[key]) { | ||
const validator = options.validators && options.validators[key]; | ||
if (validator) { | ||
const newError = await validator(f[key]); | ||
Errors.update((e) => { | ||
if (!newError) | ||
delete e[key]; | ||
else | ||
e[key] = [newError]; | ||
return e; | ||
}); | ||
} | ||
else if (options.defaultValidator == 'clear') { | ||
Errors.update((e) => { | ||
if (f[key] === previousForm[key]) | ||
continue; | ||
console.log('🚀 ~ file: index.ts:349 ~ Data.subscribe ~ UPDATE key:', key); | ||
const validator = options.validators && options.validators[key]; | ||
if (validator) { | ||
const newError = await validator(f[key]); | ||
Errors.update((e) => { | ||
if (!newError) | ||
delete e[key]; | ||
return e; | ||
}); | ||
} | ||
else { | ||
e[key] = Array.isArray(newError) | ||
? newError | ||
: [newError]; | ||
} | ||
return e; | ||
}); | ||
} | ||
else if (options.defaultValidator == 'clear') { | ||
Errors.update((e) => { | ||
delete e[key]; | ||
return e; | ||
}); | ||
} | ||
} | ||
@@ -227,4 +230,5 @@ previousForm = { ...f }; | ||
return { | ||
form: Data, | ||
errors: Errors, | ||
form: Data, | ||
message: Message, | ||
constraints: Constraints, | ||
@@ -240,5 +244,4 @@ fields: derived([Data, Errors, Constraints], ([$D, $E, $C]) => { | ||
}), | ||
message: Message, | ||
tainted: Tainted, | ||
validated: derived(Validated, ($s) => $s), | ||
tainted: derived(Tainted, ($t) => $t), | ||
valid: derived(Valid, ($s) => $s), | ||
empty: derived(Empty, ($e) => $e), | ||
@@ -245,0 +248,0 @@ submitting: derived(Submitting, ($s) => $s), |
@@ -6,3 +6,5 @@ import { type RequestEvent } from '@sveltejs/kit'; | ||
export { defaultEntity } from './entity'; | ||
export declare function setError<T extends AnyZodObject>(form: Validation<T>, field: keyof z.infer<T>, error: string | string[] | null): import("@sveltejs/kit").ActionFailure<{ | ||
export declare function setError<T extends AnyZodObject>(form: Validation<T>, field: keyof z.infer<T>, error: string | string[] | null, options?: { | ||
overwrite: boolean; | ||
}): import("@sveltejs/kit").ActionFailure<{ | ||
form: Validation<T>; | ||
@@ -9,0 +11,0 @@ }>; |
@@ -6,5 +6,5 @@ import { fail, json } from '@sveltejs/kit'; | ||
export { defaultEntity } from './entity'; | ||
export function setError(form, field, error) { | ||
export function setError(form, field, error, options = { overwrite: false }) { | ||
const errArr = Array.isArray(error) ? error : error ? [error] : []; | ||
if (form.errors[field]) { | ||
if (form.errors[field] && !options.overwrite) { | ||
form.errors[field] = form.errors[field]?.concat(errArr); | ||
@@ -11,0 +11,0 @@ } |
{ | ||
"name": "sveltekit-superforms", | ||
"version": "0.5.14", | ||
"version": "0.5.15", | ||
"author": "Andreas Söderlund <ciscoheat@gmail.com> (https://blog.encodeart.dev)", | ||
@@ -64,8 +64,8 @@ "description": "Supercharge your SvelteKit forms with this powerhouse of a library!", | ||
"@sveltejs/adapter-auto": "^2.0.0", | ||
"@sveltejs/kit": "^1.8.3", | ||
"@sveltejs/kit": "^1.8.8", | ||
"@sveltejs/package": "^2.0.2", | ||
"@typescript-eslint/eslint-plugin": "^5.53.0", | ||
"@typescript-eslint/parser": "^5.53.0", | ||
"@typescript-eslint/eslint-plugin": "^5.54.0", | ||
"@typescript-eslint/parser": "^5.54.0", | ||
"devalue": "^4.3.0", | ||
"eslint": "^8.34.0", | ||
"eslint": "^8.35.0", | ||
"eslint-config-prettier": "^8.6.0", | ||
@@ -79,3 +79,3 @@ "eslint-plugin-svelte3": "^4.0.0", | ||
"svelte": "^3.55.1", | ||
"svelte-check": "^3.0.3", | ||
"svelte-check": "^3.0.4", | ||
"sveltekit-flash-message": "^0.11.3", | ||
@@ -82,0 +82,0 @@ "tslib": "^2.5.0", |
82861
1338