@regle/core 0.0.6 → 0.0.8
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/index.cjs +133 -64
- package/dist/index.d.cts +182 -79
- package/dist/index.d.ts +182 -79
- package/dist/index.js +144 -77
- package/package.json +21 -19
package/dist/index.cjs
CHANGED
|
@@ -22,8 +22,10 @@ var src_exports = {};
|
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
InternalRuleType: () => InternalRuleType,
|
|
24
24
|
createRule: () => createRule,
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
defineRegleConfig: () => defineRegleConfig,
|
|
26
|
+
defineType: () => defineType,
|
|
27
|
+
unwrapRuleParameters: () => unwrapRuleParameters,
|
|
28
|
+
useRegle: () => useRegle
|
|
27
29
|
});
|
|
28
30
|
module.exports = __toCommonJS(src_exports);
|
|
29
31
|
|
|
@@ -47,29 +49,47 @@ function createReactiveParams(params) {
|
|
|
47
49
|
return (0, import_vue.toRef)(() => param);
|
|
48
50
|
});
|
|
49
51
|
}
|
|
52
|
+
function getFunctionParametersLength(func) {
|
|
53
|
+
const funcStr = func.toString();
|
|
54
|
+
const params = funcStr.slice(funcStr.indexOf("(") + 1, funcStr.indexOf(")")).split(",").map((param) => param.trim());
|
|
55
|
+
const defaults = params.filter((param) => param.includes("="));
|
|
56
|
+
return defaults.length + func.length;
|
|
57
|
+
}
|
|
50
58
|
|
|
51
59
|
// src/core/createRule/defineRuleProcessors.ts
|
|
52
60
|
function defineRuleProcessors(definition, ...params) {
|
|
53
61
|
const { message, validator, active, ...properties } = definition;
|
|
54
62
|
const isAsync = validator.constructor.name === "AsyncFunction";
|
|
55
|
-
const
|
|
56
|
-
|
|
63
|
+
const defaultProcessors = {
|
|
64
|
+
validator(value, ...args) {
|
|
65
|
+
return definition.validator(value, ...unwrapRuleParameters(args.length ? args : params));
|
|
66
|
+
},
|
|
67
|
+
message(value, metadata) {
|
|
57
68
|
if (typeof definition.message === "function") {
|
|
58
|
-
return definition.message(value,
|
|
69
|
+
return definition.message(value, {
|
|
70
|
+
...metadata,
|
|
71
|
+
$params: unwrapRuleParameters(metadata.$params?.length ? metadata.$params : params)
|
|
72
|
+
});
|
|
59
73
|
} else {
|
|
60
74
|
return definition.message;
|
|
61
75
|
}
|
|
62
76
|
},
|
|
63
|
-
|
|
64
|
-
return definition.validator(value, ...args.length ? args : params);
|
|
65
|
-
},
|
|
66
|
-
active(value, ...args) {
|
|
77
|
+
active(value, metadata) {
|
|
67
78
|
if (typeof definition.active === "function") {
|
|
68
|
-
return definition.active(value,
|
|
79
|
+
return definition.active(value, {
|
|
80
|
+
...metadata,
|
|
81
|
+
$params: unwrapRuleParameters(metadata.$params?.length ? metadata.$params : params)
|
|
82
|
+
});
|
|
69
83
|
} else {
|
|
70
84
|
return definition.active ?? true;
|
|
71
85
|
}
|
|
72
86
|
},
|
|
87
|
+
exec(value) {
|
|
88
|
+
return definition.validator(value, ...unwrapRuleParameters(params));
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
const processors = {
|
|
92
|
+
...defaultProcessors,
|
|
73
93
|
...properties,
|
|
74
94
|
...{
|
|
75
95
|
_validator: definition.validator,
|
|
@@ -90,7 +110,7 @@ function createRule(definition) {
|
|
|
90
110
|
let fakeParams = [];
|
|
91
111
|
const staticProcessors = defineRuleProcessors(definition, ...fakeParams);
|
|
92
112
|
const isAsync = definition.validator.constructor.name === "AsyncFunction";
|
|
93
|
-
if (definition.validator
|
|
113
|
+
if (getFunctionParametersLength(definition.validator) > 1) {
|
|
94
114
|
const ruleFactory = function(...params) {
|
|
95
115
|
return defineRuleProcessors(definition, ...params);
|
|
96
116
|
};
|
|
@@ -98,9 +118,10 @@ function createRule(definition) {
|
|
|
98
118
|
ruleFactory.message = staticProcessors.message;
|
|
99
119
|
ruleFactory.active = staticProcessors.active;
|
|
100
120
|
ruleFactory.type = staticProcessors.type;
|
|
101
|
-
ruleFactory.
|
|
102
|
-
ruleFactory.
|
|
103
|
-
ruleFactory.
|
|
121
|
+
ruleFactory.exec = staticProcessors.exec;
|
|
122
|
+
ruleFactory._validator = staticProcessors.validator;
|
|
123
|
+
ruleFactory._message = staticProcessors.message;
|
|
124
|
+
ruleFactory._active = staticProcessors.active;
|
|
104
125
|
ruleFactory._type = definition.type;
|
|
105
126
|
ruleFactory._patched = false;
|
|
106
127
|
ruleFactory._async = isAsync;
|
|
@@ -111,6 +132,9 @@ function createRule(definition) {
|
|
|
111
132
|
}
|
|
112
133
|
throw new Error("Validator must be a function");
|
|
113
134
|
}
|
|
135
|
+
function defineType(ruleName) {
|
|
136
|
+
return ruleName;
|
|
137
|
+
}
|
|
114
138
|
|
|
115
139
|
// src/core/useRegle/useRegle.ts
|
|
116
140
|
var import_vue10 = require("vue");
|
|
@@ -208,10 +232,20 @@ function useStorage() {
|
|
|
208
232
|
} else {
|
|
209
233
|
const $pending = (0, import_vue2.ref)(false);
|
|
210
234
|
const $valid = (0, import_vue2.ref)(true);
|
|
211
|
-
|
|
212
|
-
|
|
235
|
+
const $metadata = (0, import_vue2.ref)({});
|
|
236
|
+
const $validating = (0, import_vue2.ref)(false);
|
|
237
|
+
ruleStatusStorage.value.set(path, { $pending, $valid, $metadata, $validating });
|
|
238
|
+
return { $pending, $valid, $metadata, $validating };
|
|
213
239
|
}
|
|
214
240
|
}
|
|
241
|
+
(0, import_vue2.onScopeDispose)(() => {
|
|
242
|
+
ruleDeclStorage.value.clear();
|
|
243
|
+
fieldsStorage.value.clear();
|
|
244
|
+
collectionsStorage.value.clear();
|
|
245
|
+
dirtyStorage.value.clear();
|
|
246
|
+
ruleStatusStorage.value.clear();
|
|
247
|
+
arrayStatusStorage.value.clear();
|
|
248
|
+
});
|
|
215
249
|
return {
|
|
216
250
|
addRuleDeclEntry,
|
|
217
251
|
setDirtyEntry,
|
|
@@ -303,7 +337,13 @@ function extractRulesErrors(rules, externalErrors) {
|
|
|
303
337
|
return rule.$message;
|
|
304
338
|
}
|
|
305
339
|
return null;
|
|
306
|
-
}).filter((msg) => !!msg).
|
|
340
|
+
}).filter((msg) => !!msg).reduce((acc, value) => {
|
|
341
|
+
if (typeof value === "string") {
|
|
342
|
+
return acc?.concat([value]);
|
|
343
|
+
} else {
|
|
344
|
+
return acc?.concat(value);
|
|
345
|
+
}
|
|
346
|
+
}, []).concat(externalErrors ?? []);
|
|
307
347
|
}
|
|
308
348
|
function processFieldErrors(fieldStatus) {
|
|
309
349
|
if (isNestedRulesStatus(fieldStatus)) {
|
|
@@ -394,13 +434,20 @@ function createReactiveRuleStatus({
|
|
|
394
434
|
}) {
|
|
395
435
|
let scope = (0, import_vue5.effectScope)();
|
|
396
436
|
let scopeState;
|
|
397
|
-
const { $pending, $valid } = storage.trySetRuleStatusRef(
|
|
437
|
+
const { $pending, $valid, $metadata, $validating } = storage.trySetRuleStatusRef(
|
|
438
|
+
`${path}.${ruleKey}`
|
|
439
|
+
);
|
|
398
440
|
function $watch() {
|
|
399
441
|
scopeState = scope.run(() => {
|
|
442
|
+
const $defaultMetadata = (0, import_vue5.computed)(() => ({
|
|
443
|
+
$invalid: !$valid.value,
|
|
444
|
+
$params: $params.value,
|
|
445
|
+
...$metadata.value
|
|
446
|
+
}));
|
|
400
447
|
const $active = (0, import_vue5.computed)(() => {
|
|
401
448
|
if (isFormRuleDefinition(rule)) {
|
|
402
449
|
if (typeof rule.value.active === "function") {
|
|
403
|
-
return rule.value.active(state.value,
|
|
450
|
+
return rule.value.active(state.value, $defaultMetadata.value);
|
|
404
451
|
} else {
|
|
405
452
|
return rule.value.active;
|
|
406
453
|
}
|
|
@@ -410,31 +457,33 @@ function createReactiveRuleStatus({
|
|
|
410
457
|
});
|
|
411
458
|
const $message = (0, import_vue5.computed)(() => {
|
|
412
459
|
let message = "";
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
if (
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
message = customMessageRule;
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
if (isFormRuleDefinition(rule)) {
|
|
422
|
-
if (!(customMessageRule && !rule.value._patched)) {
|
|
423
|
-
if (typeof rule.value.message === "function") {
|
|
424
|
-
message = rule.value.message(state.value, ...$params.value);
|
|
460
|
+
if ($dirty.value && !$validating.value) {
|
|
461
|
+
const customMessageRule = customMessages ? customMessages[ruleKey]?.message : void 0;
|
|
462
|
+
if (customMessageRule) {
|
|
463
|
+
if (typeof customMessageRule === "function") {
|
|
464
|
+
message = customMessageRule(state.value, $defaultMetadata.value);
|
|
425
465
|
} else {
|
|
426
|
-
message =
|
|
466
|
+
message = customMessageRule;
|
|
427
467
|
}
|
|
428
468
|
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
469
|
+
if (isFormRuleDefinition(rule)) {
|
|
470
|
+
if (!(customMessageRule && !rule.value._patched)) {
|
|
471
|
+
if (typeof rule.value.message === "function") {
|
|
472
|
+
message = rule.value.message(state.value, $defaultMetadata.value);
|
|
473
|
+
} else {
|
|
474
|
+
message = rule.value.message;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
if (isEmpty(message)) {
|
|
479
|
+
message = "Error";
|
|
480
|
+
console.warn(`No error message defined for ${ruleKey}`);
|
|
481
|
+
}
|
|
433
482
|
}
|
|
434
483
|
return message;
|
|
435
484
|
});
|
|
436
485
|
const $type = (0, import_vue5.computed)(() => {
|
|
437
|
-
if (isFormRuleDefinition(rule)) {
|
|
486
|
+
if (isFormRuleDefinition(rule) && rule.value.type) {
|
|
438
487
|
return Object.values(InternalRuleType).includes(rule.value.type) ? ruleKey : rule.value.type;
|
|
439
488
|
} else {
|
|
440
489
|
return ruleKey;
|
|
@@ -469,29 +518,48 @@ function createReactiveRuleStatus({
|
|
|
469
518
|
deep: true
|
|
470
519
|
});
|
|
471
520
|
async function $validate() {
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
521
|
+
try {
|
|
522
|
+
$validating.value = true;
|
|
523
|
+
const validator = scopeState.$validator.value;
|
|
524
|
+
let ruleResult = false;
|
|
525
|
+
const resultOrPromise = validator(state.value, ...scopeState.$params.value);
|
|
526
|
+
if (resultOrPromise instanceof Promise) {
|
|
527
|
+
if ($dirty.value && !$pending.value) {
|
|
528
|
+
try {
|
|
529
|
+
$valid.value = true;
|
|
530
|
+
$pending.value = true;
|
|
531
|
+
const promiseResult = await resultOrPromise;
|
|
532
|
+
if (typeof promiseResult === "boolean") {
|
|
533
|
+
ruleResult = promiseResult;
|
|
534
|
+
} else {
|
|
535
|
+
const { $valid: $valid2, ...rest } = promiseResult;
|
|
536
|
+
ruleResult = $valid2;
|
|
537
|
+
$metadata.value = rest;
|
|
538
|
+
}
|
|
539
|
+
} catch (e) {
|
|
540
|
+
ruleResult = false;
|
|
541
|
+
} finally {
|
|
542
|
+
$pending.value = false;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
} else {
|
|
546
|
+
if (resultOrPromise != null) {
|
|
547
|
+
if (typeof resultOrPromise === "boolean") {
|
|
548
|
+
ruleResult = resultOrPromise;
|
|
549
|
+
} else {
|
|
550
|
+
const { $valid: $valid2, ...rest } = resultOrPromise;
|
|
551
|
+
ruleResult = $valid2;
|
|
552
|
+
$metadata.value = rest;
|
|
553
|
+
}
|
|
486
554
|
}
|
|
487
555
|
}
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
556
|
+
$valid.value = ruleResult;
|
|
557
|
+
if (options.$externalErrors) {
|
|
558
|
+
}
|
|
559
|
+
return ruleResult;
|
|
560
|
+
} finally {
|
|
561
|
+
$validating.value = false;
|
|
493
562
|
}
|
|
494
|
-
return ruleResult;
|
|
495
563
|
}
|
|
496
564
|
function $unwatch() {
|
|
497
565
|
$unwatchState();
|
|
@@ -577,7 +645,6 @@ function createReactiveFieldStatus({
|
|
|
577
645
|
$commit();
|
|
578
646
|
}
|
|
579
647
|
$externalErrors.value = [];
|
|
580
|
-
console.log($externalErrors.value);
|
|
581
648
|
});
|
|
582
649
|
function $unwatch() {
|
|
583
650
|
if ($rules.value) {
|
|
@@ -711,7 +778,7 @@ function createCollectionElement({
|
|
|
711
778
|
});
|
|
712
779
|
}
|
|
713
780
|
const $state = (0, import_vue7.toRefs)(value);
|
|
714
|
-
const $externalErrors = (0, import_vue7.
|
|
781
|
+
const $externalErrors = (0, import_vue7.toRef)(() => externalErrors.value?.[index]);
|
|
715
782
|
const $status = createReactiveChildrenStatus({
|
|
716
783
|
state: $state[index],
|
|
717
784
|
rulesDef: (0, import_vue7.toRef)(() => rules),
|
|
@@ -719,7 +786,7 @@ function createCollectionElement({
|
|
|
719
786
|
path: $path,
|
|
720
787
|
storage,
|
|
721
788
|
options,
|
|
722
|
-
externalErrors: $externalErrors
|
|
789
|
+
externalErrors: $externalErrors
|
|
723
790
|
});
|
|
724
791
|
if ($status) {
|
|
725
792
|
$status.$id = value[index].$id ?? $id;
|
|
@@ -1151,8 +1218,8 @@ function createUseRegleComposable(customRules, options) {
|
|
|
1151
1218
|
}
|
|
1152
1219
|
var useRegle = createUseRegleComposable();
|
|
1153
1220
|
|
|
1154
|
-
// src/core/
|
|
1155
|
-
function
|
|
1221
|
+
// src/core/defineRegleConfig.ts
|
|
1222
|
+
function defineRegleConfig({
|
|
1156
1223
|
rules,
|
|
1157
1224
|
options
|
|
1158
1225
|
}) {
|
|
@@ -1163,6 +1230,8 @@ function defineRegleOptions({
|
|
|
1163
1230
|
0 && (module.exports = {
|
|
1164
1231
|
InternalRuleType,
|
|
1165
1232
|
createRule,
|
|
1166
|
-
|
|
1167
|
-
|
|
1233
|
+
defineRegleConfig,
|
|
1234
|
+
defineType,
|
|
1235
|
+
unwrapRuleParameters,
|
|
1236
|
+
useRegle
|
|
1168
1237
|
});
|