@ptolemy2002/zod-utils 1.5.0 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/function.d.ts +2 -17
- package/dist/interpret.d.ts +3 -7
- package/dist/interpret.js +11 -2
- package/dist/issuePathStartsWith.d.ts +2 -1
- package/dist/prefixIssuePath.d.ts +2 -2
- package/dist/types.d.ts +25 -2
- package/dist/validate.d.ts +1 -1
- package/dist/validate.js +2 -2
- package/package.json +1 -1
package/dist/function.d.ts
CHANGED
|
@@ -1,21 +1,6 @@
|
|
|
1
|
-
import z, { ZodArray,
|
|
1
|
+
import z, { ZodArray, ZodUnknown } from "zod";
|
|
2
2
|
import { $ZodFunctionArgs, $ZodFunctionOut } from "zod/v4/core";
|
|
3
|
-
|
|
4
|
-
require: (e: unknown) => boolean;
|
|
5
|
-
};
|
|
6
|
-
export type FunctionTrial<Input extends unknown[]> = {
|
|
7
|
-
id?: string;
|
|
8
|
-
input: Input;
|
|
9
|
-
outputSchema?: ZodType;
|
|
10
|
-
error?: TrialErrorMode;
|
|
11
|
-
errorStringify?: (e: unknown) => string;
|
|
12
|
-
};
|
|
13
|
-
export type ZodFunctionParseOptions<In extends $ZodFunctionArgs, Out extends $ZodFunctionOut> = {
|
|
14
|
-
input?: In;
|
|
15
|
-
output?: Out;
|
|
16
|
-
inputPath?: PropertyKey | PropertyKey[];
|
|
17
|
-
outputPath?: PropertyKey | PropertyKey[];
|
|
18
|
-
};
|
|
3
|
+
import { ZodFunctionParseOptions, FunctionTrial } from "./types";
|
|
19
4
|
export type ZodFunctionSchemaOptions<In extends $ZodFunctionArgs, Out extends $ZodFunctionOut> = {
|
|
20
5
|
trials?: FunctionTrial<z.infer<In>>[];
|
|
21
6
|
} & ZodFunctionParseOptions<In, Out>;
|
package/dist/interpret.d.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import { InterpretableZodError, InterpretableZodIssue } from './types';
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
multiline?: boolean;
|
|
5
|
-
};
|
|
6
|
-
export declare function interpretZodError(err: InterpretableZodError, options?: PropertyKey | PropertyKey[] | InterpretZodErrorOptions): string;
|
|
7
|
-
export declare function interpretZodIssue(issue: InterpretableZodIssue, options?: PropertyKey | PropertyKey[] | InterpretZodErrorOptions): string;
|
|
1
|
+
import { InterpretableZodError, InterpretableZodIssue, ZodPath, InterpretZodErrorOptions } from './types';
|
|
2
|
+
export declare function interpretZodError(err: InterpretableZodError, options?: ZodPath | InterpretZodErrorOptions): string;
|
|
3
|
+
export declare function interpretZodIssue(issue: InterpretableZodIssue, options?: ZodPath | InterpretZodErrorOptions): string;
|
package/dist/interpret.js
CHANGED
|
@@ -10,9 +10,18 @@ const prefixIssuePath_1 = require("./prefixIssuePath");
|
|
|
10
10
|
function interpretZodError(err, options = "") {
|
|
11
11
|
if (typeof options !== "object" || Array.isArray(options))
|
|
12
12
|
options = { prefix: options };
|
|
13
|
-
const { multiline = true, prefix = "" } = options;
|
|
13
|
+
const { multiline = true, includeCode = false, prefix = "" } = options;
|
|
14
14
|
const modifiedErr = {
|
|
15
|
-
issues: err.issues.map(issue =>
|
|
15
|
+
issues: err.issues.map(issue => {
|
|
16
|
+
const modifiedIssue = (0, prefixIssuePath_1.prefixZodIssuePath)(issue, prefix);
|
|
17
|
+
if (includeCode && modifiedIssue.code) {
|
|
18
|
+
return {
|
|
19
|
+
...modifiedIssue,
|
|
20
|
+
message: `[${modifiedIssue.code}] ${modifiedIssue.message}`
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
return modifiedIssue;
|
|
24
|
+
})
|
|
16
25
|
};
|
|
17
26
|
let result = zod_1.default.prettifyError(modifiedErr);
|
|
18
27
|
if (!multiline) {
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { ZodPath } from "./types";
|
|
2
|
+
export declare function issuePathStartsWith(issuePath: PropertyKey[], prefix: ZodPath): boolean;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { InterpretableZodIssue } from "./types";
|
|
2
|
-
export declare function prefixZodIssuePath<I extends InterpretableZodIssue>(issue: I, prefix:
|
|
1
|
+
import { InterpretableZodIssue, ZodPath } from "./types";
|
|
2
|
+
export declare function prefixZodIssuePath<I extends InterpretableZodIssue>(issue: I, prefix: ZodPath): I;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ZodOptional, ZodSafeParseResult, ZodType } from 'zod';
|
|
2
|
+
import { $ZodFunctionArgs, $ZodFunctionOut } from 'zod/v4/core';
|
|
2
3
|
export type ZodSafeParseable<O> = {
|
|
3
4
|
safeParse: (data: unknown) => ZodSafeParseResult<O>;
|
|
4
5
|
};
|
|
@@ -13,15 +14,37 @@ export type ZodValidationResult<O> = {
|
|
|
13
14
|
error: string;
|
|
14
15
|
};
|
|
15
16
|
export type ZodValidatorWithErrors<O> = (v: unknown) => ZodValidationResult<O>;
|
|
17
|
+
export type InterpretZodErrorOptions = {
|
|
18
|
+
prefix?: ZodPath;
|
|
19
|
+
multiline?: boolean;
|
|
20
|
+
includeCode?: boolean;
|
|
21
|
+
};
|
|
16
22
|
export type ZodValidateWithErrorsOptions = {
|
|
17
23
|
_throw?: boolean;
|
|
18
|
-
|
|
19
|
-
};
|
|
24
|
+
} & InterpretZodErrorOptions;
|
|
20
25
|
export type MaybeZodOptional<ZT extends ZodType> = ZT | ZodOptional<ZT>;
|
|
26
|
+
export type ZodPath = PropertyKey | PropertyKey[];
|
|
21
27
|
export type InterpretableZodIssue = Readonly<{
|
|
28
|
+
code?: string;
|
|
22
29
|
message: string;
|
|
23
30
|
path?: PropertyKey[];
|
|
24
31
|
}>;
|
|
25
32
|
export type InterpretableZodError = Readonly<{
|
|
26
33
|
issues: InterpretableZodIssue[];
|
|
27
34
|
}>;
|
|
35
|
+
export type ZodFunctionParseOptions<In extends $ZodFunctionArgs, Out extends $ZodFunctionOut> = {
|
|
36
|
+
input?: In;
|
|
37
|
+
output?: Out;
|
|
38
|
+
inputPath?: ZodPath;
|
|
39
|
+
outputPath?: ZodPath;
|
|
40
|
+
};
|
|
41
|
+
export type TrialErrorMode = "allow" | "forbid" | "require" | ((e: unknown) => boolean) | {
|
|
42
|
+
require: (e: unknown) => boolean;
|
|
43
|
+
};
|
|
44
|
+
export type FunctionTrial<Input extends unknown[]> = {
|
|
45
|
+
id?: string;
|
|
46
|
+
input: Input;
|
|
47
|
+
outputSchema?: ZodType;
|
|
48
|
+
error?: TrialErrorMode;
|
|
49
|
+
errorStringify?: (e: unknown) => string;
|
|
50
|
+
};
|
package/dist/validate.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import * as t from './types';
|
|
2
2
|
export declare function zodValidate<O>(p: t.ZodSafeParseable<O>): t.ZodValidator<O>;
|
|
3
|
-
export declare function zodValidateWithErrors<O>(p: t.ZodSafeParseable<O>, { _throw,
|
|
3
|
+
export declare function zodValidateWithErrors<O>(p: t.ZodSafeParseable<O>, { _throw, ...options }?: t.ZodValidateWithErrorsOptions): t.ZodValidatorWithErrors<O>;
|
package/dist/validate.js
CHANGED
|
@@ -9,13 +9,13 @@ function zodValidate(p) {
|
|
|
9
9
|
return result.success;
|
|
10
10
|
};
|
|
11
11
|
}
|
|
12
|
-
function zodValidateWithErrors(p, { _throw = false,
|
|
12
|
+
function zodValidateWithErrors(p, { _throw = false, ...options } = {}) {
|
|
13
13
|
return (_v) => {
|
|
14
14
|
const { success, error, data: v } = p.safeParse(_v);
|
|
15
15
|
if (success)
|
|
16
16
|
return { success, value: v };
|
|
17
17
|
if (_throw)
|
|
18
18
|
throw error;
|
|
19
|
-
return { success, error: (0, interpret_1.interpretZodError)(error,
|
|
19
|
+
return { success, error: (0, interpret_1.interpretZodError)(error, options) };
|
|
20
20
|
};
|
|
21
21
|
}
|