zod-args-parser 1.0.16 → 1.1.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/README.md +1 -1
- package/lib/commonjs/metadata/get-arguments-metadata.js +1 -1
- package/lib/commonjs/metadata/get-arguments-metadata.js.map +1 -1
- package/lib/commonjs/metadata/get-options-metadata.js +1 -1
- package/lib/commonjs/metadata/get-options-metadata.js.map +1 -1
- package/lib/commonjs/parser/parse.js +1 -1
- package/lib/commonjs/parser/parse.js.map +1 -1
- package/lib/commonjs/utils.js +1 -1
- package/lib/commonjs/utils.js.map +1 -1
- package/lib/commonjs/zodUtils.js +1 -0
- package/lib/commonjs/zodUtils.js.map +1 -0
- package/lib/module/metadata/get-arguments-metadata.js +1 -1
- package/lib/module/metadata/get-arguments-metadata.js.map +1 -1
- package/lib/module/metadata/get-options-metadata.js +1 -1
- package/lib/module/metadata/get-options-metadata.js.map +1 -1
- package/lib/module/parser/parse.js +1 -1
- package/lib/module/parser/parse.js.map +1 -1
- package/lib/module/utils.js +1 -1
- package/lib/module/utils.js.map +1 -1
- package/lib/module/zodUtils.js +1 -0
- package/lib/module/zodUtils.js.map +1 -0
- package/lib/typescript/metadata/get-options-metadata.d.ts.map +1 -1
- package/lib/typescript/parser/parse.d.ts.map +1 -1
- package/lib/typescript/types.d.ts +17 -12
- package/lib/typescript/types.d.ts.map +1 -1
- package/lib/typescript/utils.d.ts +1 -5
- package/lib/typescript/utils.d.ts.map +1 -1
- package/lib/typescript/zodUtils.d.ts +14 -0
- package/lib/typescript/zodUtils.d.ts.map +1 -0
- package/package.json +17 -17
- package/src/metadata/get-arguments-metadata.ts +4 -4
- package/src/metadata/get-options-metadata.ts +5 -4
- package/src/parser/parse.ts +18 -16
- package/src/types.ts +17 -10
- package/src/utils.ts +1 -34
- package/src/zodUtils.ts +151 -0
package/src/zodUtils.ts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import * as Z3 from "zod/v3";
|
|
2
|
+
import * as Z4 from "zod/v4/core";
|
|
3
|
+
|
|
4
|
+
import type { Schema, SchemaV3, SchemaV4 } from "./types.js";
|
|
5
|
+
|
|
6
|
+
function isV4Schema(schema: Schema): schema is SchemaV4 {
|
|
7
|
+
return "_zod" in schema;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** - Safe parse a value against a schema */
|
|
11
|
+
export function safeParseSchema(schema: Schema, value: unknown) {
|
|
12
|
+
if (isV4Schema(schema)) {
|
|
13
|
+
return Z4.safeParse(schema, value);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return schema.safeParse(value);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** - Check if a schema is a boolean */
|
|
20
|
+
export function isBooleanSchema(schema: Schema): boolean {
|
|
21
|
+
if (isV4Schema(schema)) {
|
|
22
|
+
return isBooleanV4Schema(schema);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return isBooleanV3Schema(schema);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isBooleanV4Schema(schema: SchemaV4): boolean {
|
|
29
|
+
let def = schema._zod.def;
|
|
30
|
+
|
|
31
|
+
while (def) {
|
|
32
|
+
if (def.type === "boolean") {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (isLiteralV4Def(def)) {
|
|
37
|
+
return def.values.includes(true) || def.values.includes(false);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!isV4DefWithInnerType(def)) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
def = def.innerType._zod.def;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isBooleanV3Schema(schema: SchemaV3): boolean {
|
|
51
|
+
let type = schema;
|
|
52
|
+
while (type) {
|
|
53
|
+
if (type instanceof Z3.ZodBoolean) {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (type instanceof Z3.ZodLiteral) {
|
|
58
|
+
return type.value === true || type.value === false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
type = type._def.innerType;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** - Get the default value of a schema */
|
|
68
|
+
export function schemaDefaultValue(schema: Schema): unknown | undefined {
|
|
69
|
+
if (isV4Schema(schema)) {
|
|
70
|
+
return schemaV4DefaultValue(schema);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return schemaV3DefaultValue(schema);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function schemaV4DefaultValue(schema: SchemaV4): unknown | undefined {
|
|
77
|
+
let def = schema._zod.def;
|
|
78
|
+
|
|
79
|
+
while (def) {
|
|
80
|
+
if (isDefaultV4Def(def)) return def.defaultValue;
|
|
81
|
+
if (!isV4DefWithInnerType(def)) return undefined;
|
|
82
|
+
def = def.innerType._zod.def;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function schemaV3DefaultValue(schema: SchemaV3): unknown | undefined {
|
|
89
|
+
let type = schema;
|
|
90
|
+
while (type) {
|
|
91
|
+
if (type instanceof Z3.ZodDefault) {
|
|
92
|
+
const defaultValue = type._def.defaultValue();
|
|
93
|
+
return defaultValue;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
type = type._def.innerType;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** - Get the description of a schema */
|
|
103
|
+
export function schemaDescription(schema: Schema): string | undefined {
|
|
104
|
+
if (isV4Schema(schema)) {
|
|
105
|
+
if (!("meta" in schema) || typeof schema.meta !== "function") return;
|
|
106
|
+
return schema.meta()?.description;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return schema.description;
|
|
110
|
+
}
|
|
111
|
+
/** - Check if a schema is optional */
|
|
112
|
+
export function isOptionalSchema(schema: Schema): schema is Z4.$ZodOptional {
|
|
113
|
+
if (isV4Schema(schema)) {
|
|
114
|
+
return schema._zod.def.type === "optional" || schema._zod.def.type === "default";
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return schema.isOptional();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function isDefaultV4Def(def: Z4.$ZodTypeDef): def is Z4.$ZodDefaultDef {
|
|
121
|
+
return def.type === "default";
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function isLiteralV4Def(def: Z4.$ZodTypeDef): def is Z4.$ZodLiteralDef<any> {
|
|
125
|
+
return def.type === "literal";
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
type SchemaWithInnerType =
|
|
129
|
+
| Z4.$ZodDefaultDef
|
|
130
|
+
| Z4.$ZodPrefaultDef
|
|
131
|
+
| Z4.$ZodOptionalDef
|
|
132
|
+
| Z4.$ZodNonOptionalDef
|
|
133
|
+
| Z4.$ZodNullableDef
|
|
134
|
+
| Z4.$ZodSuccessDef
|
|
135
|
+
| Z4.$ZodCatchDef
|
|
136
|
+
| Z4.$ZodReadonlyDef
|
|
137
|
+
| Z4.$ZodPromiseDef;
|
|
138
|
+
|
|
139
|
+
function isV4DefWithInnerType(def: Z4.$ZodTypeDef): def is SchemaWithInnerType {
|
|
140
|
+
return new Set([
|
|
141
|
+
"default",
|
|
142
|
+
"prefault",
|
|
143
|
+
"optional",
|
|
144
|
+
"nonoptional",
|
|
145
|
+
"nullable",
|
|
146
|
+
"success",
|
|
147
|
+
"catch",
|
|
148
|
+
"readonly",
|
|
149
|
+
"promise",
|
|
150
|
+
]).has(def.type);
|
|
151
|
+
}
|