@regle/core 0.0.5 → 0.0.6
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 +283 -198
- package/dist/index.d.cts +273 -14
- package/dist/index.d.ts +273 -14
- package/dist/index.js +238 -161
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -84,10 +84,118 @@ function createRule(definition) {
|
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
// src/core/useRegle/useRegle.ts
|
|
87
|
-
import { computed as
|
|
87
|
+
import { computed as computed7, isRef as isRef2, ref as ref4, shallowRef as shallowRef2, toRaw } from "vue";
|
|
88
88
|
|
|
89
89
|
// src/core/useRegle/useStateProperties/useStateProperties.ts
|
|
90
|
-
import { reactive as reactive5 } from "vue";
|
|
90
|
+
import { computed as computed6, reactive as reactive5, unref as unref3 } from "vue";
|
|
91
|
+
|
|
92
|
+
// src/core/useStorage/useStorage.ts
|
|
93
|
+
import { ref, shallowRef } from "vue";
|
|
94
|
+
function useStorage() {
|
|
95
|
+
const ruleDeclStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
96
|
+
const fieldsStorage = shallowRef(
|
|
97
|
+
/* @__PURE__ */ new Map()
|
|
98
|
+
);
|
|
99
|
+
const collectionsStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
100
|
+
const dirtyStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
101
|
+
const ruleStatusStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
102
|
+
const arrayStatusStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
103
|
+
function getFieldsEntry($path) {
|
|
104
|
+
const existingFields = fieldsStorage.value.get($path);
|
|
105
|
+
if (existingFields) {
|
|
106
|
+
return existingFields;
|
|
107
|
+
} else {
|
|
108
|
+
const $fields = ref({});
|
|
109
|
+
fieldsStorage.value.set($path, $fields);
|
|
110
|
+
return $fields;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function getCollectionsEntry($path) {
|
|
114
|
+
const existingEach = collectionsStorage.value.get($path);
|
|
115
|
+
if (existingEach) {
|
|
116
|
+
return existingEach;
|
|
117
|
+
} else {
|
|
118
|
+
const $each = ref([]);
|
|
119
|
+
collectionsStorage.value.set($path, $each);
|
|
120
|
+
return $each;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function addArrayStatus($id, value) {
|
|
124
|
+
arrayStatusStorage.value.set($id, value);
|
|
125
|
+
}
|
|
126
|
+
function getArrayStatus($id) {
|
|
127
|
+
return arrayStatusStorage.value.get($id);
|
|
128
|
+
}
|
|
129
|
+
function deleteArrayStatus($id) {
|
|
130
|
+
if ($id) {
|
|
131
|
+
arrayStatusStorage.value.delete($id);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function setDirtyEntry($path, dirty) {
|
|
135
|
+
dirtyStorage.value.set($path, dirty);
|
|
136
|
+
}
|
|
137
|
+
function getDirtyState(path) {
|
|
138
|
+
return dirtyStorage.value.get(path) ?? false;
|
|
139
|
+
}
|
|
140
|
+
function addRuleDeclEntry($path, options) {
|
|
141
|
+
ruleDeclStorage.value.set($path, options);
|
|
142
|
+
}
|
|
143
|
+
function checkRuleDeclEntry($path, newRules) {
|
|
144
|
+
const storedRulesDefs = ruleDeclStorage.value.get($path);
|
|
145
|
+
if (!storedRulesDefs)
|
|
146
|
+
return void 0;
|
|
147
|
+
const storedRules = storedRulesDefs;
|
|
148
|
+
const isValidCache = areRulesChanged(newRules, storedRules);
|
|
149
|
+
if (!isValidCache)
|
|
150
|
+
return { valid: false };
|
|
151
|
+
return { valid: true };
|
|
152
|
+
}
|
|
153
|
+
function areRulesChanged(newRules, storedRules) {
|
|
154
|
+
const storedRulesKeys = Object.keys(storedRules);
|
|
155
|
+
const newRulesKeys = Object.keys(newRules);
|
|
156
|
+
if (newRulesKeys.length !== storedRulesKeys.length)
|
|
157
|
+
return false;
|
|
158
|
+
const hasAllValidators = newRulesKeys.every((ruleKey) => storedRulesKeys.includes(ruleKey));
|
|
159
|
+
if (!hasAllValidators)
|
|
160
|
+
return false;
|
|
161
|
+
return newRulesKeys.every((ruleKey) => {
|
|
162
|
+
const newRuleElement = newRules[ruleKey];
|
|
163
|
+
const storedRuleElement = storedRules[ruleKey];
|
|
164
|
+
if (!storedRuleElement || !newRuleElement || typeof newRuleElement === "function" || typeof storedRuleElement === "function")
|
|
165
|
+
return false;
|
|
166
|
+
if (!newRuleElement._params)
|
|
167
|
+
return true;
|
|
168
|
+
return newRuleElement._params?.every((paramKey, index) => {
|
|
169
|
+
const storedParams = unwrapRuleParameters(storedRuleElement._params);
|
|
170
|
+
const newParams = unwrapRuleParameters(newRuleElement._params);
|
|
171
|
+
return storedParams?.[index] === newParams?.[index];
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
function trySetRuleStatusRef(path) {
|
|
176
|
+
const ruleStatus = ruleStatusStorage.value.get(path);
|
|
177
|
+
if (ruleStatus) {
|
|
178
|
+
return ruleStatus;
|
|
179
|
+
} else {
|
|
180
|
+
const $pending = ref(false);
|
|
181
|
+
const $valid = ref(true);
|
|
182
|
+
ruleStatusStorage.value.set(path, { $pending, $valid });
|
|
183
|
+
return { $pending, $valid };
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
addRuleDeclEntry,
|
|
188
|
+
setDirtyEntry,
|
|
189
|
+
checkRuleDeclEntry,
|
|
190
|
+
getDirtyState,
|
|
191
|
+
trySetRuleStatusRef,
|
|
192
|
+
getFieldsEntry,
|
|
193
|
+
getCollectionsEntry,
|
|
194
|
+
getArrayStatus,
|
|
195
|
+
addArrayStatus,
|
|
196
|
+
deleteArrayStatus
|
|
197
|
+
};
|
|
198
|
+
}
|
|
91
199
|
|
|
92
200
|
// src/core/useRegle/useErrors.ts
|
|
93
201
|
import { computed } from "vue";
|
|
@@ -154,14 +262,19 @@ function isFieldStatus(rule) {
|
|
|
154
262
|
return !!rule && "$rules" in rule;
|
|
155
263
|
}
|
|
156
264
|
|
|
265
|
+
// src/core/useRegle/guards/rule.errors.guards.ts
|
|
266
|
+
function isExternalErrorCollection(value) {
|
|
267
|
+
return "$each" in value || "$errors" in value;
|
|
268
|
+
}
|
|
269
|
+
|
|
157
270
|
// src/core/useRegle/useErrors.ts
|
|
158
|
-
function extractRulesErrors(rules) {
|
|
271
|
+
function extractRulesErrors(rules, externalErrors) {
|
|
159
272
|
return Object.entries(rules).map(([ruleKey, rule]) => {
|
|
160
273
|
if (!rule.$valid) {
|
|
161
274
|
return rule.$message;
|
|
162
275
|
}
|
|
163
276
|
return null;
|
|
164
|
-
}).filter((msg) => !!msg);
|
|
277
|
+
}).filter((msg) => !!msg).concat(externalErrors ?? []);
|
|
165
278
|
}
|
|
166
279
|
function processFieldErrors(fieldStatus) {
|
|
167
280
|
if (isNestedRulesStatus(fieldStatus)) {
|
|
@@ -171,8 +284,12 @@ function processFieldErrors(fieldStatus) {
|
|
|
171
284
|
$errors: fieldStatus.$rules ? extractRulesErrors(fieldStatus.$rules) : [],
|
|
172
285
|
$each: fieldStatus.$each.map(processFieldErrors)
|
|
173
286
|
};
|
|
174
|
-
} else if (isFieldStatus(fieldStatus)
|
|
175
|
-
|
|
287
|
+
} else if (isFieldStatus(fieldStatus)) {
|
|
288
|
+
if (fieldStatus.$error) {
|
|
289
|
+
return extractRulesErrors(fieldStatus.$rules, fieldStatus.$externalErrors);
|
|
290
|
+
} else {
|
|
291
|
+
return fieldStatus.$externalErrors ?? [];
|
|
292
|
+
}
|
|
176
293
|
}
|
|
177
294
|
return [];
|
|
178
295
|
}
|
|
@@ -192,8 +309,12 @@ function extractNestedErrors(fields) {
|
|
|
192
309
|
$each: extractCollectionError(fieldStatus)
|
|
193
310
|
}
|
|
194
311
|
];
|
|
195
|
-
} else if (isFieldStatus(fieldStatus)
|
|
196
|
-
|
|
312
|
+
} else if (isFieldStatus(fieldStatus)) {
|
|
313
|
+
if (fieldStatus.$error) {
|
|
314
|
+
return [fieldKey, extractRulesErrors(fieldStatus.$rules, fieldStatus.$externalErrors)];
|
|
315
|
+
} else {
|
|
316
|
+
return [fieldKey, fieldStatus.$externalErrors ?? []];
|
|
317
|
+
}
|
|
197
318
|
}
|
|
198
319
|
return [fieldKey, []];
|
|
199
320
|
})
|
|
@@ -207,21 +328,19 @@ function useErrors($regle) {
|
|
|
207
328
|
}
|
|
208
329
|
|
|
209
330
|
// src/core/useRegle/useStateProperties/createReactiveNestedStatus.ts
|
|
210
|
-
import { computed as
|
|
331
|
+
import { computed as computed5, effectScope as effectScope4, reactive as reactive4, toRef as toRef4, watch as watch4 } from "vue";
|
|
211
332
|
|
|
212
333
|
// src/core/useRegle/useStateProperties/createReactiveCollectionStatus.ts
|
|
213
334
|
import { nextTick, reactive as reactive3, ref as ref3, toRef as toRef3, toRefs, watch as watch3 } from "vue";
|
|
214
335
|
|
|
336
|
+
// src/utils/randomId.ts
|
|
337
|
+
function randomId() {
|
|
338
|
+
const uint32 = window.crypto.getRandomValues(new Uint32Array(1))[0];
|
|
339
|
+
return uint32.toString(8);
|
|
340
|
+
}
|
|
341
|
+
|
|
215
342
|
// src/core/useRegle/useStateProperties/createReactiveFieldStatus.ts
|
|
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";
|
|
343
|
+
import { computed as computed3, effectScope as effectScope3, reactive as reactive2, ref as ref2, toRef as toRef2, unref as unref2, watch as watch2 } from "vue";
|
|
225
344
|
|
|
226
345
|
// src/core/useRegle/useStateProperties/createReactiveRuleStatus.ts
|
|
227
346
|
import { computed as computed2, effectScope as effectScope2, reactive, watch } from "vue";
|
|
@@ -341,6 +460,8 @@ function createReactiveRuleStatus({
|
|
|
341
460
|
ruleResult = resultOrPromise;
|
|
342
461
|
}
|
|
343
462
|
$valid.value = ruleResult;
|
|
463
|
+
if (options.$externalErrors) {
|
|
464
|
+
}
|
|
344
465
|
return ruleResult;
|
|
345
466
|
}
|
|
346
467
|
function $unwatch() {
|
|
@@ -365,13 +486,22 @@ function createReactiveFieldStatus({
|
|
|
365
486
|
customMessages,
|
|
366
487
|
path,
|
|
367
488
|
storage,
|
|
368
|
-
options
|
|
489
|
+
options,
|
|
490
|
+
externalErrors
|
|
369
491
|
}) {
|
|
370
492
|
let scope = effectScope3();
|
|
371
493
|
let scopeState;
|
|
372
494
|
const $dirty = ref2(false);
|
|
373
495
|
const $anyDirty = computed3(() => $dirty.value);
|
|
374
496
|
const triggerPunishment = ref2(false);
|
|
497
|
+
const $externalErrors = ref2([]);
|
|
498
|
+
function collectExternalErrors() {
|
|
499
|
+
if (externalErrors.value) {
|
|
500
|
+
$externalErrors.value = externalErrors.value;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
collectExternalErrors();
|
|
504
|
+
const $unwatchExternalErrors = watch2(externalErrors, collectExternalErrors);
|
|
375
505
|
function createReactiveRulesResult() {
|
|
376
506
|
const declaredRules = rulesDef.value;
|
|
377
507
|
const storeResult = storage.checkRuleDeclEntry(path, declaredRules);
|
|
@@ -417,6 +547,8 @@ function createReactiveFieldStatus({
|
|
|
417
547
|
if (!unref2(options.lazy)) {
|
|
418
548
|
$commit();
|
|
419
549
|
}
|
|
550
|
+
$externalErrors.value = [];
|
|
551
|
+
console.log($externalErrors.value);
|
|
420
552
|
});
|
|
421
553
|
function $unwatch() {
|
|
422
554
|
if ($rules.value) {
|
|
@@ -430,6 +562,7 @@ function createReactiveFieldStatus({
|
|
|
430
562
|
}
|
|
431
563
|
$unwatchState();
|
|
432
564
|
$unwatchValid();
|
|
565
|
+
$unwatchExternalErrors();
|
|
433
566
|
scope.stop();
|
|
434
567
|
scope = effectScope3();
|
|
435
568
|
}
|
|
@@ -480,6 +613,7 @@ function createReactiveFieldStatus({
|
|
|
480
613
|
});
|
|
481
614
|
function $reset() {
|
|
482
615
|
$dirty.value = false;
|
|
616
|
+
$externalErrors.value = [];
|
|
483
617
|
}
|
|
484
618
|
function $touch() {
|
|
485
619
|
$dirty.value = true;
|
|
@@ -502,6 +636,9 @@ function createReactiveFieldStatus({
|
|
|
502
636
|
return false;
|
|
503
637
|
}
|
|
504
638
|
}
|
|
639
|
+
function $clearExternalErrors() {
|
|
640
|
+
$externalErrors.value = [];
|
|
641
|
+
}
|
|
505
642
|
return reactive2({
|
|
506
643
|
$dirty,
|
|
507
644
|
$anyDirty,
|
|
@@ -509,22 +646,18 @@ function createReactiveFieldStatus({
|
|
|
509
646
|
$error: scopeState.$error,
|
|
510
647
|
$pending: scopeState.$pending,
|
|
511
648
|
$valid: scopeState.$valid,
|
|
649
|
+
$externalErrors,
|
|
512
650
|
$value: state,
|
|
513
651
|
$rules,
|
|
514
652
|
$reset,
|
|
515
653
|
$touch,
|
|
516
654
|
$validate,
|
|
517
655
|
$unwatch,
|
|
518
|
-
$watch
|
|
656
|
+
$watch,
|
|
657
|
+
$clearExternalErrors
|
|
519
658
|
});
|
|
520
659
|
}
|
|
521
660
|
|
|
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
|
-
|
|
528
661
|
// src/core/useRegle/useStateProperties/createReactiveCollectionStatus.ts
|
|
529
662
|
function createCollectionElement({
|
|
530
663
|
path,
|
|
@@ -533,7 +666,8 @@ function createCollectionElement({
|
|
|
533
666
|
storage,
|
|
534
667
|
value,
|
|
535
668
|
customMessages,
|
|
536
|
-
rules
|
|
669
|
+
rules,
|
|
670
|
+
externalErrors
|
|
537
671
|
}) {
|
|
538
672
|
const $id = randomId();
|
|
539
673
|
const $path = `${path}.${$id}`;
|
|
@@ -548,13 +682,15 @@ function createCollectionElement({
|
|
|
548
682
|
});
|
|
549
683
|
}
|
|
550
684
|
const $state = toRefs(value);
|
|
685
|
+
const $externalErrors = toRefs(externalErrors.value ?? reactive3([void 0]));
|
|
551
686
|
const $status = createReactiveChildrenStatus({
|
|
552
687
|
state: $state[index],
|
|
553
688
|
rulesDef: toRef3(() => rules),
|
|
554
689
|
customMessages,
|
|
555
690
|
path: $path,
|
|
556
691
|
storage,
|
|
557
|
-
options
|
|
692
|
+
options,
|
|
693
|
+
externalErrors: $externalErrors?.[index]
|
|
558
694
|
});
|
|
559
695
|
if ($status) {
|
|
560
696
|
$status.$id = value[index].$id ?? $id;
|
|
@@ -568,7 +704,8 @@ function createReactiveCollectionStatus({
|
|
|
568
704
|
customMessages,
|
|
569
705
|
path,
|
|
570
706
|
storage,
|
|
571
|
-
options
|
|
707
|
+
options,
|
|
708
|
+
externalErrors
|
|
572
709
|
}) {
|
|
573
710
|
if (Array.isArray(state.value) && !rulesDef.value.$each) {
|
|
574
711
|
return null;
|
|
@@ -580,13 +717,28 @@ function createReactiveCollectionStatus({
|
|
|
580
717
|
$watch();
|
|
581
718
|
function createStatus() {
|
|
582
719
|
const { $each, ...otherFields } = rulesDef.value;
|
|
720
|
+
const $externalErrorsField = toRef3(() => {
|
|
721
|
+
if (externalErrors.value) {
|
|
722
|
+
if (isExternalErrorCollection(externalErrors.value)) {
|
|
723
|
+
return externalErrors.value.$errors;
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
});
|
|
727
|
+
const $externalErrorsEach = toRef3(() => {
|
|
728
|
+
if (externalErrors.value) {
|
|
729
|
+
if (isExternalErrorCollection(externalErrors.value)) {
|
|
730
|
+
return externalErrors.value.$each;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
});
|
|
583
734
|
$fieldStatus.value = createReactiveFieldStatus({
|
|
584
735
|
state,
|
|
585
736
|
rulesDef: toRef3(() => otherFields),
|
|
586
737
|
customMessages,
|
|
587
738
|
path,
|
|
588
739
|
storage,
|
|
589
|
-
options
|
|
740
|
+
options,
|
|
741
|
+
externalErrors: $externalErrorsField
|
|
590
742
|
});
|
|
591
743
|
if (Array.isArray(state.value) && $each) {
|
|
592
744
|
$eachStatus.value = state.value.map((value, index) => {
|
|
@@ -602,7 +754,8 @@ function createReactiveCollectionStatus({
|
|
|
602
754
|
value: state.value,
|
|
603
755
|
index,
|
|
604
756
|
options,
|
|
605
|
-
storage
|
|
757
|
+
storage,
|
|
758
|
+
externalErrors: $externalErrorsEach
|
|
606
759
|
});
|
|
607
760
|
}
|
|
608
761
|
}).filter((f) => !!f);
|
|
@@ -626,6 +779,13 @@ function createReactiveCollectionStatus({
|
|
|
626
779
|
$eachStatus.value[index].$unwatch();
|
|
627
780
|
}
|
|
628
781
|
} else {
|
|
782
|
+
const $externalErrorsEach = toRef3(() => {
|
|
783
|
+
if (externalErrors.value) {
|
|
784
|
+
if ("$each" in externalErrors.value) {
|
|
785
|
+
return externalErrors.value.$each;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
});
|
|
629
789
|
const newElement = createCollectionElement({
|
|
630
790
|
value: state.value,
|
|
631
791
|
rules: $each,
|
|
@@ -633,7 +793,8 @@ function createReactiveCollectionStatus({
|
|
|
633
793
|
path,
|
|
634
794
|
storage,
|
|
635
795
|
options,
|
|
636
|
-
index
|
|
796
|
+
index,
|
|
797
|
+
externalErrors: $externalErrorsEach
|
|
637
798
|
});
|
|
638
799
|
if (newElement) {
|
|
639
800
|
$eachStatus.value[index] = newElement;
|
|
@@ -712,7 +873,8 @@ function createReactiveNestedStatus({
|
|
|
712
873
|
path = "",
|
|
713
874
|
rootRules,
|
|
714
875
|
storage,
|
|
715
|
-
options
|
|
876
|
+
options,
|
|
877
|
+
externalErrors
|
|
716
878
|
}) {
|
|
717
879
|
let scope = effectScope4();
|
|
718
880
|
let scopeState;
|
|
@@ -723,6 +885,7 @@ function createReactiveNestedStatus({
|
|
|
723
885
|
if (statePropRules) {
|
|
724
886
|
const stateRef = toRef4(state.value, statePropKey);
|
|
725
887
|
const statePropRulesRef = toRef4(() => statePropRules);
|
|
888
|
+
const $externalErrors = toRef4(() => externalErrors.value?.[statePropKey]);
|
|
726
889
|
return [
|
|
727
890
|
statePropKey,
|
|
728
891
|
createReactiveChildrenStatus({
|
|
@@ -731,7 +894,8 @@ function createReactiveNestedStatus({
|
|
|
731
894
|
customMessages,
|
|
732
895
|
path: path ? `${path}.${statePropKey}` : statePropKey,
|
|
733
896
|
storage,
|
|
734
|
-
options
|
|
897
|
+
options,
|
|
898
|
+
externalErrors: $externalErrors
|
|
735
899
|
})
|
|
736
900
|
];
|
|
737
901
|
}
|
|
@@ -778,28 +942,28 @@ function createReactiveNestedStatus({
|
|
|
778
942
|
);
|
|
779
943
|
}
|
|
780
944
|
scopeState = scope.run(() => {
|
|
781
|
-
const $dirty =
|
|
945
|
+
const $dirty = computed5(() => {
|
|
782
946
|
return Object.entries($fields.value).every(([key, statusOrField]) => {
|
|
783
947
|
return statusOrField.$dirty;
|
|
784
948
|
});
|
|
785
949
|
});
|
|
786
|
-
const $anyDirty =
|
|
950
|
+
const $anyDirty = computed5(() => {
|
|
787
951
|
return Object.entries($fields.value).some(([key, statusOrField]) => {
|
|
788
952
|
return statusOrField.$dirty;
|
|
789
953
|
});
|
|
790
954
|
});
|
|
791
|
-
const $invalid =
|
|
955
|
+
const $invalid = computed5(() => {
|
|
792
956
|
return Object.entries($fields.value).some(([key, statusOrField]) => {
|
|
793
957
|
return statusOrField.$invalid;
|
|
794
958
|
});
|
|
795
959
|
});
|
|
796
|
-
const $valid =
|
|
797
|
-
const $error =
|
|
960
|
+
const $valid = computed5(() => !$invalid.value);
|
|
961
|
+
const $error = computed5(() => {
|
|
798
962
|
return Object.entries($fields.value).some(([key, statusOrField]) => {
|
|
799
963
|
return statusOrField.$error;
|
|
800
964
|
});
|
|
801
965
|
});
|
|
802
|
-
const $pending =
|
|
966
|
+
const $pending = computed5(() => {
|
|
803
967
|
return Object.entries($fields.value).some(([key, statusOrField]) => {
|
|
804
968
|
return statusOrField.$pending;
|
|
805
969
|
});
|
|
@@ -826,6 +990,11 @@ function createReactiveNestedStatus({
|
|
|
826
990
|
scope.stop();
|
|
827
991
|
scope = effectScope4();
|
|
828
992
|
}
|
|
993
|
+
function $clearExternalErrors() {
|
|
994
|
+
Object.entries($fields.value).forEach(([_, field]) => {
|
|
995
|
+
field.$clearExternalErrors();
|
|
996
|
+
});
|
|
997
|
+
}
|
|
829
998
|
return reactive4({
|
|
830
999
|
...scopeState,
|
|
831
1000
|
$fields,
|
|
@@ -834,7 +1003,8 @@ function createReactiveNestedStatus({
|
|
|
834
1003
|
$touch,
|
|
835
1004
|
$validate,
|
|
836
1005
|
$unwatch,
|
|
837
|
-
$watch
|
|
1006
|
+
$watch,
|
|
1007
|
+
$clearExternalErrors
|
|
838
1008
|
});
|
|
839
1009
|
}
|
|
840
1010
|
function createReactiveChildrenStatus({
|
|
@@ -843,7 +1013,8 @@ function createReactiveChildrenStatus({
|
|
|
843
1013
|
customMessages,
|
|
844
1014
|
path,
|
|
845
1015
|
storage,
|
|
846
|
-
options
|
|
1016
|
+
options,
|
|
1017
|
+
externalErrors
|
|
847
1018
|
}) {
|
|
848
1019
|
if (isCollectionRulesDef(rulesDef)) {
|
|
849
1020
|
return createReactiveCollectionStatus({
|
|
@@ -852,7 +1023,8 @@ function createReactiveChildrenStatus({
|
|
|
852
1023
|
customMessages,
|
|
853
1024
|
path,
|
|
854
1025
|
storage,
|
|
855
|
-
options
|
|
1026
|
+
options,
|
|
1027
|
+
externalErrors
|
|
856
1028
|
});
|
|
857
1029
|
} else if (isNestedRulesDef(state, rulesDef) && isRefObject(state)) {
|
|
858
1030
|
return createReactiveNestedStatus({
|
|
@@ -861,7 +1033,8 @@ function createReactiveChildrenStatus({
|
|
|
861
1033
|
customMessages,
|
|
862
1034
|
path,
|
|
863
1035
|
storage,
|
|
864
|
-
options
|
|
1036
|
+
options,
|
|
1037
|
+
externalErrors
|
|
865
1038
|
});
|
|
866
1039
|
} else if (isValidatorRulesDef(rulesDef)) {
|
|
867
1040
|
return createReactiveFieldStatus({
|
|
@@ -870,123 +1043,17 @@ function createReactiveChildrenStatus({
|
|
|
870
1043
|
customMessages,
|
|
871
1044
|
path,
|
|
872
1045
|
storage,
|
|
873
|
-
options
|
|
1046
|
+
options,
|
|
1047
|
+
externalErrors
|
|
874
1048
|
});
|
|
875
1049
|
}
|
|
876
1050
|
return null;
|
|
877
1051
|
}
|
|
878
1052
|
|
|
879
|
-
// src/core/useStorage/useStorage.ts
|
|
880
|
-
import { ref as ref4, shallowRef } from "vue";
|
|
881
|
-
function useStorage() {
|
|
882
|
-
const ruleDeclStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
883
|
-
const fieldsStorage = shallowRef(
|
|
884
|
-
/* @__PURE__ */ new Map()
|
|
885
|
-
);
|
|
886
|
-
const collectionsStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
887
|
-
const dirtyStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
888
|
-
const ruleStatusStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
889
|
-
const arrayStatusStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
890
|
-
function getFieldsEntry($path) {
|
|
891
|
-
const existingFields = fieldsStorage.value.get($path);
|
|
892
|
-
if (existingFields) {
|
|
893
|
-
return existingFields;
|
|
894
|
-
} else {
|
|
895
|
-
const $fields = ref4({});
|
|
896
|
-
fieldsStorage.value.set($path, $fields);
|
|
897
|
-
return $fields;
|
|
898
|
-
}
|
|
899
|
-
}
|
|
900
|
-
function getCollectionsEntry($path) {
|
|
901
|
-
const existingEach = collectionsStorage.value.get($path);
|
|
902
|
-
if (existingEach) {
|
|
903
|
-
return existingEach;
|
|
904
|
-
} else {
|
|
905
|
-
const $each = ref4([]);
|
|
906
|
-
collectionsStorage.value.set($path, $each);
|
|
907
|
-
return $each;
|
|
908
|
-
}
|
|
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
|
-
}
|
|
921
|
-
function setDirtyEntry($path, dirty) {
|
|
922
|
-
dirtyStorage.value.set($path, dirty);
|
|
923
|
-
}
|
|
924
|
-
function getDirtyState(path) {
|
|
925
|
-
return dirtyStorage.value.get(path) ?? false;
|
|
926
|
-
}
|
|
927
|
-
function addRuleDeclEntry($path, options) {
|
|
928
|
-
ruleDeclStorage.value.set($path, options);
|
|
929
|
-
}
|
|
930
|
-
function checkRuleDeclEntry($path, newRules) {
|
|
931
|
-
const storedRulesDefs = ruleDeclStorage.value.get($path);
|
|
932
|
-
if (!storedRulesDefs)
|
|
933
|
-
return void 0;
|
|
934
|
-
const storedRules = storedRulesDefs;
|
|
935
|
-
const isValidCache = areRulesChanged(newRules, storedRules);
|
|
936
|
-
if (!isValidCache)
|
|
937
|
-
return { valid: false };
|
|
938
|
-
return { valid: true };
|
|
939
|
-
}
|
|
940
|
-
function areRulesChanged(newRules, storedRules) {
|
|
941
|
-
const storedRulesKeys = Object.keys(storedRules);
|
|
942
|
-
const newRulesKeys = Object.keys(newRules);
|
|
943
|
-
if (newRulesKeys.length !== storedRulesKeys.length)
|
|
944
|
-
return false;
|
|
945
|
-
const hasAllValidators = newRulesKeys.every((ruleKey) => storedRulesKeys.includes(ruleKey));
|
|
946
|
-
if (!hasAllValidators)
|
|
947
|
-
return false;
|
|
948
|
-
return newRulesKeys.every((ruleKey) => {
|
|
949
|
-
const newRuleElement = newRules[ruleKey];
|
|
950
|
-
const storedRuleElement = storedRules[ruleKey];
|
|
951
|
-
if (!storedRuleElement || !newRuleElement || typeof newRuleElement === "function" || typeof storedRuleElement === "function")
|
|
952
|
-
return false;
|
|
953
|
-
if (!newRuleElement._params)
|
|
954
|
-
return true;
|
|
955
|
-
return newRuleElement._params?.every((paramKey, index) => {
|
|
956
|
-
const storedParams = unwrapRuleParameters(storedRuleElement._params);
|
|
957
|
-
const newParams = unwrapRuleParameters(newRuleElement._params);
|
|
958
|
-
return storedParams?.[index] === newParams?.[index];
|
|
959
|
-
});
|
|
960
|
-
});
|
|
961
|
-
}
|
|
962
|
-
function trySetRuleStatusRef(path) {
|
|
963
|
-
const ruleStatus = ruleStatusStorage.value.get(path);
|
|
964
|
-
if (ruleStatus) {
|
|
965
|
-
return ruleStatus;
|
|
966
|
-
} else {
|
|
967
|
-
const $pending = ref4(false);
|
|
968
|
-
const $valid = ref4(true);
|
|
969
|
-
ruleStatusStorage.value.set(path, { $pending, $valid });
|
|
970
|
-
return { $pending, $valid };
|
|
971
|
-
}
|
|
972
|
-
}
|
|
973
|
-
return {
|
|
974
|
-
addRuleDeclEntry,
|
|
975
|
-
setDirtyEntry,
|
|
976
|
-
checkRuleDeclEntry,
|
|
977
|
-
getDirtyState,
|
|
978
|
-
trySetRuleStatusRef,
|
|
979
|
-
getFieldsEntry,
|
|
980
|
-
getCollectionsEntry,
|
|
981
|
-
getArrayStatus,
|
|
982
|
-
addArrayStatus,
|
|
983
|
-
deleteArrayStatus
|
|
984
|
-
};
|
|
985
|
-
}
|
|
986
|
-
|
|
987
1053
|
// src/core/useRegle/useStateProperties/useStateProperties.ts
|
|
988
1054
|
function useStateProperties(scopeRules, state, options, customRules) {
|
|
989
1055
|
const storage = useStorage();
|
|
1056
|
+
const externalErrors = computed6(() => unref3(options.$externalErrors));
|
|
990
1057
|
const $regle = reactive5(
|
|
991
1058
|
createReactiveNestedStatus({
|
|
992
1059
|
rootRules: scopeRules,
|
|
@@ -994,7 +1061,8 @@ function useStateProperties(scopeRules, state, options, customRules) {
|
|
|
994
1061
|
state,
|
|
995
1062
|
customMessages: customRules?.(),
|
|
996
1063
|
storage,
|
|
997
|
-
options
|
|
1064
|
+
options,
|
|
1065
|
+
externalErrors
|
|
998
1066
|
})
|
|
999
1067
|
);
|
|
1000
1068
|
const errors = useErrors($regle);
|
|
@@ -1009,22 +1077,29 @@ function createUseRegleComposable(customRules, options) {
|
|
|
1009
1077
|
rewardEarly: options?.rewardEarly ?? false
|
|
1010
1078
|
};
|
|
1011
1079
|
function useRegle2(state, rulesFactory, options2) {
|
|
1012
|
-
const scopeRules = isRef2(rulesFactory) ? rulesFactory :
|
|
1080
|
+
const scopeRules = isRef2(rulesFactory) ? rulesFactory : computed7(rulesFactory);
|
|
1013
1081
|
const resolvedOptions = {
|
|
1014
1082
|
...globalOptions,
|
|
1015
1083
|
...options2
|
|
1016
1084
|
};
|
|
1017
|
-
const
|
|
1085
|
+
const processedState = ref4(state);
|
|
1086
|
+
const initialState = shallowRef2(structuredClone(toRaw(processedState.value)));
|
|
1018
1087
|
const { $regle, errors } = useStateProperties(
|
|
1019
1088
|
scopeRules,
|
|
1020
|
-
|
|
1089
|
+
processedState,
|
|
1021
1090
|
resolvedOptions,
|
|
1022
1091
|
customRules
|
|
1023
1092
|
);
|
|
1024
1093
|
function resetForm() {
|
|
1025
|
-
state.value =
|
|
1094
|
+
state.value = toRaw(initialState.value);
|
|
1026
1095
|
$regle.$reset();
|
|
1027
1096
|
}
|
|
1097
|
+
const $valid = computed7(() => {
|
|
1098
|
+
return $regle.$valid && $regle.$dirty && !$regle.$pending;
|
|
1099
|
+
});
|
|
1100
|
+
const $invalid = computed7(() => {
|
|
1101
|
+
return $regle.$invalid && $regle.$dirty || $regle.$pending;
|
|
1102
|
+
});
|
|
1028
1103
|
async function validateForm() {
|
|
1029
1104
|
$regle.$touch();
|
|
1030
1105
|
const result = await $regle.$validate();
|
|
@@ -1034,11 +1109,13 @@ function createUseRegleComposable(customRules, options) {
|
|
|
1034
1109
|
return false;
|
|
1035
1110
|
}
|
|
1036
1111
|
return {
|
|
1037
|
-
state,
|
|
1112
|
+
$state: state,
|
|
1038
1113
|
$regle,
|
|
1039
|
-
errors,
|
|
1114
|
+
$errors: errors,
|
|
1040
1115
|
resetForm,
|
|
1041
|
-
validateForm
|
|
1116
|
+
validateForm,
|
|
1117
|
+
$valid,
|
|
1118
|
+
$invalid
|
|
1042
1119
|
};
|
|
1043
1120
|
}
|
|
1044
1121
|
return useRegle2;
|