@zenstackhq/zod 3.5.6 → 3.6.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/chunk-CfYAbeIz.mjs +13 -0
- package/dist/index.cjs +668 -807
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +185 -147
- package/dist/index.d.mts +265 -0
- package/dist/index.mjs +680 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +10 -9
- package/dist/index.d.ts +0 -229
- package/dist/index.js +0 -826
- package/dist/index.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,858 +1,719 @@
|
|
|
1
|
-
"
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
2
3
|
var __create = Object.create;
|
|
3
4
|
var __defProp = Object.defineProperty;
|
|
4
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
7
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
var __exportAll = (all, no_symbols) => {
|
|
10
|
+
let target = {};
|
|
11
|
+
for (var name in all) __defProp(target, name, {
|
|
12
|
+
get: all[name],
|
|
13
|
+
enumerable: true
|
|
14
|
+
});
|
|
15
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
16
|
+
return target;
|
|
12
17
|
};
|
|
13
18
|
var __copyProps = (to, from, except, desc) => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
20
|
+
key = keys[i];
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
22
|
+
get: ((k) => from[k]).bind(null, key),
|
|
23
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return to;
|
|
20
27
|
};
|
|
21
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
)
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
createSchemaFactory: () => createSchemaFactory
|
|
36
|
-
});
|
|
37
|
-
module.exports = __toCommonJS(src_exports);
|
|
38
|
-
|
|
39
|
-
// src/factory.ts
|
|
40
|
-
var import_schema2 = require("@zenstackhq/schema");
|
|
41
|
-
var import_decimal2 = __toESM(require("decimal.js"), 1);
|
|
42
|
-
var import_zod2 = __toESM(require("zod"), 1);
|
|
43
|
-
|
|
44
|
-
// src/error.ts
|
|
28
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
29
|
+
value: mod,
|
|
30
|
+
enumerable: true
|
|
31
|
+
}) : target, mod));
|
|
32
|
+
//#endregion
|
|
33
|
+
let _zenstackhq_schema = require("@zenstackhq/schema");
|
|
34
|
+
let decimal_js = require("decimal.js");
|
|
35
|
+
decimal_js = __toESM(decimal_js, 1);
|
|
36
|
+
let zod = require("zod");
|
|
37
|
+
zod = __toESM(zod, 1);
|
|
38
|
+
//#region src/error.ts
|
|
39
|
+
/**
|
|
40
|
+
* Error representing failures in Zod schema building.
|
|
41
|
+
*/
|
|
45
42
|
var SchemaFactoryError = class extends Error {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
constructor(message) {
|
|
50
|
-
super(message);
|
|
51
|
-
}
|
|
43
|
+
constructor(message) {
|
|
44
|
+
super(message);
|
|
45
|
+
}
|
|
52
46
|
};
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
var utils_exports = {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
collectFieldRefs: () => collectFieldRefs
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/utils.ts
|
|
49
|
+
var utils_exports = /* @__PURE__ */ __exportAll({
|
|
50
|
+
addBigIntValidation: () => addBigIntValidation,
|
|
51
|
+
addCustomValidation: () => addCustomValidation,
|
|
52
|
+
addDecimalValidation: () => addDecimalValidation,
|
|
53
|
+
addListValidation: () => addListValidation,
|
|
54
|
+
addNumberValidation: () => addNumberValidation,
|
|
55
|
+
addStringValidation: () => addStringValidation,
|
|
56
|
+
collectFieldRefs: () => collectFieldRefs
|
|
64
57
|
});
|
|
65
|
-
var import_schema = require("@zenstackhq/schema");
|
|
66
|
-
var import_decimal = __toESM(require("decimal.js"), 1);
|
|
67
|
-
var import_zod = require("zod");
|
|
68
58
|
function getArgValue(expr) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
return expr.value;
|
|
59
|
+
if (!expr || !_zenstackhq_schema.ExpressionUtils.isLiteral(expr)) return;
|
|
60
|
+
return expr.value;
|
|
73
61
|
}
|
|
74
|
-
__name(getArgValue, "getArgValue");
|
|
75
62
|
function addStringValidation(schema, attributes) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
break;
|
|
127
|
-
case "@url":
|
|
128
|
-
result = result.url();
|
|
129
|
-
break;
|
|
130
|
-
case "@trim":
|
|
131
|
-
result = result.trim();
|
|
132
|
-
break;
|
|
133
|
-
case "@lower":
|
|
134
|
-
result = result.toLowerCase();
|
|
135
|
-
break;
|
|
136
|
-
case "@upper":
|
|
137
|
-
result = result.toUpperCase();
|
|
138
|
-
break;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
return result;
|
|
63
|
+
if (!attributes || attributes.length === 0) return schema;
|
|
64
|
+
let result = schema;
|
|
65
|
+
for (const attr of attributes) switch (attr.name) {
|
|
66
|
+
case "@length": {
|
|
67
|
+
const min = getArgValue(attr.args?.find((a) => a.name === "min")?.value);
|
|
68
|
+
if (min !== void 0) result = result.min(min);
|
|
69
|
+
const max = getArgValue(attr.args?.find((a) => a.name === "max")?.value);
|
|
70
|
+
if (max !== void 0) result = result.max(max);
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
case "@startsWith": {
|
|
74
|
+
const value = getArgValue(attr.args?.[0]?.value);
|
|
75
|
+
if (value !== void 0) result = result.startsWith(value);
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
case "@endsWith": {
|
|
79
|
+
const value = getArgValue(attr.args?.[0]?.value);
|
|
80
|
+
if (value !== void 0) result = result.endsWith(value);
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
case "@contains": {
|
|
84
|
+
const value = getArgValue(attr.args?.[0]?.value);
|
|
85
|
+
if (value !== void 0) result = result.includes(value);
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
case "@regex": {
|
|
89
|
+
const pattern = getArgValue(attr.args?.[0]?.value);
|
|
90
|
+
if (pattern !== void 0) result = result.regex(new RegExp(pattern));
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
case "@email":
|
|
94
|
+
result = result.email();
|
|
95
|
+
break;
|
|
96
|
+
case "@datetime":
|
|
97
|
+
result = result.datetime();
|
|
98
|
+
break;
|
|
99
|
+
case "@url":
|
|
100
|
+
result = result.url();
|
|
101
|
+
break;
|
|
102
|
+
case "@trim":
|
|
103
|
+
result = result.trim();
|
|
104
|
+
break;
|
|
105
|
+
case "@lower":
|
|
106
|
+
result = result.toLowerCase();
|
|
107
|
+
break;
|
|
108
|
+
case "@upper":
|
|
109
|
+
result = result.toUpperCase();
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
return result;
|
|
142
113
|
}
|
|
143
|
-
__name(addStringValidation, "addStringValidation");
|
|
144
114
|
function addNumberValidation(schema, attributes) {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
break;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
return result;
|
|
115
|
+
if (!attributes || attributes.length === 0) return schema;
|
|
116
|
+
let result = schema;
|
|
117
|
+
for (const attr of attributes) {
|
|
118
|
+
const val = getArgValue(attr.args?.[0]?.value);
|
|
119
|
+
if (val === void 0) continue;
|
|
120
|
+
switch (attr.name) {
|
|
121
|
+
case "@gt":
|
|
122
|
+
result = result.gt(val);
|
|
123
|
+
break;
|
|
124
|
+
case "@gte":
|
|
125
|
+
result = result.gte(val);
|
|
126
|
+
break;
|
|
127
|
+
case "@lt":
|
|
128
|
+
result = result.lt(val);
|
|
129
|
+
break;
|
|
130
|
+
case "@lte":
|
|
131
|
+
result = result.lte(val);
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return result;
|
|
170
136
|
}
|
|
171
|
-
__name(addNumberValidation, "addNumberValidation");
|
|
172
137
|
function addBigIntValidation(schema, attributes) {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
break;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return result;
|
|
138
|
+
if (!attributes || attributes.length === 0) return schema;
|
|
139
|
+
let result = schema;
|
|
140
|
+
for (const attr of attributes) {
|
|
141
|
+
const val = getArgValue(attr.args?.[0]?.value);
|
|
142
|
+
if (val === void 0) continue;
|
|
143
|
+
switch (attr.name) {
|
|
144
|
+
case "@gt":
|
|
145
|
+
result = result.gt(BigInt(val));
|
|
146
|
+
break;
|
|
147
|
+
case "@gte":
|
|
148
|
+
result = result.gte(BigInt(val));
|
|
149
|
+
break;
|
|
150
|
+
case "@lt":
|
|
151
|
+
result = result.lt(BigInt(val));
|
|
152
|
+
break;
|
|
153
|
+
case "@lte":
|
|
154
|
+
result = result.lte(BigInt(val));
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return result;
|
|
198
159
|
}
|
|
199
|
-
__name(addBigIntValidation, "addBigIntValidation");
|
|
200
160
|
function addDecimalValidation(schema, attributes, addExtraValidation) {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
break;
|
|
255
|
-
case "@lt":
|
|
256
|
-
result = refine(result, "lt", val);
|
|
257
|
-
break;
|
|
258
|
-
case "@lte":
|
|
259
|
-
result = refine(result, "lte", val);
|
|
260
|
-
break;
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
return result;
|
|
161
|
+
let result = schema;
|
|
162
|
+
if (schema instanceof zod.z.ZodString) result = schema.superRefine((v, ctx) => {
|
|
163
|
+
try {
|
|
164
|
+
new decimal_js.default(v);
|
|
165
|
+
} catch (err) {
|
|
166
|
+
ctx.addIssue({
|
|
167
|
+
code: "custom",
|
|
168
|
+
message: `Invalid decimal: ${err}`
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
}).transform((val) => new decimal_js.default(val));
|
|
172
|
+
function refine(schema, op, value) {
|
|
173
|
+
return schema.superRefine((v, ctx) => {
|
|
174
|
+
const { error } = zod.z.number()[op](value).safeParse(v.toNumber());
|
|
175
|
+
error?.issues.forEach((issue) => {
|
|
176
|
+
if (op === "gt" || op === "gte") ctx.addIssue({
|
|
177
|
+
code: "too_small",
|
|
178
|
+
origin: "number",
|
|
179
|
+
minimum: value,
|
|
180
|
+
type: "decimal",
|
|
181
|
+
inclusive: op === "gte",
|
|
182
|
+
message: issue.message
|
|
183
|
+
});
|
|
184
|
+
else ctx.addIssue({
|
|
185
|
+
code: "too_big",
|
|
186
|
+
origin: "number",
|
|
187
|
+
maximum: value,
|
|
188
|
+
type: "decimal",
|
|
189
|
+
inclusive: op === "lte",
|
|
190
|
+
message: issue.message
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
if (attributes && addExtraValidation) for (const attr of attributes) {
|
|
196
|
+
const val = getArgValue(attr.args?.[0]?.value);
|
|
197
|
+
if (val === void 0) continue;
|
|
198
|
+
switch (attr.name) {
|
|
199
|
+
case "@gt":
|
|
200
|
+
result = refine(result, "gt", val);
|
|
201
|
+
break;
|
|
202
|
+
case "@gte":
|
|
203
|
+
result = refine(result, "gte", val);
|
|
204
|
+
break;
|
|
205
|
+
case "@lt":
|
|
206
|
+
result = refine(result, "lt", val);
|
|
207
|
+
break;
|
|
208
|
+
case "@lte":
|
|
209
|
+
result = refine(result, "lte", val);
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return result;
|
|
265
214
|
}
|
|
266
|
-
__name(addDecimalValidation, "addDecimalValidation");
|
|
267
215
|
function addListValidation(schema, attributes) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
}
|
|
278
|
-
const max = getArgValue(attr.args?.find((a) => a.name === "max")?.value);
|
|
279
|
-
if (max !== void 0) {
|
|
280
|
-
result = result.max(max);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
return result;
|
|
216
|
+
if (!attributes || attributes.length === 0) return schema;
|
|
217
|
+
let result = schema;
|
|
218
|
+
for (const attr of attributes) if (attr.name === "@length") {
|
|
219
|
+
const min = getArgValue(attr.args?.find((a) => a.name === "min")?.value);
|
|
220
|
+
if (min !== void 0) result = result.min(min);
|
|
221
|
+
const max = getArgValue(attr.args?.find((a) => a.name === "max")?.value);
|
|
222
|
+
if (max !== void 0) result = result.max(max);
|
|
223
|
+
}
|
|
224
|
+
return result;
|
|
285
225
|
}
|
|
286
|
-
|
|
226
|
+
/**
|
|
227
|
+
* Recursively collects all field names referenced by `kind: 'field'` nodes
|
|
228
|
+
* inside an expression tree.
|
|
229
|
+
*/
|
|
287
230
|
function collectFieldRefs(expr) {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
return refs;
|
|
231
|
+
const refs = /* @__PURE__ */ new Set();
|
|
232
|
+
function walk(e) {
|
|
233
|
+
switch (e.kind) {
|
|
234
|
+
case "field":
|
|
235
|
+
refs.add(e.field);
|
|
236
|
+
break;
|
|
237
|
+
case "unary":
|
|
238
|
+
walk(e.operand);
|
|
239
|
+
break;
|
|
240
|
+
case "binary":
|
|
241
|
+
walk(e.left);
|
|
242
|
+
walk(e.right);
|
|
243
|
+
break;
|
|
244
|
+
case "call":
|
|
245
|
+
e.args?.forEach(walk);
|
|
246
|
+
break;
|
|
247
|
+
case "array":
|
|
248
|
+
e.items.forEach(walk);
|
|
249
|
+
break;
|
|
250
|
+
case "member":
|
|
251
|
+
walk(e.receiver);
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
walk(expr);
|
|
256
|
+
return refs;
|
|
315
257
|
}
|
|
316
|
-
|
|
258
|
+
/**
|
|
259
|
+
* Applies `@@validate` rules from `attributes` to `schema` as Zod refinements.
|
|
260
|
+
*
|
|
261
|
+
* When `presentFields` is provided, only rules whose every field reference is
|
|
262
|
+
* present in the set are applied. Rules that reference a field absent from the
|
|
263
|
+
* set are silently skipped — they cannot be evaluated correctly against a
|
|
264
|
+
* partial payload (e.g. when `select` or `omit` has been used).
|
|
265
|
+
*
|
|
266
|
+
* Omit `presentFields` (or pass `undefined`) to apply all rules
|
|
267
|
+
* unconditionally, which is the correct behaviour for full-model schemas.
|
|
268
|
+
*
|
|
269
|
+
* Note: `@@validate` conditions are restricted by the ZModel compiler to
|
|
270
|
+
* scalar fields of the same model only — relation fields are a compile error.
|
|
271
|
+
* A flat field-name set is therefore sufficient.
|
|
272
|
+
*/
|
|
317
273
|
function addCustomValidation(schema, attributes, presentFields) {
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
const message = getArgValue(attr.args?.[1]?.value);
|
|
337
|
-
const pathExpr = attr.args?.[2]?.value;
|
|
338
|
-
let path = void 0;
|
|
339
|
-
if (pathExpr && import_schema.ExpressionUtils.isArray(pathExpr)) {
|
|
340
|
-
path = pathExpr.items.map((e) => import_schema.ExpressionUtils.getLiteralValue(e));
|
|
341
|
-
}
|
|
342
|
-
result = applyValidation(result, expr, message, path);
|
|
343
|
-
}
|
|
344
|
-
return result;
|
|
274
|
+
const attrs = attributes?.filter((a) => a.name === "@@validate");
|
|
275
|
+
if (!attrs || attrs.length === 0) return schema;
|
|
276
|
+
let result = schema;
|
|
277
|
+
for (const attr of attrs) {
|
|
278
|
+
const expr = attr.args?.[0]?.value;
|
|
279
|
+
if (!expr) continue;
|
|
280
|
+
if (presentFields !== void 0) {
|
|
281
|
+
if ([...collectFieldRefs(expr)].some((ref) => !presentFields.has(ref))) continue;
|
|
282
|
+
}
|
|
283
|
+
const message = getArgValue(attr.args?.[1]?.value);
|
|
284
|
+
const pathExpr = attr.args?.[2]?.value;
|
|
285
|
+
let path = void 0;
|
|
286
|
+
if (pathExpr && _zenstackhq_schema.ExpressionUtils.isArray(pathExpr)) path = pathExpr.items.map((e) => _zenstackhq_schema.ExpressionUtils.getLiteralValue(e));
|
|
287
|
+
result = applyValidation(result, expr, message, path);
|
|
288
|
+
}
|
|
289
|
+
return result;
|
|
345
290
|
}
|
|
346
|
-
__name(addCustomValidation, "addCustomValidation");
|
|
347
291
|
function applyValidation(schema, expr, message, path) {
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
if (path) {
|
|
353
|
-
options.path = path;
|
|
354
|
-
}
|
|
355
|
-
return schema.refine((data) => Boolean(evalExpression(data, expr)), options);
|
|
292
|
+
const options = {};
|
|
293
|
+
if (message) options.error = message;
|
|
294
|
+
if (path) options.path = path;
|
|
295
|
+
return schema.refine((data) => Boolean(evalExpression(data, expr)), options);
|
|
356
296
|
}
|
|
357
|
-
__name(applyValidation, "applyValidation");
|
|
358
297
|
function evalExpression(data, expr) {
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
return evalBinary(data, expr);
|
|
372
|
-
case "call":
|
|
373
|
-
return evalCall(data, expr);
|
|
374
|
-
case "this":
|
|
375
|
-
return data ?? null;
|
|
376
|
-
case "null":
|
|
377
|
-
return null;
|
|
378
|
-
case "binding":
|
|
379
|
-
throw new SchemaFactoryError("Binding expression is not supported in validation expressions");
|
|
380
|
-
}
|
|
298
|
+
switch (expr.kind) {
|
|
299
|
+
case "literal": return expr.value;
|
|
300
|
+
case "array": return expr.items.map((item) => evalExpression(data, item));
|
|
301
|
+
case "field": return evalField(data, expr);
|
|
302
|
+
case "member": return evalMember(data, expr);
|
|
303
|
+
case "unary": return evalUnary(data, expr);
|
|
304
|
+
case "binary": return evalBinary(data, expr);
|
|
305
|
+
case "call": return evalCall(data, expr);
|
|
306
|
+
case "this": return data ?? null;
|
|
307
|
+
case "null": return null;
|
|
308
|
+
case "binding": throw new SchemaFactoryError("Binding expression is not supported in validation expressions");
|
|
309
|
+
}
|
|
381
310
|
}
|
|
382
|
-
|
|
311
|
+
/**
|
|
312
|
+
* Sentinel value returned by `evalField` when a field key is entirely absent
|
|
313
|
+
* from the data object (as opposed to being present with a `null` value).
|
|
314
|
+
* Used by comparison operators to skip @@validate rules against missing
|
|
315
|
+
* optional fields (e.g. when `optionality: 'all'` produces a partial payload).
|
|
316
|
+
*/
|
|
317
|
+
const ABSENT = Symbol("absent");
|
|
383
318
|
function evalField(data, e) {
|
|
384
|
-
|
|
319
|
+
if (data == null || !(e.field in data)) return ABSENT;
|
|
320
|
+
return data[e.field] ?? null;
|
|
385
321
|
}
|
|
386
|
-
__name(evalField, "evalField");
|
|
387
322
|
function evalUnary(data, expr) {
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
throw new SchemaFactoryError(`Unsupported unary operator: ${expr.op}`);
|
|
394
|
-
}
|
|
323
|
+
const operand = evalExpression(data, expr.operand);
|
|
324
|
+
switch (expr.op) {
|
|
325
|
+
case "!": return !operand;
|
|
326
|
+
default: throw new SchemaFactoryError(`Unsupported unary operator: ${expr.op}`);
|
|
327
|
+
}
|
|
395
328
|
}
|
|
396
|
-
__name(evalUnary, "evalUnary");
|
|
397
329
|
function evalBinary(data, expr) {
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
}
|
|
426
|
-
return left.every((item) => item === right);
|
|
427
|
-
case "^":
|
|
428
|
-
if (!Array.isArray(left)) {
|
|
429
|
-
return false;
|
|
430
|
-
}
|
|
431
|
-
return !left.some((item) => item === right);
|
|
432
|
-
case "in":
|
|
433
|
-
if (!Array.isArray(right)) {
|
|
434
|
-
return false;
|
|
435
|
-
}
|
|
436
|
-
return right.includes(left);
|
|
437
|
-
default:
|
|
438
|
-
throw new SchemaFactoryError(`Unsupported binary operator: ${expr.op}`);
|
|
439
|
-
}
|
|
330
|
+
const left = evalExpression(data, expr.left);
|
|
331
|
+
const right = evalExpression(data, expr.right);
|
|
332
|
+
switch (expr.op) {
|
|
333
|
+
case "&&": return Boolean(left) && Boolean(right);
|
|
334
|
+
case "||": return Boolean(left) || Boolean(right);
|
|
335
|
+
case "==": return (left === ABSENT ? null : left) == (right === ABSENT ? null : right);
|
|
336
|
+
case "!=": return (left === ABSENT ? null : left) != (right === ABSENT ? null : right);
|
|
337
|
+
case "<":
|
|
338
|
+
case "<=":
|
|
339
|
+
case ">":
|
|
340
|
+
case ">=":
|
|
341
|
+
if (left === ABSENT || right === ABSENT) return true;
|
|
342
|
+
return expr.op === "<" ? left < right : expr.op === "<=" ? left <= right : expr.op === ">" ? left > right : left >= right;
|
|
343
|
+
case "?":
|
|
344
|
+
if (!Array.isArray(left)) return false;
|
|
345
|
+
return left.some((item) => item === right);
|
|
346
|
+
case "!":
|
|
347
|
+
if (!Array.isArray(left)) return false;
|
|
348
|
+
return left.every((item) => item === right);
|
|
349
|
+
case "^":
|
|
350
|
+
if (!Array.isArray(left)) return false;
|
|
351
|
+
return !left.some((item) => item === right);
|
|
352
|
+
case "in":
|
|
353
|
+
if (!Array.isArray(right)) return false;
|
|
354
|
+
return right.includes(left);
|
|
355
|
+
default: throw new SchemaFactoryError(`Unsupported binary operator: ${expr.op}`);
|
|
356
|
+
}
|
|
440
357
|
}
|
|
441
|
-
__name(evalBinary, "evalBinary");
|
|
442
358
|
function evalMember(data, expr) {
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
}
|
|
450
|
-
return result ?? null;
|
|
359
|
+
let result = evalExpression(data, expr.receiver);
|
|
360
|
+
for (const member of expr.members) {
|
|
361
|
+
if (!result || typeof result !== "object") return;
|
|
362
|
+
result = result[member];
|
|
363
|
+
}
|
|
364
|
+
return result ?? null;
|
|
451
365
|
}
|
|
452
|
-
__name(evalMember, "evalMember");
|
|
453
366
|
function evalCall(data, expr) {
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
const search = evalExpression(data, expr.args?.[1]);
|
|
516
|
-
if (f === "has") {
|
|
517
|
-
return fieldArg.some((item) => item === search);
|
|
518
|
-
} else if (f === "hasEvery") {
|
|
519
|
-
invariant(Array.isArray(search), "hasEvery second argument must be an array");
|
|
520
|
-
return search.every((v) => fieldArg.some((item) => item === v));
|
|
521
|
-
} else {
|
|
522
|
-
invariant(Array.isArray(search), "hasSome second argument must be an array");
|
|
523
|
-
return search.some((v) => fieldArg.some((item) => item === v));
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
case "isEmpty": {
|
|
527
|
-
if (fieldArg === void 0 || fieldArg === null) {
|
|
528
|
-
return false;
|
|
529
|
-
}
|
|
530
|
-
invariant(Array.isArray(fieldArg), `"${f}" first argument must be an array field`);
|
|
531
|
-
return fieldArg.length === 0;
|
|
532
|
-
}
|
|
533
|
-
default:
|
|
534
|
-
throw new SchemaFactoryError(`Unsupported function "${f}"`);
|
|
535
|
-
}
|
|
367
|
+
const f = expr.function;
|
|
368
|
+
const fieldArg = expr.args?.[0] ? evalExpression(data, expr.args[0]) : void 0;
|
|
369
|
+
switch (f) {
|
|
370
|
+
case "length":
|
|
371
|
+
if (fieldArg === void 0 || fieldArg === null || fieldArg === ABSENT) return false;
|
|
372
|
+
invariant(typeof fieldArg === "string" || Array.isArray(fieldArg), `"${f}" first argument must be a string or a list`);
|
|
373
|
+
return fieldArg.length;
|
|
374
|
+
case "startsWith":
|
|
375
|
+
case "endsWith":
|
|
376
|
+
case "contains": {
|
|
377
|
+
if (fieldArg === void 0 || fieldArg === null || fieldArg === ABSENT) return false;
|
|
378
|
+
invariant(typeof fieldArg === "string", `"${f}" first argument must be a string`);
|
|
379
|
+
invariant(expr.args?.[1], `"${f}" requires a search argument`);
|
|
380
|
+
const search = getArgValue(expr.args?.[1]);
|
|
381
|
+
const caseInsensitive = getArgValue(expr.args?.[2]) ?? false;
|
|
382
|
+
const applyStringOp = (x, y) => {
|
|
383
|
+
switch (f) {
|
|
384
|
+
case "startsWith": return x.startsWith(y);
|
|
385
|
+
case "endsWith": return x.endsWith(y);
|
|
386
|
+
case "contains": return x.includes(y);
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
return caseInsensitive ? applyStringOp(fieldArg.toLowerCase(), search.toLowerCase()) : applyStringOp(fieldArg, search);
|
|
390
|
+
}
|
|
391
|
+
case "regex": {
|
|
392
|
+
if (fieldArg === void 0 || fieldArg === null || fieldArg === ABSENT) return false;
|
|
393
|
+
invariant(typeof fieldArg === "string", `"${f}" first argument must be a string`);
|
|
394
|
+
const pattern = getArgValue(expr.args?.[1]);
|
|
395
|
+
invariant(pattern !== void 0, `"${f}" requires a pattern argument`);
|
|
396
|
+
return new RegExp(pattern).test(fieldArg);
|
|
397
|
+
}
|
|
398
|
+
case "isEmail":
|
|
399
|
+
case "isUrl":
|
|
400
|
+
case "isDateTime": {
|
|
401
|
+
if (fieldArg === void 0 || fieldArg === null || fieldArg === ABSENT) return false;
|
|
402
|
+
invariant(typeof fieldArg === "string", `"${f}" first argument must be a string`);
|
|
403
|
+
const fn = f === "isEmail" ? "email" : f === "isUrl" ? "url" : "datetime";
|
|
404
|
+
return zod.z.string()[fn]().safeParse(fieldArg).success;
|
|
405
|
+
}
|
|
406
|
+
case "has":
|
|
407
|
+
case "hasEvery":
|
|
408
|
+
case "hasSome": {
|
|
409
|
+
invariant(expr.args?.[1], `${f} requires a search argument`);
|
|
410
|
+
if (fieldArg === void 0 || fieldArg === null || fieldArg === ABSENT) return false;
|
|
411
|
+
invariant(Array.isArray(fieldArg), `"${f}" first argument must be an array field`);
|
|
412
|
+
const search = evalExpression(data, expr.args?.[1]);
|
|
413
|
+
if (f === "has") return fieldArg.some((item) => item === search);
|
|
414
|
+
else if (f === "hasEvery") {
|
|
415
|
+
invariant(Array.isArray(search), "hasEvery second argument must be an array");
|
|
416
|
+
return search.every((v) => fieldArg.some((item) => item === v));
|
|
417
|
+
} else {
|
|
418
|
+
invariant(Array.isArray(search), "hasSome second argument must be an array");
|
|
419
|
+
return search.some((v) => fieldArg.some((item) => item === v));
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
case "isEmpty":
|
|
423
|
+
if (fieldArg === void 0 || fieldArg === null || fieldArg === ABSENT) return false;
|
|
424
|
+
invariant(Array.isArray(fieldArg), `"${f}" first argument must be an array field`);
|
|
425
|
+
return fieldArg.length === 0;
|
|
426
|
+
default: throw new SchemaFactoryError(`Unsupported function "${f}"`);
|
|
427
|
+
}
|
|
536
428
|
}
|
|
537
|
-
__name(evalCall, "evalCall");
|
|
538
429
|
function invariant(condition, message) {
|
|
539
|
-
|
|
540
|
-
throw new SchemaFactoryError(message ?? "Invariant failed");
|
|
541
|
-
}
|
|
430
|
+
if (!condition) throw new SchemaFactoryError(message ?? "Invariant failed");
|
|
542
431
|
}
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
// src/factory.ts
|
|
432
|
+
//#endregion
|
|
433
|
+
//#region src/factory.ts
|
|
546
434
|
function createSchemaFactory(schema) {
|
|
547
|
-
|
|
435
|
+
return new SchemaFactory(schema);
|
|
548
436
|
}
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
437
|
+
/**
|
|
438
|
+
* Recursive Zod schema that validates a `RawOptions` object at runtime,
|
|
439
|
+
* enforcing the same mutual-exclusion rules that the TypeScript union type
|
|
440
|
+
* enforces at compile time:
|
|
441
|
+
* - `select` and `include` cannot be used together.
|
|
442
|
+
* - `select` and `omit` cannot be used together.
|
|
443
|
+
* Nested relation options are validated with the same rules.
|
|
444
|
+
*/
|
|
445
|
+
const rawOptionsSchema = zod.default.lazy(() => zod.default.strictObject({
|
|
446
|
+
select: zod.default.record(zod.default.string(), zod.default.union([zod.default.literal(true), rawOptionsSchema])).optional(),
|
|
447
|
+
include: zod.default.record(zod.default.string(), zod.default.union([zod.default.literal(true), rawOptionsSchema])).optional(),
|
|
448
|
+
omit: zod.default.record(zod.default.string(), zod.default.literal(true)).optional(),
|
|
449
|
+
optionality: zod.default.enum(["all", "defaults"]).optional()
|
|
560
450
|
}).superRefine((val, ctx) => {
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
code: "custom",
|
|
570
|
-
message: "`select` and `omit` cannot be used together"
|
|
571
|
-
});
|
|
572
|
-
}
|
|
451
|
+
if (val.select && val.include) ctx.addIssue({
|
|
452
|
+
code: "custom",
|
|
453
|
+
message: "`select` and `include` cannot be used together"
|
|
454
|
+
});
|
|
455
|
+
if (val.select && val.omit) ctx.addIssue({
|
|
456
|
+
code: "custom",
|
|
457
|
+
message: "`select` and `omit` cannot be used together"
|
|
458
|
+
});
|
|
573
459
|
}));
|
|
574
|
-
var SchemaFactory = class
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
return this.applyDescription(addCustomValidation(shape, typeDef.attributes), typeDef.attributes);
|
|
824
|
-
}
|
|
825
|
-
makeEnumSchema(_enum) {
|
|
826
|
-
const enumDef = this.schema.requireEnum(_enum);
|
|
827
|
-
const schema = import_zod2.default.enum(Object.keys(enumDef.values));
|
|
828
|
-
return this.applyDescription(schema, enumDef.attributes);
|
|
829
|
-
}
|
|
830
|
-
getMetaDescription(attributes) {
|
|
831
|
-
if (!attributes) return void 0;
|
|
832
|
-
for (const attr of attributes) {
|
|
833
|
-
if (attr.name !== "@meta" && attr.name !== "@@meta") continue;
|
|
834
|
-
const nameExpr = attr.args?.[0]?.value;
|
|
835
|
-
if (!nameExpr || import_schema2.ExpressionUtils.getLiteralValue(nameExpr) !== "description") continue;
|
|
836
|
-
const valueExpr = attr.args?.[1]?.value;
|
|
837
|
-
if (valueExpr) {
|
|
838
|
-
return import_schema2.ExpressionUtils.getLiteralValue(valueExpr);
|
|
839
|
-
}
|
|
840
|
-
}
|
|
841
|
-
return void 0;
|
|
842
|
-
}
|
|
843
|
-
applyDescription(schema, attributes) {
|
|
844
|
-
const description = this.getMetaDescription(attributes);
|
|
845
|
-
if (description) {
|
|
846
|
-
return schema.meta({
|
|
847
|
-
description
|
|
848
|
-
});
|
|
849
|
-
}
|
|
850
|
-
return schema;
|
|
851
|
-
}
|
|
460
|
+
var SchemaFactory = class {
|
|
461
|
+
schema;
|
|
462
|
+
constructor(_schema) {
|
|
463
|
+
this.schema = new _zenstackhq_schema.SchemaAccessor(_schema);
|
|
464
|
+
}
|
|
465
|
+
makeModelSchema(model, options) {
|
|
466
|
+
const modelDef = this.schema.requireModel(model);
|
|
467
|
+
if (!options) {
|
|
468
|
+
const fields = {};
|
|
469
|
+
for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
|
|
470
|
+
if (fieldDef.relation) continue;
|
|
471
|
+
fields[fieldName] = this.applyDescription(this.makeScalarFieldSchema(fieldDef), fieldDef.attributes);
|
|
472
|
+
}
|
|
473
|
+
const shape = zod.default.strictObject(fields);
|
|
474
|
+
return this.applyDescription(addCustomValidation(shape, modelDef.attributes), modelDef.attributes);
|
|
475
|
+
}
|
|
476
|
+
const rawOptions = rawOptionsSchema.parse(options);
|
|
477
|
+
const fields = this.buildFieldsWithOptions(model, rawOptions);
|
|
478
|
+
const optionalizedFields = this.applyOptionality(fields, model, rawOptions.optionality);
|
|
479
|
+
const shape = zod.default.strictObject(optionalizedFields);
|
|
480
|
+
const presentFields = this.buildPresentFields(model, rawOptions);
|
|
481
|
+
const withValidation = addCustomValidation(shape, modelDef.attributes, presentFields);
|
|
482
|
+
return this.applyDescription(withValidation, modelDef.attributes);
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* @deprecated Use `makeModelSchema(model, { optionality: 'defaults' })` instead.
|
|
486
|
+
*/
|
|
487
|
+
makeModelCreateSchema(model) {
|
|
488
|
+
const modelDef = this.schema.requireModel(model);
|
|
489
|
+
const fields = {};
|
|
490
|
+
for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
|
|
491
|
+
if (fieldDef.relation || fieldDef.computed || fieldDef.isDiscriminator) continue;
|
|
492
|
+
let fieldSchema = this.makeScalarFieldSchema(fieldDef);
|
|
493
|
+
if (fieldDef.optional || fieldDef.default !== void 0 || fieldDef.updatedAt) fieldSchema = fieldSchema.optional();
|
|
494
|
+
fields[fieldName] = this.applyDescription(fieldSchema, fieldDef.attributes);
|
|
495
|
+
}
|
|
496
|
+
const shape = zod.default.strictObject(fields);
|
|
497
|
+
return this.applyDescription(addCustomValidation(shape, modelDef.attributes), modelDef.attributes);
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* @deprecated Use `makeModelSchema(model, { optionality: 'all' })` instead.
|
|
501
|
+
*/
|
|
502
|
+
makeModelUpdateSchema(model) {
|
|
503
|
+
const modelDef = this.schema.requireModel(model);
|
|
504
|
+
const fields = {};
|
|
505
|
+
for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
|
|
506
|
+
if (fieldDef.relation || fieldDef.computed || fieldDef.isDiscriminator) continue;
|
|
507
|
+
let fieldSchema = this.makeScalarFieldSchema(fieldDef);
|
|
508
|
+
fieldSchema = fieldSchema.optional();
|
|
509
|
+
fields[fieldName] = this.applyDescription(fieldSchema, fieldDef.attributes);
|
|
510
|
+
}
|
|
511
|
+
const shape = zod.default.strictObject(fields);
|
|
512
|
+
return this.applyDescription(addCustomValidation(shape, modelDef.attributes), modelDef.attributes);
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Applies the `optionality` option to a fields map.
|
|
516
|
+
*
|
|
517
|
+
* - `"all"` — wraps every field in `z.ZodOptional`.
|
|
518
|
+
* - `"defaults"` — only wraps fields that have a `@default` attribute or
|
|
519
|
+
* are `@updatedAt` in `z.ZodOptional`. Fields that are
|
|
520
|
+
* already optional (nullable optional) retain their shape;
|
|
521
|
+
* we just add the outer optional layer.
|
|
522
|
+
* - `undefined` — returns the fields map unchanged.
|
|
523
|
+
*/
|
|
524
|
+
applyOptionality(fields, model, optionality) {
|
|
525
|
+
if (!optionality) return fields;
|
|
526
|
+
const modelDef = this.schema.requireModel(model);
|
|
527
|
+
const result = {};
|
|
528
|
+
for (const [fieldName, fieldSchema] of Object.entries(fields)) if (optionality === "all") result[fieldName] = this.wrapOptionalPreservingMeta(fieldSchema);
|
|
529
|
+
else {
|
|
530
|
+
const fieldDef = modelDef.fields[fieldName];
|
|
531
|
+
result[fieldName] = fieldDef && (fieldDef.default !== void 0 || fieldDef.updatedAt || fieldDef.optional) ? this.wrapOptionalPreservingMeta(fieldSchema) : fieldSchema;
|
|
532
|
+
}
|
|
533
|
+
return result;
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Internal loose options shape used at runtime (we've already validated the
|
|
537
|
+
* type-level constraints via the overload signatures).
|
|
538
|
+
*/
|
|
539
|
+
buildFieldsWithOptions(model, options) {
|
|
540
|
+
const { select, include, omit } = options;
|
|
541
|
+
const modelDef = this.schema.requireModel(model);
|
|
542
|
+
const fields = {};
|
|
543
|
+
if (select) for (const [key, value] of Object.entries(select)) {
|
|
544
|
+
const fieldDef = modelDef.fields[key];
|
|
545
|
+
if (!fieldDef) throw new SchemaFactoryError(`Field "${key}" does not exist on model "${model}"`);
|
|
546
|
+
if (fieldDef.relation) {
|
|
547
|
+
const subOptions = typeof value === "object" ? value : void 0;
|
|
548
|
+
const relSchema = this.makeRelationFieldSchema(fieldDef, subOptions);
|
|
549
|
+
fields[key] = this.applyDescription(this.applyCardinality(relSchema, fieldDef).optional(), fieldDef.attributes);
|
|
550
|
+
} else {
|
|
551
|
+
if (typeof value === "object") throw new SchemaFactoryError(`Field "${key}" on model "${model}" is a scalar field and cannot have nested options`);
|
|
552
|
+
fields[key] = this.applyDescription(this.makeScalarFieldSchema(fieldDef), fieldDef.attributes);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
else {
|
|
556
|
+
if (omit) for (const key of Object.keys(omit)) {
|
|
557
|
+
const fieldDef = modelDef.fields[key];
|
|
558
|
+
if (!fieldDef) throw new SchemaFactoryError(`Field "${key}" does not exist on model "${model}"`);
|
|
559
|
+
if (fieldDef.relation) throw new SchemaFactoryError(`Field "${key}" on model "${model}" is a relation field and cannot be used in "omit"`);
|
|
560
|
+
}
|
|
561
|
+
for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
|
|
562
|
+
if (fieldDef.relation) continue;
|
|
563
|
+
if (omit?.[fieldName] === true) continue;
|
|
564
|
+
fields[fieldName] = this.applyDescription(this.makeScalarFieldSchema(fieldDef), fieldDef.attributes);
|
|
565
|
+
}
|
|
566
|
+
if (include) for (const [key, value] of Object.entries(include)) {
|
|
567
|
+
const fieldDef = modelDef.fields[key];
|
|
568
|
+
if (!fieldDef) throw new SchemaFactoryError(`Field "${key}" does not exist on model "${model}"`);
|
|
569
|
+
if (!fieldDef.relation) throw new SchemaFactoryError(`Field "${key}" on model "${model}" is not a relation field and cannot be used in "include"`);
|
|
570
|
+
const subOptions = typeof value === "object" ? value : void 0;
|
|
571
|
+
const relSchema = this.makeRelationFieldSchema(fieldDef, subOptions);
|
|
572
|
+
fields[key] = this.applyDescription(this.applyCardinality(relSchema, fieldDef).optional(), fieldDef.attributes);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
return fields;
|
|
576
|
+
}
|
|
577
|
+
/**
|
|
578
|
+
* Returns the set of scalar field names that will be present in the
|
|
579
|
+
* resulting schema after applying `options`. Used by `addCustomValidation`
|
|
580
|
+
* to skip `@@validate` rules that reference an absent field.
|
|
581
|
+
*
|
|
582
|
+
* Only scalar fields matter here because `@@validate` conditions are
|
|
583
|
+
* restricted by the ZModel compiler to scalar fields of the same model.
|
|
584
|
+
*/
|
|
585
|
+
buildPresentFields(model, options) {
|
|
586
|
+
const { select, omit } = options;
|
|
587
|
+
const modelDef = this.schema.requireModel(model);
|
|
588
|
+
const fields = /* @__PURE__ */ new Set();
|
|
589
|
+
if (select) for (const key of Object.keys(select)) {
|
|
590
|
+
const fieldDef = modelDef.fields[key];
|
|
591
|
+
if (fieldDef && !fieldDef.relation) fields.add(key);
|
|
592
|
+
}
|
|
593
|
+
else for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
|
|
594
|
+
if (fieldDef.relation) continue;
|
|
595
|
+
if (omit?.[fieldName] === true) continue;
|
|
596
|
+
fields.add(fieldName);
|
|
597
|
+
}
|
|
598
|
+
return fields;
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Build the inner Zod schema for a relation field, optionally with nested
|
|
602
|
+
* query options. Does NOT apply cardinality/optional wrappers — the caller
|
|
603
|
+
* does that.
|
|
604
|
+
*/
|
|
605
|
+
makeRelationFieldSchema(fieldDef, subOptions) {
|
|
606
|
+
const relatedModelName = fieldDef.type;
|
|
607
|
+
if (subOptions) return this.makeModelSchema(relatedModelName, subOptions);
|
|
608
|
+
return zod.default.lazy(() => this.makeModelSchema(relatedModelName));
|
|
609
|
+
}
|
|
610
|
+
makeScalarFieldSchema(fieldDef) {
|
|
611
|
+
const { type, attributes } = fieldDef;
|
|
612
|
+
if (this.schema.getEnum(type)) return this.applyCardinality(this.makeEnumSchema(type), fieldDef);
|
|
613
|
+
if (this.schema.getTypeDef(type)) return this.applyCardinality(this.makeTypeSchema(type), fieldDef);
|
|
614
|
+
let base;
|
|
615
|
+
switch (type) {
|
|
616
|
+
case "String":
|
|
617
|
+
base = addStringValidation(zod.default.string(), attributes);
|
|
618
|
+
break;
|
|
619
|
+
case "Int":
|
|
620
|
+
base = addNumberValidation(zod.default.number().int(), attributes);
|
|
621
|
+
break;
|
|
622
|
+
case "Float":
|
|
623
|
+
base = addNumberValidation(zod.default.number(), attributes);
|
|
624
|
+
break;
|
|
625
|
+
case "Boolean":
|
|
626
|
+
base = zod.default.boolean();
|
|
627
|
+
break;
|
|
628
|
+
case "BigInt":
|
|
629
|
+
base = addBigIntValidation(zod.default.bigint(), attributes);
|
|
630
|
+
break;
|
|
631
|
+
case "Decimal":
|
|
632
|
+
base = zod.default.union([
|
|
633
|
+
addNumberValidation(zod.default.number(), attributes),
|
|
634
|
+
addDecimalValidation(zod.default.string(), attributes, true),
|
|
635
|
+
addDecimalValidation(zod.default.instanceof(decimal_js.default), attributes, true)
|
|
636
|
+
]);
|
|
637
|
+
break;
|
|
638
|
+
case "DateTime":
|
|
639
|
+
base = zod.default.union([zod.default.date(), zod.default.iso.datetime()]);
|
|
640
|
+
break;
|
|
641
|
+
case "Bytes":
|
|
642
|
+
base = zod.default.instanceof(Uint8Array);
|
|
643
|
+
break;
|
|
644
|
+
case "Json":
|
|
645
|
+
base = this.makeJsonSchema();
|
|
646
|
+
break;
|
|
647
|
+
case "Unsupported":
|
|
648
|
+
base = zod.default.unknown();
|
|
649
|
+
break;
|
|
650
|
+
default: throw new SchemaFactoryError(`Unsupported field type: ${type}`);
|
|
651
|
+
}
|
|
652
|
+
return this.applyCardinality(base, fieldDef);
|
|
653
|
+
}
|
|
654
|
+
makeJsonSchema() {
|
|
655
|
+
return zod.default.union([
|
|
656
|
+
zod.default.string(),
|
|
657
|
+
zod.default.number(),
|
|
658
|
+
zod.default.boolean(),
|
|
659
|
+
zod.default.null(),
|
|
660
|
+
zod.default.array(zod.default.lazy(() => this.makeJsonSchema())),
|
|
661
|
+
zod.default.object({}).catchall(zod.default.lazy(() => this.makeJsonSchema()))
|
|
662
|
+
]);
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Wraps a schema with `.optional()` and copies any `description` from its
|
|
666
|
+
* metadata onto the resulting `ZodOptional`, so that callers inspecting
|
|
667
|
+
* `.meta()?.description` on shape fields still find the value after
|
|
668
|
+
* optionality has been applied.
|
|
669
|
+
*/
|
|
670
|
+
wrapOptionalPreservingMeta(schema) {
|
|
671
|
+
const optional = schema.optional();
|
|
672
|
+
const description = schema.meta()?.description;
|
|
673
|
+
if (description) return optional.meta({ description });
|
|
674
|
+
return optional;
|
|
675
|
+
}
|
|
676
|
+
applyCardinality(schema, fieldDef) {
|
|
677
|
+
let result = schema;
|
|
678
|
+
if (fieldDef.array) result = result.array();
|
|
679
|
+
if (fieldDef.optional) result = result.nullable().optional();
|
|
680
|
+
return result;
|
|
681
|
+
}
|
|
682
|
+
makeTypeSchema(type) {
|
|
683
|
+
const typeDef = this.schema.requireTypeDef(type);
|
|
684
|
+
const fields = {};
|
|
685
|
+
for (const [fieldName, fieldDef] of Object.entries(typeDef.fields)) fields[fieldName] = this.applyDescription(this.makeScalarFieldSchema(fieldDef), fieldDef.attributes);
|
|
686
|
+
const shape = zod.default.strictObject(fields);
|
|
687
|
+
return this.applyDescription(addCustomValidation(shape, typeDef.attributes), typeDef.attributes);
|
|
688
|
+
}
|
|
689
|
+
makeEnumSchema(_enum) {
|
|
690
|
+
const enumDef = this.schema.requireEnum(_enum);
|
|
691
|
+
const schema = zod.default.enum(Object.keys(enumDef.values));
|
|
692
|
+
return this.applyDescription(schema, enumDef.attributes);
|
|
693
|
+
}
|
|
694
|
+
getMetaDescription(attributes) {
|
|
695
|
+
if (!attributes) return void 0;
|
|
696
|
+
for (const attr of attributes) {
|
|
697
|
+
if (attr.name !== "@meta" && attr.name !== "@@meta") continue;
|
|
698
|
+
const nameExpr = attr.args?.[0]?.value;
|
|
699
|
+
if (!nameExpr || _zenstackhq_schema.ExpressionUtils.getLiteralValue(nameExpr) !== "description") continue;
|
|
700
|
+
const valueExpr = attr.args?.[1]?.value;
|
|
701
|
+
if (valueExpr) return _zenstackhq_schema.ExpressionUtils.getLiteralValue(valueExpr);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
applyDescription(schema, attributes) {
|
|
705
|
+
const description = this.getMetaDescription(attributes);
|
|
706
|
+
if (description) return schema.meta({ description });
|
|
707
|
+
return schema;
|
|
708
|
+
}
|
|
852
709
|
};
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
710
|
+
//#endregion
|
|
711
|
+
Object.defineProperty(exports, "ZodUtils", {
|
|
712
|
+
enumerable: true,
|
|
713
|
+
get: function() {
|
|
714
|
+
return utils_exports;
|
|
715
|
+
}
|
|
857
716
|
});
|
|
717
|
+
exports.createSchemaFactory = createSchemaFactory;
|
|
718
|
+
|
|
858
719
|
//# sourceMappingURL=index.cjs.map
|