@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.mjs
CHANGED
|
@@ -1,8 +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';
|
|
4
|
-
import * as v3 from 'valibot';
|
|
5
|
-
import z, { z as z$1 } from 'zod';
|
|
2
|
+
import { ref, computed, unref, isRef, watch } from 'vue';
|
|
6
3
|
|
|
7
4
|
// src/core/useRegleSchema.ts
|
|
8
5
|
|
|
@@ -45,12 +42,6 @@ function cloneDeep(obj) {
|
|
|
45
42
|
}
|
|
46
43
|
|
|
47
44
|
// ../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
45
|
function setObjectError(obj, propsArg, value, isArray) {
|
|
55
46
|
var props, lastProp;
|
|
56
47
|
if (Array.isArray(propsArg)) {
|
|
@@ -87,447 +78,56 @@ function setObjectError(obj, propsArg, value, isArray) {
|
|
|
87
78
|
}
|
|
88
79
|
}
|
|
89
80
|
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.success) {
|
|
109
|
-
return {
|
|
110
|
-
$valid: true,
|
|
111
|
-
$issues: []
|
|
112
|
-
};
|
|
81
|
+
if (!obj[lastProp]) {
|
|
82
|
+
obj[lastProp] = { ...obj[lastProp], $self: value };
|
|
113
83
|
} else {
|
|
114
|
-
|
|
115
|
-
$valid: false,
|
|
116
|
-
$issues: result.issues
|
|
117
|
-
};
|
|
84
|
+
obj[lastProp].$self = (obj[lastProp].$self ??= []).concat(value);
|
|
118
85
|
}
|
|
119
|
-
};
|
|
120
|
-
if ("__depsArray" in schema && Array.isArray(schema.__depsArray) && schema.__depsArray.length) {
|
|
121
|
-
return isAsync ? withAsync(validatorFn, schema.__depsArray) : withParams(validatorFn, schema.__depsArray);
|
|
122
|
-
}
|
|
123
|
-
return isAsync ? 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 v3.safeParseAsync(schema, value);
|
|
130
|
-
if (result.success) {
|
|
131
|
-
resolve({
|
|
132
|
-
$valid: true,
|
|
133
|
-
$issues: []
|
|
134
|
-
});
|
|
135
|
-
} else {
|
|
136
|
-
resolve({
|
|
137
|
-
$valid: false,
|
|
138
|
-
$issues: result.issues
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
} else {
|
|
143
|
-
const result = v3.safeParse(schema, value);
|
|
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
|
-
|
|
162
|
-
// src/core/converters/valibot/validators/valibotObjectToRegle.ts
|
|
163
|
-
function getNestedValibotObjectSchema(schema) {
|
|
164
|
-
if (isIntersectSchema(schema)) {
|
|
165
|
-
const allEntries = schema.options.reduce(
|
|
166
|
-
(acc, value) => {
|
|
167
|
-
const nestedSchema = getNestedValibotObjectSchema(value);
|
|
168
|
-
if (nestedSchema) {
|
|
169
|
-
acc = {
|
|
170
|
-
...acc,
|
|
171
|
-
...nestedSchema.entries
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
return acc;
|
|
175
|
-
},
|
|
176
|
-
{}
|
|
177
|
-
);
|
|
178
|
-
return v3.object({ ...allEntries });
|
|
179
|
-
} else if (isObjectSchema(schema)) {
|
|
180
|
-
return schema;
|
|
181
|
-
} else if (isWrappedType(schema)) {
|
|
182
|
-
return getNestedValibotObjectSchema(schema.wrapped);
|
|
183
86
|
} else {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
function valibotObjectToRegle(schema, state) {
|
|
188
|
-
const nestedSchema = getNestedValibotObjectSchema(schema);
|
|
189
|
-
if (nestedSchema) {
|
|
190
|
-
return Object.fromEntries(
|
|
191
|
-
Object.entries(nestedSchema.entries).map(([key, shape]) => {
|
|
192
|
-
if (typeof shape === "object") {
|
|
193
|
-
const childState = toRef(isObject(state.value) ? state.value : {}, key);
|
|
194
|
-
return [key, processValibotTypeDef(shape, childState)];
|
|
195
|
-
}
|
|
196
|
-
return [key, {}];
|
|
197
|
-
})
|
|
198
|
-
);
|
|
199
|
-
}
|
|
200
|
-
return {};
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// src/core/converters/valibot/validators/valibotVariantToRegle.ts
|
|
204
|
-
function isLiteralSchema(schema) {
|
|
205
|
-
return schema?.type === "literal";
|
|
206
|
-
}
|
|
207
|
-
function valibotVariantToRegle(schema, state) {
|
|
208
|
-
const scope = effectScope();
|
|
209
|
-
const scopeState = scope.run(() => {
|
|
210
|
-
const valibotRule = ref({});
|
|
211
|
-
watch(
|
|
212
|
-
state,
|
|
213
|
-
() => {
|
|
214
|
-
if (isObject(state.value) && state.value[schema.key]) {
|
|
215
|
-
const selectedDiscriminant = schema.options.find((o) => {
|
|
216
|
-
const nestedSchema = getNestedValibotObjectSchema(o);
|
|
217
|
-
const schemaVariantKey = nestedSchema?.entries[schema.key];
|
|
218
|
-
if (isLiteralSchema(schemaVariantKey) && isObject(state.value)) {
|
|
219
|
-
return schemaVariantKey.literal === state.value[schema.key];
|
|
220
|
-
}
|
|
221
|
-
return false;
|
|
222
|
-
});
|
|
223
|
-
if (selectedDiscriminant && isObjectSchema(selectedDiscriminant)) {
|
|
224
|
-
valibotRule.value = Object.fromEntries(
|
|
225
|
-
Object.entries(selectedDiscriminant.entries).map(([key, shape]) => {
|
|
226
|
-
return [key, processValibotTypeDef(shape, toRef(isObject(state.value) ? state.value : {}, key))];
|
|
227
|
-
})
|
|
228
|
-
);
|
|
229
|
-
} else {
|
|
230
|
-
valibotRule.value = {};
|
|
231
|
-
}
|
|
232
|
-
} else {
|
|
233
|
-
valibotRule.value = {
|
|
234
|
-
[schema.key]: { required }
|
|
235
|
-
};
|
|
236
|
-
}
|
|
237
|
-
},
|
|
238
|
-
{ deep: true, flush: "pre", immediate: true }
|
|
239
|
-
);
|
|
240
|
-
onScopeDispose(() => {
|
|
241
|
-
scope.stop();
|
|
242
|
-
});
|
|
243
|
-
return { valibotRule };
|
|
244
|
-
});
|
|
245
|
-
return scopeState;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// src/core/converters/extractIssuesMessages.ts
|
|
249
|
-
function extractIssuesMessages() {
|
|
250
|
-
return (metadata) => {
|
|
251
|
-
const issueMessages = metadata.$issues?.map((issue) => issue.message);
|
|
252
|
-
return issueMessages.length ? issueMessages : "Error";
|
|
253
|
-
};
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// src/core/converters/valibot/processValibotTypeDef.ts
|
|
257
|
-
function isArraySchema(schema) {
|
|
258
|
-
return schema.type === "array";
|
|
259
|
-
}
|
|
260
|
-
function isObjectSchema2(schema) {
|
|
261
|
-
return schema.type === "object";
|
|
262
|
-
}
|
|
263
|
-
function isVariantSchema(schema) {
|
|
264
|
-
return schema.type === "variant";
|
|
265
|
-
}
|
|
266
|
-
function getNestedInnerType(schema) {
|
|
267
|
-
if (isWrappedType(schema)) {
|
|
268
|
-
return getNestedInnerType(schema.wrapped);
|
|
269
|
-
} else {
|
|
270
|
-
return schema;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
function processValibotTypeDef(schema, state) {
|
|
274
|
-
const nestedSchema = getNestedInnerType(schema);
|
|
275
|
-
if (isArraySchema(nestedSchema)) {
|
|
276
|
-
return valibotArrayToRegle(nestedSchema, state);
|
|
277
|
-
} else if (isObjectSchema2(nestedSchema)) {
|
|
278
|
-
return valibotObjectToRegle(nestedSchema, state);
|
|
279
|
-
} else if (isVariantSchema(nestedSchema)) {
|
|
280
|
-
const valibotRule = valibotVariantToRegle(nestedSchema, state);
|
|
281
|
-
return valibotRule.valibotRule;
|
|
282
|
-
} else {
|
|
283
|
-
return {
|
|
284
|
-
[nestedSchema.type]: withMessage(transformValibotAdapter(nestedSchema), extractIssuesMessages())
|
|
285
|
-
};
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
// src/core/converters/valibot/validators/valibotArrayToRegle.ts
|
|
290
|
-
function valibotArrayToRegle(schema, state) {
|
|
291
|
-
let filteredSelfSchema = {};
|
|
292
|
-
if ("pipe" in schema) {
|
|
293
|
-
schema.pipe.filter((f) => f.kind === "validation").forEach((validation) => {
|
|
294
|
-
filteredSelfSchema[validation.type] = withMessage(
|
|
295
|
-
transformValibotAdapter(v3.pipe(v3.array(v3.any()), validation)),
|
|
296
|
-
extractIssuesMessages()
|
|
297
|
-
);
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
return {
|
|
301
|
-
$each: processValibotTypeDef(schema.item, state),
|
|
302
|
-
...filteredSelfSchema
|
|
303
|
-
};
|
|
304
|
-
}
|
|
305
|
-
function transformZodValidatorAdapter(schema) {
|
|
306
|
-
const isAsync = hasAsyncRefinement(schema);
|
|
307
|
-
const validatorFn = (value) => {
|
|
308
|
-
const result = trySafeTransform2(schema, value);
|
|
309
|
-
if (result instanceof Promise) {
|
|
310
|
-
return result;
|
|
311
|
-
}
|
|
312
|
-
if (result.success) {
|
|
313
|
-
return {
|
|
314
|
-
$valid: true,
|
|
315
|
-
$issues: []
|
|
316
|
-
};
|
|
87
|
+
if (Array.isArray(obj[lastProp])) {
|
|
88
|
+
obj[lastProp] = obj[lastProp].concat(value);
|
|
317
89
|
} else {
|
|
318
|
-
|
|
319
|
-
$valid: false,
|
|
320
|
-
$issues: result.error.issues
|
|
321
|
-
};
|
|
322
|
-
}
|
|
323
|
-
};
|
|
324
|
-
if ("__depsArray" in schema && Array.isArray(schema.__depsArray) && schema.__depsArray.length) {
|
|
325
|
-
return isAsync ? withAsync(validatorFn, schema.__depsArray) : withParams(validatorFn, schema.__depsArray);
|
|
326
|
-
}
|
|
327
|
-
return isAsync ? withAsync(validatorFn) : validatorFn;
|
|
328
|
-
}
|
|
329
|
-
function trySafeTransform2(schema, value) {
|
|
330
|
-
try {
|
|
331
|
-
const result = schema.safeParse(value);
|
|
332
|
-
return result;
|
|
333
|
-
} catch (e) {
|
|
334
|
-
try {
|
|
335
|
-
return new Promise(async (resolve) => {
|
|
336
|
-
const result = await schema.safeParseAsync(value);
|
|
337
|
-
if (result.success) {
|
|
338
|
-
resolve({
|
|
339
|
-
$valid: true,
|
|
340
|
-
$issues: []
|
|
341
|
-
});
|
|
342
|
-
} else {
|
|
343
|
-
resolve({
|
|
344
|
-
$valid: false,
|
|
345
|
-
$issues: result.error.issues
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
});
|
|
349
|
-
} catch (e2) {
|
|
350
|
-
return {};
|
|
90
|
+
obj[lastProp] = value;
|
|
351
91
|
}
|
|
352
92
|
}
|
|
93
|
+
return true;
|
|
353
94
|
}
|
|
354
|
-
function
|
|
355
|
-
if (
|
|
356
|
-
|
|
357
|
-
return true;
|
|
95
|
+
function getDotPath(obj, propsArg, defaultValue) {
|
|
96
|
+
if (!obj) {
|
|
97
|
+
return defaultValue;
|
|
358
98
|
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
} catch {
|
|
363
|
-
return false;
|
|
99
|
+
var props, prop;
|
|
100
|
+
if (Array.isArray(propsArg)) {
|
|
101
|
+
props = propsArg.slice(0);
|
|
364
102
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
if (schema?._def) {
|
|
368
|
-
if (schema._def.typeName === "ZodEffects") {
|
|
369
|
-
const effect = schema._def.effect;
|
|
370
|
-
return isAsyncFunctionOrPromiseReturning(effect.refinement || effect.transform);
|
|
371
|
-
}
|
|
372
|
-
if (schema._def.typeName === "ZodObject") {
|
|
373
|
-
return Object.values(schema._def.shape()).some((schema2) => hasAsyncRefinement(schema2));
|
|
374
|
-
}
|
|
375
|
-
if (schema._def.typeName === "ZodUnion" || schema._def.typeName === "ZodIntersection") {
|
|
376
|
-
return schema._def.options.some(hasAsyncRefinement);
|
|
377
|
-
}
|
|
378
|
-
if (schema._def.typeName === "ZodArray") {
|
|
379
|
-
return hasAsyncRefinement(schema._def.type);
|
|
380
|
-
}
|
|
381
|
-
if (schema._def.typeName === "ZodOptional" || schema._def.typeName === "ZodNullable") {
|
|
382
|
-
return hasAsyncRefinement(schema._def.innerType);
|
|
383
|
-
}
|
|
384
|
-
if (schema._def.typeName === "ZodTuple") {
|
|
385
|
-
return schema._def.items.some(hasAsyncRefinement);
|
|
386
|
-
}
|
|
387
|
-
if (schema._def.typeName === "ZodString" || schema._def.typeName === "ZodNumber" || schema._def.typeName === "ZodDate") {
|
|
388
|
-
return schema._def.checks?.some((check) => isAsyncFunctionOrPromiseReturning(check.refinement));
|
|
389
|
-
}
|
|
103
|
+
if (typeof propsArg == "string") {
|
|
104
|
+
props = propsArg.split(".");
|
|
390
105
|
}
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
function zodDiscriminatedUnionToRegle(schema, state) {
|
|
394
|
-
const scope = effectScope();
|
|
395
|
-
const scopeState = scope.run(() => {
|
|
396
|
-
const zodRule = ref({});
|
|
397
|
-
watch(
|
|
398
|
-
state,
|
|
399
|
-
() => {
|
|
400
|
-
if (isObject(state.value) && state.value[schema._def.discriminator]) {
|
|
401
|
-
const selectedDiscriminant = schema._def.optionsMap.get(state.value[schema._def.discriminator]);
|
|
402
|
-
if (selectedDiscriminant) {
|
|
403
|
-
zodRule.value = Object.fromEntries(
|
|
404
|
-
Object.entries(selectedDiscriminant._def.shape()).map(([key, shape]) => {
|
|
405
|
-
if (typeof shape === "object" && "_def" in shape) {
|
|
406
|
-
return [key, processZodTypeDef(shape, toRef(isObject(state.value) ? state.value : {}, key))];
|
|
407
|
-
}
|
|
408
|
-
return [key, {}];
|
|
409
|
-
})
|
|
410
|
-
);
|
|
411
|
-
} else {
|
|
412
|
-
zodRule.value = {};
|
|
413
|
-
}
|
|
414
|
-
} else {
|
|
415
|
-
zodRule.value = {
|
|
416
|
-
[schema._def.discriminator]: { required: required }
|
|
417
|
-
};
|
|
418
|
-
}
|
|
419
|
-
},
|
|
420
|
-
{ deep: true, flush: "pre", immediate: true }
|
|
421
|
-
);
|
|
422
|
-
onScopeDispose(() => {
|
|
423
|
-
scope.stop();
|
|
424
|
-
});
|
|
425
|
-
return { zodRule };
|
|
426
|
-
});
|
|
427
|
-
return scopeState;
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
// src/core/converters/zod/processZodTypeDef.ts
|
|
431
|
-
var typesWithInnerTypes = [
|
|
432
|
-
z.ZodFirstPartyTypeKind.ZodDefault,
|
|
433
|
-
z.ZodFirstPartyTypeKind.ZodCatch,
|
|
434
|
-
z.ZodFirstPartyTypeKind.ZodNullable,
|
|
435
|
-
z.ZodFirstPartyTypeKind.ZodOptional,
|
|
436
|
-
z.ZodFirstPartyTypeKind.ZodReadonly
|
|
437
|
-
];
|
|
438
|
-
function isDefWithInnerType(schema) {
|
|
439
|
-
if (schema._def && typeof schema._def === "object" && "typeName" in schema._def) {
|
|
440
|
-
return typesWithInnerTypes.includes(schema._def.typeName);
|
|
106
|
+
if (typeof propsArg == "symbol") {
|
|
107
|
+
props = [propsArg];
|
|
441
108
|
}
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
function isDefWithInnerSchema(schema) {
|
|
445
|
-
if (schema._def && typeof schema._def === "object" && "typeName" in schema._def) {
|
|
446
|
-
return "schema" in schema._def;
|
|
109
|
+
if (!Array.isArray(props)) {
|
|
110
|
+
throw new Error("props arg must be an array, a string or a symbol");
|
|
447
111
|
}
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
if (isDefWithInnerType(schema)) {
|
|
453
|
-
return getNestedInnerType2(schema._def.innerType);
|
|
454
|
-
} else if (isDefWithInnerSchema(schema)) {
|
|
455
|
-
return getNestedInnerType2(schema._def.schema);
|
|
456
|
-
} else {
|
|
457
|
-
return schema;
|
|
112
|
+
while (props.length) {
|
|
113
|
+
prop = props.shift();
|
|
114
|
+
if (!obj) {
|
|
115
|
+
return defaultValue;
|
|
458
116
|
}
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
}
|
|
462
|
-
function processZodTypeDef(schema, state) {
|
|
463
|
-
const schemaDef = getNestedInnerType2(schema);
|
|
464
|
-
if (schemaDef?._def && "typeName" in schemaDef._def) {
|
|
465
|
-
if (schemaDef._def.typeName === z.ZodFirstPartyTypeKind.ZodArray || schemaDef._def.typeName === z.ZodFirstPartyTypeKind.ZodTuple) {
|
|
466
|
-
return zodArrayToRegle(schema, state);
|
|
467
|
-
} else if (schemaDef._def.typeName === z.ZodFirstPartyTypeKind.ZodObject || schemaDef._def.typeName === z.ZodFirstPartyTypeKind.ZodIntersection) {
|
|
468
|
-
return zodObjectToRegle(schemaDef, state);
|
|
469
|
-
} else if (schemaDef._def.typeName === z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion) {
|
|
470
|
-
const zodRule = zodDiscriminatedUnionToRegle(schemaDef, state);
|
|
471
|
-
return zodRule.zodRule;
|
|
472
|
-
} else {
|
|
473
|
-
return {
|
|
474
|
-
[schemaDef.constructor.name]: withMessage(
|
|
475
|
-
transformZodValidatorAdapter(schema),
|
|
476
|
-
extractIssuesMessages()
|
|
477
|
-
)
|
|
478
|
-
};
|
|
117
|
+
if (!prop) {
|
|
118
|
+
return defaultValue;
|
|
479
119
|
}
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
// src/core/converters/zod/validators/zodArrayToRegle.ts
|
|
485
|
-
function zodArrayToRegle(schema, state) {
|
|
486
|
-
const arrayValidators = schema._def.typeName === z$1.ZodFirstPartyTypeKind.ZodArray ? {
|
|
487
|
-
...!!schema._def.minLength && { minLength: minLength(schema._def.minLength?.value) },
|
|
488
|
-
...!!schema._def.maxLength && { maxLength: maxLength(schema._def.maxLength?.value) },
|
|
489
|
-
...!!schema._def.exactLength && { exactLength: exactLength(schema._def.exactLength?.value) }
|
|
490
|
-
} : {};
|
|
491
|
-
const items = schema._def.typeName === z$1.ZodFirstPartyTypeKind.ZodArray ? schema._def.type : schema._def.items;
|
|
492
|
-
return {
|
|
493
|
-
$each: processZodTypeDef(items, state),
|
|
494
|
-
...arrayValidators
|
|
495
|
-
};
|
|
496
|
-
}
|
|
497
|
-
function getNestedZodObjectSchema(schema) {
|
|
498
|
-
if (schema._def && "typeName" in schema._def) {
|
|
499
|
-
if (schema._def.typeName === z$1.ZodFirstPartyTypeKind.ZodEffects) {
|
|
500
|
-
return getNestedZodObjectSchema(schema._def.schema);
|
|
501
|
-
} else if (schema._def.typeName === z$1.ZodFirstPartyTypeKind.ZodIntersection) {
|
|
502
|
-
const leftObject = getNestedZodObjectSchema(schema._def.left);
|
|
503
|
-
const rightObject = getNestedZodObjectSchema(schema._def.right);
|
|
504
|
-
if (leftObject && rightObject) {
|
|
505
|
-
return getNestedZodObjectSchema(leftObject.merge(rightObject));
|
|
506
|
-
}
|
|
507
|
-
return undefined;
|
|
508
|
-
} else {
|
|
509
|
-
return schema;
|
|
120
|
+
obj = obj[prop];
|
|
121
|
+
if (obj === undefined) {
|
|
122
|
+
return defaultValue;
|
|
510
123
|
}
|
|
511
124
|
}
|
|
512
|
-
return
|
|
125
|
+
return obj;
|
|
513
126
|
}
|
|
514
|
-
function
|
|
515
|
-
if (
|
|
516
|
-
|
|
517
|
-
if (nestedSchema?._def?.typeName === z$1.ZodFirstPartyTypeKind.ZodObject) {
|
|
518
|
-
return Object.fromEntries(
|
|
519
|
-
Object.entries(nestedSchema._def.shape()).map(([key, shape]) => {
|
|
520
|
-
if (shape && typeof shape === "object" && "_def" in shape) {
|
|
521
|
-
const childState = toRef(isObject(state.value) ? state.value : {}, key);
|
|
522
|
-
return [key, processZodTypeDef(shape, childState)];
|
|
523
|
-
}
|
|
524
|
-
return [key, {}];
|
|
525
|
-
})
|
|
526
|
-
);
|
|
527
|
-
}
|
|
528
|
-
return {};
|
|
127
|
+
function prototypeCheck(prop) {
|
|
128
|
+
if (prop == "__proto__" || prop == "constructor" || prop == "prototype") {
|
|
129
|
+
throw new Error("setting of prototype values not supported");
|
|
529
130
|
}
|
|
530
|
-
return {};
|
|
531
131
|
}
|
|
532
132
|
|
|
533
133
|
// src/core/useRegleSchema.ts
|
|
@@ -541,72 +141,56 @@ function createUseRegleSchemaComposable(options, shortcuts) {
|
|
|
541
141
|
function useRegleSchema2(state, schema, options2) {
|
|
542
142
|
const convertedRules = ref({});
|
|
543
143
|
const computedSchema = computed(() => unref(schema));
|
|
544
|
-
const { mode = "rules", ...regleOptions } = options2 ?? {};
|
|
545
144
|
const resolvedOptions = {
|
|
546
145
|
...globalOptions,
|
|
547
|
-
...
|
|
146
|
+
...options2
|
|
548
147
|
};
|
|
549
148
|
const processedState = isRef(state) ? state : ref(state);
|
|
550
149
|
const initialState = ref({ ...cloneDeep(processedState.value) });
|
|
551
150
|
const customErrors = ref({});
|
|
552
151
|
let onValidate = undefined;
|
|
553
|
-
if (
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
}
|
|
564
|
-
},
|
|
565
|
-
{ deep: true, immediate: true, flush: "post" }
|
|
566
|
-
);
|
|
567
|
-
} else {
|
|
568
|
-
let issuesToRegleErrors2 = function(result) {
|
|
569
|
-
const output = {};
|
|
570
|
-
if (result.issues) {
|
|
571
|
-
const errors = result.issues.map((issue) => {
|
|
572
|
-
const path = issue.path?.map((item) => typeof item === "object" ? item.key : item).join(".");
|
|
573
|
-
const lastItem = issue.path?.[issue.path.length - 1];
|
|
574
|
-
const isArray = (typeof lastItem === "object" && "value" in lastItem ? Array.isArray(lastItem.value) : false) || ("type" in issue ? issue.type === "array" : false);
|
|
575
|
-
return {
|
|
576
|
-
path,
|
|
577
|
-
message: issue.message,
|
|
578
|
-
isArray
|
|
579
|
-
};
|
|
580
|
-
});
|
|
581
|
-
errors.forEach((error) => {
|
|
582
|
-
setObjectError(output, error.path, [error.message], error.isArray);
|
|
583
|
-
});
|
|
584
|
-
}
|
|
585
|
-
return output;
|
|
586
|
-
};
|
|
587
|
-
const emptySkeleton = computedSchema.value["~standard"].validate(initialState.value);
|
|
588
|
-
customErrors.value = issuesToRegleErrors2(emptySkeleton);
|
|
589
|
-
watch(
|
|
590
|
-
[processedState, computedSchema],
|
|
591
|
-
async () => {
|
|
592
|
-
const result = await computedSchema.value["~standard"].validate(processedState.value);
|
|
593
|
-
customErrors.value = issuesToRegleErrors2(result);
|
|
594
|
-
},
|
|
595
|
-
{ deep: true, immediate: true, flush: "post" }
|
|
596
|
-
);
|
|
597
|
-
onValidate = async () => {
|
|
598
|
-
try {
|
|
599
|
-
const result = await computedSchema.value["~standard"].validate(processedState.value);
|
|
600
|
-
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));
|
|
601
162
|
return {
|
|
602
|
-
|
|
603
|
-
|
|
163
|
+
path,
|
|
164
|
+
message: issue.message,
|
|
165
|
+
isArray
|
|
604
166
|
};
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
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;
|
|
609
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
|
+
};
|
|
610
194
|
const regle = useRootStorage({
|
|
611
195
|
scopeRules: convertedRules,
|
|
612
196
|
state: processedState,
|
|
@@ -614,7 +198,7 @@ function createUseRegleSchemaComposable(options, shortcuts) {
|
|
|
614
198
|
schemaErrors: customErrors,
|
|
615
199
|
initialState,
|
|
616
200
|
shortcuts,
|
|
617
|
-
schemaMode:
|
|
201
|
+
schemaMode: true,
|
|
618
202
|
onValidate
|
|
619
203
|
});
|
|
620
204
|
return {
|
|
@@ -625,7 +209,7 @@ function createUseRegleSchemaComposable(options, shortcuts) {
|
|
|
625
209
|
}
|
|
626
210
|
var useRegleSchema = createUseRegleSchemaComposable();
|
|
627
211
|
function withDeps(schema, depsArray) {
|
|
628
|
-
return
|
|
212
|
+
return schema;
|
|
629
213
|
}
|
|
630
214
|
function createInferValibotSchemaHelper() {
|
|
631
215
|
function inferRules(state, rulesFactory) {
|
package/package.json
CHANGED
|
@@ -1,13 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@regle/schemas",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Schemas adapter for Regle",
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"@standard-schema/spec": "1.0.0
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
"@standard-schema/spec": "1.0.0",
|
|
7
|
+
"@regle/core": "0.8.0",
|
|
8
|
+
"@regle/rules": "0.8.0"
|
|
9
|
+
},
|
|
10
|
+
"peerDependencies": {
|
|
11
|
+
"valibot": "^1.0.0-beta.11",
|
|
12
|
+
"zod": "^3.24.0"
|
|
13
|
+
},
|
|
14
|
+
"peerDependenciesMeta": {
|
|
15
|
+
"valibot": {
|
|
16
|
+
"optional": true
|
|
17
|
+
},
|
|
18
|
+
"zod": {
|
|
19
|
+
"optional": true
|
|
20
|
+
}
|
|
11
21
|
},
|
|
12
22
|
"devDependencies": {
|
|
13
23
|
"@total-typescript/ts-reset": "0.6.1",
|
|
@@ -25,7 +35,7 @@
|
|
|
25
35
|
"tsup": "8.3.5",
|
|
26
36
|
"type-fest": "4.28.0",
|
|
27
37
|
"typescript": "5.6.3",
|
|
28
|
-
"valibot": "1.0.0-beta.
|
|
38
|
+
"valibot": "1.0.0-beta.14",
|
|
29
39
|
"vitest": "2.1.8",
|
|
30
40
|
"vue": "3.5.13",
|
|
31
41
|
"vue-eslint-parser": "9.4.3",
|