@regle/schemas 0.7.5 → 0.8.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/regle-schemas.cjs +74 -513
- package/dist/regle-schemas.d.cts +26 -94
- package/dist/regle-schemas.d.ts +26 -94
- package/dist/regle-schemas.min.cjs +1 -1
- package/dist/regle-schemas.min.mjs +1 -1
- package/dist/regle-schemas.mjs +75 -491
- package/package.json +17 -7
package/dist/regle-schemas.cjs
CHANGED
|
@@ -2,32 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
var core = require('@regle/core');
|
|
4
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
5
|
|
|
32
6
|
// src/core/useRegleSchema.ts
|
|
33
7
|
|
|
@@ -70,12 +44,6 @@ function cloneDeep(obj) {
|
|
|
70
44
|
}
|
|
71
45
|
|
|
72
46
|
// ../shared/utils/object.utils.ts
|
|
73
|
-
function isObject(obj) {
|
|
74
|
-
if (obj && (obj instanceof Date || obj.constructor.name == "File" || obj.constructor.name == "FileList")) {
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
return typeof obj === "object" && obj !== null && !Array.isArray(obj);
|
|
78
|
-
}
|
|
79
47
|
function setObjectError(obj, propsArg, value, isArray) {
|
|
80
48
|
var props, lastProp;
|
|
81
49
|
if (Array.isArray(propsArg)) {
|
|
@@ -112,447 +80,56 @@ function setObjectError(obj, propsArg, value, isArray) {
|
|
|
112
80
|
}
|
|
113
81
|
}
|
|
114
82
|
if (isArray) {
|
|
115
|
-
obj[lastProp]
|
|
116
|
-
|
|
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
|
-
};
|
|
83
|
+
if (!obj[lastProp]) {
|
|
84
|
+
obj[lastProp] = { ...obj[lastProp], $self: value };
|
|
138
85
|
} else {
|
|
139
|
-
|
|
140
|
-
$valid: false,
|
|
141
|
-
$issues: result.issues
|
|
142
|
-
};
|
|
86
|
+
obj[lastProp].$self = (obj[lastProp].$self ??= []).concat(value);
|
|
143
87
|
}
|
|
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
88
|
} else {
|
|
295
|
-
|
|
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
|
-
};
|
|
89
|
+
if (Array.isArray(obj[lastProp])) {
|
|
90
|
+
obj[lastProp] = obj[lastProp].concat(value);
|
|
342
91
|
} else {
|
|
343
|
-
|
|
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 {};
|
|
92
|
+
obj[lastProp] = value;
|
|
376
93
|
}
|
|
377
94
|
}
|
|
95
|
+
return true;
|
|
378
96
|
}
|
|
379
|
-
function
|
|
380
|
-
if (
|
|
381
|
-
|
|
382
|
-
return true;
|
|
97
|
+
function getDotPath(obj, propsArg, defaultValue) {
|
|
98
|
+
if (!obj) {
|
|
99
|
+
return defaultValue;
|
|
383
100
|
}
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
} catch {
|
|
388
|
-
return false;
|
|
101
|
+
var props, prop;
|
|
102
|
+
if (Array.isArray(propsArg)) {
|
|
103
|
+
props = propsArg.slice(0);
|
|
389
104
|
}
|
|
390
|
-
|
|
391
|
-
|
|
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
|
-
}
|
|
105
|
+
if (typeof propsArg == "string") {
|
|
106
|
+
props = propsArg.split(".");
|
|
415
107
|
}
|
|
416
|
-
|
|
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);
|
|
108
|
+
if (typeof propsArg == "symbol") {
|
|
109
|
+
props = [propsArg];
|
|
466
110
|
}
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
function isDefWithInnerSchema(schema) {
|
|
470
|
-
if (schema._def && typeof schema._def === "object" && "typeName" in schema._def) {
|
|
471
|
-
return "schema" in schema._def;
|
|
111
|
+
if (!Array.isArray(props)) {
|
|
112
|
+
throw new Error("props arg must be an array, a string or a symbol");
|
|
472
113
|
}
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
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;
|
|
114
|
+
while (props.length) {
|
|
115
|
+
prop = props.shift();
|
|
116
|
+
if (!obj) {
|
|
117
|
+
return defaultValue;
|
|
483
118
|
}
|
|
484
|
-
|
|
485
|
-
|
|
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
|
-
};
|
|
119
|
+
if (!prop) {
|
|
120
|
+
return defaultValue;
|
|
504
121
|
}
|
|
505
|
-
|
|
506
|
-
|
|
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;
|
|
122
|
+
obj = obj[prop];
|
|
123
|
+
if (obj === undefined) {
|
|
124
|
+
return defaultValue;
|
|
535
125
|
}
|
|
536
126
|
}
|
|
537
|
-
return
|
|
127
|
+
return obj;
|
|
538
128
|
}
|
|
539
|
-
function
|
|
540
|
-
if (
|
|
541
|
-
|
|
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 {};
|
|
129
|
+
function prototypeCheck(prop) {
|
|
130
|
+
if (prop == "__proto__" || prop == "constructor" || prop == "prototype") {
|
|
131
|
+
throw new Error("setting of prototype values not supported");
|
|
554
132
|
}
|
|
555
|
-
return {};
|
|
556
133
|
}
|
|
557
134
|
|
|
558
135
|
// src/core/useRegleSchema.ts
|
|
@@ -566,72 +143,56 @@ function createUseRegleSchemaComposable(options, shortcuts) {
|
|
|
566
143
|
function useRegleSchema2(state, schema, options2) {
|
|
567
144
|
const convertedRules = vue.ref({});
|
|
568
145
|
const computedSchema = vue.computed(() => vue.unref(schema));
|
|
569
|
-
const { mode = "rules", ...regleOptions } = options2 ?? {};
|
|
570
146
|
const resolvedOptions = {
|
|
571
147
|
...globalOptions,
|
|
572
|
-
...
|
|
148
|
+
...options2
|
|
573
149
|
};
|
|
574
150
|
const processedState = vue.isRef(state) ? state : vue.ref(state);
|
|
575
151
|
const initialState = vue.ref({ ...cloneDeep(processedState.value) });
|
|
576
152
|
const customErrors = vue.ref({});
|
|
577
153
|
let onValidate = undefined;
|
|
578
|
-
if (
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
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);
|
|
154
|
+
if (!computedSchema.value?.["~standard"]) {
|
|
155
|
+
throw new Error(`Only "standard-schema" compatible libraries are supported`);
|
|
156
|
+
}
|
|
157
|
+
function issuesToRegleErrors(result) {
|
|
158
|
+
const output = {};
|
|
159
|
+
if (result.issues) {
|
|
160
|
+
const errors = result.issues.map((issue) => {
|
|
161
|
+
const path = issue.path?.map((item) => typeof item === "object" ? item.key : item.toString()).join(".") ?? "";
|
|
162
|
+
const lastItem = issue.path?.[issue.path.length - 1];
|
|
163
|
+
const isArray = (typeof lastItem === "object" && "value" in lastItem ? Array.isArray(lastItem.value) : false) || ("type" in issue ? issue.type === "array" : false) || Array.isArray(getDotPath(processedState.value, path));
|
|
626
164
|
return {
|
|
627
|
-
|
|
628
|
-
|
|
165
|
+
path,
|
|
166
|
+
message: issue.message,
|
|
167
|
+
isArray
|
|
629
168
|
};
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
169
|
+
});
|
|
170
|
+
errors.forEach((error) => {
|
|
171
|
+
setObjectError(output, error.path, [error.message], error.isArray);
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
return output;
|
|
634
175
|
}
|
|
176
|
+
async function computeErrors() {
|
|
177
|
+
let result = computedSchema.value["~standard"].validate(processedState.value);
|
|
178
|
+
if (result instanceof Promise) {
|
|
179
|
+
result = await result;
|
|
180
|
+
}
|
|
181
|
+
customErrors.value = issuesToRegleErrors(result);
|
|
182
|
+
return result;
|
|
183
|
+
}
|
|
184
|
+
vue.watch([processedState, computedSchema], computeErrors, { deep: true, immediate: true });
|
|
185
|
+
onValidate = async () => {
|
|
186
|
+
try {
|
|
187
|
+
const result = await computeErrors();
|
|
188
|
+
return {
|
|
189
|
+
result: !result.issues?.length,
|
|
190
|
+
data: processedState.value
|
|
191
|
+
};
|
|
192
|
+
} catch (e) {
|
|
193
|
+
return Promise.reject(e);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
635
196
|
const regle = core.useRootStorage({
|
|
636
197
|
scopeRules: convertedRules,
|
|
637
198
|
state: processedState,
|
|
@@ -639,7 +200,7 @@ function createUseRegleSchemaComposable(options, shortcuts) {
|
|
|
639
200
|
schemaErrors: customErrors,
|
|
640
201
|
initialState,
|
|
641
202
|
shortcuts,
|
|
642
|
-
schemaMode:
|
|
203
|
+
schemaMode: true,
|
|
643
204
|
onValidate
|
|
644
205
|
});
|
|
645
206
|
return {
|
|
@@ -650,7 +211,7 @@ function createUseRegleSchemaComposable(options, shortcuts) {
|
|
|
650
211
|
}
|
|
651
212
|
var useRegleSchema = createUseRegleSchemaComposable();
|
|
652
213
|
function withDeps(schema, depsArray) {
|
|
653
|
-
return
|
|
214
|
+
return schema;
|
|
654
215
|
}
|
|
655
216
|
function createInferValibotSchemaHelper() {
|
|
656
217
|
function inferRules(state, rulesFactory) {
|