@regle/core 0.0.4-beta.0 → 0.0.5
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 +254 -60
- package/dist/index.d.cts +84 -21
- package/dist/index.d.ts +84 -21
- package/dist/index.js +270 -75
- package/package.json +4 -3
package/dist/index.js
CHANGED
|
@@ -22,6 +22,7 @@ function createReactiveParams(params) {
|
|
|
22
22
|
// src/core/createRule/defineRuleProcessors.ts
|
|
23
23
|
function defineRuleProcessors(definition, ...params) {
|
|
24
24
|
const { message, validator, active, ...properties } = definition;
|
|
25
|
+
const isAsync = validator.constructor.name === "AsyncFunction";
|
|
25
26
|
const processors = {
|
|
26
27
|
message(value, ...args) {
|
|
27
28
|
if (typeof definition.message === "function") {
|
|
@@ -47,6 +48,7 @@ function defineRuleProcessors(definition, ...params) {
|
|
|
47
48
|
_active: definition.active,
|
|
48
49
|
_type: definition.type,
|
|
49
50
|
_patched: false,
|
|
51
|
+
_async: isAsync,
|
|
50
52
|
_params: createReactiveParams(params)
|
|
51
53
|
}
|
|
52
54
|
};
|
|
@@ -58,6 +60,7 @@ function createRule(definition) {
|
|
|
58
60
|
if (typeof definition.validator === "function") {
|
|
59
61
|
let fakeParams = [];
|
|
60
62
|
const staticProcessors = defineRuleProcessors(definition, ...fakeParams);
|
|
63
|
+
const isAsync = definition.validator.constructor.name === "AsyncFunction";
|
|
61
64
|
if (definition.validator.length > 1) {
|
|
62
65
|
const ruleFactory = function(...params) {
|
|
63
66
|
return defineRuleProcessors(definition, ...params);
|
|
@@ -71,6 +74,7 @@ function createRule(definition) {
|
|
|
71
74
|
ruleFactory._active = definition.active;
|
|
72
75
|
ruleFactory._type = definition.type;
|
|
73
76
|
ruleFactory._patched = false;
|
|
77
|
+
ruleFactory._async = isAsync;
|
|
74
78
|
return ruleFactory;
|
|
75
79
|
} else {
|
|
76
80
|
return staticProcessors;
|
|
@@ -80,7 +84,7 @@ function createRule(definition) {
|
|
|
80
84
|
}
|
|
81
85
|
|
|
82
86
|
// src/core/useRegle/useRegle.ts
|
|
83
|
-
import { computed as computed5, isRef as isRef2, shallowRef as shallowRef2, toRaw } from "vue";
|
|
87
|
+
import { computed as computed5, isRef as isRef2, shallowRef as shallowRef2, toRaw as toRaw2 } from "vue";
|
|
84
88
|
|
|
85
89
|
// src/core/useRegle/useStateProperties/useStateProperties.ts
|
|
86
90
|
import { reactive as reactive5 } from "vue";
|
|
@@ -203,16 +207,21 @@ function useErrors($regle) {
|
|
|
203
207
|
}
|
|
204
208
|
|
|
205
209
|
// src/core/useRegle/useStateProperties/createReactiveNestedStatus.ts
|
|
206
|
-
import {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
toRef as toRef4,
|
|
211
|
-
watch as watch4
|
|
212
|
-
} from "vue";
|
|
210
|
+
import { computed as computed4, effectScope as effectScope4, reactive as reactive4, toRef as toRef4, watch as watch4 } from "vue";
|
|
211
|
+
|
|
212
|
+
// src/core/useRegle/useStateProperties/createReactiveCollectionStatus.ts
|
|
213
|
+
import { nextTick, reactive as reactive3, ref as ref3, toRef as toRef3, toRefs, watch as watch3 } from "vue";
|
|
213
214
|
|
|
214
215
|
// src/core/useRegle/useStateProperties/createReactiveFieldStatus.ts
|
|
215
|
-
import {
|
|
216
|
+
import {
|
|
217
|
+
computed as computed3,
|
|
218
|
+
effectScope as effectScope3,
|
|
219
|
+
reactive as reactive2,
|
|
220
|
+
ref as ref2,
|
|
221
|
+
toRef as toRef2,
|
|
222
|
+
unref as unref2,
|
|
223
|
+
watch as watch2
|
|
224
|
+
} from "vue";
|
|
216
225
|
|
|
217
226
|
// src/core/useRegle/useStateProperties/createReactiveRuleStatus.ts
|
|
218
227
|
import { computed as computed2, effectScope as effectScope2, reactive, watch } from "vue";
|
|
@@ -232,7 +241,8 @@ function createReactiveRuleStatus({
|
|
|
232
241
|
ruleKey,
|
|
233
242
|
state,
|
|
234
243
|
path,
|
|
235
|
-
storage
|
|
244
|
+
storage,
|
|
245
|
+
options
|
|
236
246
|
}) {
|
|
237
247
|
let scope = effectScope2();
|
|
238
248
|
let scopeState;
|
|
@@ -252,7 +262,7 @@ function createReactiveRuleStatus({
|
|
|
252
262
|
});
|
|
253
263
|
const $message = computed2(() => {
|
|
254
264
|
let message = "";
|
|
255
|
-
const customMessageRule = customMessages[ruleKey]?.message;
|
|
265
|
+
const customMessageRule = customMessages ? customMessages[ruleKey]?.message : void 0;
|
|
256
266
|
if (customMessageRule) {
|
|
257
267
|
if (typeof customMessageRule === "function") {
|
|
258
268
|
message = customMessageRule(state.value, ...$params.value);
|
|
@@ -305,7 +315,6 @@ function createReactiveRuleStatus({
|
|
|
305
315
|
$path
|
|
306
316
|
};
|
|
307
317
|
});
|
|
308
|
-
$validate();
|
|
309
318
|
}
|
|
310
319
|
$watch();
|
|
311
320
|
const $unwatchState = watch(scopeState.$params, $validate, {
|
|
@@ -338,7 +347,6 @@ function createReactiveRuleStatus({
|
|
|
338
347
|
$unwatchState();
|
|
339
348
|
scope.stop();
|
|
340
349
|
scope = effectScope2();
|
|
341
|
-
scopeState = null;
|
|
342
350
|
}
|
|
343
351
|
return reactive({
|
|
344
352
|
...scopeState,
|
|
@@ -356,12 +364,14 @@ function createReactiveFieldStatus({
|
|
|
356
364
|
rulesDef,
|
|
357
365
|
customMessages,
|
|
358
366
|
path,
|
|
359
|
-
storage
|
|
367
|
+
storage,
|
|
368
|
+
options
|
|
360
369
|
}) {
|
|
361
370
|
let scope = effectScope3();
|
|
362
371
|
let scopeState;
|
|
363
372
|
const $dirty = ref2(false);
|
|
364
373
|
const $anyDirty = computed3(() => $dirty.value);
|
|
374
|
+
const triggerPunishment = ref2(false);
|
|
365
375
|
function createReactiveRulesResult() {
|
|
366
376
|
const declaredRules = rulesDef.value;
|
|
367
377
|
const storeResult = storage.checkRuleDeclEntry(path, declaredRules);
|
|
@@ -378,7 +388,8 @@ function createReactiveFieldStatus({
|
|
|
378
388
|
ruleKey,
|
|
379
389
|
state,
|
|
380
390
|
path,
|
|
381
|
-
storage
|
|
391
|
+
storage,
|
|
392
|
+
options
|
|
382
393
|
})
|
|
383
394
|
];
|
|
384
395
|
}
|
|
@@ -388,6 +399,9 @@ function createReactiveFieldStatus({
|
|
|
388
399
|
$watch();
|
|
389
400
|
if (storeResult?.valid != null) {
|
|
390
401
|
$dirty.value = storage.getDirtyState(path);
|
|
402
|
+
if ($dirty.value) {
|
|
403
|
+
$commit();
|
|
404
|
+
}
|
|
391
405
|
}
|
|
392
406
|
storage.addRuleDeclEntry(path, declaredRules);
|
|
393
407
|
}
|
|
@@ -395,10 +409,14 @@ function createReactiveFieldStatus({
|
|
|
395
409
|
storage.setDirtyEntry(path, $dirty.value);
|
|
396
410
|
});
|
|
397
411
|
const $unwatchState = watch2(state, () => {
|
|
398
|
-
if (
|
|
399
|
-
|
|
412
|
+
if (unref2(options.autoDirty)) {
|
|
413
|
+
if (!$dirty.value) {
|
|
414
|
+
$dirty.value = true;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
if (!unref2(options.lazy)) {
|
|
418
|
+
$commit();
|
|
400
419
|
}
|
|
401
|
-
$validate();
|
|
402
420
|
});
|
|
403
421
|
function $unwatch() {
|
|
404
422
|
if ($rules.value) {
|
|
@@ -411,9 +429,9 @@ function createReactiveFieldStatus({
|
|
|
411
429
|
storage.setDirtyEntry(path, $dirty.value);
|
|
412
430
|
}
|
|
413
431
|
$unwatchState();
|
|
432
|
+
$unwatchValid();
|
|
414
433
|
scope.stop();
|
|
415
434
|
scope = effectScope3();
|
|
416
|
-
scopeState = null;
|
|
417
435
|
}
|
|
418
436
|
function $watch() {
|
|
419
437
|
scopeState = scope.run(() => {
|
|
@@ -421,16 +439,30 @@ function createReactiveFieldStatus({
|
|
|
421
439
|
return $invalid.value && !$pending.value && $dirty.value;
|
|
422
440
|
});
|
|
423
441
|
const $pending = computed3(() => {
|
|
424
|
-
|
|
425
|
-
return
|
|
426
|
-
|
|
442
|
+
if (triggerPunishment.value || !unref2(options.rewardEarly)) {
|
|
443
|
+
return Object.entries($rules.value).some(([key, ruleResult]) => {
|
|
444
|
+
return ruleResult.$pending;
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
return false;
|
|
427
448
|
});
|
|
428
449
|
const $invalid = computed3(() => {
|
|
429
|
-
|
|
430
|
-
return
|
|
431
|
-
|
|
450
|
+
if (triggerPunishment.value || !unref2(options.rewardEarly)) {
|
|
451
|
+
return Object.entries($rules.value).some(([key, ruleResult]) => {
|
|
452
|
+
return !ruleResult.$valid;
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
return false;
|
|
456
|
+
});
|
|
457
|
+
const $valid = computed3(() => {
|
|
458
|
+
if (unref2(options.rewardEarly)) {
|
|
459
|
+
return Object.entries($rules.value).every(([key, ruleResult]) => {
|
|
460
|
+
return ruleResult.$valid;
|
|
461
|
+
});
|
|
462
|
+
} else {
|
|
463
|
+
return !$invalid.value;
|
|
464
|
+
}
|
|
432
465
|
});
|
|
433
|
-
const $valid = computed3(() => !$invalid.value);
|
|
434
466
|
return {
|
|
435
467
|
$error,
|
|
436
468
|
$pending,
|
|
@@ -441,15 +473,25 @@ function createReactiveFieldStatus({
|
|
|
441
473
|
}
|
|
442
474
|
const $rules = ref2();
|
|
443
475
|
createReactiveRulesResult();
|
|
476
|
+
const $unwatchValid = watch2(scopeState.$valid, (valid) => {
|
|
477
|
+
if (unref2(options.rewardEarly) && valid) {
|
|
478
|
+
triggerPunishment.value = false;
|
|
479
|
+
}
|
|
480
|
+
});
|
|
444
481
|
function $reset() {
|
|
445
482
|
$dirty.value = false;
|
|
446
483
|
}
|
|
447
484
|
function $touch() {
|
|
448
485
|
$dirty.value = true;
|
|
449
|
-
|
|
486
|
+
}
|
|
487
|
+
function $commit() {
|
|
488
|
+
Object.entries($rules.value).map(([key, rule]) => {
|
|
489
|
+
return rule.$validate();
|
|
490
|
+
});
|
|
450
491
|
}
|
|
451
492
|
async function $validate() {
|
|
452
493
|
try {
|
|
494
|
+
triggerPunishment.value = true;
|
|
453
495
|
const results = await Promise.all(
|
|
454
496
|
Object.entries($rules.value).map(([key, rule]) => {
|
|
455
497
|
return rule.$validate();
|
|
@@ -477,14 +519,56 @@ function createReactiveFieldStatus({
|
|
|
477
519
|
});
|
|
478
520
|
}
|
|
479
521
|
|
|
522
|
+
// src/utils/randomId.ts
|
|
523
|
+
function randomId() {
|
|
524
|
+
const uint32 = window.crypto.getRandomValues(new Uint32Array(1))[0];
|
|
525
|
+
return uint32.toString(8);
|
|
526
|
+
}
|
|
527
|
+
|
|
480
528
|
// src/core/useRegle/useStateProperties/createReactiveCollectionStatus.ts
|
|
481
|
-
|
|
529
|
+
function createCollectionElement({
|
|
530
|
+
path,
|
|
531
|
+
index,
|
|
532
|
+
options,
|
|
533
|
+
storage,
|
|
534
|
+
value,
|
|
535
|
+
customMessages,
|
|
536
|
+
rules
|
|
537
|
+
}) {
|
|
538
|
+
const $id = randomId();
|
|
539
|
+
const $path = `${path}.${$id}`;
|
|
540
|
+
if (!value[index].$id) {
|
|
541
|
+
Object.defineProperties(value[index], {
|
|
542
|
+
$id: {
|
|
543
|
+
value: $id,
|
|
544
|
+
enumerable: false,
|
|
545
|
+
configurable: false,
|
|
546
|
+
writable: false
|
|
547
|
+
}
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
const $state = toRefs(value);
|
|
551
|
+
const $status = createReactiveChildrenStatus({
|
|
552
|
+
state: $state[index],
|
|
553
|
+
rulesDef: toRef3(() => rules),
|
|
554
|
+
customMessages,
|
|
555
|
+
path: $path,
|
|
556
|
+
storage,
|
|
557
|
+
options
|
|
558
|
+
});
|
|
559
|
+
if ($status) {
|
|
560
|
+
$status.$id = value[index].$id ?? $id;
|
|
561
|
+
storage.addArrayStatus($status.$id, $status);
|
|
562
|
+
}
|
|
563
|
+
return $status;
|
|
564
|
+
}
|
|
482
565
|
function createReactiveCollectionStatus({
|
|
483
566
|
state,
|
|
484
567
|
rulesDef,
|
|
485
568
|
customMessages,
|
|
486
569
|
path,
|
|
487
|
-
storage
|
|
570
|
+
storage,
|
|
571
|
+
options
|
|
488
572
|
}) {
|
|
489
573
|
if (Array.isArray(state.value) && !rulesDef.value.$each) {
|
|
490
574
|
return null;
|
|
@@ -501,23 +585,72 @@ function createReactiveCollectionStatus({
|
|
|
501
585
|
rulesDef: toRef3(() => otherFields),
|
|
502
586
|
customMessages,
|
|
503
587
|
path,
|
|
504
|
-
storage
|
|
588
|
+
storage,
|
|
589
|
+
options
|
|
505
590
|
});
|
|
506
591
|
if (Array.isArray(state.value) && $each) {
|
|
507
592
|
$eachStatus.value = state.value.map((value, index) => {
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
593
|
+
if (value.$id) {
|
|
594
|
+
const previousStatus = storage.getArrayStatus(value.$id);
|
|
595
|
+
if (previousStatus) {
|
|
596
|
+
return previousStatus;
|
|
597
|
+
}
|
|
598
|
+
} else {
|
|
599
|
+
return createCollectionElement({
|
|
600
|
+
path,
|
|
601
|
+
rules: $each,
|
|
602
|
+
value: state.value,
|
|
603
|
+
index,
|
|
604
|
+
options,
|
|
605
|
+
storage
|
|
606
|
+
});
|
|
607
|
+
}
|
|
516
608
|
}).filter((f) => !!f);
|
|
517
609
|
} else {
|
|
518
610
|
$eachStatus.value = [];
|
|
519
611
|
}
|
|
520
612
|
}
|
|
613
|
+
async function updateChildrenStatus() {
|
|
614
|
+
const { $each } = rulesDef.value;
|
|
615
|
+
if (Array.isArray(state.value) && $eachStatus.value && $each) {
|
|
616
|
+
$unwatchState?.();
|
|
617
|
+
state.value.forEach((value, index) => {
|
|
618
|
+
if (value.$id) {
|
|
619
|
+
if (Array.isArray(state.value) && !state.value.find((val) => val.$id === $eachStatus.value[index].$id)) {
|
|
620
|
+
$eachStatus.value[index].$unwatch();
|
|
621
|
+
}
|
|
622
|
+
const previousStatus = storage.getArrayStatus(value.$id);
|
|
623
|
+
if (previousStatus) {
|
|
624
|
+
$eachStatus.value[index] = previousStatus;
|
|
625
|
+
} else {
|
|
626
|
+
$eachStatus.value[index].$unwatch();
|
|
627
|
+
}
|
|
628
|
+
} else {
|
|
629
|
+
const newElement = createCollectionElement({
|
|
630
|
+
value: state.value,
|
|
631
|
+
rules: $each,
|
|
632
|
+
customMessages,
|
|
633
|
+
path,
|
|
634
|
+
storage,
|
|
635
|
+
options,
|
|
636
|
+
index
|
|
637
|
+
});
|
|
638
|
+
if (newElement) {
|
|
639
|
+
$eachStatus.value[index] = newElement;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
});
|
|
643
|
+
const deletedItems = $eachStatus.value.filter(($each2) => {
|
|
644
|
+
return Array.isArray(state.value) && !state.value.find((val) => val.$id === $each2.$id);
|
|
645
|
+
});
|
|
646
|
+
deletedItems.forEach((item) => {
|
|
647
|
+
storage.deleteArrayStatus(item.$id);
|
|
648
|
+
item.$unwatch();
|
|
649
|
+
});
|
|
650
|
+
$eachStatus.value.length = state.value.length;
|
|
651
|
+
nextTick($watch);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
521
654
|
function $unwatch() {
|
|
522
655
|
if ($unwatchState) {
|
|
523
656
|
$unwatchState();
|
|
@@ -532,19 +665,42 @@ function createReactiveCollectionStatus({
|
|
|
532
665
|
}
|
|
533
666
|
}
|
|
534
667
|
function $watch() {
|
|
535
|
-
$unwatchState = watch3(
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
)
|
|
668
|
+
$unwatchState = watch3(() => state.value.length, updateChildrenStatus, {
|
|
669
|
+
flush: "sync"
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
function $touch() {
|
|
673
|
+
$fieldStatus.value.$touch();
|
|
674
|
+
$eachStatus.value.forEach(($each) => {
|
|
675
|
+
$each.$touch();
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
function $reset() {
|
|
679
|
+
$fieldStatus.value.$reset();
|
|
680
|
+
$eachStatus.value.forEach(($each) => {
|
|
681
|
+
$each.$reset();
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
async function $validate() {
|
|
685
|
+
try {
|
|
686
|
+
const results = await Promise.all(
|
|
687
|
+
$eachStatus.value.map((rule) => {
|
|
688
|
+
return rule.$validate();
|
|
689
|
+
})
|
|
690
|
+
);
|
|
691
|
+
return results.every((value) => !!value);
|
|
692
|
+
} catch (e) {
|
|
693
|
+
return false;
|
|
694
|
+
}
|
|
542
695
|
}
|
|
543
696
|
return reactive3({
|
|
544
697
|
...$fieldStatus.value,
|
|
545
698
|
$each: $eachStatus,
|
|
699
|
+
$validate,
|
|
546
700
|
$unwatch,
|
|
547
|
-
$watch
|
|
701
|
+
$watch,
|
|
702
|
+
$touch,
|
|
703
|
+
$reset
|
|
548
704
|
});
|
|
549
705
|
}
|
|
550
706
|
|
|
@@ -555,7 +711,8 @@ function createReactiveNestedStatus({
|
|
|
555
711
|
customMessages,
|
|
556
712
|
path = "",
|
|
557
713
|
rootRules,
|
|
558
|
-
storage
|
|
714
|
+
storage,
|
|
715
|
+
options
|
|
559
716
|
}) {
|
|
560
717
|
let scope = effectScope4();
|
|
561
718
|
let scopeState;
|
|
@@ -573,7 +730,8 @@ function createReactiveNestedStatus({
|
|
|
573
730
|
rulesDef: statePropRulesRef,
|
|
574
731
|
customMessages,
|
|
575
732
|
path: path ? `${path}.${statePropKey}` : statePropKey,
|
|
576
|
-
storage
|
|
733
|
+
storage,
|
|
734
|
+
options
|
|
577
735
|
})
|
|
578
736
|
];
|
|
579
737
|
}
|
|
@@ -667,11 +825,11 @@ function createReactiveNestedStatus({
|
|
|
667
825
|
}
|
|
668
826
|
scope.stop();
|
|
669
827
|
scope = effectScope4();
|
|
670
|
-
scopeState = null;
|
|
671
828
|
}
|
|
672
829
|
return reactive4({
|
|
673
830
|
...scopeState,
|
|
674
831
|
$fields,
|
|
832
|
+
$value: state,
|
|
675
833
|
$reset,
|
|
676
834
|
$touch,
|
|
677
835
|
$validate,
|
|
@@ -684,7 +842,8 @@ function createReactiveChildrenStatus({
|
|
|
684
842
|
rulesDef,
|
|
685
843
|
customMessages,
|
|
686
844
|
path,
|
|
687
|
-
storage
|
|
845
|
+
storage,
|
|
846
|
+
options
|
|
688
847
|
}) {
|
|
689
848
|
if (isCollectionRulesDef(rulesDef)) {
|
|
690
849
|
return createReactiveCollectionStatus({
|
|
@@ -692,7 +851,8 @@ function createReactiveChildrenStatus({
|
|
|
692
851
|
rulesDef,
|
|
693
852
|
customMessages,
|
|
694
853
|
path,
|
|
695
|
-
storage
|
|
854
|
+
storage,
|
|
855
|
+
options
|
|
696
856
|
});
|
|
697
857
|
} else if (isNestedRulesDef(state, rulesDef) && isRefObject(state)) {
|
|
698
858
|
return createReactiveNestedStatus({
|
|
@@ -700,7 +860,8 @@ function createReactiveChildrenStatus({
|
|
|
700
860
|
state,
|
|
701
861
|
customMessages,
|
|
702
862
|
path,
|
|
703
|
-
storage
|
|
863
|
+
storage,
|
|
864
|
+
options
|
|
704
865
|
});
|
|
705
866
|
} else if (isValidatorRulesDef(rulesDef)) {
|
|
706
867
|
return createReactiveFieldStatus({
|
|
@@ -708,14 +869,15 @@ function createReactiveChildrenStatus({
|
|
|
708
869
|
rulesDef,
|
|
709
870
|
customMessages,
|
|
710
871
|
path,
|
|
711
|
-
storage
|
|
872
|
+
storage,
|
|
873
|
+
options
|
|
712
874
|
});
|
|
713
875
|
}
|
|
714
876
|
return null;
|
|
715
877
|
}
|
|
716
878
|
|
|
717
879
|
// src/core/useStorage/useStorage.ts
|
|
718
|
-
import { ref as
|
|
880
|
+
import { ref as ref4, shallowRef } from "vue";
|
|
719
881
|
function useStorage() {
|
|
720
882
|
const ruleDeclStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
721
883
|
const fieldsStorage = shallowRef(
|
|
@@ -724,12 +886,13 @@ function useStorage() {
|
|
|
724
886
|
const collectionsStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
725
887
|
const dirtyStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
726
888
|
const ruleStatusStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
889
|
+
const arrayStatusStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
727
890
|
function getFieldsEntry($path) {
|
|
728
891
|
const existingFields = fieldsStorage.value.get($path);
|
|
729
892
|
if (existingFields) {
|
|
730
893
|
return existingFields;
|
|
731
894
|
} else {
|
|
732
|
-
const $fields =
|
|
895
|
+
const $fields = ref4({});
|
|
733
896
|
fieldsStorage.value.set($path, $fields);
|
|
734
897
|
return $fields;
|
|
735
898
|
}
|
|
@@ -739,11 +902,22 @@ function useStorage() {
|
|
|
739
902
|
if (existingEach) {
|
|
740
903
|
return existingEach;
|
|
741
904
|
} else {
|
|
742
|
-
const $each =
|
|
905
|
+
const $each = ref4([]);
|
|
743
906
|
collectionsStorage.value.set($path, $each);
|
|
744
907
|
return $each;
|
|
745
908
|
}
|
|
746
909
|
}
|
|
910
|
+
function addArrayStatus($id, value) {
|
|
911
|
+
arrayStatusStorage.value.set($id, value);
|
|
912
|
+
}
|
|
913
|
+
function getArrayStatus($id) {
|
|
914
|
+
return arrayStatusStorage.value.get($id);
|
|
915
|
+
}
|
|
916
|
+
function deleteArrayStatus($id) {
|
|
917
|
+
if ($id) {
|
|
918
|
+
arrayStatusStorage.value.delete($id);
|
|
919
|
+
}
|
|
920
|
+
}
|
|
747
921
|
function setDirtyEntry($path, dirty) {
|
|
748
922
|
dirtyStorage.value.set($path, dirty);
|
|
749
923
|
}
|
|
@@ -790,8 +964,8 @@ function useStorage() {
|
|
|
790
964
|
if (ruleStatus) {
|
|
791
965
|
return ruleStatus;
|
|
792
966
|
} else {
|
|
793
|
-
const $pending =
|
|
794
|
-
const $valid =
|
|
967
|
+
const $pending = ref4(false);
|
|
968
|
+
const $valid = ref4(true);
|
|
795
969
|
ruleStatusStorage.value.set(path, { $pending, $valid });
|
|
796
970
|
return { $pending, $valid };
|
|
797
971
|
}
|
|
@@ -803,20 +977,24 @@ function useStorage() {
|
|
|
803
977
|
getDirtyState,
|
|
804
978
|
trySetRuleStatusRef,
|
|
805
979
|
getFieldsEntry,
|
|
806
|
-
getCollectionsEntry
|
|
980
|
+
getCollectionsEntry,
|
|
981
|
+
getArrayStatus,
|
|
982
|
+
addArrayStatus,
|
|
983
|
+
deleteArrayStatus
|
|
807
984
|
};
|
|
808
985
|
}
|
|
809
986
|
|
|
810
987
|
// src/core/useRegle/useStateProperties/useStateProperties.ts
|
|
811
|
-
function useStateProperties(scopeRules, state, customRules) {
|
|
988
|
+
function useStateProperties(scopeRules, state, options, customRules) {
|
|
812
989
|
const storage = useStorage();
|
|
813
990
|
const $regle = reactive5(
|
|
814
991
|
createReactiveNestedStatus({
|
|
815
992
|
rootRules: scopeRules,
|
|
816
993
|
scopeRules,
|
|
817
994
|
state,
|
|
818
|
-
customMessages: customRules(),
|
|
819
|
-
storage
|
|
995
|
+
customMessages: customRules?.(),
|
|
996
|
+
storage,
|
|
997
|
+
options
|
|
820
998
|
})
|
|
821
999
|
);
|
|
822
1000
|
const errors = useErrors($regle);
|
|
@@ -824,22 +1002,36 @@ function useStateProperties(scopeRules, state, customRules) {
|
|
|
824
1002
|
}
|
|
825
1003
|
|
|
826
1004
|
// src/core/useRegle/useRegle.ts
|
|
827
|
-
function createUseRegleComposable(customRules) {
|
|
828
|
-
|
|
1005
|
+
function createUseRegleComposable(customRules, options) {
|
|
1006
|
+
const globalOptions = {
|
|
1007
|
+
autoDirty: options?.autoDirty ?? true,
|
|
1008
|
+
lazy: options?.lazy ?? false,
|
|
1009
|
+
rewardEarly: options?.rewardEarly ?? false
|
|
1010
|
+
};
|
|
1011
|
+
function useRegle2(state, rulesFactory, options2) {
|
|
829
1012
|
const scopeRules = isRef2(rulesFactory) ? rulesFactory : computed5(rulesFactory);
|
|
830
|
-
const
|
|
1013
|
+
const resolvedOptions = {
|
|
1014
|
+
...globalOptions,
|
|
1015
|
+
...options2
|
|
1016
|
+
};
|
|
1017
|
+
const initialState = shallowRef2(structuredClone(toRaw2(state.value)));
|
|
831
1018
|
const { $regle, errors } = useStateProperties(
|
|
832
1019
|
scopeRules,
|
|
833
1020
|
state,
|
|
1021
|
+
resolvedOptions,
|
|
834
1022
|
customRules
|
|
835
1023
|
);
|
|
836
1024
|
function resetForm() {
|
|
837
|
-
state.value =
|
|
1025
|
+
state.value = toRaw2(initialState.value);
|
|
838
1026
|
$regle.$reset();
|
|
839
1027
|
}
|
|
840
1028
|
async function validateForm() {
|
|
841
1029
|
$regle.$touch();
|
|
842
|
-
|
|
1030
|
+
const result = await $regle.$validate();
|
|
1031
|
+
if (result) {
|
|
1032
|
+
return state.value;
|
|
1033
|
+
}
|
|
1034
|
+
return false;
|
|
843
1035
|
}
|
|
844
1036
|
return {
|
|
845
1037
|
state,
|
|
@@ -849,18 +1041,21 @@ function createUseRegleComposable(customRules) {
|
|
|
849
1041
|
validateForm
|
|
850
1042
|
};
|
|
851
1043
|
}
|
|
852
|
-
return
|
|
1044
|
+
return useRegle2;
|
|
853
1045
|
}
|
|
1046
|
+
var useRegle = createUseRegleComposable();
|
|
854
1047
|
|
|
855
|
-
// src/core/
|
|
856
|
-
function
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
1048
|
+
// src/core/defineRegleOptions.ts
|
|
1049
|
+
function defineRegleOptions({
|
|
1050
|
+
rules,
|
|
1051
|
+
options
|
|
1052
|
+
}) {
|
|
1053
|
+
const useRegle2 = createUseRegleComposable(rules, options);
|
|
1054
|
+
return useRegle2;
|
|
861
1055
|
}
|
|
862
1056
|
export {
|
|
863
1057
|
InternalRuleType,
|
|
864
1058
|
createRule,
|
|
865
|
-
|
|
1059
|
+
defineRegleOptions,
|
|
1060
|
+
unwrapRuleParameters
|
|
866
1061
|
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@regle/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Vue form validator",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"lint": "eslint --ext .ts --ext .vue .",
|
|
7
|
-
"typecheck": "
|
|
7
|
+
"typecheck": "tsc --noEmit",
|
|
8
8
|
"release": "npm publish",
|
|
9
9
|
"build": "tsup",
|
|
10
10
|
"dev": "tsup --config=tsup.dev.ts --watch"
|
|
@@ -27,7 +27,8 @@
|
|
|
27
27
|
"vue-eslint-parser": "9.3.1",
|
|
28
28
|
"vue-tsc": "1.8.15",
|
|
29
29
|
"tsup": "7.2.0",
|
|
30
|
-
"@total-typescript/ts-reset": "0.5.1"
|
|
30
|
+
"@total-typescript/ts-reset": "0.5.1",
|
|
31
|
+
"type-fest": "4.4.0"
|
|
31
32
|
},
|
|
32
33
|
"type": "module",
|
|
33
34
|
"exports": {
|