@moluoxixi/zod3-to-rule 0.1.3 → 0.2.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 +24 -27
- package/dist/index.js +129 -142
- package/dist/src/constants.d.ts +1 -1
- package/dist/src/parse.d.ts.map +1 -1
- package/dist/src/rules.d.ts.map +1 -1
- package/dist/src/to-zod.d.ts.map +1 -1
- package/dist/src/types.d.ts +0 -5
- package/dist/src/types.d.ts.map +1 -1
- package/index.ts +7 -7
- package/package.json +1 -1
- package/src/constants.ts +1 -1
- package/src/diagnostics.ts +27 -27
- package/src/from-zod.ts +213 -213
- package/src/parse.ts +81 -82
- package/src/rules.ts +49 -65
- package/src/to-zod.ts +125 -142
- package/src/types.ts +99 -102
package/README.md
CHANGED
|
@@ -1,27 +1,24 @@
|
|
|
1
|
-
# Zod 3 to Rule
|
|
2
|
-
|
|
3
|
-
`@moluoxixi/zod3-to-rule` 提供可序列化校验规则与 Zod 3 之间的桥接,不依赖 Vue 或任何表单 UI。
|
|
4
|
-
|
|
5
|
-
规则 JSON 是稳定协议。外部输入先通过 `parseRuleSet()` 严格解析,再使用 `compileRules()` 编译为 Zod schema
|
|
6
|
-
|
|
7
|
-
```ts
|
|
8
|
-
import { compileRules, parseRuleSet } from '@moluoxixi/zod3-to-rule'
|
|
9
|
-
|
|
10
|
-
const parsed = parseRuleSet({
|
|
11
|
-
version:
|
|
12
|
-
base: { type: 'string' },
|
|
13
|
-
rules: [
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
支持的基础类型包括 string、number、boolean、date、enum 和 JSON primitive literal。内置规则覆盖字符串长度、regex、email、URL、UUID、数值范围、integer、finite、multipleOf、日期范围、required、基础跨字段比较和命名 custom validator。
|
|
26
|
-
|
|
27
|
-
`zodToRules()` 是受限的最佳努力导出器。它支持上述基础类型和 Zod 3 内置 checks,并返回结构化 diagnostics。`refine`、`superRefine`、transform、preprocess、coerce、default 和其他无法由规则 JSON 等价表达的行为不会被静默丢弃;存在不可恢复的 effects 或 coercion 时不会返回 `ruleSet`。
|
|
1
|
+
# Zod 3 to Rule
|
|
2
|
+
|
|
3
|
+
`@moluoxixi/zod3-to-rule` 提供可序列化校验规则与 Zod 3 之间的桥接,不依赖 Vue 或任何表单 UI。
|
|
4
|
+
|
|
5
|
+
规则 JSON 是稳定协议。外部输入先通过 `parseRuleSet()` 严格解析,再使用 `compileRules()` 编译为 Zod schema 和跨字段/custom validator。`rulesToZod()` 只编译单字段 Zod 能力。字段是否必填由 ConfigForm 字段合同独立表达,不属于通用 RuleSet。
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { compileRules, parseRuleSet } from '@moluoxixi/zod3-to-rule'
|
|
9
|
+
|
|
10
|
+
const parsed = parseRuleSet({
|
|
11
|
+
version: 2,
|
|
12
|
+
base: { type: 'string' },
|
|
13
|
+
rules: [{ kind: 'email', message: '邮箱格式不正确' }],
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
if (parsed.success) {
|
|
17
|
+
const compiled = compileRules(parsed.data)
|
|
18
|
+
compiled.schema.safeParse('user@example.com')
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
支持的基础类型包括 string、number、boolean、date、enum 和 JSON primitive literal。内置规则覆盖字符串长度、regex、email、URL、UUID、数值范围、integer、finite、multipleOf、日期范围、基础跨字段比较和命名 custom validator。
|
|
23
|
+
|
|
24
|
+
`zodToRules()` 是受限的最佳努力导出器。它支持上述基础类型和 Zod 3 内置 checks,并返回结构化 diagnostics。`refine`、`superRefine`、transform、preprocess、coerce、default 和其他无法由规则 JSON 等价表达的行为不会被静默丢弃;存在不可恢复的 effects 或 coercion 时不会返回 `ruleSet`。
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { z as t } from "zod";
|
|
2
|
-
const
|
|
3
|
-
function o(n, e, r = [],
|
|
4
|
-
return { code: n, severity:
|
|
2
|
+
const w = 2;
|
|
3
|
+
function o(n, e, r = [], a = "error", i) {
|
|
4
|
+
return { code: n, severity: a, path: r, message: e, ...i === void 0 ? {} : { ruleIndex: i } };
|
|
5
5
|
}
|
|
6
|
-
function
|
|
6
|
+
function T(n) {
|
|
7
7
|
return n.map((e) => o(
|
|
8
8
|
"RULE_DOCUMENT_INVALID",
|
|
9
9
|
e.message,
|
|
@@ -15,18 +15,18 @@ class l extends Error {
|
|
|
15
15
|
super(e.map((r) => r.message).join("; ")), this.diagnostics = e, this.name = "RuleCompileError";
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
|
-
function
|
|
18
|
+
function y(n) {
|
|
19
19
|
return typeof n.message == "string" ? n.message : void 0;
|
|
20
20
|
}
|
|
21
|
-
function
|
|
22
|
-
const r =
|
|
21
|
+
function g(n, e) {
|
|
22
|
+
const r = y(e);
|
|
23
23
|
return r ? { ...n, message: r } : n;
|
|
24
24
|
}
|
|
25
25
|
function p(n, e = []) {
|
|
26
26
|
return o("ZOD_UNSUPPORTED", n, e, "warning");
|
|
27
27
|
}
|
|
28
|
-
function
|
|
29
|
-
let e = n, r = !1,
|
|
28
|
+
function M(n) {
|
|
29
|
+
let e = n, r = !1, a = !1;
|
|
30
30
|
const i = [];
|
|
31
31
|
for (; ; ) {
|
|
32
32
|
if (e instanceof t.ZodOptional) {
|
|
@@ -34,7 +34,7 @@ function Z(n) {
|
|
|
34
34
|
continue;
|
|
35
35
|
}
|
|
36
36
|
if (e instanceof t.ZodNullable) {
|
|
37
|
-
|
|
37
|
+
a = !0, e = e.unwrap();
|
|
38
38
|
continue;
|
|
39
39
|
}
|
|
40
40
|
if (e instanceof t.ZodDefault) {
|
|
@@ -42,119 +42,119 @@ function Z(n) {
|
|
|
42
42
|
continue;
|
|
43
43
|
}
|
|
44
44
|
if (e instanceof t.ZodEffects)
|
|
45
|
-
return i.push(p("Zod effects/refine/transform cannot be represented as rules")), { schema: e, optional: r, nullable:
|
|
45
|
+
return i.push(p("Zod effects/refine/transform cannot be represented as rules")), { schema: e, optional: r, nullable: a, unsupported: !0, diagnostics: i };
|
|
46
46
|
break;
|
|
47
47
|
}
|
|
48
|
-
return { schema: e, optional: r, nullable:
|
|
48
|
+
return { schema: e, optional: r, nullable: a, unsupported: !1, diagnostics: i };
|
|
49
49
|
}
|
|
50
50
|
function S(n) {
|
|
51
|
-
const e = [], r = [],
|
|
52
|
-
for (const i of
|
|
53
|
-
const
|
|
54
|
-
switch (
|
|
51
|
+
const e = [], r = [], a = n._def.checks;
|
|
52
|
+
for (const i of a) {
|
|
53
|
+
const s = i.kind;
|
|
54
|
+
switch (s) {
|
|
55
55
|
case "min":
|
|
56
|
-
e.push(
|
|
56
|
+
e.push(g({ kind: "minLength", value: i.value }, i));
|
|
57
57
|
break;
|
|
58
58
|
case "max":
|
|
59
|
-
e.push(
|
|
59
|
+
e.push(g({ kind: "maxLength", value: i.value }, i));
|
|
60
60
|
break;
|
|
61
61
|
case "length":
|
|
62
|
-
e.push(
|
|
62
|
+
e.push(g({ kind: "length", value: i.value }, i));
|
|
63
63
|
break;
|
|
64
64
|
case "email":
|
|
65
|
-
e.push(
|
|
65
|
+
e.push(g({ kind: "email" }, i));
|
|
66
66
|
break;
|
|
67
67
|
case "url":
|
|
68
|
-
e.push(
|
|
68
|
+
e.push(g({ kind: "url" }, i));
|
|
69
69
|
break;
|
|
70
70
|
case "uuid":
|
|
71
|
-
e.push(
|
|
71
|
+
e.push(g({ kind: "uuid" }, i));
|
|
72
72
|
break;
|
|
73
73
|
case "regex": {
|
|
74
74
|
const f = i.regex;
|
|
75
|
-
f instanceof RegExp ? e.push(
|
|
75
|
+
f instanceof RegExp ? e.push(g({ kind: "regex", source: f.source, flags: f.flags }, i)) : r.push(p("Zod regex check does not contain a RegExp"));
|
|
76
76
|
break;
|
|
77
77
|
}
|
|
78
78
|
default:
|
|
79
|
-
r.push(p(`Zod string check is not supported: ${String(
|
|
79
|
+
r.push(p(`Zod string check is not supported: ${String(s)}`));
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
return { rules: e, diagnostics: r };
|
|
83
83
|
}
|
|
84
|
-
function
|
|
85
|
-
const e = [], r = [],
|
|
86
|
-
for (const i of
|
|
87
|
-
const
|
|
88
|
-
switch (
|
|
84
|
+
function Z(n) {
|
|
85
|
+
const e = [], r = [], a = n._def.checks;
|
|
86
|
+
for (const i of a) {
|
|
87
|
+
const s = i.kind;
|
|
88
|
+
switch (s) {
|
|
89
89
|
case "min":
|
|
90
|
-
e.push(
|
|
90
|
+
e.push(g({ kind: "min", value: i.value, inclusive: i.inclusive }, i));
|
|
91
91
|
break;
|
|
92
92
|
case "max":
|
|
93
|
-
e.push(
|
|
93
|
+
e.push(g({ kind: "max", value: i.value, inclusive: i.inclusive }, i));
|
|
94
94
|
break;
|
|
95
95
|
case "int":
|
|
96
|
-
e.push(
|
|
96
|
+
e.push(g({ kind: "integer" }, i));
|
|
97
97
|
break;
|
|
98
98
|
case "finite":
|
|
99
|
-
e.push(
|
|
99
|
+
e.push(g({ kind: "finite" }, i));
|
|
100
100
|
break;
|
|
101
101
|
case "multipleOf":
|
|
102
|
-
e.push(
|
|
102
|
+
e.push(g({ kind: "multipleOf", value: i.value }, i));
|
|
103
103
|
break;
|
|
104
104
|
default:
|
|
105
|
-
r.push(p(`Zod number check is not supported: ${String(
|
|
105
|
+
r.push(p(`Zod number check is not supported: ${String(s)}`));
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
return { rules: e, diagnostics: r };
|
|
109
109
|
}
|
|
110
|
-
function
|
|
111
|
-
const e = [], r = [],
|
|
112
|
-
for (const i of
|
|
113
|
-
const
|
|
114
|
-
if (Number.isNaN(
|
|
110
|
+
function R(n) {
|
|
111
|
+
const e = [], r = [], a = n._def.checks;
|
|
112
|
+
for (const i of a) {
|
|
113
|
+
const s = new Date(i.value);
|
|
114
|
+
if (Number.isNaN(s.getTime())) {
|
|
115
115
|
r.push(p("Zod date check does not contain a valid date value"));
|
|
116
116
|
continue;
|
|
117
117
|
}
|
|
118
|
-
i.kind === "min" ? e.push(
|
|
118
|
+
i.kind === "min" ? e.push(g({ kind: "dateMin", value: s.toISOString() }, i)) : e.push(g({ kind: "dateMax", value: s.toISOString() }, i));
|
|
119
119
|
}
|
|
120
120
|
return { rules: e, diagnostics: r };
|
|
121
121
|
}
|
|
122
|
-
function
|
|
123
|
-
const e =
|
|
122
|
+
function H(n) {
|
|
123
|
+
const e = M(n), r = [...e.diagnostics];
|
|
124
124
|
if (e.unsupported)
|
|
125
125
|
return { diagnostics: r };
|
|
126
126
|
if ((e.schema instanceof t.ZodString || e.schema instanceof t.ZodNumber || e.schema instanceof t.ZodBoolean || e.schema instanceof t.ZodDate) && e.schema._def.coerce)
|
|
127
127
|
return r.push(p("Zod coercion cannot be represented as rules")), { diagnostics: r };
|
|
128
|
-
let
|
|
128
|
+
let a, i = [];
|
|
129
129
|
if (e.schema instanceof t.ZodString) {
|
|
130
|
-
|
|
131
|
-
const
|
|
132
|
-
i =
|
|
130
|
+
a = { type: "string" };
|
|
131
|
+
const s = S(e.schema);
|
|
132
|
+
i = s.rules, r.push(...s.diagnostics);
|
|
133
133
|
} else if (e.schema instanceof t.ZodNumber) {
|
|
134
|
-
|
|
135
|
-
const
|
|
136
|
-
i =
|
|
134
|
+
a = { type: "number" };
|
|
135
|
+
const s = Z(e.schema);
|
|
136
|
+
i = s.rules, r.push(...s.diagnostics);
|
|
137
137
|
} else if (e.schema instanceof t.ZodBoolean)
|
|
138
|
-
|
|
138
|
+
a = { type: "boolean" };
|
|
139
139
|
else if (e.schema instanceof t.ZodDate) {
|
|
140
|
-
|
|
141
|
-
const
|
|
142
|
-
i =
|
|
140
|
+
a = { type: "date" };
|
|
141
|
+
const s = R(e.schema);
|
|
142
|
+
i = s.rules, r.push(...s.diagnostics);
|
|
143
143
|
} else if (e.schema instanceof t.ZodEnum)
|
|
144
|
-
|
|
144
|
+
a = { type: "enum", values: e.schema.options };
|
|
145
145
|
else if (e.schema instanceof t.ZodLiteral) {
|
|
146
|
-
const
|
|
147
|
-
if (typeof
|
|
146
|
+
const s = e.schema.value;
|
|
147
|
+
if (typeof s != "string" && typeof s != "number" && typeof s != "boolean" && s !== null)
|
|
148
148
|
return r.push(p("Zod literal value is not JSON-safe")), { diagnostics: r };
|
|
149
|
-
|
|
149
|
+
a = { type: "literal", value: s };
|
|
150
150
|
} else {
|
|
151
|
-
const
|
|
152
|
-
return r.push(p(`Zod schema type is not supported: ${String(
|
|
151
|
+
const s = e.schema._def;
|
|
152
|
+
return r.push(p(`Zod schema type is not supported: ${String(s.typeName ?? "unknown")}`)), { diagnostics: r };
|
|
153
153
|
}
|
|
154
154
|
return {
|
|
155
155
|
ruleSet: {
|
|
156
|
-
version:
|
|
157
|
-
base:
|
|
156
|
+
version: w,
|
|
157
|
+
base: a,
|
|
158
158
|
rules: i,
|
|
159
159
|
...e.optional ? { optional: !0 } : {},
|
|
160
160
|
...e.nullable ? { nullable: !0 } : {}
|
|
@@ -162,19 +162,18 @@ function x(n) {
|
|
|
162
162
|
diagnostics: r
|
|
163
163
|
};
|
|
164
164
|
}
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
t.array(
|
|
168
|
-
t.record(t.string(),
|
|
169
|
-
])), u = t.object({ message: t.string().min(1).optional() }).strict(),
|
|
165
|
+
const E = t.union([t.string(), t.number().finite(), t.boolean(), t.null()]), h = t.lazy(() => t.union([
|
|
166
|
+
E,
|
|
167
|
+
t.array(h),
|
|
168
|
+
t.record(t.string(), h)
|
|
169
|
+
])), u = t.object({ message: t.string().min(1).optional() }).strict(), L = t.discriminatedUnion("type", [
|
|
170
170
|
t.object({ type: t.literal("string") }).strict(),
|
|
171
171
|
t.object({ type: t.literal("number") }).strict(),
|
|
172
172
|
t.object({ type: t.literal("boolean") }).strict(),
|
|
173
173
|
t.object({ type: t.literal("date") }).strict(),
|
|
174
174
|
t.object({ type: t.literal("enum"), values: t.array(t.string()).min(1) }).strict(),
|
|
175
|
-
t.object({ type: t.literal("literal"), value:
|
|
176
|
-
]),
|
|
177
|
-
t.object({ kind: t.literal("required") }).merge(u),
|
|
175
|
+
t.object({ type: t.literal("literal"), value: E }).strict()
|
|
176
|
+
]), N = t.discriminatedUnion("kind", [
|
|
178
177
|
t.object({ kind: t.literal("minLength"), value: t.number().int().nonnegative() }).merge(u),
|
|
179
178
|
t.object({ kind: t.literal("maxLength"), value: t.number().int().nonnegative() }).merge(u),
|
|
180
179
|
t.object({ kind: t.literal("length"), value: t.number().int().nonnegative() }).merge(u),
|
|
@@ -194,7 +193,7 @@ const T = t.union([t.string(), t.number().finite(), t.boolean(), t.null()]), E =
|
|
|
194
193
|
field: t.string().min(1),
|
|
195
194
|
operator: t.enum(["eq", "neq", "gt", "gte", "lt", "lte"])
|
|
196
195
|
}).merge(u),
|
|
197
|
-
t.object({ kind: t.literal("custom"), key: t.string().min(1), params:
|
|
196
|
+
t.object({ kind: t.literal("custom"), key: t.string().min(1), params: h.optional() }).merge(u)
|
|
198
197
|
]).superRefine((n, e) => {
|
|
199
198
|
if (n.kind === "regex")
|
|
200
199
|
try {
|
|
@@ -206,25 +205,25 @@ const T = t.union([t.string(), t.number().finite(), t.boolean(), t.null()]), E =
|
|
|
206
205
|
path: ["source"]
|
|
207
206
|
});
|
|
208
207
|
}
|
|
209
|
-
}),
|
|
210
|
-
version: t.literal(
|
|
211
|
-
base:
|
|
212
|
-
rules: t.array(
|
|
208
|
+
}), j = t.object({
|
|
209
|
+
version: t.literal(w),
|
|
210
|
+
base: L,
|
|
211
|
+
rules: t.array(N),
|
|
213
212
|
optional: t.boolean().optional(),
|
|
214
213
|
nullable: t.boolean().optional()
|
|
215
214
|
}).strict();
|
|
216
|
-
function
|
|
217
|
-
const e =
|
|
215
|
+
function Y(n) {
|
|
216
|
+
const e = j.safeParse(n);
|
|
218
217
|
return e.success ? {
|
|
219
218
|
success: !0,
|
|
220
219
|
data: e.data,
|
|
221
220
|
diagnostics: []
|
|
222
|
-
} : { success: !1, diagnostics:
|
|
221
|
+
} : { success: !1, diagnostics: T(e.error.issues) };
|
|
223
222
|
}
|
|
224
223
|
function c(n) {
|
|
225
224
|
return n.message ? { message: n.message } : {};
|
|
226
225
|
}
|
|
227
|
-
function
|
|
226
|
+
function U(n) {
|
|
228
227
|
switch (n.base.type) {
|
|
229
228
|
case "string":
|
|
230
229
|
return t.string();
|
|
@@ -240,7 +239,7 @@ function A(n) {
|
|
|
240
239
|
return t.literal(n.base.value);
|
|
241
240
|
}
|
|
242
241
|
}
|
|
243
|
-
function
|
|
242
|
+
function A(n, e) {
|
|
244
243
|
try {
|
|
245
244
|
return new RegExp(n.source, n.flags);
|
|
246
245
|
} catch (r) {
|
|
@@ -253,10 +252,10 @@ function C(n, e) {
|
|
|
253
252
|
)]);
|
|
254
253
|
}
|
|
255
254
|
}
|
|
256
|
-
function
|
|
257
|
-
const
|
|
258
|
-
if (!Number.isNaN(
|
|
259
|
-
return
|
|
255
|
+
function k(n, e, r) {
|
|
256
|
+
const a = new Date(n);
|
|
257
|
+
if (!Number.isNaN(a.getTime()))
|
|
258
|
+
return a;
|
|
260
259
|
throw new l([o(
|
|
261
260
|
"RULE_DATE_INVALID",
|
|
262
261
|
`${r} requires a valid ISO date`,
|
|
@@ -267,8 +266,6 @@ function w(n, e, r) {
|
|
|
267
266
|
}
|
|
268
267
|
function D(n, e, r) {
|
|
269
268
|
switch (e.kind) {
|
|
270
|
-
case "required":
|
|
271
|
-
return n;
|
|
272
269
|
case "minLength":
|
|
273
270
|
if (!(n instanceof t.ZodString))
|
|
274
271
|
throw new l([o("RULE_TYPE_MISMATCH", "minLength requires a string base", ["rules", r], "error", r)]);
|
|
@@ -284,7 +281,7 @@ function D(n, e, r) {
|
|
|
284
281
|
case "regex":
|
|
285
282
|
if (!(n instanceof t.ZodString))
|
|
286
283
|
throw new l([o("RULE_TYPE_MISMATCH", "regex requires a string base", ["rules", r], "error", r)]);
|
|
287
|
-
return n.regex(
|
|
284
|
+
return n.regex(A(e, r), c(e));
|
|
288
285
|
case "email":
|
|
289
286
|
if (!(n instanceof t.ZodString))
|
|
290
287
|
throw new l([o("RULE_TYPE_MISMATCH", "email requires a string base", ["rules", r], "error", r)]);
|
|
@@ -320,102 +317,92 @@ function D(n, e, r) {
|
|
|
320
317
|
case "dateMin":
|
|
321
318
|
if (!(n instanceof t.ZodDate))
|
|
322
319
|
throw new l([o("RULE_TYPE_MISMATCH", "dateMin requires a date base", ["rules", r], "error", r)]);
|
|
323
|
-
return n.min(
|
|
320
|
+
return n.min(k(e.value, r, e.kind), c(e));
|
|
324
321
|
case "dateMax":
|
|
325
322
|
if (!(n instanceof t.ZodDate))
|
|
326
323
|
throw new l([o("RULE_TYPE_MISMATCH", "dateMax requires a date base", ["rules", r], "error", r)]);
|
|
327
|
-
return n.max(
|
|
324
|
+
return n.max(k(e.value, r, e.kind), c(e));
|
|
328
325
|
case "compare":
|
|
329
326
|
case "custom":
|
|
330
327
|
return n;
|
|
331
328
|
}
|
|
332
329
|
}
|
|
333
|
-
function
|
|
334
|
-
let e =
|
|
335
|
-
for (const [
|
|
336
|
-
e = D(e,
|
|
337
|
-
return n.base.type === "date" && (e = t.preprocess((
|
|
330
|
+
function C(n) {
|
|
331
|
+
let e = U(n);
|
|
332
|
+
for (const [r, a] of n.rules.entries())
|
|
333
|
+
e = D(e, a, r);
|
|
334
|
+
return n.base.type === "date" && (e = t.preprocess((r) => typeof r == "string" ? new Date(r) : r, e)), n.nullable && (e = e.nullable()), n.optional && (e = e.optional()), e;
|
|
338
335
|
}
|
|
339
|
-
function
|
|
336
|
+
function q(n) {
|
|
340
337
|
return n ? Array.isArray(n) ? n.filter(Boolean) : [n] : [];
|
|
341
338
|
}
|
|
342
|
-
function
|
|
343
|
-
const i = (
|
|
344
|
-
if (
|
|
345
|
-
return Number.isNaN(
|
|
346
|
-
if (
|
|
347
|
-
const
|
|
348
|
-
return Number.isNaN(
|
|
339
|
+
function O(n, e, r, a) {
|
|
340
|
+
const i = (d) => {
|
|
341
|
+
if (d instanceof Date)
|
|
342
|
+
return Number.isNaN(d.getTime()) ? void 0 : { kind: "date", value: d.getTime() };
|
|
343
|
+
if (a === "date" && typeof d == "string") {
|
|
344
|
+
const m = new Date(d);
|
|
345
|
+
return Number.isNaN(m.getTime()) ? void 0 : { kind: "date", value: m.getTime() };
|
|
349
346
|
}
|
|
350
|
-
if (typeof
|
|
351
|
-
return { kind: typeof
|
|
352
|
-
},
|
|
353
|
-
if (!
|
|
347
|
+
if (typeof d == "number" || typeof d == "string")
|
|
348
|
+
return { kind: typeof d == "number" ? "number" : "string", value: d };
|
|
349
|
+
}, s = i(n), f = i(e);
|
|
350
|
+
if (!s || !f || s.kind !== f.kind)
|
|
354
351
|
return !1;
|
|
355
352
|
switch (r) {
|
|
356
353
|
case "eq":
|
|
357
|
-
return Object.is(
|
|
354
|
+
return Object.is(s.value, f.value);
|
|
358
355
|
case "neq":
|
|
359
|
-
return !Object.is(
|
|
356
|
+
return !Object.is(s.value, f.value);
|
|
360
357
|
case "gt":
|
|
361
|
-
return
|
|
358
|
+
return s.value > f.value;
|
|
362
359
|
case "gte":
|
|
363
|
-
return
|
|
360
|
+
return s.value >= f.value;
|
|
364
361
|
case "lt":
|
|
365
|
-
return
|
|
362
|
+
return s.value < f.value;
|
|
366
363
|
case "lte":
|
|
367
|
-
return
|
|
364
|
+
return s.value <= f.value;
|
|
368
365
|
}
|
|
369
366
|
}
|
|
370
|
-
function
|
|
371
|
-
var
|
|
372
|
-
const r = [],
|
|
373
|
-
|
|
374
|
-
for (const [k, m] of n.rules.entries()) {
|
|
375
|
-
if (m.kind === "required") {
|
|
376
|
-
i = !0, a = m.message;
|
|
377
|
-
continue;
|
|
378
|
-
}
|
|
367
|
+
function x(n, e = {}) {
|
|
368
|
+
var f;
|
|
369
|
+
const r = [], a = [];
|
|
370
|
+
for (const [d, m] of n.rules.entries()) {
|
|
379
371
|
if (m.kind === "compare") {
|
|
380
|
-
|
|
372
|
+
a.push((b, v) => O(b, v[m.field], m.operator, n.base.type) ? void 0 : m.message ?? `字段 ${m.field} 比较失败`);
|
|
381
373
|
continue;
|
|
382
374
|
}
|
|
383
375
|
if (m.kind === "custom") {
|
|
384
|
-
const b = (
|
|
376
|
+
const b = (f = e.custom) == null ? void 0 : f[m.key];
|
|
385
377
|
if (!b) {
|
|
386
378
|
r.push(o(
|
|
387
379
|
"RULE_CUSTOM_VALIDATOR_MISSING",
|
|
388
380
|
`Custom validator not registered: ${m.key}`,
|
|
389
|
-
["rules",
|
|
381
|
+
["rules", d],
|
|
390
382
|
"error",
|
|
391
|
-
|
|
383
|
+
d
|
|
392
384
|
));
|
|
393
385
|
continue;
|
|
394
386
|
}
|
|
395
|
-
|
|
387
|
+
a.push((v, _) => b(v, _, m.params));
|
|
396
388
|
}
|
|
397
389
|
}
|
|
398
|
-
n.
|
|
399
|
-
"RULE_OPTIONAL_REQUIRED_CONFLICT",
|
|
400
|
-
"A rule set cannot be both optional and required",
|
|
401
|
-
["optional"]
|
|
402
|
-
));
|
|
403
|
-
const f = O(n), g = s.length === 0 ? void 0 : async (k, m) => {
|
|
390
|
+
const i = C(n), s = a.length === 0 ? void 0 : async (d, m) => {
|
|
404
391
|
const b = [];
|
|
405
|
-
for (const v of
|
|
406
|
-
b.push(...
|
|
392
|
+
for (const v of a)
|
|
393
|
+
b.push(...q(await v(d, m)));
|
|
407
394
|
return b;
|
|
408
395
|
};
|
|
409
|
-
return { schema:
|
|
396
|
+
return { schema: i, validator: s, diagnostics: r };
|
|
410
397
|
}
|
|
411
398
|
export {
|
|
412
|
-
|
|
399
|
+
w as RULE_SET_VERSION,
|
|
413
400
|
l as RuleCompileError,
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
401
|
+
x as compileRules,
|
|
402
|
+
T as formatRuleZodIssues,
|
|
403
|
+
Y as parseRuleSet,
|
|
417
404
|
o as ruleDiagnostic,
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
405
|
+
j as ruleSetSchema,
|
|
406
|
+
C as rulesToZod,
|
|
407
|
+
H as zodToRules
|
|
421
408
|
};
|
package/dist/src/constants.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const RULE_SET_VERSION:
|
|
1
|
+
export declare const RULE_SET_VERSION: 2;
|
|
2
2
|
//# sourceMappingURL=constants.d.ts.map
|
package/dist/src/parse.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../../src/parse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAiB,eAAe,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AACtE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;
|
|
1
|
+
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../../src/parse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAiB,eAAe,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AACtE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AA2DvB,QAAA,MAAM,aAAa,EAMO,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;AAE5C,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,eAAe,CAU5D;AAED,OAAO,EAAE,aAAa,EAAE,CAAA"}
|
package/dist/src/rules.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rules.d.ts","sourceRoot":"","sources":["../../src/rules.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EAEf,kBAAkB,EAElB,OAAO,EAER,MAAM,SAAS,CAAA;AA4ChB,wBAAgB,YAAY,CAC1B,OAAO,EAAE,OAAO,EAChB,OAAO,GAAE,kBAAuB,GAC/B,eAAe,
|
|
1
|
+
{"version":3,"file":"rules.d.ts","sourceRoot":"","sources":["../../src/rules.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,eAAe,EAEf,kBAAkB,EAElB,OAAO,EAER,MAAM,SAAS,CAAA;AA4ChB,wBAAgB,YAAY,CAC1B,OAAO,EAAE,OAAO,EAChB,OAAO,GAAE,kBAAuB,GAC/B,eAAe,CAuCjB"}
|
package/dist/src/to-zod.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"to-zod.d.ts","sourceRoot":"","sources":["../../src/to-zod.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAkB,OAAO,EAAE,MAAM,SAAS,CAAA;AACtD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;
|
|
1
|
+
{"version":3,"file":"to-zod.d.ts","sourceRoot":"","sources":["../../src/to-zod.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAkB,OAAO,EAAE,MAAM,SAAS,CAAA;AACtD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AA2HvB,wBAAgB,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,CAAC,CAAC,UAAU,CAgBzD"}
|
package/dist/src/types.d.ts
CHANGED
|
@@ -19,9 +19,6 @@ export type RuleBase = {
|
|
|
19
19
|
value: RulePrimitive;
|
|
20
20
|
};
|
|
21
21
|
export type RuleDescriptor = {
|
|
22
|
-
kind: 'required';
|
|
23
|
-
message?: string;
|
|
24
|
-
} | {
|
|
25
22
|
kind: 'minLength';
|
|
26
23
|
value: number;
|
|
27
24
|
message?: string;
|
|
@@ -109,8 +106,6 @@ export interface RuleCompileContext {
|
|
|
109
106
|
}
|
|
110
107
|
export interface CompiledRuleSet {
|
|
111
108
|
schema: import('zod').ZodTypeAny;
|
|
112
|
-
required?: boolean;
|
|
113
|
-
requiredMessage?: string;
|
|
114
109
|
validator?: RuleValidator;
|
|
115
110
|
diagnostics: RuleDiagnostic[];
|
|
116
111
|
}
|
package/dist/src/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAEnD,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAA;AAE5D,MAAM,MAAM,aAAa,GACnB,aAAa,GACb,aAAa,EAAE,GACf;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,aAAa,CAAA;CAAE,CAAA;AAEtC,MAAM,MAAM,QAAQ,GACd;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,GAClB;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,GAClB;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,GACnB;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAChB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,CAAA;CAAE,GAC/C;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,KAAK,EAAE,aAAa,CAAA;CAAE,CAAA;AAE/C,MAAM,MAAM,cAAc,GACpB;IAAE,IAAI,EAAE,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAEnD,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAA;AAE5D,MAAM,MAAM,aAAa,GACnB,aAAa,GACb,aAAa,EAAE,GACf;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,aAAa,CAAA;CAAE,CAAA;AAEtC,MAAM,MAAM,QAAQ,GACd;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,GAClB;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE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,CAAA;IACjB,WAAW,EAAE,cAAc,EAAE,CAAA;CAC9B"}
|
package/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export * from './src/constants'
|
|
2
|
-
export * from './src/diagnostics'
|
|
3
|
-
export * from './src/from-zod'
|
|
4
|
-
export * from './src/parse'
|
|
5
|
-
export * from './src/rules'
|
|
6
|
-
export * from './src/to-zod'
|
|
7
|
-
export type * from './src/types'
|
|
1
|
+
export * from './src/constants'
|
|
2
|
+
export * from './src/diagnostics'
|
|
3
|
+
export * from './src/from-zod'
|
|
4
|
+
export * from './src/parse'
|
|
5
|
+
export * from './src/rules'
|
|
6
|
+
export * from './src/to-zod'
|
|
7
|
+
export type * from './src/types'
|
package/package.json
CHANGED
package/src/constants.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const RULE_SET_VERSION =
|
|
1
|
+
export const RULE_SET_VERSION = 2 as const
|