@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 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、required 元数据和跨字段/custom validator。`rulesToZod()` 只编译单字段 Zod 能力。
6
-
7
- ```ts
8
- import { compileRules, parseRuleSet } from '@moluoxixi/zod3-to-rule'
9
-
10
- const parsed = parseRuleSet({
11
- version: 1,
12
- base: { type: 'string' },
13
- rules: [
14
- { kind: 'required', message: '请输入邮箱' },
15
- { kind: 'email', message: '邮箱格式不正确' },
16
- ],
17
- })
18
-
19
- if (parsed.success) {
20
- const compiled = compileRules(parsed.data)
21
- compiled.schema.safeParse('user@example.com')
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 _ = 1;
3
- function o(n, e, r = [], s = "error", i) {
4
- return { code: n, severity: s, path: r, message: e, ...i === void 0 ? {} : { ruleIndex: i } };
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 M(n) {
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 R(n) {
18
+ function y(n) {
19
19
  return typeof n.message == "string" ? n.message : void 0;
20
20
  }
21
- function d(n, e) {
22
- const r = R(e);
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 Z(n) {
29
- let e = n, r = !1, s = !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
- s = !0, e = e.unwrap();
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: s, unsupported: !0, diagnostics: i };
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: s, unsupported: !1, diagnostics: i };
48
+ return { schema: e, optional: r, nullable: a, unsupported: !1, diagnostics: i };
49
49
  }
50
50
  function S(n) {
51
- const e = [], r = [], s = n._def.checks;
52
- for (const i of s) {
53
- const a = i.kind;
54
- switch (a) {
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(d({ kind: "minLength", value: i.value }, i));
56
+ e.push(g({ kind: "minLength", value: i.value }, i));
57
57
  break;
58
58
  case "max":
59
- e.push(d({ kind: "maxLength", value: i.value }, i));
59
+ e.push(g({ kind: "maxLength", value: i.value }, i));
60
60
  break;
61
61
  case "length":
62
- e.push(d({ kind: "length", value: i.value }, i));
62
+ e.push(g({ kind: "length", value: i.value }, i));
63
63
  break;
64
64
  case "email":
65
- e.push(d({ kind: "email" }, i));
65
+ e.push(g({ kind: "email" }, i));
66
66
  break;
67
67
  case "url":
68
- e.push(d({ kind: "url" }, i));
68
+ e.push(g({ kind: "url" }, i));
69
69
  break;
70
70
  case "uuid":
71
- e.push(d({ kind: "uuid" }, i));
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(d({ kind: "regex", source: f.source, flags: f.flags }, i)) : r.push(p("Zod regex check does not contain a RegExp"));
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(a)}`));
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 L(n) {
85
- const e = [], r = [], s = n._def.checks;
86
- for (const i of s) {
87
- const a = i.kind;
88
- switch (a) {
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(d({ kind: "min", value: i.value, inclusive: i.inclusive }, i));
90
+ e.push(g({ kind: "min", value: i.value, inclusive: i.inclusive }, i));
91
91
  break;
92
92
  case "max":
93
- e.push(d({ kind: "max", value: i.value, inclusive: i.inclusive }, i));
93
+ e.push(g({ kind: "max", value: i.value, inclusive: i.inclusive }, i));
94
94
  break;
95
95
  case "int":
96
- e.push(d({ kind: "integer" }, i));
96
+ e.push(g({ kind: "integer" }, i));
97
97
  break;
98
98
  case "finite":
99
- e.push(d({ kind: "finite" }, i));
99
+ e.push(g({ kind: "finite" }, i));
100
100
  break;
101
101
  case "multipleOf":
102
- e.push(d({ kind: "multipleOf", value: i.value }, i));
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(a)}`));
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 N(n) {
111
- const e = [], r = [], s = n._def.checks;
112
- for (const i of s) {
113
- const a = new Date(i.value);
114
- if (Number.isNaN(a.getTime())) {
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(d({ kind: "dateMin", value: a.toISOString() }, i)) : e.push(d({ kind: "dateMax", value: a.toISOString() }, i));
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 x(n) {
123
- const e = Z(n), r = [...e.diagnostics];
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 s, i = [];
128
+ let a, i = [];
129
129
  if (e.schema instanceof t.ZodString) {
130
- s = { type: "string" };
131
- const a = S(e.schema);
132
- i = a.rules, r.push(...a.diagnostics);
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
- s = { type: "number" };
135
- const a = L(e.schema);
136
- i = a.rules, r.push(...a.diagnostics);
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
- s = { type: "boolean" };
138
+ a = { type: "boolean" };
139
139
  else if (e.schema instanceof t.ZodDate) {
140
- s = { type: "date" };
141
- const a = N(e.schema);
142
- i = a.rules, r.push(...a.diagnostics);
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
- s = { type: "enum", values: e.schema.options };
144
+ a = { type: "enum", values: e.schema.options };
145
145
  else if (e.schema instanceof t.ZodLiteral) {
146
- const a = e.schema.value;
147
- if (typeof a != "string" && typeof a != "number" && typeof a != "boolean" && a !== null)
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
- s = { type: "literal", value: a };
149
+ a = { type: "literal", value: s };
150
150
  } else {
151
- const a = e.schema._def;
152
- return r.push(p(`Zod schema type is not supported: ${String(a.typeName ?? "unknown")}`)), { diagnostics: r };
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: s,
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 T = t.union([t.string(), t.number().finite(), t.boolean(), t.null()]), E = t.lazy(() => t.union([
166
- T,
167
- t.array(E),
168
- t.record(t.string(), E)
169
- ])), u = t.object({ message: t.string().min(1).optional() }).strict(), j = t.discriminatedUnion("type", [
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: T }).strict()
176
- ]), q = t.discriminatedUnion("kind", [
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: E.optional() }).merge(u)
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
- }), U = t.object({
210
- version: t.literal(_),
211
- base: j,
212
- rules: t.array(q),
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 V(n) {
217
- const e = U.safeParse(n);
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: M(e.error.issues) };
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 A(n) {
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 C(n, e) {
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 w(n, e, r) {
257
- const s = new Date(n);
258
- if (!Number.isNaN(s.getTime()))
259
- return s;
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(C(e, r), c(e));
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(w(e.value, r, e.kind), c(e));
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(w(e.value, r, e.kind), c(e));
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 O(n) {
334
- let e = A(n), r;
335
- for (const [s, i] of n.rules.entries())
336
- e = D(e, i, s), i.kind === "required" && (r = i);
337
- return n.base.type === "date" && (e = t.preprocess((s) => typeof s == "string" ? new Date(s) : s, e)), n.nullable && (e = e.nullable()), n.optional && (e = e.optional()), r && (e = e.refine((s) => s != null && (typeof s != "string" || s.trim().length > 0), c(r))), e;
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 P(n) {
336
+ function q(n) {
340
337
  return n ? Array.isArray(n) ? n.filter(Boolean) : [n] : [];
341
338
  }
342
- function H(n, e, r, s) {
343
- const i = (g) => {
344
- if (g instanceof Date)
345
- return Number.isNaN(g.getTime()) ? void 0 : { kind: "date", value: g.getTime() };
346
- if (s === "date" && typeof g == "string") {
347
- const h = new Date(g);
348
- return Number.isNaN(h.getTime()) ? void 0 : { kind: "date", value: h.getTime() };
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 g == "number" || typeof g == "string")
351
- return { kind: typeof g == "number" ? "number" : "string", value: g };
352
- }, a = i(n), f = i(e);
353
- if (!a || !f || a.kind !== f.kind)
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(a.value, f.value);
354
+ return Object.is(s.value, f.value);
358
355
  case "neq":
359
- return !Object.is(a.value, f.value);
356
+ return !Object.is(s.value, f.value);
360
357
  case "gt":
361
- return a.value > f.value;
358
+ return s.value > f.value;
362
359
  case "gte":
363
- return a.value >= f.value;
360
+ return s.value >= f.value;
364
361
  case "lt":
365
- return a.value < f.value;
362
+ return s.value < f.value;
366
363
  case "lte":
367
- return a.value <= f.value;
364
+ return s.value <= f.value;
368
365
  }
369
366
  }
370
- function $(n, e = {}) {
371
- var h;
372
- const r = [], s = [];
373
- let i, a;
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
- s.push((b, v) => H(b, v[m.field], m.operator, n.base.type) ? void 0 : m.message ?? `字段 ${m.field} 比较失败`);
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 = (h = e.custom) == null ? void 0 : h[m.key];
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", k],
381
+ ["rules", d],
390
382
  "error",
391
- k
383
+ d
392
384
  ));
393
385
  continue;
394
386
  }
395
- s.push((v, y) => b(v, y, m.params));
387
+ a.push((v, _) => b(v, _, m.params));
396
388
  }
397
389
  }
398
- n.optional && i && r.push(o(
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 s)
406
- b.push(...P(await v(k, m)));
392
+ for (const v of a)
393
+ b.push(...q(await v(d, m)));
407
394
  return b;
408
395
  };
409
- return { schema: f, required: i, requiredMessage: a, validator: g, diagnostics: r };
396
+ return { schema: i, validator: s, diagnostics: r };
410
397
  }
411
398
  export {
412
- _ as RULE_SET_VERSION,
399
+ w as RULE_SET_VERSION,
413
400
  l as RuleCompileError,
414
- $ as compileRules,
415
- M as formatRuleZodIssues,
416
- V as parseRuleSet,
401
+ x as compileRules,
402
+ T as formatRuleZodIssues,
403
+ Y as parseRuleSet,
417
404
  o as ruleDiagnostic,
418
- U as ruleSetSchema,
419
- O as rulesToZod,
420
- x as zodToRules
405
+ j as ruleSetSchema,
406
+ C as rulesToZod,
407
+ H as zodToRules
421
408
  };
@@ -1,2 +1,2 @@
1
- export declare const RULE_SET_VERSION: 1;
1
+ export declare const RULE_SET_VERSION: 2;
2
2
  //# sourceMappingURL=constants.d.ts.map
@@ -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;AA4DvB,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"}
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"}
@@ -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,CAuDjB"}
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"}
@@ -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;AA6HvB,wBAAgB,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,CAAC,CAAC,UAAU,CA+BzD"}
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"}
@@ -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
  }
@@ -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,UAAU,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,GACtC;IAAE,IAAI,EAAE,WAAW,C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gB;IAC/B,OAAO,EAAE,KAAK,CAAA;IACd,IAAI,CAAC,EAAE,SAAS,CAAA;IAChB,WAAW,EAAE,cAAc,EAAE,CAAA;CAC9B;AAED,MAAM,MAAM,eAAe,GAAG,gBAAgB,GAAG,gBAAgB,CAAA;AAEjE,MAAM,WAAW,gBAAgB;IAC/B,OAAO,CAAC,EAAE,OAAO,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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@moluoxixi/zod3-to-rule",
3
3
  "type": "module",
4
- "version": "0.1.3",
4
+ "version": "0.2.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/moluoxixi/vue-components",
package/src/constants.ts CHANGED
@@ -1 +1 @@
1
- export const RULE_SET_VERSION = 1 as const
1
+ export const RULE_SET_VERSION = 2 as const