@regle/core 0.0.5 → 0.0.7

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
@@ -22,8 +22,10 @@ var src_exports = {};
22
22
  __export(src_exports, {
23
23
  InternalRuleType: () => InternalRuleType,
24
24
  createRule: () => createRule,
25
- defineRegleOptions: () => defineRegleOptions,
26
- unwrapRuleParameters: () => unwrapRuleParameters
25
+ defineRegleConfig: () => defineRegleConfig,
26
+ defineType: () => defineType,
27
+ unwrapRuleParameters: () => unwrapRuleParameters,
28
+ useRegle: () => useRegle
27
29
  });
28
30
  module.exports = __toCommonJS(src_exports);
29
31
 
@@ -47,29 +49,47 @@ function createReactiveParams(params) {
47
49
  return (0, import_vue.toRef)(() => param);
48
50
  });
49
51
  }
52
+ function getFunctionParametersLength(func) {
53
+ const funcStr = func.toString();
54
+ const params = funcStr.slice(funcStr.indexOf("(") + 1, funcStr.indexOf(")")).split(",").map((param) => param.trim());
55
+ const defaults = params.filter((param) => param.includes("="));
56
+ return defaults.length + func.length;
57
+ }
50
58
 
51
59
  // src/core/createRule/defineRuleProcessors.ts
52
60
  function defineRuleProcessors(definition, ...params) {
53
61
  const { message, validator, active, ...properties } = definition;
54
62
  const isAsync = validator.constructor.name === "AsyncFunction";
55
- const processors = {
56
- message(value, ...args) {
63
+ const defaultProcessors = {
64
+ validator(value, ...args) {
65
+ return definition.validator(value, ...unwrapRuleParameters(args.length ? args : params));
66
+ },
67
+ message(value, metadata) {
57
68
  if (typeof definition.message === "function") {
58
- return definition.message(value, ...args.length ? args : params);
69
+ return definition.message(value, {
70
+ ...metadata,
71
+ $params: unwrapRuleParameters(metadata.$params?.length ? metadata.$params : params)
72
+ });
59
73
  } else {
60
74
  return definition.message;
61
75
  }
62
76
  },
63
- validator(value, ...args) {
64
- return definition.validator(value, ...args.length ? args : params);
65
- },
66
- active(value, ...args) {
77
+ active(value, metadata) {
67
78
  if (typeof definition.active === "function") {
68
- return definition.active(value, ...args.length ? args : params);
79
+ return definition.active(value, {
80
+ ...metadata,
81
+ $params: unwrapRuleParameters(metadata.$params?.length ? metadata.$params : params)
82
+ });
69
83
  } else {
70
84
  return definition.active ?? true;
71
85
  }
72
86
  },
87
+ exec(value) {
88
+ return definition.validator(value, ...unwrapRuleParameters(params));
89
+ }
90
+ };
91
+ const processors = {
92
+ ...defaultProcessors,
73
93
  ...properties,
74
94
  ...{
75
95
  _validator: definition.validator,
@@ -90,7 +110,7 @@ function createRule(definition) {
90
110
  let fakeParams = [];
91
111
  const staticProcessors = defineRuleProcessors(definition, ...fakeParams);
92
112
  const isAsync = definition.validator.constructor.name === "AsyncFunction";
93
- if (definition.validator.length > 1) {
113
+ if (getFunctionParametersLength(definition.validator) > 1) {
94
114
  const ruleFactory = function(...params) {
95
115
  return defineRuleProcessors(definition, ...params);
96
116
  };
@@ -98,9 +118,10 @@ function createRule(definition) {
98
118
  ruleFactory.message = staticProcessors.message;
99
119
  ruleFactory.active = staticProcessors.active;
100
120
  ruleFactory.type = staticProcessors.type;
101
- ruleFactory._validator = definition.validator;
102
- ruleFactory._message = definition.message;
103
- ruleFactory._active = definition.active;
121
+ ruleFactory.exec = staticProcessors.exec;
122
+ ruleFactory._validator = staticProcessors.validator;
123
+ ruleFactory._message = staticProcessors.message;
124
+ ruleFactory._active = staticProcessors.active;
104
125
  ruleFactory._type = definition.type;
105
126
  ruleFactory._patched = false;
106
127
  ruleFactory._async = isAsync;
@@ -111,6 +132,9 @@ function createRule(definition) {
111
132
  }
112
133
  throw new Error("Validator must be a function");
113
134
  }
135
+ function defineType(ruleName) {
136
+ return ruleName;
137
+ }
114
138
 
115
139
  // src/core/useRegle/useRegle.ts
116
140
  var import_vue10 = require("vue");
@@ -118,8 +142,125 @@ var import_vue10 = require("vue");
118
142
  // src/core/useRegle/useStateProperties/useStateProperties.ts
119
143
  var import_vue9 = require("vue");
120
144
 
145
+ // src/core/useStorage/useStorage.ts
146
+ var import_vue2 = require("vue");
147
+ function useStorage() {
148
+ const ruleDeclStorage = (0, import_vue2.shallowRef)(/* @__PURE__ */ new Map());
149
+ const fieldsStorage = (0, import_vue2.shallowRef)(
150
+ /* @__PURE__ */ new Map()
151
+ );
152
+ const collectionsStorage = (0, import_vue2.shallowRef)(/* @__PURE__ */ new Map());
153
+ const dirtyStorage = (0, import_vue2.shallowRef)(/* @__PURE__ */ new Map());
154
+ const ruleStatusStorage = (0, import_vue2.shallowRef)(/* @__PURE__ */ new Map());
155
+ const arrayStatusStorage = (0, import_vue2.shallowRef)(/* @__PURE__ */ new Map());
156
+ function getFieldsEntry($path) {
157
+ const existingFields = fieldsStorage.value.get($path);
158
+ if (existingFields) {
159
+ return existingFields;
160
+ } else {
161
+ const $fields = (0, import_vue2.ref)({});
162
+ fieldsStorage.value.set($path, $fields);
163
+ return $fields;
164
+ }
165
+ }
166
+ function getCollectionsEntry($path) {
167
+ const existingEach = collectionsStorage.value.get($path);
168
+ if (existingEach) {
169
+ return existingEach;
170
+ } else {
171
+ const $each = (0, import_vue2.ref)([]);
172
+ collectionsStorage.value.set($path, $each);
173
+ return $each;
174
+ }
175
+ }
176
+ function addArrayStatus($id, value) {
177
+ arrayStatusStorage.value.set($id, value);
178
+ }
179
+ function getArrayStatus($id) {
180
+ return arrayStatusStorage.value.get($id);
181
+ }
182
+ function deleteArrayStatus($id) {
183
+ if ($id) {
184
+ arrayStatusStorage.value.delete($id);
185
+ }
186
+ }
187
+ function setDirtyEntry($path, dirty) {
188
+ dirtyStorage.value.set($path, dirty);
189
+ }
190
+ function getDirtyState(path) {
191
+ return dirtyStorage.value.get(path) ?? false;
192
+ }
193
+ function addRuleDeclEntry($path, options) {
194
+ ruleDeclStorage.value.set($path, options);
195
+ }
196
+ function checkRuleDeclEntry($path, newRules) {
197
+ const storedRulesDefs = ruleDeclStorage.value.get($path);
198
+ if (!storedRulesDefs)
199
+ return void 0;
200
+ const storedRules = storedRulesDefs;
201
+ const isValidCache = areRulesChanged(newRules, storedRules);
202
+ if (!isValidCache)
203
+ return { valid: false };
204
+ return { valid: true };
205
+ }
206
+ function areRulesChanged(newRules, storedRules) {
207
+ const storedRulesKeys = Object.keys(storedRules);
208
+ const newRulesKeys = Object.keys(newRules);
209
+ if (newRulesKeys.length !== storedRulesKeys.length)
210
+ return false;
211
+ const hasAllValidators = newRulesKeys.every((ruleKey) => storedRulesKeys.includes(ruleKey));
212
+ if (!hasAllValidators)
213
+ return false;
214
+ return newRulesKeys.every((ruleKey) => {
215
+ const newRuleElement = newRules[ruleKey];
216
+ const storedRuleElement = storedRules[ruleKey];
217
+ if (!storedRuleElement || !newRuleElement || typeof newRuleElement === "function" || typeof storedRuleElement === "function")
218
+ return false;
219
+ if (!newRuleElement._params)
220
+ return true;
221
+ return newRuleElement._params?.every((paramKey, index) => {
222
+ const storedParams = unwrapRuleParameters(storedRuleElement._params);
223
+ const newParams = unwrapRuleParameters(newRuleElement._params);
224
+ return storedParams?.[index] === newParams?.[index];
225
+ });
226
+ });
227
+ }
228
+ function trySetRuleStatusRef(path) {
229
+ const ruleStatus = ruleStatusStorage.value.get(path);
230
+ if (ruleStatus) {
231
+ return ruleStatus;
232
+ } else {
233
+ const $pending = (0, import_vue2.ref)(false);
234
+ const $valid = (0, import_vue2.ref)(true);
235
+ const $metadata = (0, import_vue2.ref)({});
236
+ ruleStatusStorage.value.set(path, { $pending, $valid, $metadata });
237
+ return { $pending, $valid, $metadata };
238
+ }
239
+ }
240
+ (0, import_vue2.onScopeDispose)(() => {
241
+ ruleDeclStorage.value.clear();
242
+ fieldsStorage.value.clear();
243
+ collectionsStorage.value.clear();
244
+ dirtyStorage.value.clear();
245
+ ruleStatusStorage.value.clear();
246
+ arrayStatusStorage.value.clear();
247
+ });
248
+ return {
249
+ addRuleDeclEntry,
250
+ setDirtyEntry,
251
+ checkRuleDeclEntry,
252
+ getDirtyState,
253
+ trySetRuleStatusRef,
254
+ getFieldsEntry,
255
+ getCollectionsEntry,
256
+ getArrayStatus,
257
+ addArrayStatus,
258
+ deleteArrayStatus
259
+ };
260
+ }
261
+
121
262
  // src/core/useRegle/useErrors.ts
122
- var import_vue3 = require("vue");
263
+ var import_vue4 = require("vue");
123
264
 
124
265
  // src/utils/object.utils.ts
125
266
  function isObject(obj) {
@@ -153,7 +294,7 @@ function isEmpty(value) {
153
294
  }
154
295
 
155
296
  // src/utils/composables.ts
156
- var import_vue2 = require("vue");
297
+ var import_vue3 = require("vue");
157
298
 
158
299
  // src/core/useRegle/guards/ruleDef.guards.ts
159
300
  function isNestedRulesDef(state, rule) {
@@ -183,14 +324,19 @@ function isFieldStatus(rule) {
183
324
  return !!rule && "$rules" in rule;
184
325
  }
185
326
 
327
+ // src/core/useRegle/guards/rule.errors.guards.ts
328
+ function isExternalErrorCollection(value) {
329
+ return "$each" in value || "$errors" in value;
330
+ }
331
+
186
332
  // src/core/useRegle/useErrors.ts
187
- function extractRulesErrors(rules) {
333
+ function extractRulesErrors(rules, externalErrors) {
188
334
  return Object.entries(rules).map(([ruleKey, rule]) => {
189
335
  if (!rule.$valid) {
190
336
  return rule.$message;
191
337
  }
192
338
  return null;
193
- }).filter((msg) => !!msg);
339
+ }).filter((msg) => !!msg).concat(externalErrors ?? []);
194
340
  }
195
341
  function processFieldErrors(fieldStatus) {
196
342
  if (isNestedRulesStatus(fieldStatus)) {
@@ -200,8 +346,12 @@ function processFieldErrors(fieldStatus) {
200
346
  $errors: fieldStatus.$rules ? extractRulesErrors(fieldStatus.$rules) : [],
201
347
  $each: fieldStatus.$each.map(processFieldErrors)
202
348
  };
203
- } else if (isFieldStatus(fieldStatus) && fieldStatus.$error) {
204
- return extractRulesErrors(fieldStatus.$rules);
349
+ } else if (isFieldStatus(fieldStatus)) {
350
+ if (fieldStatus.$error) {
351
+ return extractRulesErrors(fieldStatus.$rules, fieldStatus.$externalErrors);
352
+ } else {
353
+ return fieldStatus.$externalErrors ?? [];
354
+ }
205
355
  }
206
356
  return [];
207
357
  }
@@ -221,31 +371,41 @@ function extractNestedErrors(fields) {
221
371
  $each: extractCollectionError(fieldStatus)
222
372
  }
223
373
  ];
224
- } else if (isFieldStatus(fieldStatus) && fieldStatus.$error) {
225
- return [fieldKey, extractRulesErrors(fieldStatus.$rules)];
374
+ } else if (isFieldStatus(fieldStatus)) {
375
+ if (fieldStatus.$error) {
376
+ return [fieldKey, extractRulesErrors(fieldStatus.$rules, fieldStatus.$externalErrors)];
377
+ } else {
378
+ return [fieldKey, fieldStatus.$externalErrors ?? []];
379
+ }
226
380
  }
227
381
  return [fieldKey, []];
228
382
  })
229
383
  );
230
384
  }
231
385
  function useErrors($regle) {
232
- const errors = (0, import_vue3.computed)(() => {
386
+ const errors = (0, import_vue4.computed)(() => {
233
387
  return extractNestedErrors($regle.$fields);
234
388
  });
235
389
  return errors;
236
390
  }
237
391
 
238
392
  // src/core/useRegle/useStateProperties/createReactiveNestedStatus.ts
239
- var import_vue7 = require("vue");
393
+ var import_vue8 = require("vue");
240
394
 
241
395
  // src/core/useRegle/useStateProperties/createReactiveCollectionStatus.ts
242
- var import_vue6 = require("vue");
396
+ var import_vue7 = require("vue");
397
+
398
+ // src/utils/randomId.ts
399
+ function randomId() {
400
+ const uint32 = window.crypto.getRandomValues(new Uint32Array(1))[0];
401
+ return uint32.toString(8);
402
+ }
243
403
 
244
404
  // src/core/useRegle/useStateProperties/createReactiveFieldStatus.ts
245
- var import_vue5 = require("vue");
405
+ var import_vue6 = require("vue");
246
406
 
247
407
  // src/core/useRegle/useStateProperties/createReactiveRuleStatus.ts
248
- var import_vue4 = require("vue");
408
+ var import_vue5 = require("vue");
249
409
 
250
410
  // src/types/rules/rule.internal.types.ts
251
411
  var InternalRuleType = /* @__PURE__ */ ((InternalRuleType2) => {
@@ -265,15 +425,20 @@ function createReactiveRuleStatus({
265
425
  storage,
266
426
  options
267
427
  }) {
268
- let scope = (0, import_vue4.effectScope)();
428
+ let scope = (0, import_vue5.effectScope)();
269
429
  let scopeState;
270
- const { $pending, $valid } = storage.trySetRuleStatusRef(`${path}.${ruleKey}`);
430
+ const { $pending, $valid, $metadata } = storage.trySetRuleStatusRef(`${path}.${ruleKey}`);
271
431
  function $watch() {
272
432
  scopeState = scope.run(() => {
273
- const $active = (0, import_vue4.computed)(() => {
433
+ const $defaultMetadata = (0, import_vue5.computed)(() => ({
434
+ $invalid: !$valid.value,
435
+ $params: $params.value,
436
+ ...$metadata.value
437
+ }));
438
+ const $active = (0, import_vue5.computed)(() => {
274
439
  if (isFormRuleDefinition(rule)) {
275
440
  if (typeof rule.value.active === "function") {
276
- return rule.value.active(state.value, ...$params.value);
441
+ return rule.value.active(state.value, $defaultMetadata.value);
277
442
  } else {
278
443
  return rule.value.active;
279
444
  }
@@ -281,12 +446,12 @@ function createReactiveRuleStatus({
281
446
  return true;
282
447
  }
283
448
  });
284
- const $message = (0, import_vue4.computed)(() => {
449
+ const $message = (0, import_vue5.computed)(() => {
285
450
  let message = "";
286
451
  const customMessageRule = customMessages ? customMessages[ruleKey]?.message : void 0;
287
452
  if (customMessageRule) {
288
453
  if (typeof customMessageRule === "function") {
289
- message = customMessageRule(state.value, ...$params.value);
454
+ message = customMessageRule(state.value, $defaultMetadata.value);
290
455
  } else {
291
456
  message = customMessageRule;
292
457
  }
@@ -294,7 +459,7 @@ function createReactiveRuleStatus({
294
459
  if (isFormRuleDefinition(rule)) {
295
460
  if (!(customMessageRule && !rule.value._patched)) {
296
461
  if (typeof rule.value.message === "function") {
297
- message = rule.value.message(state.value, ...$params.value);
462
+ message = rule.value.message(state.value, $defaultMetadata.value);
298
463
  } else {
299
464
  message = rule.value.message;
300
465
  }
@@ -306,27 +471,27 @@ function createReactiveRuleStatus({
306
471
  }
307
472
  return message;
308
473
  });
309
- const $type = (0, import_vue4.computed)(() => {
310
- if (isFormRuleDefinition(rule)) {
474
+ const $type = (0, import_vue5.computed)(() => {
475
+ if (isFormRuleDefinition(rule) && rule.value.type) {
311
476
  return Object.values(InternalRuleType).includes(rule.value.type) ? ruleKey : rule.value.type;
312
477
  } else {
313
478
  return ruleKey;
314
479
  }
315
480
  });
316
- const $validator = (0, import_vue4.computed)(() => {
481
+ const $validator = (0, import_vue5.computed)(() => {
317
482
  if (isFormRuleDefinition(rule)) {
318
483
  return rule.value.validator;
319
484
  } else {
320
485
  return rule.value;
321
486
  }
322
487
  });
323
- const $params = (0, import_vue4.computed)(() => {
488
+ const $params = (0, import_vue5.computed)(() => {
324
489
  if (typeof rule.value === "function") {
325
490
  return [];
326
491
  }
327
492
  return unwrapRuleParameters(rule.value._params ?? []);
328
493
  });
329
- const $path = (0, import_vue4.computed)(() => `${path}.${$type.value}`);
494
+ const $path = (0, import_vue5.computed)(() => `${path}.${$type.value}`);
330
495
  return {
331
496
  $active,
332
497
  $message,
@@ -338,7 +503,7 @@ function createReactiveRuleStatus({
338
503
  });
339
504
  }
340
505
  $watch();
341
- const $unwatchState = (0, import_vue4.watch)(scopeState.$params, $validate, {
506
+ const $unwatchState = (0, import_vue5.watch)(scopeState.$params, $validate, {
342
507
  deep: true
343
508
  });
344
509
  async function $validate() {
@@ -351,7 +516,13 @@ function createReactiveRuleStatus({
351
516
  $valid.value = true;
352
517
  $pending.value = true;
353
518
  const promiseResult = await resultOrPromise;
354
- ruleResult = promiseResult;
519
+ if (typeof promiseResult === "boolean") {
520
+ ruleResult = promiseResult;
521
+ } else {
522
+ const { $valid: $valid2, ...rest } = promiseResult;
523
+ ruleResult = $valid2;
524
+ $metadata.value = rest;
525
+ }
355
526
  } catch (e) {
356
527
  ruleResult = false;
357
528
  } finally {
@@ -359,17 +530,27 @@ function createReactiveRuleStatus({
359
530
  }
360
531
  }
361
532
  } else {
362
- ruleResult = resultOrPromise;
533
+ if (resultOrPromise != null) {
534
+ if (typeof resultOrPromise === "boolean") {
535
+ ruleResult = resultOrPromise;
536
+ } else {
537
+ const { $valid: $valid2, ...rest } = resultOrPromise;
538
+ ruleResult = $valid2;
539
+ $metadata.value = rest;
540
+ }
541
+ }
363
542
  }
364
543
  $valid.value = ruleResult;
544
+ if (options.$externalErrors) {
545
+ }
365
546
  return ruleResult;
366
547
  }
367
548
  function $unwatch() {
368
549
  $unwatchState();
369
550
  scope.stop();
370
- scope = (0, import_vue4.effectScope)();
551
+ scope = (0, import_vue5.effectScope)();
371
552
  }
372
- return (0, import_vue4.reactive)({
553
+ return (0, import_vue5.reactive)({
373
554
  ...scopeState,
374
555
  $pending,
375
556
  $valid,
@@ -386,20 +567,29 @@ function createReactiveFieldStatus({
386
567
  customMessages,
387
568
  path,
388
569
  storage,
389
- options
570
+ options,
571
+ externalErrors
390
572
  }) {
391
- let scope = (0, import_vue5.effectScope)();
573
+ let scope = (0, import_vue6.effectScope)();
392
574
  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);
575
+ const $dirty = (0, import_vue6.ref)(false);
576
+ const $anyDirty = (0, import_vue6.computed)(() => $dirty.value);
577
+ const triggerPunishment = (0, import_vue6.ref)(false);
578
+ const $externalErrors = (0, import_vue6.ref)([]);
579
+ function collectExternalErrors() {
580
+ if (externalErrors.value) {
581
+ $externalErrors.value = externalErrors.value;
582
+ }
583
+ }
584
+ collectExternalErrors();
585
+ const $unwatchExternalErrors = (0, import_vue6.watch)(externalErrors, collectExternalErrors);
396
586
  function createReactiveRulesResult() {
397
587
  const declaredRules = rulesDef.value;
398
588
  const storeResult = storage.checkRuleDeclEntry(path, declaredRules);
399
589
  $rules.value = Object.fromEntries(
400
590
  Object.entries(declaredRules).map(([ruleKey, rule]) => {
401
591
  if (rule) {
402
- const ruleRef = (0, import_vue5.toRef)(() => rule);
592
+ const ruleRef = (0, import_vue6.toRef)(() => rule);
403
593
  return [
404
594
  ruleKey,
405
595
  createReactiveRuleStatus({
@@ -426,18 +616,19 @@ function createReactiveFieldStatus({
426
616
  }
427
617
  storage.addRuleDeclEntry(path, declaredRules);
428
618
  }
429
- const $unwatchDirty = (0, import_vue5.watch)($dirty, () => {
619
+ const $unwatchDirty = (0, import_vue6.watch)($dirty, () => {
430
620
  storage.setDirtyEntry(path, $dirty.value);
431
621
  });
432
- const $unwatchState = (0, import_vue5.watch)(state, () => {
433
- if ((0, import_vue5.unref)(options.autoDirty)) {
622
+ const $unwatchState = (0, import_vue6.watch)(state, () => {
623
+ if ((0, import_vue6.unref)(options.autoDirty)) {
434
624
  if (!$dirty.value) {
435
625
  $dirty.value = true;
436
626
  }
437
627
  }
438
- if (!(0, import_vue5.unref)(options.lazy)) {
628
+ if (!(0, import_vue6.unref)(options.lazy)) {
439
629
  $commit();
440
630
  }
631
+ $externalErrors.value = [];
441
632
  });
442
633
  function $unwatch() {
443
634
  if ($rules.value) {
@@ -451,32 +642,33 @@ function createReactiveFieldStatus({
451
642
  }
452
643
  $unwatchState();
453
644
  $unwatchValid();
645
+ $unwatchExternalErrors();
454
646
  scope.stop();
455
- scope = (0, import_vue5.effectScope)();
647
+ scope = (0, import_vue6.effectScope)();
456
648
  }
457
649
  function $watch() {
458
650
  scopeState = scope.run(() => {
459
- const $error = (0, import_vue5.computed)(() => {
651
+ const $error = (0, import_vue6.computed)(() => {
460
652
  return $invalid.value && !$pending.value && $dirty.value;
461
653
  });
462
- const $pending = (0, import_vue5.computed)(() => {
463
- if (triggerPunishment.value || !(0, import_vue5.unref)(options.rewardEarly)) {
654
+ const $pending = (0, import_vue6.computed)(() => {
655
+ if (triggerPunishment.value || !(0, import_vue6.unref)(options.rewardEarly)) {
464
656
  return Object.entries($rules.value).some(([key, ruleResult]) => {
465
657
  return ruleResult.$pending;
466
658
  });
467
659
  }
468
660
  return false;
469
661
  });
470
- const $invalid = (0, import_vue5.computed)(() => {
471
- if (triggerPunishment.value || !(0, import_vue5.unref)(options.rewardEarly)) {
662
+ const $invalid = (0, import_vue6.computed)(() => {
663
+ if (triggerPunishment.value || !(0, import_vue6.unref)(options.rewardEarly)) {
472
664
  return Object.entries($rules.value).some(([key, ruleResult]) => {
473
665
  return !ruleResult.$valid;
474
666
  });
475
667
  }
476
668
  return false;
477
669
  });
478
- const $valid = (0, import_vue5.computed)(() => {
479
- if ((0, import_vue5.unref)(options.rewardEarly)) {
670
+ const $valid = (0, import_vue6.computed)(() => {
671
+ if ((0, import_vue6.unref)(options.rewardEarly)) {
480
672
  return Object.entries($rules.value).every(([key, ruleResult]) => {
481
673
  return ruleResult.$valid;
482
674
  });
@@ -492,15 +684,16 @@ function createReactiveFieldStatus({
492
684
  };
493
685
  });
494
686
  }
495
- const $rules = (0, import_vue5.ref)();
687
+ const $rules = (0, import_vue6.ref)();
496
688
  createReactiveRulesResult();
497
- const $unwatchValid = (0, import_vue5.watch)(scopeState.$valid, (valid) => {
498
- if ((0, import_vue5.unref)(options.rewardEarly) && valid) {
689
+ const $unwatchValid = (0, import_vue6.watch)(scopeState.$valid, (valid) => {
690
+ if ((0, import_vue6.unref)(options.rewardEarly) && valid) {
499
691
  triggerPunishment.value = false;
500
692
  }
501
693
  });
502
694
  function $reset() {
503
695
  $dirty.value = false;
696
+ $externalErrors.value = [];
504
697
  }
505
698
  function $touch() {
506
699
  $dirty.value = true;
@@ -523,29 +716,28 @@ function createReactiveFieldStatus({
523
716
  return false;
524
717
  }
525
718
  }
526
- return (0, import_vue5.reactive)({
719
+ function $clearExternalErrors() {
720
+ $externalErrors.value = [];
721
+ }
722
+ return (0, import_vue6.reactive)({
527
723
  $dirty,
528
724
  $anyDirty,
529
725
  $invalid: scopeState.$invalid,
530
726
  $error: scopeState.$error,
531
727
  $pending: scopeState.$pending,
532
728
  $valid: scopeState.$valid,
729
+ $externalErrors,
533
730
  $value: state,
534
731
  $rules,
535
732
  $reset,
536
733
  $touch,
537
734
  $validate,
538
735
  $unwatch,
539
- $watch
736
+ $watch,
737
+ $clearExternalErrors
540
738
  });
541
739
  }
542
740
 
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
741
  // src/core/useRegle/useStateProperties/createReactiveCollectionStatus.ts
550
742
  function createCollectionElement({
551
743
  path,
@@ -554,7 +746,8 @@ function createCollectionElement({
554
746
  storage,
555
747
  value,
556
748
  customMessages,
557
- rules
749
+ rules,
750
+ externalErrors
558
751
  }) {
559
752
  const $id = randomId();
560
753
  const $path = `${path}.${$id}`;
@@ -568,14 +761,16 @@ function createCollectionElement({
568
761
  }
569
762
  });
570
763
  }
571
- const $state = (0, import_vue6.toRefs)(value);
764
+ const $state = (0, import_vue7.toRefs)(value);
765
+ const $externalErrors = (0, import_vue7.toRef)(() => externalErrors.value?.[index]);
572
766
  const $status = createReactiveChildrenStatus({
573
767
  state: $state[index],
574
- rulesDef: (0, import_vue6.toRef)(() => rules),
768
+ rulesDef: (0, import_vue7.toRef)(() => rules),
575
769
  customMessages,
576
770
  path: $path,
577
771
  storage,
578
- options
772
+ options,
773
+ externalErrors: $externalErrors
579
774
  });
580
775
  if ($status) {
581
776
  $status.$id = value[index].$id ?? $id;
@@ -589,25 +784,41 @@ function createReactiveCollectionStatus({
589
784
  customMessages,
590
785
  path,
591
786
  storage,
592
- options
787
+ options,
788
+ externalErrors
593
789
  }) {
594
790
  if (Array.isArray(state.value) && !rulesDef.value.$each) {
595
791
  return null;
596
792
  }
597
793
  let $unwatchState = null;
598
- const $fieldStatus = (0, import_vue6.ref)({});
794
+ const $fieldStatus = (0, import_vue7.ref)({});
599
795
  const $eachStatus = storage.getCollectionsEntry(path);
600
796
  createStatus();
601
797
  $watch();
602
798
  function createStatus() {
603
799
  const { $each, ...otherFields } = rulesDef.value;
800
+ const $externalErrorsField = (0, import_vue7.toRef)(() => {
801
+ if (externalErrors.value) {
802
+ if (isExternalErrorCollection(externalErrors.value)) {
803
+ return externalErrors.value.$errors;
804
+ }
805
+ }
806
+ });
807
+ const $externalErrorsEach = (0, import_vue7.toRef)(() => {
808
+ if (externalErrors.value) {
809
+ if (isExternalErrorCollection(externalErrors.value)) {
810
+ return externalErrors.value.$each;
811
+ }
812
+ }
813
+ });
604
814
  $fieldStatus.value = createReactiveFieldStatus({
605
815
  state,
606
- rulesDef: (0, import_vue6.toRef)(() => otherFields),
816
+ rulesDef: (0, import_vue7.toRef)(() => otherFields),
607
817
  customMessages,
608
818
  path,
609
819
  storage,
610
- options
820
+ options,
821
+ externalErrors: $externalErrorsField
611
822
  });
612
823
  if (Array.isArray(state.value) && $each) {
613
824
  $eachStatus.value = state.value.map((value, index) => {
@@ -623,7 +834,8 @@ function createReactiveCollectionStatus({
623
834
  value: state.value,
624
835
  index,
625
836
  options,
626
- storage
837
+ storage,
838
+ externalErrors: $externalErrorsEach
627
839
  });
628
840
  }
629
841
  }).filter((f) => !!f);
@@ -647,6 +859,13 @@ function createReactiveCollectionStatus({
647
859
  $eachStatus.value[index].$unwatch();
648
860
  }
649
861
  } else {
862
+ const $externalErrorsEach = (0, import_vue7.toRef)(() => {
863
+ if (externalErrors.value) {
864
+ if ("$each" in externalErrors.value) {
865
+ return externalErrors.value.$each;
866
+ }
867
+ }
868
+ });
650
869
  const newElement = createCollectionElement({
651
870
  value: state.value,
652
871
  rules: $each,
@@ -654,7 +873,8 @@ function createReactiveCollectionStatus({
654
873
  path,
655
874
  storage,
656
875
  options,
657
- index
876
+ index,
877
+ externalErrors: $externalErrorsEach
658
878
  });
659
879
  if (newElement) {
660
880
  $eachStatus.value[index] = newElement;
@@ -669,7 +889,7 @@ function createReactiveCollectionStatus({
669
889
  item.$unwatch();
670
890
  });
671
891
  $eachStatus.value.length = state.value.length;
672
- (0, import_vue6.nextTick)($watch);
892
+ (0, import_vue7.nextTick)($watch);
673
893
  }
674
894
  }
675
895
  function $unwatch() {
@@ -686,7 +906,7 @@ function createReactiveCollectionStatus({
686
906
  }
687
907
  }
688
908
  function $watch() {
689
- $unwatchState = (0, import_vue6.watch)(() => state.value.length, updateChildrenStatus, {
909
+ $unwatchState = (0, import_vue7.watch)(() => state.value.length, updateChildrenStatus, {
690
910
  flush: "sync"
691
911
  });
692
912
  }
@@ -714,7 +934,7 @@ function createReactiveCollectionStatus({
714
934
  return false;
715
935
  }
716
936
  }
717
- return (0, import_vue6.reactive)({
937
+ return (0, import_vue7.reactive)({
718
938
  ...$fieldStatus.value,
719
939
  $each: $eachStatus,
720
940
  $validate,
@@ -733,17 +953,19 @@ function createReactiveNestedStatus({
733
953
  path = "",
734
954
  rootRules,
735
955
  storage,
736
- options
956
+ options,
957
+ externalErrors
737
958
  }) {
738
- let scope = (0, import_vue7.effectScope)();
959
+ let scope = (0, import_vue8.effectScope)();
739
960
  let scopeState;
740
961
  let $unwatchFields;
741
962
  function createReactiveFieldsStatus() {
742
963
  $fields.value = Object.fromEntries(
743
964
  Object.entries(scopeRules.value).map(([statePropKey, statePropRules]) => {
744
965
  if (statePropRules) {
745
- const stateRef = (0, import_vue7.toRef)(state.value, statePropKey);
746
- const statePropRulesRef = (0, import_vue7.toRef)(() => statePropRules);
966
+ const stateRef = (0, import_vue8.toRef)(state.value, statePropKey);
967
+ const statePropRulesRef = (0, import_vue8.toRef)(() => statePropRules);
968
+ const $externalErrors = (0, import_vue8.toRef)(() => externalErrors.value?.[statePropKey]);
747
969
  return [
748
970
  statePropKey,
749
971
  createReactiveChildrenStatus({
@@ -752,7 +974,8 @@ function createReactiveNestedStatus({
752
974
  customMessages,
753
975
  path: path ? `${path}.${statePropKey}` : statePropKey,
754
976
  storage,
755
- options
977
+ options,
978
+ externalErrors: $externalErrors
756
979
  })
757
980
  ];
758
981
  }
@@ -789,7 +1012,7 @@ function createReactiveNestedStatus({
789
1012
  }
790
1013
  function $watch() {
791
1014
  if (rootRules) {
792
- $unwatchFields = (0, import_vue7.watch)(
1015
+ $unwatchFields = (0, import_vue8.watch)(
793
1016
  rootRules,
794
1017
  () => {
795
1018
  $unwatch();
@@ -799,28 +1022,28 @@ function createReactiveNestedStatus({
799
1022
  );
800
1023
  }
801
1024
  scopeState = scope.run(() => {
802
- const $dirty = (0, import_vue7.computed)(() => {
1025
+ const $dirty = (0, import_vue8.computed)(() => {
803
1026
  return Object.entries($fields.value).every(([key, statusOrField]) => {
804
1027
  return statusOrField.$dirty;
805
1028
  });
806
1029
  });
807
- const $anyDirty = (0, import_vue7.computed)(() => {
1030
+ const $anyDirty = (0, import_vue8.computed)(() => {
808
1031
  return Object.entries($fields.value).some(([key, statusOrField]) => {
809
1032
  return statusOrField.$dirty;
810
1033
  });
811
1034
  });
812
- const $invalid = (0, import_vue7.computed)(() => {
1035
+ const $invalid = (0, import_vue8.computed)(() => {
813
1036
  return Object.entries($fields.value).some(([key, statusOrField]) => {
814
1037
  return statusOrField.$invalid;
815
1038
  });
816
1039
  });
817
- const $valid = (0, import_vue7.computed)(() => !$invalid.value);
818
- const $error = (0, import_vue7.computed)(() => {
1040
+ const $valid = (0, import_vue8.computed)(() => !$invalid.value);
1041
+ const $error = (0, import_vue8.computed)(() => {
819
1042
  return Object.entries($fields.value).some(([key, statusOrField]) => {
820
1043
  return statusOrField.$error;
821
1044
  });
822
1045
  });
823
- const $pending = (0, import_vue7.computed)(() => {
1046
+ const $pending = (0, import_vue8.computed)(() => {
824
1047
  return Object.entries($fields.value).some(([key, statusOrField]) => {
825
1048
  return statusOrField.$pending;
826
1049
  });
@@ -845,9 +1068,14 @@ function createReactiveNestedStatus({
845
1068
  $unwatchFields();
846
1069
  }
847
1070
  scope.stop();
848
- scope = (0, import_vue7.effectScope)();
1071
+ scope = (0, import_vue8.effectScope)();
849
1072
  }
850
- return (0, import_vue7.reactive)({
1073
+ function $clearExternalErrors() {
1074
+ Object.entries($fields.value).forEach(([_, field]) => {
1075
+ field.$clearExternalErrors();
1076
+ });
1077
+ }
1078
+ return (0, import_vue8.reactive)({
851
1079
  ...scopeState,
852
1080
  $fields,
853
1081
  $value: state,
@@ -855,7 +1083,8 @@ function createReactiveNestedStatus({
855
1083
  $touch,
856
1084
  $validate,
857
1085
  $unwatch,
858
- $watch
1086
+ $watch,
1087
+ $clearExternalErrors
859
1088
  });
860
1089
  }
861
1090
  function createReactiveChildrenStatus({
@@ -864,7 +1093,8 @@ function createReactiveChildrenStatus({
864
1093
  customMessages,
865
1094
  path,
866
1095
  storage,
867
- options
1096
+ options,
1097
+ externalErrors
868
1098
  }) {
869
1099
  if (isCollectionRulesDef(rulesDef)) {
870
1100
  return createReactiveCollectionStatus({
@@ -873,7 +1103,8 @@ function createReactiveChildrenStatus({
873
1103
  customMessages,
874
1104
  path,
875
1105
  storage,
876
- options
1106
+ options,
1107
+ externalErrors
877
1108
  });
878
1109
  } else if (isNestedRulesDef(state, rulesDef) && isRefObject(state)) {
879
1110
  return createReactiveNestedStatus({
@@ -882,7 +1113,8 @@ function createReactiveChildrenStatus({
882
1113
  customMessages,
883
1114
  path,
884
1115
  storage,
885
- options
1116
+ options,
1117
+ externalErrors
886
1118
  });
887
1119
  } else if (isValidatorRulesDef(rulesDef)) {
888
1120
  return createReactiveFieldStatus({
@@ -891,123 +1123,17 @@ function createReactiveChildrenStatus({
891
1123
  customMessages,
892
1124
  path,
893
1125
  storage,
894
- options
1126
+ options,
1127
+ externalErrors
895
1128
  });
896
1129
  }
897
1130
  return null;
898
1131
  }
899
1132
 
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
1133
  // src/core/useRegle/useStateProperties/useStateProperties.ts
1009
1134
  function useStateProperties(scopeRules, state, options, customRules) {
1010
1135
  const storage = useStorage();
1136
+ const externalErrors = (0, import_vue9.computed)(() => (0, import_vue9.unref)(options.$externalErrors));
1011
1137
  const $regle = (0, import_vue9.reactive)(
1012
1138
  createReactiveNestedStatus({
1013
1139
  rootRules: scopeRules,
@@ -1015,7 +1141,8 @@ function useStateProperties(scopeRules, state, options, customRules) {
1015
1141
  state,
1016
1142
  customMessages: customRules?.(),
1017
1143
  storage,
1018
- options
1144
+ options,
1145
+ externalErrors
1019
1146
  })
1020
1147
  );
1021
1148
  const errors = useErrors($regle);
@@ -1035,10 +1162,11 @@ function createUseRegleComposable(customRules, options) {
1035
1162
  ...globalOptions,
1036
1163
  ...options2
1037
1164
  };
1038
- const initialState = (0, import_vue10.shallowRef)(structuredClone((0, import_vue10.toRaw)(state.value)));
1165
+ const processedState = (0, import_vue10.ref)(state);
1166
+ const initialState = (0, import_vue10.shallowRef)(structuredClone((0, import_vue10.toRaw)(processedState.value)));
1039
1167
  const { $regle, errors } = useStateProperties(
1040
1168
  scopeRules,
1041
- state,
1169
+ processedState,
1042
1170
  resolvedOptions,
1043
1171
  customRules
1044
1172
  );
@@ -1046,6 +1174,12 @@ function createUseRegleComposable(customRules, options) {
1046
1174
  state.value = (0, import_vue10.toRaw)(initialState.value);
1047
1175
  $regle.$reset();
1048
1176
  }
1177
+ const $valid = (0, import_vue10.computed)(() => {
1178
+ return $regle.$valid && $regle.$dirty && !$regle.$pending;
1179
+ });
1180
+ const $invalid = (0, import_vue10.computed)(() => {
1181
+ return $regle.$invalid && $regle.$dirty || $regle.$pending;
1182
+ });
1049
1183
  async function validateForm() {
1050
1184
  $regle.$touch();
1051
1185
  const result = await $regle.$validate();
@@ -1055,19 +1189,21 @@ function createUseRegleComposable(customRules, options) {
1055
1189
  return false;
1056
1190
  }
1057
1191
  return {
1058
- state,
1192
+ $state: state,
1059
1193
  $regle,
1060
- errors,
1194
+ $errors: errors,
1061
1195
  resetForm,
1062
- validateForm
1196
+ validateForm,
1197
+ $valid,
1198
+ $invalid
1063
1199
  };
1064
1200
  }
1065
1201
  return useRegle2;
1066
1202
  }
1067
1203
  var useRegle = createUseRegleComposable();
1068
1204
 
1069
- // src/core/defineRegleOptions.ts
1070
- function defineRegleOptions({
1205
+ // src/core/defineRegleConfig.ts
1206
+ function defineRegleConfig({
1071
1207
  rules,
1072
1208
  options
1073
1209
  }) {
@@ -1078,6 +1214,8 @@ function defineRegleOptions({
1078
1214
  0 && (module.exports = {
1079
1215
  InternalRuleType,
1080
1216
  createRule,
1081
- defineRegleOptions,
1082
- unwrapRuleParameters
1217
+ defineRegleConfig,
1218
+ defineType,
1219
+ unwrapRuleParameters,
1220
+ useRegle
1083
1221
  });