File tree Expand file tree Collapse file tree 5 files changed +35
-47
lines changed Expand file tree Collapse file tree 5 files changed +35
-47
lines changed Original file line number Diff line number Diff line change @@ -18,9 +18,9 @@ import {
1818 traversePathsAsync
1919} from '../traversal.js' ;
2020import type { FormOptions , SuperForm , TaintOption } from './index.js' ;
21- import { errorShape , mapErrors } from '../errors.js' ;
21+ import { errorShape , mapErrors , clearErrors } from '../errors.js' ;
2222import type { FormPathType } from '../stringPath.js' ;
23- import { clearErrors , clone } from '../utils.js' ;
23+ import { clone } from '../utils.js' ;
2424import { get } from 'svelte/store' ;
2525
2626export type ValidateOptions < V > = Partial < {
Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ import {
2929 isInvalidPath
3030} from '../traversal.js' ;
3131import { fieldProxy } from './proxies.js' ;
32- import { clearErrors , clone } from '../utils.js' ;
32+ import { clone } from '../utils.js' ;
3333import {
3434 splitPath ,
3535 type FormPath ,
@@ -48,7 +48,7 @@ import {
4848 type SuperFormEvents ,
4949 type SuperFormEventList
5050} from './formEnhance.js' ;
51- import { flattenErrors } from '../errors.js' ;
51+ import { clearErrors , flattenErrors } from '../errors.js' ;
5252import { clientValidation , validateForm } from './clientValidation.js' ;
5353
5454export {
Original file line number Diff line number Diff line change @@ -9,6 +9,8 @@ import type {
99} from 'zod' ;
1010import { mergePath } from './stringPath.js' ;
1111import { unwrapZodType } from './schemaEntity.js' ;
12+ import type { Writable } from 'svelte/store' ;
13+ import { setPaths , traversePaths } from './traversal.js' ;
1214
1315/**
1416 * A tree structure where the existence of a node means that its not a leaf.
@@ -122,3 +124,31 @@ function _flattenErrors(
122124 }
123125 } ) ;
124126}
127+
128+ export function clearErrors < T extends AnyZodObject > (
129+ Errors : Writable < ValidationErrors < T > > ,
130+ options : {
131+ undefinePath : string [ ] | null ;
132+ clearFormLevelErrors : boolean ;
133+ }
134+ ) {
135+ Errors . update ( ( $errors ) => {
136+ traversePaths ( $errors , ( pathData ) => {
137+ if (
138+ pathData . path . length == 1 &&
139+ pathData . path [ 0 ] == '_errors' &&
140+ ! options . clearFormLevelErrors
141+ ) {
142+ return ;
143+ }
144+ if ( Array . isArray ( pathData . value ) ) {
145+ return pathData . set ( undefined ) ;
146+ }
147+ } ) ;
148+
149+ if ( options . undefinePath )
150+ setPaths ( $errors , [ options . undefinePath ] , undefined ) ;
151+
152+ return $errors ;
153+ } ) ;
154+ }
Original file line number Diff line number Diff line change @@ -55,16 +55,6 @@ type IntersectArray<T extends readonly unknown[]> = T extends [
5555type IntersectUnion < T extends ZodUnion < ZodUnionOptions > > =
5656 T extends ZodUnion < infer O > ? IntersectArray < O > : never ;
5757
58- /*
59- const schema = z
60- .object({ id: z.number() })
61- .or(z.object({ name: z.string() }));
62-
63- type U = typeof schema; //ZodUnion<[AnyZodObject, AnyZodObject]>
64-
65- type P = IntersectUnion<U>;
66- */
67-
6858type SuperStructArray < T extends AnyZodObject , Data , ArrayData = unknown > = {
6959 // eslint-disable-next-line @typescript-eslint/no-explicit-any
7060 [ Property in keyof RawShape < T > ] ?: T extends any
@@ -137,7 +127,7 @@ export type TaintedFields<T extends AnyZodObject> = SuperStructArray<
137127
138128export type ValidationErrors < T extends AnyZodObject > = {
139129 _errors ?: string [ ] ;
140- } & SuperStructArray < T , string [ ] , { _errors ?: string [ ] } > ;
130+ } & SuperStructArray < T , string [ ] , { _errors ?: string [ ] } > ;
141131
142132export type InputConstraint = Partial < {
143133 pattern : string ; // RegExp
Original file line number Diff line number Diff line change 11import { parse , stringify } from 'devalue' ;
2- import type { Writable } from 'svelte/store' ;
3- import type { AnyZodObject } from 'zod' ;
4- import type { ValidationErrors } from './index.js' ;
5- import { setPaths , traversePaths } from './traversal.js' ;
62
73export function clone < T > ( data : T ) : T {
84 if ( 'structuredClone' in globalThis ) {
95 return structuredClone ( data ) ;
106 }
117 return parse ( stringify ( data ) ) ;
128}
13-
14- export function clearErrors < T extends AnyZodObject > (
15- Errors : Writable < ValidationErrors < T > > ,
16- options : {
17- undefinePath : string [ ] | null ;
18- clearFormLevelErrors : boolean ;
19- }
20- ) {
21- Errors . update ( ( $errors ) => {
22- traversePaths ( $errors , ( pathData ) => {
23- if (
24- pathData . path . length == 1 &&
25- pathData . path [ 0 ] == '_errors' &&
26- ! options . clearFormLevelErrors
27- ) {
28- return ;
29- }
30- if ( Array . isArray ( pathData . value ) ) {
31- return pathData . set ( undefined ) ;
32- }
33- } ) ;
34-
35- if ( options . undefinePath )
36- setPaths ( $errors , [ options . undefinePath ] , undefined ) ;
37-
38- return $errors ;
39- } ) ;
40- }
You can’t perform that action at this time.
0 commit comments