@optique/zod 0.10.7 → 1.0.0-dev.1116
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 +22 -8
- package/dist/index.cjs +10 -2
- package/dist/index.js +10 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
@optique/zod
|
|
2
2
|
============
|
|
3
3
|
|
|
4
|
-
> [!WARNING]
|
|
5
|
-
> The API is stabilizing, but may change before the 1.0 release.
|
|
6
|
-
|
|
7
4
|
Zod value parsers for Optique. This package provides seamless integration
|
|
8
5
|
between [Zod] schemas and *@optique/core*, enabling powerful validation and
|
|
9
6
|
type-safe parsing of command-line arguments.
|
|
@@ -33,13 +30,16 @@ address.
|
|
|
33
30
|
|
|
34
31
|
~~~~ typescript
|
|
35
32
|
import { run } from "@optique/run";
|
|
36
|
-
import {
|
|
33
|
+
import { object } from "@optique/core/constructs";
|
|
34
|
+
import { option } from "@optique/core/primitives";
|
|
37
35
|
import { zod } from "@optique/zod";
|
|
38
36
|
import { z } from "zod";
|
|
39
37
|
|
|
40
|
-
const cli = run(
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
const cli = run(
|
|
39
|
+
object({
|
|
40
|
+
email: option("--email", zod(z.string().email())),
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
43
|
|
|
44
44
|
console.log(`Welcome, ${cli.email}!`);
|
|
45
45
|
~~~~
|
|
@@ -61,6 +61,7 @@ Common use cases
|
|
|
61
61
|
### Email validation
|
|
62
62
|
|
|
63
63
|
~~~~ typescript
|
|
64
|
+
import { option } from "@optique/core/primitives";
|
|
64
65
|
import { zod } from "@optique/zod";
|
|
65
66
|
import { z } from "zod";
|
|
66
67
|
|
|
@@ -70,6 +71,7 @@ const email = option("--email", zod(z.string().email()));
|
|
|
70
71
|
### URL validation
|
|
71
72
|
|
|
72
73
|
~~~~ typescript
|
|
74
|
+
import { option } from "@optique/core/primitives";
|
|
73
75
|
import { zod } from "@optique/zod";
|
|
74
76
|
import { z } from "zod";
|
|
75
77
|
|
|
@@ -83,6 +85,7 @@ const url = option("--url", zod(z.string().url()));
|
|
|
83
85
|
> always strings.
|
|
84
86
|
|
|
85
87
|
~~~~ typescript
|
|
88
|
+
import { option } from "@optique/core/primitives";
|
|
86
89
|
import { zod } from "@optique/zod";
|
|
87
90
|
import { z } from "zod";
|
|
88
91
|
|
|
@@ -94,6 +97,7 @@ const port = option("-p", "--port",
|
|
|
94
97
|
### Enum choices
|
|
95
98
|
|
|
96
99
|
~~~~ typescript
|
|
100
|
+
import { option } from "@optique/core/primitives";
|
|
97
101
|
import { zod } from "@optique/zod";
|
|
98
102
|
import { z } from "zod";
|
|
99
103
|
|
|
@@ -105,6 +109,7 @@ const logLevel = option("--log-level",
|
|
|
105
109
|
### Date transformations
|
|
106
110
|
|
|
107
111
|
~~~~ typescript
|
|
112
|
+
import { argument } from "@optique/core/primitives";
|
|
108
113
|
import { zod } from "@optique/zod";
|
|
109
114
|
import { z } from "zod";
|
|
110
115
|
|
|
@@ -120,6 +125,7 @@ Custom error messages
|
|
|
120
125
|
You can customize error messages using the `errors` option:
|
|
121
126
|
|
|
122
127
|
~~~~ typescript
|
|
128
|
+
import { option } from "@optique/core/primitives";
|
|
123
129
|
import { zod } from "@optique/zod";
|
|
124
130
|
import { message } from "@optique/core/message";
|
|
125
131
|
import { z } from "zod";
|
|
@@ -143,11 +149,15 @@ CLI arguments are always strings. If you want to parse numbers, booleans,
|
|
|
143
149
|
or other types, you must use `z.coerce`:
|
|
144
150
|
|
|
145
151
|
~~~~ typescript
|
|
152
|
+
import { option } from "@optique/core/primitives";
|
|
153
|
+
import { zod } from "@optique/zod";
|
|
154
|
+
import { z } from "zod";
|
|
155
|
+
|
|
146
156
|
// ✅ Correct
|
|
147
157
|
const port = option("-p", zod(z.coerce.number()));
|
|
148
158
|
|
|
149
159
|
// ❌ Won't work (CLI arguments are always strings)
|
|
150
|
-
const port = option("-p", zod(z.number()));
|
|
160
|
+
// const port = option("-p", zod(z.number()));
|
|
151
161
|
~~~~
|
|
152
162
|
|
|
153
163
|
### Async refinements are not supported
|
|
@@ -156,6 +166,10 @@ Optique's `ValueParser.parse()` is synchronous, so async Zod features like
|
|
|
156
166
|
async refinements cannot be supported:
|
|
157
167
|
|
|
158
168
|
~~~~ typescript
|
|
169
|
+
import { option } from "@optique/core/primitives";
|
|
170
|
+
import { zod } from "@optique/zod";
|
|
171
|
+
import { z } from "zod";
|
|
172
|
+
|
|
159
173
|
// ❌ Not supported
|
|
160
174
|
const email = option("--email",
|
|
161
175
|
zod(z.string().refine(async (val) => await checkDB(val)))
|
package/dist/index.cjs
CHANGED
|
@@ -78,8 +78,16 @@ function inferMetavar(schema) {
|
|
|
78
78
|
if (typeName === "ZodDate" || typeName === "date") return "DATE";
|
|
79
79
|
if (typeName === "ZodEnum" || typeName === "enum" || typeName === "ZodNativeEnum" || typeName === "nativeEnum") return "CHOICE";
|
|
80
80
|
if (typeName === "ZodUnion" || typeName === "union" || typeName === "ZodLiteral" || typeName === "literal") return "VALUE";
|
|
81
|
-
if (typeName === "ZodOptional" || typeName === "optional" || typeName === "ZodNullable" || typeName === "nullable")
|
|
82
|
-
|
|
81
|
+
if (typeName === "ZodOptional" || typeName === "optional" || typeName === "ZodNullable" || typeName === "nullable") {
|
|
82
|
+
const innerType = def.innerType;
|
|
83
|
+
if (innerType != null) return inferMetavar(innerType);
|
|
84
|
+
return "VALUE";
|
|
85
|
+
}
|
|
86
|
+
if (typeName === "ZodDefault" || typeName === "default") {
|
|
87
|
+
const innerType = def.innerType;
|
|
88
|
+
if (innerType != null) return inferMetavar(innerType);
|
|
89
|
+
return "VALUE";
|
|
90
|
+
}
|
|
83
91
|
return "VALUE";
|
|
84
92
|
}
|
|
85
93
|
/**
|
package/dist/index.js
CHANGED
|
@@ -55,8 +55,16 @@ function inferMetavar(schema) {
|
|
|
55
55
|
if (typeName === "ZodDate" || typeName === "date") return "DATE";
|
|
56
56
|
if (typeName === "ZodEnum" || typeName === "enum" || typeName === "ZodNativeEnum" || typeName === "nativeEnum") return "CHOICE";
|
|
57
57
|
if (typeName === "ZodUnion" || typeName === "union" || typeName === "ZodLiteral" || typeName === "literal") return "VALUE";
|
|
58
|
-
if (typeName === "ZodOptional" || typeName === "optional" || typeName === "ZodNullable" || typeName === "nullable")
|
|
59
|
-
|
|
58
|
+
if (typeName === "ZodOptional" || typeName === "optional" || typeName === "ZodNullable" || typeName === "nullable") {
|
|
59
|
+
const innerType = def.innerType;
|
|
60
|
+
if (innerType != null) return inferMetavar(innerType);
|
|
61
|
+
return "VALUE";
|
|
62
|
+
}
|
|
63
|
+
if (typeName === "ZodDefault" || typeName === "default") {
|
|
64
|
+
const innerType = def.innerType;
|
|
65
|
+
if (innerType != null) return inferMetavar(innerType);
|
|
66
|
+
return "VALUE";
|
|
67
|
+
}
|
|
60
68
|
return "VALUE";
|
|
61
69
|
}
|
|
62
70
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@optique/zod",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-dev.1116+6084dd3a",
|
|
4
4
|
"description": "Zod value parsers for Optique",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"CLI",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"zod": "^3.25.0 || ^4.0.0"
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
|
-
"@optique/core": "0.
|
|
60
|
+
"@optique/core": "1.0.0-dev.1116+6084dd3a"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
63
|
"@types/node": "^20.19.9",
|