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