@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 CHANGED
@@ -118,8 +118,116 @@ var import_vue10 = require("vue");
118
118
  // src/core/useRegle/useStateProperties/useStateProperties.ts
119
119
  var import_vue9 = require("vue");
120
120
 
121
+ // src/core/useStorage/useStorage.ts
122
+ var import_vue2 = require("vue");
123
+ function useStorage() {
124
+ const ruleDeclStorage = (0, import_vue2.shallowRef)(/* @__PURE__ */ new Map());
125
+ const fieldsStorage = (0, import_vue2.shallowRef)(
126
+ /* @__PURE__ */ new Map()
127
+ );
128
+ const collectionsStorage = (0, import_vue2.shallowRef)(/* @__PURE__ */ new Map());
129
+ const dirtyStorage = (0, import_vue2.shallowRef)(/* @__PURE__ */ new Map());
130
+ const ruleStatusStorage = (0, import_vue2.shallowRef)(/* @__PURE__ */ new Map());
131
+ const arrayStatusStorage = (0, import_vue2.shallowRef)(/* @__PURE__ */ new Map());
132
+ function getFieldsEntry($path) {
133
+ const existingFields = fieldsStorage.value.get($path);
134
+ if (existingFields) {
135
+ return existingFields;
136
+ } else {
137
+ const $fields = (0, import_vue2.ref)({});
138
+ fieldsStorage.value.set($path, $fields);
139
+ return $fields;
140
+ }
141
+ }
142
+ function getCollectionsEntry($path) {
143
+ const existingEach = collectionsStorage.value.get($path);
144
+ if (existingEach) {
145
+ return existingEach;
146
+ } else {
147
+ const $each = (0, import_vue2.ref)([]);
148
+ collectionsStorage.value.set($path, $each);
149
+ return $each;
150
+ }
151
+ }
152
+ function addArrayStatus($id, value) {
153
+ arrayStatusStorage.value.set($id, value);
154
+ }
155
+ function getArrayStatus($id) {
156
+ return arrayStatusStorage.value.get($id);
157
+ }
158
+ function deleteArrayStatus($id) {
159
+ if ($id) {
160
+ arrayStatusStorage.value.delete($id);
161
+ }
162
+ }
163
+ function setDirtyEntry($path, dirty) {
164
+ dirtyStorage.value.set($path, dirty);
165
+ }
166
+ function getDirtyState(path) {
167
+ return dirtyStorage.value.get(path) ?? false;
168
+ }
169
+ function addRuleDeclEntry($path, options) {
170
+ ruleDeclStorage.value.set($path, options);
171
+ }
172
+ function checkRuleDeclEntry($path, newRules) {
173
+ const storedRulesDefs = ruleDeclStorage.value.get($path);
174
+ if (!storedRulesDefs)
175
+ return void 0;
176
+ const storedRules = storedRulesDefs;
177
+ const isValidCache = areRulesChanged(newRules, storedRules);
178
+ if (!isValidCache)
179
+ return { valid: false };
180
+ return { valid: true };
181
+ }
182
+ function areRulesChanged(newRules, storedRules) {
183
+ const storedRulesKeys = Object.keys(storedRules);
184
+ const newRulesKeys = Object.keys(newRules);
185
+ if (newRulesKeys.length !== storedRulesKeys.length)
186
+ return false;
187
+ const hasAllValidators = newRulesKeys.every((ruleKey) => storedRulesKeys.includes(ruleKey));
188
+ if (!hasAllValidators)
189
+ return false;
190
+ return newRulesKeys.every((ruleKey) => {
191
+ const newRuleElement = newRules[ruleKey];
192
+ const storedRuleElement = storedRules[ruleKey];
193
+ if (!storedRuleElement || !newRuleElement || typeof newRuleElement === "function" || typeof storedRuleElement === "function")
194
+ return false;
195
+ if (!newRuleElement._params)
196
+ return true;
197
+ return newRuleElement._params?.every((paramKey, index) => {
198
+ const storedParams = unwrapRuleParameters(storedRuleElement._params);
199
+ const newParams = unwrapRuleParameters(newRuleElement._params);
200
+ return storedParams?.[index] === newParams?.[index];
201
+ });
202
+ });
203
+ }
204
+ function trySetRuleStatusRef(path) {
205
+ const ruleStatus = ruleStatusStorage.value.get(path);
206
+ if (ruleStatus) {
207
+ return ruleStatus;
208
+ } else {
209
+ const $pending = (0, import_vue2.ref)(false);
210
+ const $valid = (0, import_vue2.ref)(true);
211
+ ruleStatusStorage.value.set(path, { $pending, $valid });
212
+ return { $pending, $valid };
213
+ }
214
+ }
215
+ return {
216
+ addRuleDeclEntry,
217
+ setDirtyEntry,
218
+ checkRuleDeclEntry,
219
+ getDirtyState,
220
+ trySetRuleStatusRef,
221
+ getFieldsEntry,
222
+ getCollectionsEntry,
223
+ getArrayStatus,
224
+ addArrayStatus,
225
+ deleteArrayStatus
226
+ };
227
+ }
228
+
121
229
  // src/core/useRegle/useErrors.ts
122
- var import_vue3 = require("vue");
230
+ var import_vue4 = require("vue");
123
231
 
124
232
  // src/utils/object.utils.ts
125
233
  function isObject(obj) {
@@ -153,7 +261,7 @@ function isEmpty(value) {
153
261
  }
154
262
 
155
263
  // src/utils/composables.ts
156
- var import_vue2 = require("vue");
264
+ var import_vue3 = require("vue");
157
265
 
158
266
  // src/core/useRegle/guards/ruleDef.guards.ts
159
267
  function isNestedRulesDef(state, rule) {
@@ -183,14 +291,19 @@ function isFieldStatus(rule) {
183
291
  return !!rule && "$rules" in rule;
184
292
  }
185
293
 
294
+ // src/core/useRegle/guards/rule.errors.guards.ts
295
+ function isExternalErrorCollection(value) {
296
+ return "$each" in value || "$errors" in value;
297
+ }
298
+
186
299
  // src/core/useRegle/useErrors.ts
187
- function extractRulesErrors(rules) {
300
+ function extractRulesErrors(rules, externalErrors) {
188
301
  return Object.entries(rules).map(([ruleKey, rule]) => {
189
302
  if (!rule.$valid) {
190
303
  return rule.$message;
191
304
  }
192
305
  return null;
193
- }).filter((msg) => !!msg);
306
+ }).filter((msg) => !!msg).concat(externalErrors ?? []);
194
307
  }
195
308
  function processFieldErrors(fieldStatus) {
196
309
  if (isNestedRulesStatus(fieldStatus)) {
@@ -200,8 +313,12 @@ function processFieldErrors(fieldStatus) {
200
313
  $errors: fieldStatus.$rules ? extractRulesErrors(fieldStatus.$rules) : [],
201
314
  $each: fieldStatus.$each.map(processFieldErrors)
202
315
  };
203
- } else if (isFieldStatus(fieldStatus) && fieldStatus.$error) {
204
- return extractRulesErrors(fieldStatus.$rules);
316
+ } else if (isFieldStatus(fieldStatus)) {
317
+ if (fieldStatus.$error) {
318
+ return extractRulesErrors(fieldStatus.$rules, fieldStatus.$externalErrors);
319
+ } else {
320
+ return fieldStatus.$externalErrors ?? [];
321
+ }
205
322
  }
206
323
  return [];
207
324
  }
@@ -221,31 +338,41 @@ function extractNestedErrors(fields) {
221
338
  $each: extractCollectionError(fieldStatus)
222
339
  }
223
340
  ];
224
- } else if (isFieldStatus(fieldStatus) && fieldStatus.$error) {
225
- return [fieldKey, extractRulesErrors(fieldStatus.$rules)];
341
+ } else if (isFieldStatus(fieldStatus)) {
342
+ if (fieldStatus.$error) {
343
+ return [fieldKey, extractRulesErrors(fieldStatus.$rules, fieldStatus.$externalErrors)];
344
+ } else {
345
+ return [fieldKey, fieldStatus.$externalErrors ?? []];
346
+ }
226
347
  }
227
348
  return [fieldKey, []];
228
349
  })
229
350
  );
230
351
  }
231
352
  function useErrors($regle) {
232
- const errors = (0, import_vue3.computed)(() => {
353
+ const errors = (0, import_vue4.computed)(() => {
233
354
  return extractNestedErrors($regle.$fields);
234
355
  });
235
356
  return errors;
236
357
  }
237
358
 
238
359
  // src/core/useRegle/useStateProperties/createReactiveNestedStatus.ts
239
- var import_vue7 = require("vue");
360
+ var import_vue8 = require("vue");
240
361
 
241
362
  // src/core/useRegle/useStateProperties/createReactiveCollectionStatus.ts
242
- var import_vue6 = require("vue");
363
+ var import_vue7 = require("vue");
364
+
365
+ // src/utils/randomId.ts
366
+ function randomId() {
367
+ const uint32 = window.crypto.getRandomValues(new Uint32Array(1))[0];
368
+ return uint32.toString(8);
369
+ }
243
370
 
244
371
  // src/core/useRegle/useStateProperties/createReactiveFieldStatus.ts
245
- var import_vue5 = require("vue");
372
+ var import_vue6 = require("vue");
246
373
 
247
374
  // src/core/useRegle/useStateProperties/createReactiveRuleStatus.ts
248
- var import_vue4 = require("vue");
375
+ var import_vue5 = require("vue");
249
376
 
250
377
  // src/types/rules/rule.internal.types.ts
251
378
  var InternalRuleType = /* @__PURE__ */ ((InternalRuleType2) => {
@@ -265,12 +392,12 @@ function createReactiveRuleStatus({
265
392
  storage,
266
393
  options
267
394
  }) {
268
- let scope = (0, import_vue4.effectScope)();
395
+ let scope = (0, import_vue5.effectScope)();
269
396
  let scopeState;
270
397
  const { $pending, $valid } = storage.trySetRuleStatusRef(`${path}.${ruleKey}`);
271
398
  function $watch() {
272
399
  scopeState = scope.run(() => {
273
- const $active = (0, import_vue4.computed)(() => {
400
+ const $active = (0, import_vue5.computed)(() => {
274
401
  if (isFormRuleDefinition(rule)) {
275
402
  if (typeof rule.value.active === "function") {
276
403
  return rule.value.active(state.value, ...$params.value);
@@ -281,7 +408,7 @@ function createReactiveRuleStatus({
281
408
  return true;
282
409
  }
283
410
  });
284
- const $message = (0, import_vue4.computed)(() => {
411
+ const $message = (0, import_vue5.computed)(() => {
285
412
  let message = "";
286
413
  const customMessageRule = customMessages ? customMessages[ruleKey]?.message : void 0;
287
414
  if (customMessageRule) {
@@ -306,27 +433,27 @@ function createReactiveRuleStatus({
306
433
  }
307
434
  return message;
308
435
  });
309
- const $type = (0, import_vue4.computed)(() => {
436
+ const $type = (0, import_vue5.computed)(() => {
310
437
  if (isFormRuleDefinition(rule)) {
311
438
  return Object.values(InternalRuleType).includes(rule.value.type) ? ruleKey : rule.value.type;
312
439
  } else {
313
440
  return ruleKey;
314
441
  }
315
442
  });
316
- const $validator = (0, import_vue4.computed)(() => {
443
+ const $validator = (0, import_vue5.computed)(() => {
317
444
  if (isFormRuleDefinition(rule)) {
318
445
  return rule.value.validator;
319
446
  } else {
320
447
  return rule.value;
321
448
  }
322
449
  });
323
- const $params = (0, import_vue4.computed)(() => {
450
+ const $params = (0, import_vue5.computed)(() => {
324
451
  if (typeof rule.value === "function") {
325
452
  return [];
326
453
  }
327
454
  return unwrapRuleParameters(rule.value._params ?? []);
328
455
  });
329
- const $path = (0, import_vue4.computed)(() => `${path}.${$type.value}`);
456
+ const $path = (0, import_vue5.computed)(() => `${path}.${$type.value}`);
330
457
  return {
331
458
  $active,
332
459
  $message,
@@ -338,7 +465,7 @@ function createReactiveRuleStatus({
338
465
  });
339
466
  }
340
467
  $watch();
341
- const $unwatchState = (0, import_vue4.watch)(scopeState.$params, $validate, {
468
+ const $unwatchState = (0, import_vue5.watch)(scopeState.$params, $validate, {
342
469
  deep: true
343
470
  });
344
471
  async function $validate() {
@@ -362,14 +489,16 @@ function createReactiveRuleStatus({
362
489
  ruleResult = resultOrPromise;
363
490
  }
364
491
  $valid.value = ruleResult;
492
+ if (options.$externalErrors) {
493
+ }
365
494
  return ruleResult;
366
495
  }
367
496
  function $unwatch() {
368
497
  $unwatchState();
369
498
  scope.stop();
370
- scope = (0, import_vue4.effectScope)();
499
+ scope = (0, import_vue5.effectScope)();
371
500
  }
372
- return (0, import_vue4.reactive)({
501
+ return (0, import_vue5.reactive)({
373
502
  ...scopeState,
374
503
  $pending,
375
504
  $valid,
@@ -386,20 +515,29 @@ function createReactiveFieldStatus({
386
515
  customMessages,
387
516
  path,
388
517
  storage,
389
- options
518
+ options,
519
+ externalErrors
390
520
  }) {
391
- let scope = (0, import_vue5.effectScope)();
521
+ let scope = (0, import_vue6.effectScope)();
392
522
  let scopeState;
393
- const $dirty = (0, import_vue5.ref)(false);
394
- const $anyDirty = (0, import_vue5.computed)(() => $dirty.value);
395
- const triggerPunishment = (0, import_vue5.ref)(false);
523
+ const $dirty = (0, import_vue6.ref)(false);
524
+ const $anyDirty = (0, import_vue6.computed)(() => $dirty.value);
525
+ const triggerPunishment = (0, import_vue6.ref)(false);
526
+ const $externalErrors = (0, import_vue6.ref)([]);
527
+ function collectExternalErrors() {
528
+ if (externalErrors.value) {
529
+ $externalErrors.value = externalErrors.value;
530
+ }
531
+ }
532
+ collectExternalErrors();
533
+ const $unwatchExternalErrors = (0, import_vue6.watch)(externalErrors, collectExternalErrors);
396
534
  function createReactiveRulesResult() {
397
535
  const declaredRules = rulesDef.value;
398
536
  const storeResult = storage.checkRuleDeclEntry(path, declaredRules);
399
537
  $rules.value = Object.fromEntries(
400
538
  Object.entries(declaredRules).map(([ruleKey, rule]) => {
401
539
  if (rule) {
402
- const ruleRef = (0, import_vue5.toRef)(() => rule);
540
+ const ruleRef = (0, import_vue6.toRef)(() => rule);
403
541
  return [
404
542
  ruleKey,
405
543
  createReactiveRuleStatus({
@@ -426,18 +564,20 @@ function createReactiveFieldStatus({
426
564
  }
427
565
  storage.addRuleDeclEntry(path, declaredRules);
428
566
  }
429
- const $unwatchDirty = (0, import_vue5.watch)($dirty, () => {
567
+ const $unwatchDirty = (0, import_vue6.watch)($dirty, () => {
430
568
  storage.setDirtyEntry(path, $dirty.value);
431
569
  });
432
- const $unwatchState = (0, import_vue5.watch)(state, () => {
433
- if ((0, import_vue5.unref)(options.autoDirty)) {
570
+ const $unwatchState = (0, import_vue6.watch)(state, () => {
571
+ if ((0, import_vue6.unref)(options.autoDirty)) {
434
572
  if (!$dirty.value) {
435
573
  $dirty.value = true;
436
574
  }
437
575
  }
438
- if (!(0, import_vue5.unref)(options.lazy)) {
576
+ if (!(0, import_vue6.unref)(options.lazy)) {
439
577
  $commit();
440
578
  }
579
+ $externalErrors.value = [];
580
+ console.log($externalErrors.value);
441
581
  });
442
582
  function $unwatch() {
443
583
  if ($rules.value) {
@@ -451,32 +591,33 @@ function createReactiveFieldStatus({
451
591
  }
452
592
  $unwatchState();
453
593
  $unwatchValid();
594
+ $unwatchExternalErrors();
454
595
  scope.stop();
455
- scope = (0, import_vue5.effectScope)();
596
+ scope = (0, import_vue6.effectScope)();
456
597
  }
457
598
  function $watch() {
458
599
  scopeState = scope.run(() => {
459
- const $error = (0, import_vue5.computed)(() => {
600
+ const $error = (0, import_vue6.computed)(() => {
460
601
  return $invalid.value && !$pending.value && $dirty.value;
461
602
  });
462
- const $pending = (0, import_vue5.computed)(() => {
463
- if (triggerPunishment.value || !(0, import_vue5.unref)(options.rewardEarly)) {
603
+ const $pending = (0, import_vue6.computed)(() => {
604
+ if (triggerPunishment.value || !(0, import_vue6.unref)(options.rewardEarly)) {
464
605
  return Object.entries($rules.value).some(([key, ruleResult]) => {
465
606
  return ruleResult.$pending;
466
607
  });
467
608
  }
468
609
  return false;
469
610
  });
470
- const $invalid = (0, import_vue5.computed)(() => {
471
- if (triggerPunishment.value || !(0, import_vue5.unref)(options.rewardEarly)) {
611
+ const $invalid = (0, import_vue6.computed)(() => {
612
+ if (triggerPunishment.value || !(0, import_vue6.unref)(options.rewardEarly)) {
472
613
  return Object.entries($rules.value).some(([key, ruleResult]) => {
473
614
  return !ruleResult.$valid;
474
615
  });
475
616
  }
476
617
  return false;
477
618
  });
478
- const $valid = (0, import_vue5.computed)(() => {
479
- if ((0, import_vue5.unref)(options.rewardEarly)) {
619
+ const $valid = (0, import_vue6.computed)(() => {
620
+ if ((0, import_vue6.unref)(options.rewardEarly)) {
480
621
  return Object.entries($rules.value).every(([key, ruleResult]) => {
481
622
  return ruleResult.$valid;
482
623
  });
@@ -492,15 +633,16 @@ function createReactiveFieldStatus({
492
633
  };
493
634
  });
494
635
  }
495
- const $rules = (0, import_vue5.ref)();
636
+ const $rules = (0, import_vue6.ref)();
496
637
  createReactiveRulesResult();
497
- const $unwatchValid = (0, import_vue5.watch)(scopeState.$valid, (valid) => {
498
- if ((0, import_vue5.unref)(options.rewardEarly) && valid) {
638
+ const $unwatchValid = (0, import_vue6.watch)(scopeState.$valid, (valid) => {
639
+ if ((0, import_vue6.unref)(options.rewardEarly) && valid) {
499
640
  triggerPunishment.value = false;
500
641
  }
501
642
  });
502
643
  function $reset() {
503
644
  $dirty.value = false;
645
+ $externalErrors.value = [];
504
646
  }
505
647
  function $touch() {
506
648
  $dirty.value = true;
@@ -523,29 +665,28 @@ function createReactiveFieldStatus({
523
665
  return false;
524
666
  }
525
667
  }
526
- return (0, import_vue5.reactive)({
668
+ function $clearExternalErrors() {
669
+ $externalErrors.value = [];
670
+ }
671
+ return (0, import_vue6.reactive)({
527
672
  $dirty,
528
673
  $anyDirty,
529
674
  $invalid: scopeState.$invalid,
530
675
  $error: scopeState.$error,
531
676
  $pending: scopeState.$pending,
532
677
  $valid: scopeState.$valid,
678
+ $externalErrors,
533
679
  $value: state,
534
680
  $rules,
535
681
  $reset,
536
682
  $touch,
537
683
  $validate,
538
684
  $unwatch,
539
- $watch
685
+ $watch,
686
+ $clearExternalErrors
540
687
  });
541
688
  }
542
689
 
543
- // src/utils/randomId.ts
544
- function randomId() {
545
- const uint32 = window.crypto.getRandomValues(new Uint32Array(1))[0];
546
- return uint32.toString(8);
547
- }
548
-
549
690
  // src/core/useRegle/useStateProperties/createReactiveCollectionStatus.ts
550
691
  function createCollectionElement({
551
692
  path,
@@ -554,7 +695,8 @@ function createCollectionElement({
554
695
  storage,
555
696
  value,
556
697
  customMessages,
557
- rules
698
+ rules,
699
+ externalErrors
558
700
  }) {
559
701
  const $id = randomId();
560
702
  const $path = `${path}.${$id}`;
@@ -568,14 +710,16 @@ function createCollectionElement({
568
710
  }
569
711
  });
570
712
  }
571
- const $state = (0, import_vue6.toRefs)(value);
713
+ const $state = (0, import_vue7.toRefs)(value);
714
+ const $externalErrors = (0, import_vue7.toRefs)(externalErrors.value ?? (0, import_vue7.reactive)([void 0]));
572
715
  const $status = createReactiveChildrenStatus({
573
716
  state: $state[index],
574
- rulesDef: (0, import_vue6.toRef)(() => rules),
717
+ rulesDef: (0, import_vue7.toRef)(() => rules),
575
718
  customMessages,
576
719
  path: $path,
577
720
  storage,
578
- options
721
+ options,
722
+ externalErrors: $externalErrors?.[index]
579
723
  });
580
724
  if ($status) {
581
725
  $status.$id = value[index].$id ?? $id;
@@ -589,25 +733,41 @@ function createReactiveCollectionStatus({
589
733
  customMessages,
590
734
  path,
591
735
  storage,
592
- options
736
+ options,
737
+ externalErrors
593
738
  }) {
594
739
  if (Array.isArray(state.value) && !rulesDef.value.$each) {
595
740
  return null;
596
741
  }
597
742
  let $unwatchState = null;
598
- const $fieldStatus = (0, import_vue6.ref)({});
743
+ const $fieldStatus = (0, import_vue7.ref)({});
599
744
  const $eachStatus = storage.getCollectionsEntry(path);
600
745
  createStatus();
601
746
  $watch();
602
747
  function createStatus() {
603
748
  const { $each, ...otherFields } = rulesDef.value;
749
+ const $externalErrorsField = (0, import_vue7.toRef)(() => {
750
+ if (externalErrors.value) {
751
+ if (isExternalErrorCollection(externalErrors.value)) {
752
+ return externalErrors.value.$errors;
753
+ }
754
+ }
755
+ });
756
+ const $externalErrorsEach = (0, import_vue7.toRef)(() => {
757
+ if (externalErrors.value) {
758
+ if (isExternalErrorCollection(externalErrors.value)) {
759
+ return externalErrors.value.$each;
760
+ }
761
+ }
762
+ });
604
763
  $fieldStatus.value = createReactiveFieldStatus({
605
764
  state,
606
- rulesDef: (0, import_vue6.toRef)(() => otherFields),
765
+ rulesDef: (0, import_vue7.toRef)(() => otherFields),
607
766
  customMessages,
608
767
  path,
609
768
  storage,
610
- options
769
+ options,
770
+ externalErrors: $externalErrorsField
611
771
  });
612
772
  if (Array.isArray(state.value) && $each) {
613
773
  $eachStatus.value = state.value.map((value, index) => {
@@ -623,7 +783,8 @@ function createReactiveCollectionStatus({
623
783
  value: state.value,
624
784
  index,
625
785
  options,
626
- storage
786
+ storage,
787
+ externalErrors: $externalErrorsEach
627
788
  });
628
789
  }
629
790
  }).filter((f) => !!f);
@@ -647,6 +808,13 @@ function createReactiveCollectionStatus({
647
808
  $eachStatus.value[index].$unwatch();
648
809
  }
649
810
  } else {
811
+ const $externalErrorsEach = (0, import_vue7.toRef)(() => {
812
+ if (externalErrors.value) {
813
+ if ("$each" in externalErrors.value) {
814
+ return externalErrors.value.$each;
815
+ }
816
+ }
817
+ });
650
818
  const newElement = createCollectionElement({
651
819
  value: state.value,
652
820
  rules: $each,
@@ -654,7 +822,8 @@ function createReactiveCollectionStatus({
654
822
  path,
655
823
  storage,
656
824
  options,
657
- index
825
+ index,
826
+ externalErrors: $externalErrorsEach
658
827
  });
659
828
  if (newElement) {
660
829
  $eachStatus.value[index] = newElement;
@@ -669,7 +838,7 @@ function createReactiveCollectionStatus({
669
838
  item.$unwatch();
670
839
  });
671
840
  $eachStatus.value.length = state.value.length;
672
- (0, import_vue6.nextTick)($watch);
841
+ (0, import_vue7.nextTick)($watch);
673
842
  }
674
843
  }
675
844
  function $unwatch() {
@@ -686,7 +855,7 @@ function createReactiveCollectionStatus({
686
855
  }
687
856
  }
688
857
  function $watch() {
689
- $unwatchState = (0, import_vue6.watch)(() => state.value.length, updateChildrenStatus, {
858
+ $unwatchState = (0, import_vue7.watch)(() => state.value.length, updateChildrenStatus, {
690
859
  flush: "sync"
691
860
  });
692
861
  }
@@ -714,7 +883,7 @@ function createReactiveCollectionStatus({
714
883
  return false;
715
884
  }
716
885
  }
717
- return (0, import_vue6.reactive)({
886
+ return (0, import_vue7.reactive)({
718
887
  ...$fieldStatus.value,
719
888
  $each: $eachStatus,
720
889
  $validate,
@@ -733,17 +902,19 @@ function createReactiveNestedStatus({
733
902
  path = "",
734
903
  rootRules,
735
904
  storage,
736
- options
905
+ options,
906
+ externalErrors
737
907
  }) {
738
- let scope = (0, import_vue7.effectScope)();
908
+ let scope = (0, import_vue8.effectScope)();
739
909
  let scopeState;
740
910
  let $unwatchFields;
741
911
  function createReactiveFieldsStatus() {
742
912
  $fields.value = Object.fromEntries(
743
913
  Object.entries(scopeRules.value).map(([statePropKey, statePropRules]) => {
744
914
  if (statePropRules) {
745
- const stateRef = (0, import_vue7.toRef)(state.value, statePropKey);
746
- const statePropRulesRef = (0, import_vue7.toRef)(() => statePropRules);
915
+ const stateRef = (0, import_vue8.toRef)(state.value, statePropKey);
916
+ const statePropRulesRef = (0, import_vue8.toRef)(() => statePropRules);
917
+ const $externalErrors = (0, import_vue8.toRef)(() => externalErrors.value?.[statePropKey]);
747
918
  return [
748
919
  statePropKey,
749
920
  createReactiveChildrenStatus({
@@ -752,7 +923,8 @@ function createReactiveNestedStatus({
752
923
  customMessages,
753
924
  path: path ? `${path}.${statePropKey}` : statePropKey,
754
925
  storage,
755
- options
926
+ options,
927
+ externalErrors: $externalErrors
756
928
  })
757
929
  ];
758
930
  }
@@ -789,7 +961,7 @@ function createReactiveNestedStatus({
789
961
  }
790
962
  function $watch() {
791
963
  if (rootRules) {
792
- $unwatchFields = (0, import_vue7.watch)(
964
+ $unwatchFields = (0, import_vue8.watch)(
793
965
  rootRules,
794
966
  () => {
795
967
  $unwatch();
@@ -799,28 +971,28 @@ function createReactiveNestedStatus({
799
971
  );
800
972
  }
801
973
  scopeState = scope.run(() => {
802
- const $dirty = (0, import_vue7.computed)(() => {
974
+ const $dirty = (0, import_vue8.computed)(() => {
803
975
  return Object.entries($fields.value).every(([key, statusOrField]) => {
804
976
  return statusOrField.$dirty;
805
977
  });
806
978
  });
807
- const $anyDirty = (0, import_vue7.computed)(() => {
979
+ const $anyDirty = (0, import_vue8.computed)(() => {
808
980
  return Object.entries($fields.value).some(([key, statusOrField]) => {
809
981
  return statusOrField.$dirty;
810
982
  });
811
983
  });
812
- const $invalid = (0, import_vue7.computed)(() => {
984
+ const $invalid = (0, import_vue8.computed)(() => {
813
985
  return Object.entries($fields.value).some(([key, statusOrField]) => {
814
986
  return statusOrField.$invalid;
815
987
  });
816
988
  });
817
- const $valid = (0, import_vue7.computed)(() => !$invalid.value);
818
- const $error = (0, import_vue7.computed)(() => {
989
+ const $valid = (0, import_vue8.computed)(() => !$invalid.value);
990
+ const $error = (0, import_vue8.computed)(() => {
819
991
  return Object.entries($fields.value).some(([key, statusOrField]) => {
820
992
  return statusOrField.$error;
821
993
  });
822
994
  });
823
- const $pending = (0, import_vue7.computed)(() => {
995
+ const $pending = (0, import_vue8.computed)(() => {
824
996
  return Object.entries($fields.value).some(([key, statusOrField]) => {
825
997
  return statusOrField.$pending;
826
998
  });
@@ -845,9 +1017,14 @@ function createReactiveNestedStatus({
845
1017
  $unwatchFields();
846
1018
  }
847
1019
  scope.stop();
848
- scope = (0, import_vue7.effectScope)();
1020
+ scope = (0, import_vue8.effectScope)();
849
1021
  }
850
- return (0, import_vue7.reactive)({
1022
+ function $clearExternalErrors() {
1023
+ Object.entries($fields.value).forEach(([_, field]) => {
1024
+ field.$clearExternalErrors();
1025
+ });
1026
+ }
1027
+ return (0, import_vue8.reactive)({
851
1028
  ...scopeState,
852
1029
  $fields,
853
1030
  $value: state,
@@ -855,7 +1032,8 @@ function createReactiveNestedStatus({
855
1032
  $touch,
856
1033
  $validate,
857
1034
  $unwatch,
858
- $watch
1035
+ $watch,
1036
+ $clearExternalErrors
859
1037
  });
860
1038
  }
861
1039
  function createReactiveChildrenStatus({
@@ -864,7 +1042,8 @@ function createReactiveChildrenStatus({
864
1042
  customMessages,
865
1043
  path,
866
1044
  storage,
867
- options
1045
+ options,
1046
+ externalErrors
868
1047
  }) {
869
1048
  if (isCollectionRulesDef(rulesDef)) {
870
1049
  return createReactiveCollectionStatus({
@@ -873,7 +1052,8 @@ function createReactiveChildrenStatus({
873
1052
  customMessages,
874
1053
  path,
875
1054
  storage,
876
- options
1055
+ options,
1056
+ externalErrors
877
1057
  });
878
1058
  } else if (isNestedRulesDef(state, rulesDef) && isRefObject(state)) {
879
1059
  return createReactiveNestedStatus({
@@ -882,7 +1062,8 @@ function createReactiveChildrenStatus({
882
1062
  customMessages,
883
1063
  path,
884
1064
  storage,
885
- options
1065
+ options,
1066
+ externalErrors
886
1067
  });
887
1068
  } else if (isValidatorRulesDef(rulesDef)) {
888
1069
  return createReactiveFieldStatus({
@@ -891,123 +1072,17 @@ function createReactiveChildrenStatus({
891
1072
  customMessages,
892
1073
  path,
893
1074
  storage,
894
- options
1075
+ options,
1076
+ externalErrors
895
1077
  });
896
1078
  }
897
1079
  return null;
898
1080
  }
899
1081
 
900
- // src/core/useStorage/useStorage.ts
901
- var import_vue8 = require("vue");
902
- function useStorage() {
903
- const ruleDeclStorage = (0, import_vue8.shallowRef)(/* @__PURE__ */ new Map());
904
- const fieldsStorage = (0, import_vue8.shallowRef)(
905
- /* @__PURE__ */ new Map()
906
- );
907
- const collectionsStorage = (0, import_vue8.shallowRef)(/* @__PURE__ */ new Map());
908
- const dirtyStorage = (0, import_vue8.shallowRef)(/* @__PURE__ */ new Map());
909
- const ruleStatusStorage = (0, import_vue8.shallowRef)(/* @__PURE__ */ new Map());
910
- const arrayStatusStorage = (0, import_vue8.shallowRef)(/* @__PURE__ */ new Map());
911
- function getFieldsEntry($path) {
912
- const existingFields = fieldsStorage.value.get($path);
913
- if (existingFields) {
914
- return existingFields;
915
- } else {
916
- const $fields = (0, import_vue8.ref)({});
917
- fieldsStorage.value.set($path, $fields);
918
- return $fields;
919
- }
920
- }
921
- function getCollectionsEntry($path) {
922
- const existingEach = collectionsStorage.value.get($path);
923
- if (existingEach) {
924
- return existingEach;
925
- } else {
926
- const $each = (0, import_vue8.ref)([]);
927
- collectionsStorage.value.set($path, $each);
928
- return $each;
929
- }
930
- }
931
- function addArrayStatus($id, value) {
932
- arrayStatusStorage.value.set($id, value);
933
- }
934
- function getArrayStatus($id) {
935
- return arrayStatusStorage.value.get($id);
936
- }
937
- function deleteArrayStatus($id) {
938
- if ($id) {
939
- arrayStatusStorage.value.delete($id);
940
- }
941
- }
942
- function setDirtyEntry($path, dirty) {
943
- dirtyStorage.value.set($path, dirty);
944
- }
945
- function getDirtyState(path) {
946
- return dirtyStorage.value.get(path) ?? false;
947
- }
948
- function addRuleDeclEntry($path, options) {
949
- ruleDeclStorage.value.set($path, options);
950
- }
951
- function checkRuleDeclEntry($path, newRules) {
952
- const storedRulesDefs = ruleDeclStorage.value.get($path);
953
- if (!storedRulesDefs)
954
- return void 0;
955
- const storedRules = storedRulesDefs;
956
- const isValidCache = areRulesChanged(newRules, storedRules);
957
- if (!isValidCache)
958
- return { valid: false };
959
- return { valid: true };
960
- }
961
- function areRulesChanged(newRules, storedRules) {
962
- const storedRulesKeys = Object.keys(storedRules);
963
- const newRulesKeys = Object.keys(newRules);
964
- if (newRulesKeys.length !== storedRulesKeys.length)
965
- return false;
966
- const hasAllValidators = newRulesKeys.every((ruleKey) => storedRulesKeys.includes(ruleKey));
967
- if (!hasAllValidators)
968
- return false;
969
- return newRulesKeys.every((ruleKey) => {
970
- const newRuleElement = newRules[ruleKey];
971
- const storedRuleElement = storedRules[ruleKey];
972
- if (!storedRuleElement || !newRuleElement || typeof newRuleElement === "function" || typeof storedRuleElement === "function")
973
- return false;
974
- if (!newRuleElement._params)
975
- return true;
976
- return newRuleElement._params?.every((paramKey, index) => {
977
- const storedParams = unwrapRuleParameters(storedRuleElement._params);
978
- const newParams = unwrapRuleParameters(newRuleElement._params);
979
- return storedParams?.[index] === newParams?.[index];
980
- });
981
- });
982
- }
983
- function trySetRuleStatusRef(path) {
984
- const ruleStatus = ruleStatusStorage.value.get(path);
985
- if (ruleStatus) {
986
- return ruleStatus;
987
- } else {
988
- const $pending = (0, import_vue8.ref)(false);
989
- const $valid = (0, import_vue8.ref)(true);
990
- ruleStatusStorage.value.set(path, { $pending, $valid });
991
- return { $pending, $valid };
992
- }
993
- }
994
- return {
995
- addRuleDeclEntry,
996
- setDirtyEntry,
997
- checkRuleDeclEntry,
998
- getDirtyState,
999
- trySetRuleStatusRef,
1000
- getFieldsEntry,
1001
- getCollectionsEntry,
1002
- getArrayStatus,
1003
- addArrayStatus,
1004
- deleteArrayStatus
1005
- };
1006
- }
1007
-
1008
1082
  // src/core/useRegle/useStateProperties/useStateProperties.ts
1009
1083
  function useStateProperties(scopeRules, state, options, customRules) {
1010
1084
  const storage = useStorage();
1085
+ const externalErrors = (0, import_vue9.computed)(() => (0, import_vue9.unref)(options.$externalErrors));
1011
1086
  const $regle = (0, import_vue9.reactive)(
1012
1087
  createReactiveNestedStatus({
1013
1088
  rootRules: scopeRules,
@@ -1015,7 +1090,8 @@ function useStateProperties(scopeRules, state, options, customRules) {
1015
1090
  state,
1016
1091
  customMessages: customRules?.(),
1017
1092
  storage,
1018
- options
1093
+ options,
1094
+ externalErrors
1019
1095
  })
1020
1096
  );
1021
1097
  const errors = useErrors($regle);
@@ -1035,10 +1111,11 @@ function createUseRegleComposable(customRules, options) {
1035
1111
  ...globalOptions,
1036
1112
  ...options2
1037
1113
  };
1038
- const initialState = (0, import_vue10.shallowRef)(structuredClone((0, import_vue10.toRaw)(state.value)));
1114
+ const processedState = (0, import_vue10.ref)(state);
1115
+ const initialState = (0, import_vue10.shallowRef)(structuredClone((0, import_vue10.toRaw)(processedState.value)));
1039
1116
  const { $regle, errors } = useStateProperties(
1040
1117
  scopeRules,
1041
- state,
1118
+ processedState,
1042
1119
  resolvedOptions,
1043
1120
  customRules
1044
1121
  );
@@ -1046,6 +1123,12 @@ function createUseRegleComposable(customRules, options) {
1046
1123
  state.value = (0, import_vue10.toRaw)(initialState.value);
1047
1124
  $regle.$reset();
1048
1125
  }
1126
+ const $valid = (0, import_vue10.computed)(() => {
1127
+ return $regle.$valid && $regle.$dirty && !$regle.$pending;
1128
+ });
1129
+ const $invalid = (0, import_vue10.computed)(() => {
1130
+ return $regle.$invalid && $regle.$dirty || $regle.$pending;
1131
+ });
1049
1132
  async function validateForm() {
1050
1133
  $regle.$touch();
1051
1134
  const result = await $regle.$validate();
@@ -1055,11 +1138,13 @@ function createUseRegleComposable(customRules, options) {
1055
1138
  return false;
1056
1139
  }
1057
1140
  return {
1058
- state,
1141
+ $state: state,
1059
1142
  $regle,
1060
- errors,
1143
+ $errors: errors,
1061
1144
  resetForm,
1062
- validateForm
1145
+ validateForm,
1146
+ $valid,
1147
+ $invalid
1063
1148
  };
1064
1149
  }
1065
1150
  return useRegle2;