@regle/core 0.0.5-beta.0 → 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 +319 -210
- package/dist/index.d.cts +288 -14
- package/dist/index.d.ts +288 -14
- package/dist/index.js +276 -175
- 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
|
-
import { reactive as reactive3, ref as ref3, toRef as toRef3, toRefs, watch as watch3 } from "vue";
|
|
334
|
+
import { nextTick, reactive as reactive3, ref as ref3, toRef as toRef3, toRefs, watch as watch3 } from "vue";
|
|
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
|
+
}
|
|
214
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);
|
|
@@ -399,6 +529,9 @@ function createReactiveFieldStatus({
|
|
|
399
529
|
$watch();
|
|
400
530
|
if (storeResult?.valid != null) {
|
|
401
531
|
$dirty.value = storage.getDirtyState(path);
|
|
532
|
+
if ($dirty.value) {
|
|
533
|
+
$commit();
|
|
534
|
+
}
|
|
402
535
|
}
|
|
403
536
|
storage.addRuleDeclEntry(path, declaredRules);
|
|
404
537
|
}
|
|
@@ -414,6 +547,8 @@ function createReactiveFieldStatus({
|
|
|
414
547
|
if (!unref2(options.lazy)) {
|
|
415
548
|
$commit();
|
|
416
549
|
}
|
|
550
|
+
$externalErrors.value = [];
|
|
551
|
+
console.log($externalErrors.value);
|
|
417
552
|
});
|
|
418
553
|
function $unwatch() {
|
|
419
554
|
if ($rules.value) {
|
|
@@ -427,6 +562,7 @@ function createReactiveFieldStatus({
|
|
|
427
562
|
}
|
|
428
563
|
$unwatchState();
|
|
429
564
|
$unwatchValid();
|
|
565
|
+
$unwatchExternalErrors();
|
|
430
566
|
scope.stop();
|
|
431
567
|
scope = effectScope3();
|
|
432
568
|
}
|
|
@@ -477,6 +613,7 @@ function createReactiveFieldStatus({
|
|
|
477
613
|
});
|
|
478
614
|
function $reset() {
|
|
479
615
|
$dirty.value = false;
|
|
616
|
+
$externalErrors.value = [];
|
|
480
617
|
}
|
|
481
618
|
function $touch() {
|
|
482
619
|
$dirty.value = true;
|
|
@@ -499,6 +636,9 @@ function createReactiveFieldStatus({
|
|
|
499
636
|
return false;
|
|
500
637
|
}
|
|
501
638
|
}
|
|
639
|
+
function $clearExternalErrors() {
|
|
640
|
+
$externalErrors.value = [];
|
|
641
|
+
}
|
|
502
642
|
return reactive2({
|
|
503
643
|
$dirty,
|
|
504
644
|
$anyDirty,
|
|
@@ -506,22 +646,18 @@ function createReactiveFieldStatus({
|
|
|
506
646
|
$error: scopeState.$error,
|
|
507
647
|
$pending: scopeState.$pending,
|
|
508
648
|
$valid: scopeState.$valid,
|
|
649
|
+
$externalErrors,
|
|
509
650
|
$value: state,
|
|
510
651
|
$rules,
|
|
511
652
|
$reset,
|
|
512
653
|
$touch,
|
|
513
654
|
$validate,
|
|
514
655
|
$unwatch,
|
|
515
|
-
$watch
|
|
656
|
+
$watch,
|
|
657
|
+
$clearExternalErrors
|
|
516
658
|
});
|
|
517
659
|
}
|
|
518
660
|
|
|
519
|
-
// src/utils/randomId.ts
|
|
520
|
-
function randomId() {
|
|
521
|
-
const uint32 = window.crypto.getRandomValues(new Uint32Array(1))[0];
|
|
522
|
-
return uint32.toString(8);
|
|
523
|
-
}
|
|
524
|
-
|
|
525
661
|
// src/core/useRegle/useStateProperties/createReactiveCollectionStatus.ts
|
|
526
662
|
function createCollectionElement({
|
|
527
663
|
path,
|
|
@@ -530,25 +666,31 @@ function createCollectionElement({
|
|
|
530
666
|
storage,
|
|
531
667
|
value,
|
|
532
668
|
customMessages,
|
|
533
|
-
rules
|
|
669
|
+
rules,
|
|
670
|
+
externalErrors
|
|
534
671
|
}) {
|
|
535
|
-
const $path = `${path}.${index}`;
|
|
536
672
|
const $id = randomId();
|
|
673
|
+
const $path = `${path}.${$id}`;
|
|
537
674
|
if (!value[index].$id) {
|
|
538
675
|
Object.defineProperties(value[index], {
|
|
539
676
|
$id: {
|
|
540
|
-
value: $id
|
|
677
|
+
value: $id,
|
|
678
|
+
enumerable: false,
|
|
679
|
+
configurable: false,
|
|
680
|
+
writable: false
|
|
541
681
|
}
|
|
542
682
|
});
|
|
543
683
|
}
|
|
544
684
|
const $state = toRefs(value);
|
|
685
|
+
const $externalErrors = toRefs(externalErrors.value ?? reactive3([void 0]));
|
|
545
686
|
const $status = createReactiveChildrenStatus({
|
|
546
687
|
state: $state[index],
|
|
547
688
|
rulesDef: toRef3(() => rules),
|
|
548
689
|
customMessages,
|
|
549
690
|
path: $path,
|
|
550
691
|
storage,
|
|
551
|
-
options
|
|
692
|
+
options,
|
|
693
|
+
externalErrors: $externalErrors?.[index]
|
|
552
694
|
});
|
|
553
695
|
if ($status) {
|
|
554
696
|
$status.$id = value[index].$id ?? $id;
|
|
@@ -562,7 +704,8 @@ function createReactiveCollectionStatus({
|
|
|
562
704
|
customMessages,
|
|
563
705
|
path,
|
|
564
706
|
storage,
|
|
565
|
-
options
|
|
707
|
+
options,
|
|
708
|
+
externalErrors
|
|
566
709
|
}) {
|
|
567
710
|
if (Array.isArray(state.value) && !rulesDef.value.$each) {
|
|
568
711
|
return null;
|
|
@@ -574,39 +717,75 @@ function createReactiveCollectionStatus({
|
|
|
574
717
|
$watch();
|
|
575
718
|
function createStatus() {
|
|
576
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
|
+
});
|
|
577
734
|
$fieldStatus.value = createReactiveFieldStatus({
|
|
578
735
|
state,
|
|
579
736
|
rulesDef: toRef3(() => otherFields),
|
|
580
737
|
customMessages,
|
|
581
738
|
path,
|
|
582
739
|
storage,
|
|
583
|
-
options
|
|
740
|
+
options,
|
|
741
|
+
externalErrors: $externalErrorsField
|
|
584
742
|
});
|
|
585
743
|
if (Array.isArray(state.value) && $each) {
|
|
586
744
|
$eachStatus.value = state.value.map((value, index) => {
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
745
|
+
if (value.$id) {
|
|
746
|
+
const previousStatus = storage.getArrayStatus(value.$id);
|
|
747
|
+
if (previousStatus) {
|
|
748
|
+
return previousStatus;
|
|
749
|
+
}
|
|
750
|
+
} else {
|
|
751
|
+
return createCollectionElement({
|
|
752
|
+
path,
|
|
753
|
+
rules: $each,
|
|
754
|
+
value: state.value,
|
|
755
|
+
index,
|
|
756
|
+
options,
|
|
757
|
+
storage,
|
|
758
|
+
externalErrors: $externalErrorsEach
|
|
759
|
+
});
|
|
760
|
+
}
|
|
595
761
|
}).filter((f) => !!f);
|
|
596
762
|
} else {
|
|
597
763
|
$eachStatus.value = [];
|
|
598
764
|
}
|
|
599
765
|
}
|
|
600
|
-
function updateChildrenStatus() {
|
|
766
|
+
async function updateChildrenStatus() {
|
|
601
767
|
const { $each } = rulesDef.value;
|
|
602
768
|
if (Array.isArray(state.value) && $eachStatus.value && $each) {
|
|
769
|
+
$unwatchState?.();
|
|
603
770
|
state.value.forEach((value, index) => {
|
|
604
771
|
if (value.$id) {
|
|
772
|
+
if (Array.isArray(state.value) && !state.value.find((val) => val.$id === $eachStatus.value[index].$id)) {
|
|
773
|
+
$eachStatus.value[index].$unwatch();
|
|
774
|
+
}
|
|
605
775
|
const previousStatus = storage.getArrayStatus(value.$id);
|
|
606
776
|
if (previousStatus) {
|
|
607
777
|
$eachStatus.value[index] = previousStatus;
|
|
778
|
+
} else {
|
|
779
|
+
$eachStatus.value[index].$unwatch();
|
|
608
780
|
}
|
|
609
781
|
} else {
|
|
782
|
+
const $externalErrorsEach = toRef3(() => {
|
|
783
|
+
if (externalErrors.value) {
|
|
784
|
+
if ("$each" in externalErrors.value) {
|
|
785
|
+
return externalErrors.value.$each;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
});
|
|
610
789
|
const newElement = createCollectionElement({
|
|
611
790
|
value: state.value,
|
|
612
791
|
rules: $each,
|
|
@@ -614,19 +793,23 @@ function createReactiveCollectionStatus({
|
|
|
614
793
|
path,
|
|
615
794
|
storage,
|
|
616
795
|
options,
|
|
617
|
-
index
|
|
796
|
+
index,
|
|
797
|
+
externalErrors: $externalErrorsEach
|
|
618
798
|
});
|
|
619
799
|
if (newElement) {
|
|
620
800
|
$eachStatus.value[index] = newElement;
|
|
621
801
|
}
|
|
622
802
|
}
|
|
623
803
|
});
|
|
624
|
-
}
|
|
625
|
-
if ($eachStatus.value) {
|
|
626
804
|
const deletedItems = $eachStatus.value.filter(($each2) => {
|
|
627
805
|
return Array.isArray(state.value) && !state.value.find((val) => val.$id === $each2.$id);
|
|
628
806
|
});
|
|
629
|
-
deletedItems.forEach((item) =>
|
|
807
|
+
deletedItems.forEach((item) => {
|
|
808
|
+
storage.deleteArrayStatus(item.$id);
|
|
809
|
+
item.$unwatch();
|
|
810
|
+
});
|
|
811
|
+
$eachStatus.value.length = state.value.length;
|
|
812
|
+
nextTick($watch);
|
|
630
813
|
}
|
|
631
814
|
}
|
|
632
815
|
function $unwatch() {
|
|
@@ -643,13 +826,9 @@ function createReactiveCollectionStatus({
|
|
|
643
826
|
}
|
|
644
827
|
}
|
|
645
828
|
function $watch() {
|
|
646
|
-
$unwatchState = watch3(
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
updateChildrenStatus();
|
|
650
|
-
},
|
|
651
|
-
{ deep: true, flush: "sync" }
|
|
652
|
-
);
|
|
829
|
+
$unwatchState = watch3(() => state.value.length, updateChildrenStatus, {
|
|
830
|
+
flush: "sync"
|
|
831
|
+
});
|
|
653
832
|
}
|
|
654
833
|
function $touch() {
|
|
655
834
|
$fieldStatus.value.$touch();
|
|
@@ -694,7 +873,8 @@ function createReactiveNestedStatus({
|
|
|
694
873
|
path = "",
|
|
695
874
|
rootRules,
|
|
696
875
|
storage,
|
|
697
|
-
options
|
|
876
|
+
options,
|
|
877
|
+
externalErrors
|
|
698
878
|
}) {
|
|
699
879
|
let scope = effectScope4();
|
|
700
880
|
let scopeState;
|
|
@@ -705,6 +885,7 @@ function createReactiveNestedStatus({
|
|
|
705
885
|
if (statePropRules) {
|
|
706
886
|
const stateRef = toRef4(state.value, statePropKey);
|
|
707
887
|
const statePropRulesRef = toRef4(() => statePropRules);
|
|
888
|
+
const $externalErrors = toRef4(() => externalErrors.value?.[statePropKey]);
|
|
708
889
|
return [
|
|
709
890
|
statePropKey,
|
|
710
891
|
createReactiveChildrenStatus({
|
|
@@ -713,7 +894,8 @@ function createReactiveNestedStatus({
|
|
|
713
894
|
customMessages,
|
|
714
895
|
path: path ? `${path}.${statePropKey}` : statePropKey,
|
|
715
896
|
storage,
|
|
716
|
-
options
|
|
897
|
+
options,
|
|
898
|
+
externalErrors: $externalErrors
|
|
717
899
|
})
|
|
718
900
|
];
|
|
719
901
|
}
|
|
@@ -760,28 +942,28 @@ function createReactiveNestedStatus({
|
|
|
760
942
|
);
|
|
761
943
|
}
|
|
762
944
|
scopeState = scope.run(() => {
|
|
763
|
-
const $dirty =
|
|
945
|
+
const $dirty = computed5(() => {
|
|
764
946
|
return Object.entries($fields.value).every(([key, statusOrField]) => {
|
|
765
947
|
return statusOrField.$dirty;
|
|
766
948
|
});
|
|
767
949
|
});
|
|
768
|
-
const $anyDirty =
|
|
950
|
+
const $anyDirty = computed5(() => {
|
|
769
951
|
return Object.entries($fields.value).some(([key, statusOrField]) => {
|
|
770
952
|
return statusOrField.$dirty;
|
|
771
953
|
});
|
|
772
954
|
});
|
|
773
|
-
const $invalid =
|
|
955
|
+
const $invalid = computed5(() => {
|
|
774
956
|
return Object.entries($fields.value).some(([key, statusOrField]) => {
|
|
775
957
|
return statusOrField.$invalid;
|
|
776
958
|
});
|
|
777
959
|
});
|
|
778
|
-
const $valid =
|
|
779
|
-
const $error =
|
|
960
|
+
const $valid = computed5(() => !$invalid.value);
|
|
961
|
+
const $error = computed5(() => {
|
|
780
962
|
return Object.entries($fields.value).some(([key, statusOrField]) => {
|
|
781
963
|
return statusOrField.$error;
|
|
782
964
|
});
|
|
783
965
|
});
|
|
784
|
-
const $pending =
|
|
966
|
+
const $pending = computed5(() => {
|
|
785
967
|
return Object.entries($fields.value).some(([key, statusOrField]) => {
|
|
786
968
|
return statusOrField.$pending;
|
|
787
969
|
});
|
|
@@ -808,6 +990,11 @@ function createReactiveNestedStatus({
|
|
|
808
990
|
scope.stop();
|
|
809
991
|
scope = effectScope4();
|
|
810
992
|
}
|
|
993
|
+
function $clearExternalErrors() {
|
|
994
|
+
Object.entries($fields.value).forEach(([_, field]) => {
|
|
995
|
+
field.$clearExternalErrors();
|
|
996
|
+
});
|
|
997
|
+
}
|
|
811
998
|
return reactive4({
|
|
812
999
|
...scopeState,
|
|
813
1000
|
$fields,
|
|
@@ -816,7 +1003,8 @@ function createReactiveNestedStatus({
|
|
|
816
1003
|
$touch,
|
|
817
1004
|
$validate,
|
|
818
1005
|
$unwatch,
|
|
819
|
-
$watch
|
|
1006
|
+
$watch,
|
|
1007
|
+
$clearExternalErrors
|
|
820
1008
|
});
|
|
821
1009
|
}
|
|
822
1010
|
function createReactiveChildrenStatus({
|
|
@@ -825,7 +1013,8 @@ function createReactiveChildrenStatus({
|
|
|
825
1013
|
customMessages,
|
|
826
1014
|
path,
|
|
827
1015
|
storage,
|
|
828
|
-
options
|
|
1016
|
+
options,
|
|
1017
|
+
externalErrors
|
|
829
1018
|
}) {
|
|
830
1019
|
if (isCollectionRulesDef(rulesDef)) {
|
|
831
1020
|
return createReactiveCollectionStatus({
|
|
@@ -834,7 +1023,8 @@ function createReactiveChildrenStatus({
|
|
|
834
1023
|
customMessages,
|
|
835
1024
|
path,
|
|
836
1025
|
storage,
|
|
837
|
-
options
|
|
1026
|
+
options,
|
|
1027
|
+
externalErrors
|
|
838
1028
|
});
|
|
839
1029
|
} else if (isNestedRulesDef(state, rulesDef) && isRefObject(state)) {
|
|
840
1030
|
return createReactiveNestedStatus({
|
|
@@ -843,7 +1033,8 @@ function createReactiveChildrenStatus({
|
|
|
843
1033
|
customMessages,
|
|
844
1034
|
path,
|
|
845
1035
|
storage,
|
|
846
|
-
options
|
|
1036
|
+
options,
|
|
1037
|
+
externalErrors
|
|
847
1038
|
});
|
|
848
1039
|
} else if (isValidatorRulesDef(rulesDef)) {
|
|
849
1040
|
return createReactiveFieldStatus({
|
|
@@ -852,117 +1043,17 @@ function createReactiveChildrenStatus({
|
|
|
852
1043
|
customMessages,
|
|
853
1044
|
path,
|
|
854
1045
|
storage,
|
|
855
|
-
options
|
|
1046
|
+
options,
|
|
1047
|
+
externalErrors
|
|
856
1048
|
});
|
|
857
1049
|
}
|
|
858
1050
|
return null;
|
|
859
1051
|
}
|
|
860
1052
|
|
|
861
|
-
// src/core/useStorage/useStorage.ts
|
|
862
|
-
import { ref as ref4, shallowRef } from "vue";
|
|
863
|
-
function useStorage() {
|
|
864
|
-
const ruleDeclStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
865
|
-
const fieldsStorage = shallowRef(
|
|
866
|
-
/* @__PURE__ */ new Map()
|
|
867
|
-
);
|
|
868
|
-
const collectionsStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
869
|
-
const dirtyStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
870
|
-
const ruleStatusStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
871
|
-
const arrayStatusStorage = shallowRef(/* @__PURE__ */ new Map());
|
|
872
|
-
function getFieldsEntry($path) {
|
|
873
|
-
const existingFields = fieldsStorage.value.get($path);
|
|
874
|
-
if (existingFields) {
|
|
875
|
-
return existingFields;
|
|
876
|
-
} else {
|
|
877
|
-
const $fields = ref4({});
|
|
878
|
-
fieldsStorage.value.set($path, $fields);
|
|
879
|
-
return $fields;
|
|
880
|
-
}
|
|
881
|
-
}
|
|
882
|
-
function getCollectionsEntry($path) {
|
|
883
|
-
const existingEach = collectionsStorage.value.get($path);
|
|
884
|
-
if (existingEach) {
|
|
885
|
-
return existingEach;
|
|
886
|
-
} else {
|
|
887
|
-
const $each = ref4([]);
|
|
888
|
-
collectionsStorage.value.set($path, $each);
|
|
889
|
-
return $each;
|
|
890
|
-
}
|
|
891
|
-
}
|
|
892
|
-
function addArrayStatus($id, value) {
|
|
893
|
-
arrayStatusStorage.value.set($id, value);
|
|
894
|
-
}
|
|
895
|
-
function getArrayStatus($id) {
|
|
896
|
-
return arrayStatusStorage.value.get($id);
|
|
897
|
-
}
|
|
898
|
-
function setDirtyEntry($path, dirty) {
|
|
899
|
-
dirtyStorage.value.set($path, dirty);
|
|
900
|
-
}
|
|
901
|
-
function getDirtyState(path) {
|
|
902
|
-
return dirtyStorage.value.get(path) ?? false;
|
|
903
|
-
}
|
|
904
|
-
function addRuleDeclEntry($path, options) {
|
|
905
|
-
ruleDeclStorage.value.set($path, options);
|
|
906
|
-
}
|
|
907
|
-
function checkRuleDeclEntry($path, newRules) {
|
|
908
|
-
const storedRulesDefs = ruleDeclStorage.value.get($path);
|
|
909
|
-
if (!storedRulesDefs)
|
|
910
|
-
return void 0;
|
|
911
|
-
const storedRules = storedRulesDefs;
|
|
912
|
-
const isValidCache = areRulesChanged(newRules, storedRules);
|
|
913
|
-
if (!isValidCache)
|
|
914
|
-
return { valid: false };
|
|
915
|
-
return { valid: true };
|
|
916
|
-
}
|
|
917
|
-
function areRulesChanged(newRules, storedRules) {
|
|
918
|
-
const storedRulesKeys = Object.keys(storedRules);
|
|
919
|
-
const newRulesKeys = Object.keys(newRules);
|
|
920
|
-
if (newRulesKeys.length !== storedRulesKeys.length)
|
|
921
|
-
return false;
|
|
922
|
-
const hasAllValidators = newRulesKeys.every((ruleKey) => storedRulesKeys.includes(ruleKey));
|
|
923
|
-
if (!hasAllValidators)
|
|
924
|
-
return false;
|
|
925
|
-
return newRulesKeys.every((ruleKey) => {
|
|
926
|
-
const newRuleElement = newRules[ruleKey];
|
|
927
|
-
const storedRuleElement = storedRules[ruleKey];
|
|
928
|
-
if (!storedRuleElement || !newRuleElement || typeof newRuleElement === "function" || typeof storedRuleElement === "function")
|
|
929
|
-
return false;
|
|
930
|
-
if (!newRuleElement._params)
|
|
931
|
-
return true;
|
|
932
|
-
return newRuleElement._params?.every((paramKey, index) => {
|
|
933
|
-
const storedParams = unwrapRuleParameters(storedRuleElement._params);
|
|
934
|
-
const newParams = unwrapRuleParameters(newRuleElement._params);
|
|
935
|
-
return storedParams?.[index] === newParams?.[index];
|
|
936
|
-
});
|
|
937
|
-
});
|
|
938
|
-
}
|
|
939
|
-
function trySetRuleStatusRef(path) {
|
|
940
|
-
const ruleStatus = ruleStatusStorage.value.get(path);
|
|
941
|
-
if (ruleStatus) {
|
|
942
|
-
return ruleStatus;
|
|
943
|
-
} else {
|
|
944
|
-
const $pending = ref4(false);
|
|
945
|
-
const $valid = ref4(true);
|
|
946
|
-
ruleStatusStorage.value.set(path, { $pending, $valid });
|
|
947
|
-
return { $pending, $valid };
|
|
948
|
-
}
|
|
949
|
-
}
|
|
950
|
-
return {
|
|
951
|
-
addRuleDeclEntry,
|
|
952
|
-
setDirtyEntry,
|
|
953
|
-
checkRuleDeclEntry,
|
|
954
|
-
getDirtyState,
|
|
955
|
-
trySetRuleStatusRef,
|
|
956
|
-
getFieldsEntry,
|
|
957
|
-
getCollectionsEntry,
|
|
958
|
-
getArrayStatus,
|
|
959
|
-
addArrayStatus
|
|
960
|
-
};
|
|
961
|
-
}
|
|
962
|
-
|
|
963
1053
|
// src/core/useRegle/useStateProperties/useStateProperties.ts
|
|
964
1054
|
function useStateProperties(scopeRules, state, options, customRules) {
|
|
965
1055
|
const storage = useStorage();
|
|
1056
|
+
const externalErrors = computed6(() => unref3(options.$externalErrors));
|
|
966
1057
|
const $regle = reactive5(
|
|
967
1058
|
createReactiveNestedStatus({
|
|
968
1059
|
rootRules: scopeRules,
|
|
@@ -970,7 +1061,8 @@ function useStateProperties(scopeRules, state, options, customRules) {
|
|
|
970
1061
|
state,
|
|
971
1062
|
customMessages: customRules?.(),
|
|
972
1063
|
storage,
|
|
973
|
-
options
|
|
1064
|
+
options,
|
|
1065
|
+
externalErrors
|
|
974
1066
|
})
|
|
975
1067
|
);
|
|
976
1068
|
const errors = useErrors($regle);
|
|
@@ -985,15 +1077,16 @@ function createUseRegleComposable(customRules, options) {
|
|
|
985
1077
|
rewardEarly: options?.rewardEarly ?? false
|
|
986
1078
|
};
|
|
987
1079
|
function useRegle2(state, rulesFactory, options2) {
|
|
988
|
-
const scopeRules = isRef2(rulesFactory) ? rulesFactory :
|
|
1080
|
+
const scopeRules = isRef2(rulesFactory) ? rulesFactory : computed7(rulesFactory);
|
|
989
1081
|
const resolvedOptions = {
|
|
990
1082
|
...globalOptions,
|
|
991
1083
|
...options2
|
|
992
1084
|
};
|
|
993
|
-
const
|
|
1085
|
+
const processedState = ref4(state);
|
|
1086
|
+
const initialState = shallowRef2(structuredClone(toRaw(processedState.value)));
|
|
994
1087
|
const { $regle, errors } = useStateProperties(
|
|
995
1088
|
scopeRules,
|
|
996
|
-
|
|
1089
|
+
processedState,
|
|
997
1090
|
resolvedOptions,
|
|
998
1091
|
customRules
|
|
999
1092
|
);
|
|
@@ -1001,6 +1094,12 @@ function createUseRegleComposable(customRules, options) {
|
|
|
1001
1094
|
state.value = toRaw(initialState.value);
|
|
1002
1095
|
$regle.$reset();
|
|
1003
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
|
+
});
|
|
1004
1103
|
async function validateForm() {
|
|
1005
1104
|
$regle.$touch();
|
|
1006
1105
|
const result = await $regle.$validate();
|
|
@@ -1010,11 +1109,13 @@ function createUseRegleComposable(customRules, options) {
|
|
|
1010
1109
|
return false;
|
|
1011
1110
|
}
|
|
1012
1111
|
return {
|
|
1013
|
-
state,
|
|
1112
|
+
$state: state,
|
|
1014
1113
|
$regle,
|
|
1015
|
-
errors,
|
|
1114
|
+
$errors: errors,
|
|
1016
1115
|
resetForm,
|
|
1017
|
-
validateForm
|
|
1116
|
+
validateForm,
|
|
1117
|
+
$valid,
|
|
1118
|
+
$invalid
|
|
1018
1119
|
};
|
|
1019
1120
|
}
|
|
1020
1121
|
return useRegle2;
|