From 94feac96ca7797e3a70e25c63473c3ebe5da6820 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20S=C3=B6derlund?= Date: Mon, 2 Oct 2023 15:03:06 +0200 Subject: [PATCH] Added tainted to formFieldProxy. --- CHANGELOG.md | 8 ++- src/lib/client/proxies.ts | 39 ++++++++++++++- src/routes/Navigation.svelte | 1 + .../tests/tainted-proxy/+page.server.ts | 22 +++++++++ src/routes/tests/tainted-proxy/+page.svelte | 49 +++++++++++++++++++ src/routes/tests/tainted-proxy/schema.ts | 7 +++ 6 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 src/routes/tests/tainted-proxy/+page.server.ts create mode 100644 src/routes/tests/tainted-proxy/+page.svelte create mode 100644 src/routes/tests/tainted-proxy/schema.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 98fe6652..2dcc9454 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,11 +5,15 @@ Headlines: Added, Changed, Deprecated, Removed, Fixed, Security The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased] +## [1.8.0] ### Fixed -- [Array errors](https://superforms.rocks/concepts/error-handling#form-level-and-array-errors) were always added, even if the form wasn't tainted. +- [Array errors](https://superforms.rocks/concepts/error-handling#form-level-and-array-errors) were always added, even if the array or any data in it hadn't tainted the form. + +### Added + +- [formFieldProxy](https://superforms.rocks/components#using-a-formfieldproxy) now contains a proxy for `tainted`. ## [1.7.4] - 2023-09-29 diff --git a/src/lib/client/proxies.ts b/src/lib/client/proxies.ts index 32bed857..b5773750 100644 --- a/src/lib/client/proxies.ts +++ b/src/lib/client/proxies.ts @@ -245,6 +245,7 @@ export function formFieldProxy< value: Writable>, Path>>; errors: Writable; constraints: Writable; + tainted: Writable; } { const path2 = splitPath>>(path); // Filter out array indices, the constraints structure doesn't contain these. @@ -252,6 +253,41 @@ export function formFieldProxy< .filter((p) => isNaN(parseInt(String(p)))) .join('.'); + const taintedProxy = derived( + form.tainted, + ($tainted) => { + if (!$tainted) return $tainted; + const taintedPath = traversePath($tainted, path2); + return taintedPath ? taintedPath.value : undefined; + } + ); + + const tainted = { + subscribe: taintedProxy.subscribe, + update(upd: Updater) { + form.tainted.update(($tainted) => { + if (!$tainted) $tainted = {}; + const output = traversePath($tainted, path2, (path) => { + if (!path.value) path.parent[path.key] = {}; + return path.parent[path.key]; + }); + if (output) output.parent[output.key] = upd(output.value); + return $tainted; + }); + }, + set(value: boolean | undefined) { + form.tainted.update(($tainted) => { + if (!$tainted) $tainted = {}; + const output = traversePath($tainted, path2, (path) => { + if (!path.value) path.parent[path.key] = {}; + return path.parent[path.key]; + }); + if (output) output.parent[output.key] = value; + return $tainted; + }); + } + }; + return { path, value: fieldProxy(form.form, path), @@ -261,7 +297,8 @@ export function formFieldProxy< constraints: fieldProxy( form.constraints, constraintsPath as never - ) as Writable + ) as Writable, + tainted }; } diff --git a/src/routes/Navigation.svelte b/src/routes/Navigation.svelte index 2b487842..68448ed0 100644 --- a/src/routes/Navigation.svelte +++ b/src/routes/Navigation.svelte @@ -27,6 +27,7 @@ Flash onError Reset component 1 Reset component 2 + Tainted formFieldProxy diff --git a/src/routes/tests/tainted-proxy/schema.ts b/src/routes/tests/tainted-proxy/schema.ts new file mode 100644 index 00000000..9b731188 --- /dev/null +++ b/src/routes/tests/tainted-proxy/schema.ts @@ -0,0 +1,7 @@ +import { z } from 'zod'; + +export const schema = z.object({ + user: z.object({ + name: z.string().min(1) + }) +});