@regle/schemas 0.7.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/LICENSE +21 -0
- package/README.md +28 -0
- package/dist/regle-schemas.cjs +676 -0
- package/dist/regle-schemas.d.cts +411 -0
- package/dist/regle-schemas.d.ts +411 -0
- package/dist/regle-schemas.min.cjs +1 -0
- package/dist/regle-schemas.min.mjs +1 -0
- package/dist/regle-schemas.mjs +648 -0
- package/index.cjs +7 -0
- package/index.js +7 -0
- package/package.json +90 -0
|
@@ -0,0 +1,676 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@regle/core');
|
|
4
|
+
var vue = require('vue');
|
|
5
|
+
var rules = require('@regle/rules');
|
|
6
|
+
var v3 = require('valibot');
|
|
7
|
+
var z = require('zod');
|
|
8
|
+
|
|
9
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
+
|
|
11
|
+
function _interopNamespace(e) {
|
|
12
|
+
if (e && e.__esModule) return e;
|
|
13
|
+
var n = Object.create(null);
|
|
14
|
+
if (e) {
|
|
15
|
+
Object.keys(e).forEach(function (k) {
|
|
16
|
+
if (k !== 'default') {
|
|
17
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
18
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () { return e[k]; }
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
n.default = e;
|
|
26
|
+
return Object.freeze(n);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
var v3__namespace = /*#__PURE__*/_interopNamespace(v3);
|
|
30
|
+
var z__default = /*#__PURE__*/_interopDefault(z);
|
|
31
|
+
|
|
32
|
+
// src/core/useRegleSchema.ts
|
|
33
|
+
|
|
34
|
+
// ../shared/utils/cloneDeep.ts
|
|
35
|
+
function getRegExpFlags(regExp) {
|
|
36
|
+
if (typeof regExp.source.flags == "string") {
|
|
37
|
+
return regExp.source.flags;
|
|
38
|
+
} else {
|
|
39
|
+
let flags = [];
|
|
40
|
+
regExp.global && flags.push("g");
|
|
41
|
+
regExp.ignoreCase && flags.push("i");
|
|
42
|
+
regExp.multiline && flags.push("m");
|
|
43
|
+
regExp.sticky && flags.push("y");
|
|
44
|
+
regExp.unicode && flags.push("u");
|
|
45
|
+
return flags.join("");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function cloneDeep(obj) {
|
|
49
|
+
let result = obj;
|
|
50
|
+
let type = {}.toString.call(obj).slice(8, -1);
|
|
51
|
+
if (type == "Set") {
|
|
52
|
+
result = new Set([...obj].map((value) => cloneDeep(value)));
|
|
53
|
+
}
|
|
54
|
+
if (type == "Map") {
|
|
55
|
+
result = new Map([...obj].map((kv) => [cloneDeep(kv[0]), cloneDeep(kv[1])]));
|
|
56
|
+
}
|
|
57
|
+
if (type == "Date") {
|
|
58
|
+
result = new Date(obj.getTime());
|
|
59
|
+
}
|
|
60
|
+
if (type == "RegExp") {
|
|
61
|
+
result = RegExp(obj.source, getRegExpFlags(obj));
|
|
62
|
+
}
|
|
63
|
+
if (type == "Array" || type == "Object") {
|
|
64
|
+
result = Array.isArray(obj) ? [] : {};
|
|
65
|
+
for (let key in obj) {
|
|
66
|
+
result[key] = cloneDeep(obj[key]);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// ../shared/utils/object.utils.ts
|
|
73
|
+
function isObject(obj) {
|
|
74
|
+
if (obj instanceof Date || obj instanceof File) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
return typeof obj === "object" && obj !== null && !Array.isArray(obj);
|
|
78
|
+
}
|
|
79
|
+
function setObjectError(obj, propsArg, value, isArray) {
|
|
80
|
+
var props, lastProp;
|
|
81
|
+
if (Array.isArray(propsArg)) {
|
|
82
|
+
props = propsArg.slice(0);
|
|
83
|
+
}
|
|
84
|
+
if (typeof propsArg == "string") {
|
|
85
|
+
props = propsArg.split(".");
|
|
86
|
+
}
|
|
87
|
+
if (typeof propsArg == "symbol") {
|
|
88
|
+
props = [propsArg];
|
|
89
|
+
}
|
|
90
|
+
if (!Array.isArray(props)) {
|
|
91
|
+
throw new Error("props arg must be an array, a string or a symbol");
|
|
92
|
+
}
|
|
93
|
+
lastProp = props.pop();
|
|
94
|
+
if (!lastProp) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
prototypeCheck(lastProp);
|
|
98
|
+
var thisProp;
|
|
99
|
+
while (thisProp = props.shift()) {
|
|
100
|
+
prototypeCheck(thisProp);
|
|
101
|
+
if (!isNaN(parseInt(thisProp))) {
|
|
102
|
+
(obj.$each ??= [])[thisProp] = {};
|
|
103
|
+
obj = obj.$each[thisProp];
|
|
104
|
+
} else {
|
|
105
|
+
if (typeof obj[thisProp] == "undefined") {
|
|
106
|
+
obj[thisProp] = {};
|
|
107
|
+
}
|
|
108
|
+
obj = obj[thisProp];
|
|
109
|
+
}
|
|
110
|
+
if (!obj || typeof obj != "object") {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (isArray) {
|
|
115
|
+
obj[lastProp] = { ...obj[lastProp], $self: value };
|
|
116
|
+
} else {
|
|
117
|
+
obj[lastProp] = value;
|
|
118
|
+
}
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
function prototypeCheck(prop) {
|
|
122
|
+
if (prop == "__proto__" || prop == "constructor" || prop == "prototype") {
|
|
123
|
+
throw new Error("setting of prototype values not supported");
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function transformValibotAdapter(schema) {
|
|
127
|
+
const isAsync = schema.async;
|
|
128
|
+
const validatorFn = (value) => {
|
|
129
|
+
const result = trySafeTransform(schema, value);
|
|
130
|
+
if (result instanceof Promise) {
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
if (result.success) {
|
|
134
|
+
return {
|
|
135
|
+
$valid: true,
|
|
136
|
+
$issues: []
|
|
137
|
+
};
|
|
138
|
+
} else {
|
|
139
|
+
return {
|
|
140
|
+
$valid: false,
|
|
141
|
+
$issues: result.issues
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
if ("__depsArray" in schema && Array.isArray(schema.__depsArray) && schema.__depsArray.length) {
|
|
146
|
+
return isAsync ? rules.withAsync(validatorFn, schema.__depsArray) : rules.withParams(validatorFn, schema.__depsArray);
|
|
147
|
+
}
|
|
148
|
+
return isAsync ? rules.withAsync(validatorFn) : validatorFn;
|
|
149
|
+
}
|
|
150
|
+
function trySafeTransform(schema, value) {
|
|
151
|
+
try {
|
|
152
|
+
if (schema.async) {
|
|
153
|
+
return new Promise(async (resolve) => {
|
|
154
|
+
const result = await v3__namespace.safeParseAsync(schema, value);
|
|
155
|
+
if (result.success) {
|
|
156
|
+
resolve({
|
|
157
|
+
$valid: true,
|
|
158
|
+
$issues: []
|
|
159
|
+
});
|
|
160
|
+
} else {
|
|
161
|
+
resolve({
|
|
162
|
+
$valid: false,
|
|
163
|
+
$issues: result.issues
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
} else {
|
|
168
|
+
const result = v3__namespace.safeParse(schema, value);
|
|
169
|
+
return result;
|
|
170
|
+
}
|
|
171
|
+
} catch (e) {
|
|
172
|
+
return {};
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// src/core/converters/valibot/guards.ts
|
|
177
|
+
function isIntersectSchema(schema) {
|
|
178
|
+
return schema.type === "intersect";
|
|
179
|
+
}
|
|
180
|
+
function isObjectSchema(schema) {
|
|
181
|
+
return schema.type === "object";
|
|
182
|
+
}
|
|
183
|
+
function isWrappedType(schema) {
|
|
184
|
+
return "wrapped" in schema;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// src/core/converters/valibot/validators/valibotObjectToRegle.ts
|
|
188
|
+
function getNestedValibotObjectSchema(schema) {
|
|
189
|
+
if (isIntersectSchema(schema)) {
|
|
190
|
+
const allEntries = schema.options.reduce(
|
|
191
|
+
(acc, value) => {
|
|
192
|
+
const nestedSchema = getNestedValibotObjectSchema(value);
|
|
193
|
+
if (nestedSchema) {
|
|
194
|
+
acc = {
|
|
195
|
+
...acc,
|
|
196
|
+
...nestedSchema.entries
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
return acc;
|
|
200
|
+
},
|
|
201
|
+
{}
|
|
202
|
+
);
|
|
203
|
+
return v3__namespace.object({ ...allEntries });
|
|
204
|
+
} else if (isObjectSchema(schema)) {
|
|
205
|
+
return schema;
|
|
206
|
+
} else if (isWrappedType(schema)) {
|
|
207
|
+
return getNestedValibotObjectSchema(schema.wrapped);
|
|
208
|
+
} else {
|
|
209
|
+
return undefined;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
function valibotObjectToRegle(schema, state) {
|
|
213
|
+
const nestedSchema = getNestedValibotObjectSchema(schema);
|
|
214
|
+
if (nestedSchema) {
|
|
215
|
+
return Object.fromEntries(
|
|
216
|
+
Object.entries(nestedSchema.entries).map(([key, shape]) => {
|
|
217
|
+
if (typeof shape === "object") {
|
|
218
|
+
const childState = vue.toRef(isObject(state.value) ? state.value : {}, key);
|
|
219
|
+
return [key, processValibotTypeDef(shape, childState)];
|
|
220
|
+
}
|
|
221
|
+
return [key, {}];
|
|
222
|
+
})
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
return {};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// src/core/converters/valibot/validators/valibotVariantToRegle.ts
|
|
229
|
+
function isLiteralSchema(schema) {
|
|
230
|
+
return schema?.type === "literal";
|
|
231
|
+
}
|
|
232
|
+
function valibotVariantToRegle(schema, state) {
|
|
233
|
+
const scope = vue.effectScope();
|
|
234
|
+
const scopeState = scope.run(() => {
|
|
235
|
+
const valibotRule = vue.ref({});
|
|
236
|
+
vue.watch(
|
|
237
|
+
state,
|
|
238
|
+
() => {
|
|
239
|
+
if (isObject(state.value) && state.value[schema.key]) {
|
|
240
|
+
const selectedDiscriminant = schema.options.find((o) => {
|
|
241
|
+
const nestedSchema = getNestedValibotObjectSchema(o);
|
|
242
|
+
const schemaVariantKey = nestedSchema?.entries[schema.key];
|
|
243
|
+
if (isLiteralSchema(schemaVariantKey) && isObject(state.value)) {
|
|
244
|
+
return schemaVariantKey.literal === state.value[schema.key];
|
|
245
|
+
}
|
|
246
|
+
return false;
|
|
247
|
+
});
|
|
248
|
+
if (selectedDiscriminant && isObjectSchema(selectedDiscriminant)) {
|
|
249
|
+
valibotRule.value = Object.fromEntries(
|
|
250
|
+
Object.entries(selectedDiscriminant.entries).map(([key, shape]) => {
|
|
251
|
+
return [key, processValibotTypeDef(shape, vue.toRef(isObject(state.value) ? state.value : {}, key))];
|
|
252
|
+
})
|
|
253
|
+
);
|
|
254
|
+
} else {
|
|
255
|
+
valibotRule.value = {};
|
|
256
|
+
}
|
|
257
|
+
} else {
|
|
258
|
+
valibotRule.value = {
|
|
259
|
+
[schema.key]: { required: rules.required }
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
{ deep: true, flush: "pre", immediate: true }
|
|
264
|
+
);
|
|
265
|
+
vue.onScopeDispose(() => {
|
|
266
|
+
scope.stop();
|
|
267
|
+
});
|
|
268
|
+
return { valibotRule };
|
|
269
|
+
});
|
|
270
|
+
return scopeState;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// src/core/converters/extractIssuesMessages.ts
|
|
274
|
+
function extractIssuesMessages() {
|
|
275
|
+
return (metadata) => {
|
|
276
|
+
const issueMessages = metadata.$issues?.map((issue) => issue.message);
|
|
277
|
+
return issueMessages.length ? issueMessages : "Error";
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// src/core/converters/valibot/processValibotTypeDef.ts
|
|
282
|
+
function isArraySchema(schema) {
|
|
283
|
+
return schema.type === "array";
|
|
284
|
+
}
|
|
285
|
+
function isObjectSchema2(schema) {
|
|
286
|
+
return schema.type === "object";
|
|
287
|
+
}
|
|
288
|
+
function isVariantSchema(schema) {
|
|
289
|
+
return schema.type === "variant";
|
|
290
|
+
}
|
|
291
|
+
function getNestedInnerType(schema) {
|
|
292
|
+
if (isWrappedType(schema)) {
|
|
293
|
+
return getNestedInnerType(schema.wrapped);
|
|
294
|
+
} else {
|
|
295
|
+
return schema;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
function processValibotTypeDef(schema, state) {
|
|
299
|
+
const nestedSchema = getNestedInnerType(schema);
|
|
300
|
+
if (isArraySchema(nestedSchema)) {
|
|
301
|
+
return valibotArrayToRegle(nestedSchema, state);
|
|
302
|
+
} else if (isObjectSchema2(nestedSchema)) {
|
|
303
|
+
return valibotObjectToRegle(nestedSchema, state);
|
|
304
|
+
} else if (isVariantSchema(nestedSchema)) {
|
|
305
|
+
const valibotRule = valibotVariantToRegle(nestedSchema, state);
|
|
306
|
+
return valibotRule.valibotRule;
|
|
307
|
+
} else {
|
|
308
|
+
return {
|
|
309
|
+
[nestedSchema.type]: rules.withMessage(transformValibotAdapter(nestedSchema), extractIssuesMessages())
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// src/core/converters/valibot/validators/valibotArrayToRegle.ts
|
|
315
|
+
function valibotArrayToRegle(schema, state) {
|
|
316
|
+
let filteredSelfSchema = {};
|
|
317
|
+
if ("pipe" in schema) {
|
|
318
|
+
schema.pipe.filter((f) => f.kind === "validation").forEach((validation) => {
|
|
319
|
+
filteredSelfSchema[validation.type] = rules.withMessage(
|
|
320
|
+
transformValibotAdapter(v3__namespace.pipe(v3__namespace.array(v3__namespace.any()), validation)),
|
|
321
|
+
extractIssuesMessages()
|
|
322
|
+
);
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
return {
|
|
326
|
+
$each: processValibotTypeDef(schema.item, state),
|
|
327
|
+
...filteredSelfSchema
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
function transformZodValidatorAdapter(schema) {
|
|
331
|
+
const isAsync = hasAsyncRefinement(schema);
|
|
332
|
+
const validatorFn = (value) => {
|
|
333
|
+
const result = trySafeTransform2(schema, value);
|
|
334
|
+
if (result instanceof Promise) {
|
|
335
|
+
return result;
|
|
336
|
+
}
|
|
337
|
+
if (result.success) {
|
|
338
|
+
return {
|
|
339
|
+
$valid: true,
|
|
340
|
+
$issues: []
|
|
341
|
+
};
|
|
342
|
+
} else {
|
|
343
|
+
return {
|
|
344
|
+
$valid: false,
|
|
345
|
+
$issues: result.error.issues
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
if ("__depsArray" in schema && Array.isArray(schema.__depsArray) && schema.__depsArray.length) {
|
|
350
|
+
return isAsync ? rules.withAsync(validatorFn, schema.__depsArray) : rules.withParams(validatorFn, schema.__depsArray);
|
|
351
|
+
}
|
|
352
|
+
return isAsync ? rules.withAsync(validatorFn) : validatorFn;
|
|
353
|
+
}
|
|
354
|
+
function trySafeTransform2(schema, value) {
|
|
355
|
+
try {
|
|
356
|
+
const result = schema.safeParse(value);
|
|
357
|
+
return result;
|
|
358
|
+
} catch (e) {
|
|
359
|
+
try {
|
|
360
|
+
return new Promise(async (resolve) => {
|
|
361
|
+
const result = await schema.safeParseAsync(value);
|
|
362
|
+
if (result.success) {
|
|
363
|
+
resolve({
|
|
364
|
+
$valid: true,
|
|
365
|
+
$issues: []
|
|
366
|
+
});
|
|
367
|
+
} else {
|
|
368
|
+
resolve({
|
|
369
|
+
$valid: false,
|
|
370
|
+
$issues: result.error.issues
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
} catch (e2) {
|
|
375
|
+
return {};
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
function isAsyncFunctionOrPromiseReturning(fn) {
|
|
380
|
+
if (typeof fn !== "function") return false;
|
|
381
|
+
if (fn.constructor.name === "AsyncFunction") {
|
|
382
|
+
return true;
|
|
383
|
+
}
|
|
384
|
+
try {
|
|
385
|
+
const result = fn();
|
|
386
|
+
return result instanceof Promise;
|
|
387
|
+
} catch {
|
|
388
|
+
return false;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
function hasAsyncRefinement(schema) {
|
|
392
|
+
if (schema?._def) {
|
|
393
|
+
if (schema._def.typeName === "ZodEffects") {
|
|
394
|
+
const effect = schema._def.effect;
|
|
395
|
+
return isAsyncFunctionOrPromiseReturning(effect.refinement || effect.transform);
|
|
396
|
+
}
|
|
397
|
+
if (schema._def.typeName === "ZodObject") {
|
|
398
|
+
return Object.values(schema._def.shape()).some((schema2) => hasAsyncRefinement(schema2));
|
|
399
|
+
}
|
|
400
|
+
if (schema._def.typeName === "ZodUnion" || schema._def.typeName === "ZodIntersection") {
|
|
401
|
+
return schema._def.options.some(hasAsyncRefinement);
|
|
402
|
+
}
|
|
403
|
+
if (schema._def.typeName === "ZodArray") {
|
|
404
|
+
return hasAsyncRefinement(schema._def.type);
|
|
405
|
+
}
|
|
406
|
+
if (schema._def.typeName === "ZodOptional" || schema._def.typeName === "ZodNullable") {
|
|
407
|
+
return hasAsyncRefinement(schema._def.innerType);
|
|
408
|
+
}
|
|
409
|
+
if (schema._def.typeName === "ZodTuple") {
|
|
410
|
+
return schema._def.items.some(hasAsyncRefinement);
|
|
411
|
+
}
|
|
412
|
+
if (schema._def.typeName === "ZodString" || schema._def.typeName === "ZodNumber" || schema._def.typeName === "ZodDate") {
|
|
413
|
+
return schema._def.checks?.some((check) => isAsyncFunctionOrPromiseReturning(check.refinement));
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return false;
|
|
417
|
+
}
|
|
418
|
+
function zodDiscriminatedUnionToRegle(schema, state) {
|
|
419
|
+
const scope = vue.effectScope();
|
|
420
|
+
const scopeState = scope.run(() => {
|
|
421
|
+
const zodRule = vue.ref({});
|
|
422
|
+
vue.watch(
|
|
423
|
+
state,
|
|
424
|
+
() => {
|
|
425
|
+
if (isObject(state.value) && state.value[schema._def.discriminator]) {
|
|
426
|
+
const selectedDiscriminant = schema._def.optionsMap.get(state.value[schema._def.discriminator]);
|
|
427
|
+
if (selectedDiscriminant) {
|
|
428
|
+
zodRule.value = Object.fromEntries(
|
|
429
|
+
Object.entries(selectedDiscriminant._def.shape()).map(([key, shape]) => {
|
|
430
|
+
if (typeof shape === "object" && "_def" in shape) {
|
|
431
|
+
return [key, processZodTypeDef(shape, vue.toRef(isObject(state.value) ? state.value : {}, key))];
|
|
432
|
+
}
|
|
433
|
+
return [key, {}];
|
|
434
|
+
})
|
|
435
|
+
);
|
|
436
|
+
} else {
|
|
437
|
+
zodRule.value = {};
|
|
438
|
+
}
|
|
439
|
+
} else {
|
|
440
|
+
zodRule.value = {
|
|
441
|
+
[schema._def.discriminator]: { required: rules.required }
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
},
|
|
445
|
+
{ deep: true, flush: "pre", immediate: true }
|
|
446
|
+
);
|
|
447
|
+
vue.onScopeDispose(() => {
|
|
448
|
+
scope.stop();
|
|
449
|
+
});
|
|
450
|
+
return { zodRule };
|
|
451
|
+
});
|
|
452
|
+
return scopeState;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// src/core/converters/zod/processZodTypeDef.ts
|
|
456
|
+
var typesWithInnerTypes = [
|
|
457
|
+
z__default.default.ZodFirstPartyTypeKind.ZodDefault,
|
|
458
|
+
z__default.default.ZodFirstPartyTypeKind.ZodCatch,
|
|
459
|
+
z__default.default.ZodFirstPartyTypeKind.ZodNullable,
|
|
460
|
+
z__default.default.ZodFirstPartyTypeKind.ZodOptional,
|
|
461
|
+
z__default.default.ZodFirstPartyTypeKind.ZodReadonly
|
|
462
|
+
];
|
|
463
|
+
function isDefWithInnerType(schema) {
|
|
464
|
+
if (schema._def && typeof schema._def === "object" && "typeName" in schema._def) {
|
|
465
|
+
return typesWithInnerTypes.includes(schema._def.typeName);
|
|
466
|
+
}
|
|
467
|
+
return false;
|
|
468
|
+
}
|
|
469
|
+
function isDefWithInnerSchema(schema) {
|
|
470
|
+
if (schema._def && typeof schema._def === "object" && "typeName" in schema._def) {
|
|
471
|
+
return "schema" in schema._def;
|
|
472
|
+
}
|
|
473
|
+
return false;
|
|
474
|
+
}
|
|
475
|
+
function getNestedInnerType2(schema) {
|
|
476
|
+
if (schema?._def && typeof schema._def === "object" && "typeName" in schema._def) {
|
|
477
|
+
if (isDefWithInnerType(schema)) {
|
|
478
|
+
return getNestedInnerType2(schema._def.innerType);
|
|
479
|
+
} else if (isDefWithInnerSchema(schema)) {
|
|
480
|
+
return getNestedInnerType2(schema._def.schema);
|
|
481
|
+
} else {
|
|
482
|
+
return schema;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return undefined;
|
|
486
|
+
}
|
|
487
|
+
function processZodTypeDef(schema, state) {
|
|
488
|
+
const schemaDef = getNestedInnerType2(schema);
|
|
489
|
+
if (schemaDef?._def && "typeName" in schemaDef._def) {
|
|
490
|
+
if (schemaDef._def.typeName === z__default.default.ZodFirstPartyTypeKind.ZodArray || schemaDef._def.typeName === z__default.default.ZodFirstPartyTypeKind.ZodTuple) {
|
|
491
|
+
return zodArrayToRegle(schema, state);
|
|
492
|
+
} else if (schemaDef._def.typeName === z__default.default.ZodFirstPartyTypeKind.ZodObject || schemaDef._def.typeName === z__default.default.ZodFirstPartyTypeKind.ZodIntersection) {
|
|
493
|
+
return zodObjectToRegle(schemaDef, state);
|
|
494
|
+
} else if (schemaDef._def.typeName === z__default.default.ZodFirstPartyTypeKind.ZodDiscriminatedUnion) {
|
|
495
|
+
const zodRule = zodDiscriminatedUnionToRegle(schemaDef, state);
|
|
496
|
+
return zodRule.zodRule;
|
|
497
|
+
} else {
|
|
498
|
+
return {
|
|
499
|
+
[schemaDef.constructor.name]: rules.withMessage(
|
|
500
|
+
transformZodValidatorAdapter(schema),
|
|
501
|
+
extractIssuesMessages()
|
|
502
|
+
)
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
return {};
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// src/core/converters/zod/validators/zodArrayToRegle.ts
|
|
510
|
+
function zodArrayToRegle(schema, state) {
|
|
511
|
+
const arrayValidators = schema._def.typeName === z.z.ZodFirstPartyTypeKind.ZodArray ? {
|
|
512
|
+
...!!schema._def.minLength && { minLength: rules.minLength(schema._def.minLength?.value) },
|
|
513
|
+
...!!schema._def.maxLength && { maxLength: rules.maxLength(schema._def.maxLength?.value) },
|
|
514
|
+
...!!schema._def.exactLength && { exactLength: rules.exactLength(schema._def.exactLength?.value) }
|
|
515
|
+
} : {};
|
|
516
|
+
const items = schema._def.typeName === z.z.ZodFirstPartyTypeKind.ZodArray ? schema._def.type : schema._def.items;
|
|
517
|
+
return {
|
|
518
|
+
$each: processZodTypeDef(items, state),
|
|
519
|
+
...arrayValidators
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
function getNestedZodObjectSchema(schema) {
|
|
523
|
+
if (schema._def && "typeName" in schema._def) {
|
|
524
|
+
if (schema._def.typeName === z.z.ZodFirstPartyTypeKind.ZodEffects) {
|
|
525
|
+
return getNestedZodObjectSchema(schema._def.schema);
|
|
526
|
+
} else if (schema._def.typeName === z.z.ZodFirstPartyTypeKind.ZodIntersection) {
|
|
527
|
+
const leftObject = getNestedZodObjectSchema(schema._def.left);
|
|
528
|
+
const rightObject = getNestedZodObjectSchema(schema._def.right);
|
|
529
|
+
if (leftObject && rightObject) {
|
|
530
|
+
return getNestedZodObjectSchema(leftObject.merge(rightObject));
|
|
531
|
+
}
|
|
532
|
+
return undefined;
|
|
533
|
+
} else {
|
|
534
|
+
return schema;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
return undefined;
|
|
538
|
+
}
|
|
539
|
+
function zodObjectToRegle(schema, state) {
|
|
540
|
+
if (schema && "_def" in schema && "typeName" in schema._def) {
|
|
541
|
+
const nestedSchema = getNestedZodObjectSchema(schema);
|
|
542
|
+
if (nestedSchema?._def?.typeName === z.z.ZodFirstPartyTypeKind.ZodObject) {
|
|
543
|
+
return Object.fromEntries(
|
|
544
|
+
Object.entries(nestedSchema._def.shape()).map(([key, shape]) => {
|
|
545
|
+
if (shape && typeof shape === "object" && "_def" in shape) {
|
|
546
|
+
const childState = vue.toRef(isObject(state.value) ? state.value : {}, key);
|
|
547
|
+
return [key, processZodTypeDef(shape, childState)];
|
|
548
|
+
}
|
|
549
|
+
return [key, {}];
|
|
550
|
+
})
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
return {};
|
|
554
|
+
}
|
|
555
|
+
return {};
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
// src/core/useRegleSchema.ts
|
|
559
|
+
function createUseRegleSchemaComposable(options, shortcuts) {
|
|
560
|
+
const globalOptions = {
|
|
561
|
+
autoDirty: options?.autoDirty,
|
|
562
|
+
lazy: options?.lazy,
|
|
563
|
+
rewardEarly: options?.rewardEarly,
|
|
564
|
+
clearExternalErrorsOnChange: options?.clearExternalErrorsOnChange
|
|
565
|
+
};
|
|
566
|
+
function useRegleSchema2(state, schema, options2) {
|
|
567
|
+
const convertedRules = vue.ref({});
|
|
568
|
+
const computedSchema = vue.computed(() => vue.unref(schema));
|
|
569
|
+
const { mode = "rules", ...regleOptions } = options2 ?? {};
|
|
570
|
+
const resolvedOptions = {
|
|
571
|
+
...globalOptions,
|
|
572
|
+
...regleOptions
|
|
573
|
+
};
|
|
574
|
+
const processedState = vue.isRef(state) ? state : vue.ref(state);
|
|
575
|
+
const initialState = vue.ref({ ...cloneDeep(processedState.value) });
|
|
576
|
+
const customErrors = vue.ref({});
|
|
577
|
+
let onValidate = undefined;
|
|
578
|
+
if (mode === "rules") {
|
|
579
|
+
vue.watch(
|
|
580
|
+
computedSchema,
|
|
581
|
+
() => {
|
|
582
|
+
if (computedSchema.value && typeof computedSchema.value === "object") {
|
|
583
|
+
if (computedSchema.value["~standard"].vendor === "zod") {
|
|
584
|
+
convertedRules.value = vue.reactive(zodObjectToRegle(computedSchema.value, processedState));
|
|
585
|
+
} else if (computedSchema.value["~standard"].vendor === "valibot") {
|
|
586
|
+
convertedRules.value = vue.reactive(valibotObjectToRegle(computedSchema.value, processedState));
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
},
|
|
590
|
+
{ deep: true, immediate: true, flush: "post" }
|
|
591
|
+
);
|
|
592
|
+
} else {
|
|
593
|
+
let issuesToRegleErrors2 = function(result) {
|
|
594
|
+
const output = {};
|
|
595
|
+
if (result.issues) {
|
|
596
|
+
const errors = result.issues.map((issue) => {
|
|
597
|
+
const path = issue.path?.map((item) => typeof item === "object" ? item.key : item).join(".");
|
|
598
|
+
const lastItem = issue.path?.[issue.path.length - 1];
|
|
599
|
+
const isArray = (typeof lastItem === "object" && "value" in lastItem ? Array.isArray(lastItem.value) : false) || ("type" in issue ? issue.type === "array" : false);
|
|
600
|
+
return {
|
|
601
|
+
path,
|
|
602
|
+
message: issue.message,
|
|
603
|
+
isArray
|
|
604
|
+
};
|
|
605
|
+
});
|
|
606
|
+
errors.forEach((error) => {
|
|
607
|
+
setObjectError(output, error.path, [error.message], error.isArray);
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
return output;
|
|
611
|
+
};
|
|
612
|
+
const emptySkeleton = computedSchema.value["~standard"].validate(initialState.value);
|
|
613
|
+
customErrors.value = issuesToRegleErrors2(emptySkeleton);
|
|
614
|
+
vue.watch(
|
|
615
|
+
[processedState, computedSchema],
|
|
616
|
+
async () => {
|
|
617
|
+
const result = await computedSchema.value["~standard"].validate(processedState.value);
|
|
618
|
+
customErrors.value = issuesToRegleErrors2(result);
|
|
619
|
+
},
|
|
620
|
+
{ deep: true, immediate: true, flush: "post" }
|
|
621
|
+
);
|
|
622
|
+
onValidate = async () => {
|
|
623
|
+
try {
|
|
624
|
+
const result = await computedSchema.value["~standard"].validate(processedState.value);
|
|
625
|
+
customErrors.value = issuesToRegleErrors2(result);
|
|
626
|
+
return {
|
|
627
|
+
result: !result.issues?.length,
|
|
628
|
+
data: processedState.value
|
|
629
|
+
};
|
|
630
|
+
} catch (e) {
|
|
631
|
+
return Promise.reject(e);
|
|
632
|
+
}
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
const regle = core.useRootStorage({
|
|
636
|
+
scopeRules: convertedRules,
|
|
637
|
+
state: processedState,
|
|
638
|
+
options: resolvedOptions,
|
|
639
|
+
schemaErrors: customErrors,
|
|
640
|
+
initialState,
|
|
641
|
+
shortcuts,
|
|
642
|
+
schemaMode: mode === "schema",
|
|
643
|
+
onValidate
|
|
644
|
+
});
|
|
645
|
+
return {
|
|
646
|
+
r$: regle.regle
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
return useRegleSchema2;
|
|
650
|
+
}
|
|
651
|
+
var useRegleSchema = createUseRegleSchemaComposable();
|
|
652
|
+
function withDeps(schema, depsArray) {
|
|
653
|
+
return { ...schema, __depsArray: depsArray };
|
|
654
|
+
}
|
|
655
|
+
function createInferValibotSchemaHelper() {
|
|
656
|
+
function inferRules(state, rulesFactory) {
|
|
657
|
+
return rulesFactory;
|
|
658
|
+
}
|
|
659
|
+
return inferRules;
|
|
660
|
+
}
|
|
661
|
+
var inferSchema = createInferValibotSchemaHelper();
|
|
662
|
+
|
|
663
|
+
// src/core/defineRegleSchemaConfig.ts
|
|
664
|
+
function defineRegleSchemaConfig({
|
|
665
|
+
modifiers,
|
|
666
|
+
shortcuts
|
|
667
|
+
}) {
|
|
668
|
+
const useRegleSchema2 = createUseRegleSchemaComposable(modifiers, shortcuts);
|
|
669
|
+
const inferSchema2 = createInferValibotSchemaHelper();
|
|
670
|
+
return { useRegleSchema: useRegleSchema2, inferSchema: inferSchema2 };
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
exports.defineRegleSchemaConfig = defineRegleSchemaConfig;
|
|
674
|
+
exports.inferSchema = inferSchema;
|
|
675
|
+
exports.useRegleSchema = useRegleSchema;
|
|
676
|
+
exports.withDeps = withDeps;
|