@regle/schemas 0.7.6 → 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 +73 -684
- package/dist/regle-schemas.d.cts +21 -92
- package/dist/regle-schemas.d.ts +21 -92
- package/dist/regle-schemas.min.cjs +1 -1
- package/dist/regle-schemas.min.mjs +1 -1
- package/dist/regle-schemas.mjs +74 -685
- package/package.json +3 -3
package/dist/regle-schemas.mjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { useRootStorage } from '@regle/core';
|
|
2
|
-
import { ref, computed, unref, isRef, watch
|
|
3
|
-
import { withMessage, withAsync, withParams, required, minLength, maxLength, exactLength } from '@regle/rules';
|
|
2
|
+
import { ref, computed, unref, isRef, watch } from 'vue';
|
|
4
3
|
|
|
5
4
|
// src/core/useRegleSchema.ts
|
|
6
5
|
|
|
@@ -43,12 +42,6 @@ function cloneDeep(obj) {
|
|
|
43
42
|
}
|
|
44
43
|
|
|
45
44
|
// ../shared/utils/object.utils.ts
|
|
46
|
-
function isObject(obj) {
|
|
47
|
-
if (obj && (obj instanceof Date || obj.constructor.name == "File" || obj.constructor.name == "FileList")) {
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
return typeof obj === "object" && obj !== null && !Array.isArray(obj);
|
|
51
|
-
}
|
|
52
45
|
function setObjectError(obj, propsArg, value, isArray) {
|
|
53
46
|
var props, lastProp;
|
|
54
47
|
if (Array.isArray(propsArg)) {
|
|
@@ -85,624 +78,56 @@ function setObjectError(obj, propsArg, value, isArray) {
|
|
|
85
78
|
}
|
|
86
79
|
}
|
|
87
80
|
if (isArray) {
|
|
88
|
-
obj[lastProp]
|
|
89
|
-
|
|
90
|
-
obj[lastProp] = value;
|
|
91
|
-
}
|
|
92
|
-
return true;
|
|
93
|
-
}
|
|
94
|
-
function prototypeCheck(prop) {
|
|
95
|
-
if (prop == "__proto__" || prop == "constructor" || prop == "prototype") {
|
|
96
|
-
throw new Error("setting of prototype values not supported");
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
function transformValibotAdapter(schema) {
|
|
100
|
-
const isAsync = schema.async;
|
|
101
|
-
const validatorFn = (value) => {
|
|
102
|
-
const result = trySafeTransform(schema, value);
|
|
103
|
-
if (result instanceof Promise) {
|
|
104
|
-
return result;
|
|
105
|
-
}
|
|
106
|
-
if (!result.issues) {
|
|
107
|
-
return {
|
|
108
|
-
$valid: true,
|
|
109
|
-
$issues: []
|
|
110
|
-
};
|
|
111
|
-
} else {
|
|
112
|
-
return {
|
|
113
|
-
$valid: false,
|
|
114
|
-
$issues: result.issues
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
if ("__depsArray" in schema && Array.isArray(schema.__depsArray) && schema.__depsArray.length) {
|
|
119
|
-
return isAsync ? withAsync(validatorFn, schema.__depsArray) : withParams(validatorFn, schema.__depsArray);
|
|
120
|
-
}
|
|
121
|
-
return isAsync ? withAsync(validatorFn) : validatorFn;
|
|
122
|
-
}
|
|
123
|
-
function trySafeTransform(schema, value) {
|
|
124
|
-
try {
|
|
125
|
-
if (schema.async) {
|
|
126
|
-
return new Promise(async (resolve) => {
|
|
127
|
-
const result = await schema["~standard"].validate(value);
|
|
128
|
-
if (!result.issues) {
|
|
129
|
-
resolve({
|
|
130
|
-
$valid: true,
|
|
131
|
-
$issues: []
|
|
132
|
-
});
|
|
133
|
-
} else {
|
|
134
|
-
resolve({
|
|
135
|
-
$valid: false,
|
|
136
|
-
$issues: result.issues
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
});
|
|
81
|
+
if (!obj[lastProp]) {
|
|
82
|
+
obj[lastProp] = { ...obj[lastProp], $self: value };
|
|
140
83
|
} else {
|
|
141
|
-
|
|
142
|
-
return result;
|
|
143
|
-
}
|
|
144
|
-
} catch (e) {
|
|
145
|
-
return {};
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// src/core/converters/valibot/guards.ts
|
|
150
|
-
function isIntersectSchema(schema) {
|
|
151
|
-
return schema.type === "intersect";
|
|
152
|
-
}
|
|
153
|
-
function isObjectSchema(schema) {
|
|
154
|
-
return schema.type === "object";
|
|
155
|
-
}
|
|
156
|
-
function isWrappedType(schema) {
|
|
157
|
-
return "wrapped" in schema;
|
|
158
|
-
}
|
|
159
|
-
function getNestedValibotObjectSchema(schema) {
|
|
160
|
-
if (isIntersectSchema(schema)) {
|
|
161
|
-
const allEntries = schema.options.reduce(
|
|
162
|
-
(acc, value) => {
|
|
163
|
-
const nestedSchema = getNestedValibotObjectSchema(value);
|
|
164
|
-
if (nestedSchema) {
|
|
165
|
-
acc = {
|
|
166
|
-
...acc,
|
|
167
|
-
...nestedSchema.entries
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
return acc;
|
|
171
|
-
},
|
|
172
|
-
{}
|
|
173
|
-
);
|
|
174
|
-
return { entries: allEntries, type: "object", kind: "schema", async: false };
|
|
175
|
-
} else if (isObjectSchema(schema)) {
|
|
176
|
-
return schema;
|
|
177
|
-
} else if (isWrappedType(schema)) {
|
|
178
|
-
return getNestedValibotObjectSchema(schema.wrapped);
|
|
179
|
-
} else {
|
|
180
|
-
return undefined;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
function valibotObjectToRegle(schema, state) {
|
|
184
|
-
const nestedSchema = getNestedValibotObjectSchema(schema);
|
|
185
|
-
if (nestedSchema) {
|
|
186
|
-
return Object.fromEntries(
|
|
187
|
-
Object.entries(nestedSchema.entries).map(([key, shape]) => {
|
|
188
|
-
if (typeof shape === "object") {
|
|
189
|
-
const childState = toRef(isObject(state.value) ? state.value : {}, key);
|
|
190
|
-
return [key, processValibotTypeDef(shape, childState)];
|
|
191
|
-
}
|
|
192
|
-
return [key, {}];
|
|
193
|
-
})
|
|
194
|
-
);
|
|
195
|
-
}
|
|
196
|
-
return {};
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// src/core/converters/valibot/validators/valibotVariantToRegle.ts
|
|
200
|
-
function isLiteralSchema(schema) {
|
|
201
|
-
return schema?.type === "literal";
|
|
202
|
-
}
|
|
203
|
-
function valibotVariantToRegle(schema, state) {
|
|
204
|
-
const scope = effectScope();
|
|
205
|
-
const scopeState = scope.run(() => {
|
|
206
|
-
const valibotRule = ref({});
|
|
207
|
-
watch(
|
|
208
|
-
state,
|
|
209
|
-
() => {
|
|
210
|
-
if (isObject(state.value) && state.value[schema.key]) {
|
|
211
|
-
const selectedDiscriminant = schema.options.find((o) => {
|
|
212
|
-
const nestedSchema = getNestedValibotObjectSchema(o);
|
|
213
|
-
const schemaVariantKey = nestedSchema?.entries[schema.key];
|
|
214
|
-
if (isLiteralSchema(schemaVariantKey) && isObject(state.value)) {
|
|
215
|
-
return schemaVariantKey.literal === state.value[schema.key];
|
|
216
|
-
}
|
|
217
|
-
return false;
|
|
218
|
-
});
|
|
219
|
-
if (selectedDiscriminant && isObjectSchema(selectedDiscriminant)) {
|
|
220
|
-
valibotRule.value = Object.fromEntries(
|
|
221
|
-
Object.entries(selectedDiscriminant.entries).map(([key, shape]) => {
|
|
222
|
-
return [key, processValibotTypeDef(shape, toRef(isObject(state.value) ? state.value : {}, key))];
|
|
223
|
-
})
|
|
224
|
-
);
|
|
225
|
-
} else {
|
|
226
|
-
valibotRule.value = {};
|
|
227
|
-
}
|
|
228
|
-
} else {
|
|
229
|
-
valibotRule.value = {
|
|
230
|
-
[schema.key]: { required }
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
},
|
|
234
|
-
{ deep: true, flush: "pre", immediate: true }
|
|
235
|
-
);
|
|
236
|
-
onScopeDispose(() => {
|
|
237
|
-
scope.stop();
|
|
238
|
-
});
|
|
239
|
-
return { valibotRule };
|
|
240
|
-
});
|
|
241
|
-
return scopeState;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// src/core/converters/extractIssuesMessages.ts
|
|
245
|
-
function extractIssuesMessages() {
|
|
246
|
-
return (metadata) => {
|
|
247
|
-
const issueMessages = metadata.$issues?.map((issue) => issue.message);
|
|
248
|
-
if (issueMessages?.length) {
|
|
249
|
-
return issueMessages.length ? issueMessages : "Error";
|
|
84
|
+
obj[lastProp].$self = (obj[lastProp].$self ??= []).concat(value);
|
|
250
85
|
}
|
|
251
|
-
return [];
|
|
252
|
-
};
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// src/core/converters/valibot/processValibotTypeDef.ts
|
|
256
|
-
function isArraySchema(schema) {
|
|
257
|
-
return schema.type === "array";
|
|
258
|
-
}
|
|
259
|
-
function isObjectSchema2(schema) {
|
|
260
|
-
return schema.type === "object";
|
|
261
|
-
}
|
|
262
|
-
function isVariantSchema(schema) {
|
|
263
|
-
return schema.type === "variant";
|
|
264
|
-
}
|
|
265
|
-
function getNestedInnerType(schema) {
|
|
266
|
-
if (isWrappedType(schema)) {
|
|
267
|
-
return getNestedInnerType(schema.wrapped);
|
|
268
86
|
} else {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
}
|
|
272
|
-
function processValibotTypeDef(schema, state) {
|
|
273
|
-
const nestedSchema = getNestedInnerType(schema);
|
|
274
|
-
if (isArraySchema(nestedSchema)) {
|
|
275
|
-
return valibotArrayToRegle(nestedSchema, state);
|
|
276
|
-
} else if (isObjectSchema2(nestedSchema)) {
|
|
277
|
-
return valibotObjectToRegle(nestedSchema, state);
|
|
278
|
-
} else if (isVariantSchema(nestedSchema)) {
|
|
279
|
-
const valibotRule = valibotVariantToRegle(nestedSchema, state);
|
|
280
|
-
return valibotRule.valibotRule;
|
|
281
|
-
} else {
|
|
282
|
-
return {
|
|
283
|
-
[nestedSchema.type]: withMessage(transformValibotAdapter(nestedSchema), extractIssuesMessages())
|
|
284
|
-
};
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
// src/core/converters/valibot/validators/valibotArrayToRegle.ts
|
|
289
|
-
function valibotArrayToRegle(schema, state) {
|
|
290
|
-
let filteredSelfSchema = {};
|
|
291
|
-
if ("pipe" in schema) {
|
|
292
|
-
schema.pipe.filter((f) => f.kind === "validation").forEach((validation) => {
|
|
293
|
-
filteredSelfSchema[validation.type] = withMessage((value) => {
|
|
294
|
-
const result = validation["~run"]({ value, typed: true }, {});
|
|
295
|
-
return {
|
|
296
|
-
$valid: !result.issues,
|
|
297
|
-
$issues: result.issues
|
|
298
|
-
};
|
|
299
|
-
}, extractIssuesMessages());
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
return {
|
|
303
|
-
$each: processValibotTypeDef(schema.item, state),
|
|
304
|
-
...filteredSelfSchema
|
|
305
|
-
};
|
|
306
|
-
}
|
|
307
|
-
function transformZodValidatorAdapter(schema, additionalIssues) {
|
|
308
|
-
const isAsync = hasAsyncRefinement(schema);
|
|
309
|
-
function validatorFn(value) {
|
|
310
|
-
if (additionalIssues?.value?.length) {
|
|
311
|
-
return {
|
|
312
|
-
$valid: false,
|
|
313
|
-
// additionalIssues should already contain field error if there is a refinement in a parent
|
|
314
|
-
$issues: additionalIssues.value
|
|
315
|
-
};
|
|
316
|
-
}
|
|
317
|
-
const result = trySafeTransform2(schema, value);
|
|
318
|
-
if (result instanceof Promise) {
|
|
319
|
-
return result;
|
|
320
|
-
}
|
|
321
|
-
if (result.success) {
|
|
322
|
-
return {
|
|
323
|
-
$valid: true,
|
|
324
|
-
$issues: []
|
|
325
|
-
};
|
|
87
|
+
if (Array.isArray(obj[lastProp])) {
|
|
88
|
+
obj[lastProp] = obj[lastProp].concat(value);
|
|
326
89
|
} else {
|
|
327
|
-
|
|
328
|
-
$valid: false,
|
|
329
|
-
// additionalIssues should already contain field error if there is a refinement in a parent
|
|
330
|
-
$issues: result.error?.issues ?? []
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
if (schema && "__depsArray" in schema && Array.isArray(schema.__depsArray) && schema.__depsArray.length) {
|
|
335
|
-
return isAsync ? withAsync(validatorFn, schema.__depsArray) : withParams(validatorFn, schema.__depsArray);
|
|
336
|
-
}
|
|
337
|
-
return isAsync ? withAsync(validatorFn) : validatorFn;
|
|
338
|
-
}
|
|
339
|
-
function trySafeTransform2(schema, value) {
|
|
340
|
-
if (schema) {
|
|
341
|
-
try {
|
|
342
|
-
const result = schema.safeParse(value);
|
|
343
|
-
return result;
|
|
344
|
-
} catch (e) {
|
|
345
|
-
try {
|
|
346
|
-
return new Promise(async (resolve) => {
|
|
347
|
-
const result = await schema.safeParseAsync(value);
|
|
348
|
-
if (result.success) {
|
|
349
|
-
resolve({
|
|
350
|
-
$valid: true,
|
|
351
|
-
$issues: []
|
|
352
|
-
});
|
|
353
|
-
} else {
|
|
354
|
-
resolve({
|
|
355
|
-
$valid: false,
|
|
356
|
-
$issues: result.error?.issues
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
});
|
|
360
|
-
} catch (e2) {
|
|
361
|
-
return {};
|
|
362
|
-
}
|
|
90
|
+
obj[lastProp] = value;
|
|
363
91
|
}
|
|
364
|
-
} else {
|
|
365
|
-
return { success: true, data: {} };
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
function isAsyncFunctionOrPromiseReturning(fn) {
|
|
369
|
-
if (typeof fn !== "function") return false;
|
|
370
|
-
if (fn.constructor.name === "AsyncFunction") {
|
|
371
|
-
return true;
|
|
372
|
-
}
|
|
373
|
-
try {
|
|
374
|
-
const result = fn();
|
|
375
|
-
return result instanceof Promise;
|
|
376
|
-
} catch {
|
|
377
|
-
return false;
|
|
378
92
|
}
|
|
93
|
+
return true;
|
|
379
94
|
}
|
|
380
|
-
function
|
|
381
|
-
if (
|
|
382
|
-
|
|
383
|
-
const effect = schema._def.effect;
|
|
384
|
-
return isAsyncFunctionOrPromiseReturning(effect.refinement || effect.transform);
|
|
385
|
-
}
|
|
386
|
-
if (schema._def.typeName === "ZodObject") {
|
|
387
|
-
return Object.values(schema._def.shape()).some((schema2) => hasAsyncRefinement(schema2));
|
|
388
|
-
}
|
|
389
|
-
if (schema._def.typeName === "ZodUnion" || schema._def.typeName === "ZodIntersection") {
|
|
390
|
-
return schema._def.options.some(hasAsyncRefinement);
|
|
391
|
-
}
|
|
392
|
-
if (schema._def.typeName === "ZodArray") {
|
|
393
|
-
return hasAsyncRefinement(schema._def.type);
|
|
394
|
-
}
|
|
395
|
-
if (schema._def.typeName === "ZodOptional" || schema._def.typeName === "ZodNullable") {
|
|
396
|
-
return hasAsyncRefinement(schema._def.innerType);
|
|
397
|
-
}
|
|
398
|
-
if (schema._def.typeName === "ZodTuple") {
|
|
399
|
-
return schema._def.items.some(hasAsyncRefinement);
|
|
400
|
-
}
|
|
401
|
-
if (schema._def.typeName === "ZodString" || schema._def.typeName === "ZodNumber" || schema._def.typeName === "ZodDate") {
|
|
402
|
-
return schema._def.checks?.some((check) => isAsyncFunctionOrPromiseReturning(check.refinement));
|
|
403
|
-
}
|
|
95
|
+
function getDotPath(obj, propsArg, defaultValue) {
|
|
96
|
+
if (!obj) {
|
|
97
|
+
return defaultValue;
|
|
404
98
|
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
const scope = effectScope();
|
|
409
|
-
const scopeState = scope.run(() => {
|
|
410
|
-
const zodRule = ref({});
|
|
411
|
-
watch(
|
|
412
|
-
state,
|
|
413
|
-
() => {
|
|
414
|
-
if (isObject(state.value) && state.value[schema._def.discriminator]) {
|
|
415
|
-
const selectedDiscriminant = schema._def.optionsMap.get(state.value[schema._def.discriminator]);
|
|
416
|
-
if (selectedDiscriminant) {
|
|
417
|
-
zodRule.value = Object.fromEntries(
|
|
418
|
-
Object.entries(selectedDiscriminant._def.shape()).map(([key, shape]) => {
|
|
419
|
-
if (typeof shape === "object" && "_def" in shape) {
|
|
420
|
-
return [
|
|
421
|
-
key,
|
|
422
|
-
processZodTypeDef({
|
|
423
|
-
schema: shape,
|
|
424
|
-
state: toRef(isObject(state.value) ? state.value : {}),
|
|
425
|
-
additionalIssues
|
|
426
|
-
})
|
|
427
|
-
];
|
|
428
|
-
}
|
|
429
|
-
return [key, {}];
|
|
430
|
-
})
|
|
431
|
-
);
|
|
432
|
-
} else {
|
|
433
|
-
zodRule.value = {};
|
|
434
|
-
}
|
|
435
|
-
} else {
|
|
436
|
-
zodRule.value = {
|
|
437
|
-
[schema._def.discriminator]: { required: required }
|
|
438
|
-
};
|
|
439
|
-
}
|
|
440
|
-
},
|
|
441
|
-
{ deep: true, flush: "pre", immediate: true }
|
|
442
|
-
);
|
|
443
|
-
onScopeDispose(() => {
|
|
444
|
-
scope.stop();
|
|
445
|
-
});
|
|
446
|
-
return { zodRule };
|
|
447
|
-
});
|
|
448
|
-
return scopeState;
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
// src/core/converters/zod/processZodTypeDef.ts
|
|
452
|
-
var typesWithInnerTypes = ["ZodDefault", "ZodCatch", "ZodNullable", "ZodOptional", "ZodReadonly"];
|
|
453
|
-
function isDefWithInnerType(schema) {
|
|
454
|
-
if (schema._def && typeof schema._def === "object" && "typeName" in schema._def) {
|
|
455
|
-
return typesWithInnerTypes.includes(schema._def.typeName);
|
|
99
|
+
var props, prop;
|
|
100
|
+
if (Array.isArray(propsArg)) {
|
|
101
|
+
props = propsArg.slice(0);
|
|
456
102
|
}
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
function isDefWithInnerSchema(schema) {
|
|
460
|
-
if (schema._def && typeof schema._def === "object" && "typeName" in schema._def) {
|
|
461
|
-
return "schema" in schema._def;
|
|
103
|
+
if (typeof propsArg == "string") {
|
|
104
|
+
props = propsArg.split(".");
|
|
462
105
|
}
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
function getNestedInnerType2(schema) {
|
|
466
|
-
if (schema?._def && typeof schema._def === "object" && "typeName" in schema._def) {
|
|
467
|
-
if (isDefWithInnerType(schema)) {
|
|
468
|
-
return getNestedInnerType2(schema._def.innerType);
|
|
469
|
-
} else if (isDefWithInnerSchema(schema)) {
|
|
470
|
-
return getNestedInnerType2(schema._def.schema);
|
|
471
|
-
} else {
|
|
472
|
-
return schema;
|
|
473
|
-
}
|
|
106
|
+
if (typeof propsArg == "symbol") {
|
|
107
|
+
props = [propsArg];
|
|
474
108
|
}
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
function processZodTypeDef({
|
|
478
|
-
schema,
|
|
479
|
-
state,
|
|
480
|
-
additionalIssues
|
|
481
|
-
}) {
|
|
482
|
-
const schemaDef = getNestedInnerType2(schema);
|
|
483
|
-
if (schemaDef?._def && "typeName" in schemaDef._def) {
|
|
484
|
-
if (schemaDef._def.typeName === "ZodArray" || schemaDef._def.typeName === "ZodTuple") {
|
|
485
|
-
const schemaRef = zodArrayToRegle(schema, state, additionalIssues);
|
|
486
|
-
return schemaRef.zodRule;
|
|
487
|
-
} else if (schemaDef._def.typeName === "ZodObject" || schemaDef._def.typeName === "ZodIntersection") {
|
|
488
|
-
const schemaRef = zodObjectToRegle(
|
|
489
|
-
schemaDef,
|
|
490
|
-
state,
|
|
491
|
-
additionalIssues
|
|
492
|
-
);
|
|
493
|
-
return schemaRef.zodRule;
|
|
494
|
-
} else if (schemaDef._def.typeName === "ZodDiscriminatedUnion") {
|
|
495
|
-
const schemaRef = zodDiscriminatedUnionToRegle(
|
|
496
|
-
schemaDef,
|
|
497
|
-
state,
|
|
498
|
-
additionalIssues
|
|
499
|
-
);
|
|
500
|
-
return schemaRef.zodRule;
|
|
501
|
-
} else {
|
|
502
|
-
if (additionalIssues) {
|
|
503
|
-
return {
|
|
504
|
-
[schemaDef.constructor.name]: withMessage(
|
|
505
|
-
withParams(transformZodValidatorAdapter(schema, additionalIssues), [state]),
|
|
506
|
-
extractIssuesMessages()
|
|
507
|
-
)
|
|
508
|
-
};
|
|
509
|
-
}
|
|
510
|
-
return {
|
|
511
|
-
[schemaDef.constructor.name]: withMessage(
|
|
512
|
-
transformZodValidatorAdapter(schema),
|
|
513
|
-
extractIssuesMessages()
|
|
514
|
-
)
|
|
515
|
-
};
|
|
516
|
-
}
|
|
109
|
+
if (!Array.isArray(props)) {
|
|
110
|
+
throw new Error("props arg must be an array, a string or a symbol");
|
|
517
111
|
}
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
function getNestedZodArraySchema(schema, effects = []) {
|
|
523
|
-
if (schema._def && "typeName" in schema._def) {
|
|
524
|
-
if (schema._def.typeName === "ZodEffects") {
|
|
525
|
-
return getNestedZodArraySchema(
|
|
526
|
-
schema._def.schema,
|
|
527
|
-
effects.concat([schema._def.effect])
|
|
528
|
-
);
|
|
529
|
-
} else {
|
|
530
|
-
return { nestedSchema: schema, nestedEffects: effects };
|
|
112
|
+
while (props.length) {
|
|
113
|
+
prop = props.shift();
|
|
114
|
+
if (!obj) {
|
|
115
|
+
return defaultValue;
|
|
531
116
|
}
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
}
|
|
535
|
-
function zodArrayToRegle(schema, state, rootAdditionalIssues) {
|
|
536
|
-
const { nestedSchema, nestedEffects } = getNestedZodArraySchema(schema);
|
|
537
|
-
const zodRule = ref({});
|
|
538
|
-
if (nestedSchema) {
|
|
539
|
-
const arrayValidators = nestedSchema._def.typeName === "ZodArray" ? {
|
|
540
|
-
...!!nestedSchema._def.minLength && { minLength: minLength(nestedSchema._def.minLength?.value) },
|
|
541
|
-
...!!nestedSchema._def.maxLength && { maxLength: maxLength(nestedSchema._def.maxLength?.value) },
|
|
542
|
-
...!!nestedSchema._def.exactLength && { exactLength: exactLength(nestedSchema._def.exactLength?.value) }
|
|
543
|
-
} : {};
|
|
544
|
-
if (nestedEffects?.length) {
|
|
545
|
-
const scope = effectScope();
|
|
546
|
-
scope.run(() => {
|
|
547
|
-
const localAdditionalIssues = ref([]);
|
|
548
|
-
const additionalIssues = computed(() => {
|
|
549
|
-
return localAdditionalIssues.value.concat(rootAdditionalIssues?.value ?? []);
|
|
550
|
-
});
|
|
551
|
-
if (!rootAdditionalIssues) {
|
|
552
|
-
watch(
|
|
553
|
-
state,
|
|
554
|
-
() => {
|
|
555
|
-
if (nestedSchema) {
|
|
556
|
-
localAdditionalIssues.value = schema.safeParse(state.value).error?.issues.filter((f) => f.code === "custom") ?? [];
|
|
557
|
-
}
|
|
558
|
-
},
|
|
559
|
-
{ deep: true, immediate: true }
|
|
560
|
-
);
|
|
561
|
-
}
|
|
562
|
-
if (nestedSchema) {
|
|
563
|
-
const items = nestedSchema._def.typeName === "ZodArray" ? nestedSchema._def.type : nestedSchema._def.items;
|
|
564
|
-
const selfAdditionalIssues = computed(() => additionalIssues.value.filter((f) => !f.path.length));
|
|
565
|
-
zodRule.value = {
|
|
566
|
-
$each: (_, index) => {
|
|
567
|
-
const fieldIssues = computed(() => {
|
|
568
|
-
return additionalIssues.value?.filter((f) => f.code === "custom")?.filter((f) => f.path[0] === index.toString()).map((m) => {
|
|
569
|
-
const [first, ...rest] = m.path;
|
|
570
|
-
return {
|
|
571
|
-
...m,
|
|
572
|
-
path: rest
|
|
573
|
-
};
|
|
574
|
-
});
|
|
575
|
-
});
|
|
576
|
-
return processZodTypeDef({ schema: items, state, additionalIssues: fieldIssues });
|
|
577
|
-
},
|
|
578
|
-
selfValidator: withMessage(
|
|
579
|
-
withParams(transformZodValidatorAdapter(undefined, selfAdditionalIssues), [
|
|
580
|
-
state,
|
|
581
|
-
selfAdditionalIssues
|
|
582
|
-
]),
|
|
583
|
-
extractIssuesMessages()
|
|
584
|
-
)
|
|
585
|
-
};
|
|
586
|
-
} else {
|
|
587
|
-
zodRule.value = {};
|
|
588
|
-
}
|
|
589
|
-
onScopeDispose(() => {
|
|
590
|
-
scope.stop();
|
|
591
|
-
});
|
|
592
|
-
});
|
|
593
|
-
} else {
|
|
594
|
-
const items = nestedSchema._def.typeName === "ZodArray" ? nestedSchema._def.type : nestedSchema._def.items;
|
|
595
|
-
zodRule.value = {
|
|
596
|
-
$each: processZodTypeDef({ schema: items, state, additionalIssues: rootAdditionalIssues }),
|
|
597
|
-
...arrayValidators
|
|
598
|
-
};
|
|
117
|
+
if (!prop) {
|
|
118
|
+
return defaultValue;
|
|
599
119
|
}
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
function getNestedZodObjectSchema(schema, effects = []) {
|
|
604
|
-
if (schema._def && "typeName" in schema._def) {
|
|
605
|
-
if (schema._def.typeName === "ZodEffects") {
|
|
606
|
-
return getNestedZodObjectSchema(
|
|
607
|
-
schema._def.schema,
|
|
608
|
-
effects.concat([schema._def.effect])
|
|
609
|
-
);
|
|
610
|
-
} else if (schema._def.typeName === "ZodIntersection") {
|
|
611
|
-
const { nestedSchema: leftObject, nestedEffects: leftEffects = [] } = getNestedZodObjectSchema(
|
|
612
|
-
schema._def.left
|
|
613
|
-
);
|
|
614
|
-
const { nestedSchema: rightObject, nestedEffects: rightEffects = [] } = getNestedZodObjectSchema(
|
|
615
|
-
schema._def.right
|
|
616
|
-
);
|
|
617
|
-
if (leftObject && rightObject) {
|
|
618
|
-
return getNestedZodObjectSchema(
|
|
619
|
-
leftObject.merge(rightObject),
|
|
620
|
-
effects.concat(leftEffects).concat(rightEffects)
|
|
621
|
-
);
|
|
622
|
-
}
|
|
623
|
-
return { nestedSchema: undefined };
|
|
624
|
-
} else {
|
|
625
|
-
return { nestedSchema: schema, nestedEffects: effects };
|
|
120
|
+
obj = obj[prop];
|
|
121
|
+
if (obj === undefined) {
|
|
122
|
+
return defaultValue;
|
|
626
123
|
}
|
|
627
124
|
}
|
|
628
|
-
return
|
|
125
|
+
return obj;
|
|
629
126
|
}
|
|
630
|
-
function
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
if (nestedSchema) {
|
|
634
|
-
if (nestedEffects?.length) {
|
|
635
|
-
const scope = effectScope();
|
|
636
|
-
scope.run(() => {
|
|
637
|
-
const localAdditionalIssues = ref([]);
|
|
638
|
-
const additionalIssues = computed(() => {
|
|
639
|
-
return localAdditionalIssues.value.concat(rootAdditionalIssues?.value ?? []);
|
|
640
|
-
});
|
|
641
|
-
watch(
|
|
642
|
-
state,
|
|
643
|
-
() => {
|
|
644
|
-
if (nestedSchema?._def?.typeName === "ZodObject") {
|
|
645
|
-
localAdditionalIssues.value = (rootAdditionalIssues?.value ?? []).concat(
|
|
646
|
-
schema.safeParse(state.value).error?.issues.filter((f) => f.code === "custom") ?? []
|
|
647
|
-
);
|
|
648
|
-
}
|
|
649
|
-
},
|
|
650
|
-
{ deep: true, flush: "sync", immediate: true }
|
|
651
|
-
);
|
|
652
|
-
if (nestedSchema?._def?.typeName === "ZodObject") {
|
|
653
|
-
zodRule.value = Object.fromEntries(
|
|
654
|
-
Object.entries(nestedSchema._def.shape()).map(([key, shape]) => {
|
|
655
|
-
if (shape && typeof shape === "object" && "_def" in shape) {
|
|
656
|
-
const childState = toRef(isObject(state.value) ? state.value : {}, key);
|
|
657
|
-
const fieldIssues = computed(() => {
|
|
658
|
-
return additionalIssues.value?.filter((f) => f.path[0] === key).map((m) => {
|
|
659
|
-
const [_, ...rest] = m.path;
|
|
660
|
-
return {
|
|
661
|
-
...m,
|
|
662
|
-
path: rest
|
|
663
|
-
};
|
|
664
|
-
});
|
|
665
|
-
});
|
|
666
|
-
return [
|
|
667
|
-
key,
|
|
668
|
-
processZodTypeDef({
|
|
669
|
-
schema: shape,
|
|
670
|
-
state: childState,
|
|
671
|
-
additionalIssues: fieldIssues
|
|
672
|
-
}),
|
|
673
|
-
[state]
|
|
674
|
-
];
|
|
675
|
-
}
|
|
676
|
-
return [key, {}];
|
|
677
|
-
})
|
|
678
|
-
);
|
|
679
|
-
} else {
|
|
680
|
-
zodRule.value = {};
|
|
681
|
-
}
|
|
682
|
-
onScopeDispose(() => {
|
|
683
|
-
scope.stop();
|
|
684
|
-
});
|
|
685
|
-
});
|
|
686
|
-
} else {
|
|
687
|
-
zodRule.value = Object.fromEntries(
|
|
688
|
-
Object.entries(nestedSchema._def.shape()).map(([key, shape]) => {
|
|
689
|
-
if (shape && typeof shape === "object" && "_def" in shape) {
|
|
690
|
-
const childState = toRef(isObject(state.value) ? state.value : {}, key);
|
|
691
|
-
return [
|
|
692
|
-
key,
|
|
693
|
-
processZodTypeDef({
|
|
694
|
-
schema: shape,
|
|
695
|
-
state: childState,
|
|
696
|
-
additionalIssues: rootAdditionalIssues
|
|
697
|
-
})
|
|
698
|
-
];
|
|
699
|
-
}
|
|
700
|
-
return [key, {}];
|
|
701
|
-
})
|
|
702
|
-
);
|
|
703
|
-
}
|
|
127
|
+
function prototypeCheck(prop) {
|
|
128
|
+
if (prop == "__proto__" || prop == "constructor" || prop == "prototype") {
|
|
129
|
+
throw new Error("setting of prototype values not supported");
|
|
704
130
|
}
|
|
705
|
-
return reactive({ zodRule });
|
|
706
131
|
}
|
|
707
132
|
|
|
708
133
|
// src/core/useRegleSchema.ts
|
|
@@ -716,82 +141,56 @@ function createUseRegleSchemaComposable(options, shortcuts) {
|
|
|
716
141
|
function useRegleSchema2(state, schema, options2) {
|
|
717
142
|
const convertedRules = ref({});
|
|
718
143
|
const computedSchema = computed(() => unref(schema));
|
|
719
|
-
const { mode = "rules", ...regleOptions } = options2 ?? {};
|
|
720
144
|
const resolvedOptions = {
|
|
721
145
|
...globalOptions,
|
|
722
|
-
...
|
|
146
|
+
...options2
|
|
723
147
|
};
|
|
724
148
|
const processedState = isRef(state) ? state : ref(state);
|
|
725
149
|
const initialState = ref({ ...cloneDeep(processedState.value) });
|
|
726
150
|
const customErrors = ref({});
|
|
727
151
|
let onValidate = undefined;
|
|
728
|
-
if (
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
} else if (computedSchema.value?.["~standard"]?.vendor) {
|
|
739
|
-
console.warn(
|
|
740
|
-
`This RPC library "${computedSchema.value["~standard"].vendor}" is not supported yet in 'rules' mode, switch to the "schema" mode option`
|
|
741
|
-
);
|
|
742
|
-
} else {
|
|
743
|
-
console.warn(`Only "standard-schema" compatible libraries are supported`);
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
},
|
|
747
|
-
{ deep: true, immediate: true, flush: "post" }
|
|
748
|
-
);
|
|
749
|
-
} else {
|
|
750
|
-
let issuesToRegleErrors2 = function(result) {
|
|
751
|
-
const output = {};
|
|
752
|
-
if (result.issues) {
|
|
753
|
-
const errors = result.issues.map((issue) => {
|
|
754
|
-
const path = issue.path?.map((item) => typeof item === "object" ? item.key : item).join(".");
|
|
755
|
-
const lastItem = issue.path?.[issue.path.length - 1];
|
|
756
|
-
const isArray = (typeof lastItem === "object" && "value" in lastItem ? Array.isArray(lastItem.value) : false) || ("type" in issue ? issue.type === "array" : false);
|
|
757
|
-
return {
|
|
758
|
-
path,
|
|
759
|
-
message: issue.message,
|
|
760
|
-
isArray
|
|
761
|
-
};
|
|
762
|
-
});
|
|
763
|
-
errors.forEach((error) => {
|
|
764
|
-
setObjectError(output, error.path, [error.message], error.isArray);
|
|
765
|
-
});
|
|
766
|
-
}
|
|
767
|
-
return output;
|
|
768
|
-
};
|
|
769
|
-
if (!computedSchema.value?.["~standard"]) {
|
|
770
|
-
throw new Error(`Only "standard-schema" compatible libraries are supported`);
|
|
771
|
-
}
|
|
772
|
-
const emptySkeleton = computedSchema.value["~standard"].validate(initialState.value);
|
|
773
|
-
customErrors.value = issuesToRegleErrors2(emptySkeleton);
|
|
774
|
-
watch(
|
|
775
|
-
[processedState, computedSchema],
|
|
776
|
-
async () => {
|
|
777
|
-
const result = await computedSchema.value["~standard"].validate(processedState.value);
|
|
778
|
-
customErrors.value = issuesToRegleErrors2(result);
|
|
779
|
-
},
|
|
780
|
-
{ deep: true, immediate: true, flush: "post" }
|
|
781
|
-
);
|
|
782
|
-
onValidate = async () => {
|
|
783
|
-
try {
|
|
784
|
-
const result = await computedSchema.value["~standard"].validate(processedState.value);
|
|
785
|
-
customErrors.value = issuesToRegleErrors2(result);
|
|
152
|
+
if (!computedSchema.value?.["~standard"]) {
|
|
153
|
+
throw new Error(`Only "standard-schema" compatible libraries are supported`);
|
|
154
|
+
}
|
|
155
|
+
function issuesToRegleErrors(result) {
|
|
156
|
+
const output = {};
|
|
157
|
+
if (result.issues) {
|
|
158
|
+
const errors = result.issues.map((issue) => {
|
|
159
|
+
const path = issue.path?.map((item) => typeof item === "object" ? item.key : item.toString()).join(".") ?? "";
|
|
160
|
+
const lastItem = issue.path?.[issue.path.length - 1];
|
|
161
|
+
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));
|
|
786
162
|
return {
|
|
787
|
-
|
|
788
|
-
|
|
163
|
+
path,
|
|
164
|
+
message: issue.message,
|
|
165
|
+
isArray
|
|
789
166
|
};
|
|
790
|
-
}
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
167
|
+
});
|
|
168
|
+
errors.forEach((error) => {
|
|
169
|
+
setObjectError(output, error.path, [error.message], error.isArray);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
return output;
|
|
173
|
+
}
|
|
174
|
+
async function computeErrors() {
|
|
175
|
+
let result = computedSchema.value["~standard"].validate(processedState.value);
|
|
176
|
+
if (result instanceof Promise) {
|
|
177
|
+
result = await result;
|
|
178
|
+
}
|
|
179
|
+
customErrors.value = issuesToRegleErrors(result);
|
|
180
|
+
return result;
|
|
794
181
|
}
|
|
182
|
+
watch([processedState, computedSchema], computeErrors, { deep: true, immediate: true });
|
|
183
|
+
onValidate = async () => {
|
|
184
|
+
try {
|
|
185
|
+
const result = await computeErrors();
|
|
186
|
+
return {
|
|
187
|
+
result: !result.issues?.length,
|
|
188
|
+
data: processedState.value
|
|
189
|
+
};
|
|
190
|
+
} catch (e) {
|
|
191
|
+
return Promise.reject(e);
|
|
192
|
+
}
|
|
193
|
+
};
|
|
795
194
|
const regle = useRootStorage({
|
|
796
195
|
scopeRules: convertedRules,
|
|
797
196
|
state: processedState,
|
|
@@ -799,7 +198,7 @@ function createUseRegleSchemaComposable(options, shortcuts) {
|
|
|
799
198
|
schemaErrors: customErrors,
|
|
800
199
|
initialState,
|
|
801
200
|
shortcuts,
|
|
802
|
-
schemaMode:
|
|
201
|
+
schemaMode: true,
|
|
803
202
|
onValidate
|
|
804
203
|
});
|
|
805
204
|
return {
|
|
@@ -810,16 +209,6 @@ function createUseRegleSchemaComposable(options, shortcuts) {
|
|
|
810
209
|
}
|
|
811
210
|
var useRegleSchema = createUseRegleSchemaComposable();
|
|
812
211
|
function withDeps(schema, depsArray) {
|
|
813
|
-
if (!Object.hasOwn(schema, "__depsArray")) {
|
|
814
|
-
Object.defineProperties(schema, {
|
|
815
|
-
__depsArray: {
|
|
816
|
-
value: depsArray,
|
|
817
|
-
enumerable: false,
|
|
818
|
-
configurable: false,
|
|
819
|
-
writable: false
|
|
820
|
-
}
|
|
821
|
-
});
|
|
822
|
-
}
|
|
823
212
|
return schema;
|
|
824
213
|
}
|
|
825
214
|
function createInferValibotSchemaHelper() {
|