Skip to content

Commit 7da39a4

Browse files
committed
Factorized errors function.
1 parent 8e7f253 commit 7da39a4

File tree

5 files changed

+35
-47
lines changed

5 files changed

+35
-47
lines changed

src/lib/client/clientValidation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import {
1818
traversePathsAsync
1919
} from '../traversal.js';
2020
import type { FormOptions, SuperForm, TaintOption } from './index.js';
21-
import { errorShape, mapErrors } from '../errors.js';
21+
import { errorShape, mapErrors, clearErrors } from '../errors.js';
2222
import type { FormPathType } from '../stringPath.js';
23-
import { clearErrors, clone } from '../utils.js';
23+
import { clone } from '../utils.js';
2424
import { get } from 'svelte/store';
2525

2626
export type ValidateOptions<V> = Partial<{

src/lib/client/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
isInvalidPath
3030
} from '../traversal.js';
3131
import { fieldProxy } from './proxies.js';
32-
import { clearErrors, clone } from '../utils.js';
32+
import { clone } from '../utils.js';
3333
import {
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';
5252
import { clientValidation, validateForm } from './clientValidation.js';
5353

5454
export {

src/lib/errors.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import type {
99
} from 'zod';
1010
import { mergePath } from './stringPath.js';
1111
import { 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+
}

src/lib/index.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,6 @@ type IntersectArray<T extends readonly unknown[]> = T extends [
5555
type 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-
6858
type 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

138128
export type ValidationErrors<T extends AnyZodObject> = {
139129
_errors?: string[];
140-
} & SuperStructArray<T, string[], {_errors?: string[]}>;
130+
} & SuperStructArray<T, string[], { _errors?: string[] }>;
141131

142132
export type InputConstraint = Partial<{
143133
pattern: string; // RegExp

src/lib/utils.ts

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,8 @@
11
import { 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

73
export 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-
}

0 commit comments

Comments
 (0)