@regle/core 0.0.4-beta.0 → 0.0.5-beta.0

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,7 +22,8 @@ var src_exports = {};
22
22
  __export(src_exports, {
23
23
  InternalRuleType: () => InternalRuleType,
24
24
  createRule: () => createRule,
25
- defineCustomValidators: () => defineCustomValidators
25
+ defineRegleOptions: () => defineRegleOptions,
26
+ unwrapRuleParameters: () => unwrapRuleParameters
26
27
  });
27
28
  module.exports = __toCommonJS(src_exports);
28
29
 
@@ -50,6 +51,7 @@ function createReactiveParams(params) {
50
51
  // src/core/createRule/defineRuleProcessors.ts
51
52
  function defineRuleProcessors(definition, ...params) {
52
53
  const { message, validator, active, ...properties } = definition;
54
+ const isAsync = validator.constructor.name === "AsyncFunction";
53
55
  const processors = {
54
56
  message(value, ...args) {
55
57
  if (typeof definition.message === "function") {
@@ -75,6 +77,7 @@ function defineRuleProcessors(definition, ...params) {
75
77
  _active: definition.active,
76
78
  _type: definition.type,
77
79
  _patched: false,
80
+ _async: isAsync,
78
81
  _params: createReactiveParams(params)
79
82
  }
80
83
  };
@@ -86,6 +89,7 @@ function createRule(definition) {
86
89
  if (typeof definition.validator === "function") {
87
90
  let fakeParams = [];
88
91
  const staticProcessors = defineRuleProcessors(definition, ...fakeParams);
92
+ const isAsync = definition.validator.constructor.name === "AsyncFunction";
89
93
  if (definition.validator.length > 1) {
90
94
  const ruleFactory = function(...params) {
91
95
  return defineRuleProcessors(definition, ...params);
@@ -99,6 +103,7 @@ function createRule(definition) {
99
103
  ruleFactory._active = definition.active;
100
104
  ruleFactory._type = definition.type;
101
105
  ruleFactory._patched = false;
106
+ ruleFactory._async = isAsync;
102
107
  return ruleFactory;
103
108
  } else {
104
109
  return staticProcessors;
@@ -233,6 +238,9 @@ function useErrors($regle) {
233
238
  // src/core/useRegle/useStateProperties/createReactiveNestedStatus.ts
234
239
  var import_vue7 = require("vue");
235
240
 
241
+ // src/core/useRegle/useStateProperties/createReactiveCollectionStatus.ts
242
+ var import_vue6 = require("vue");
243
+
236
244
  // src/core/useRegle/useStateProperties/createReactiveFieldStatus.ts
237
245
  var import_vue5 = require("vue");
238
246
 
@@ -254,7 +262,8 @@ function createReactiveRuleStatus({
254
262
  ruleKey,
255
263
  state,
256
264
  path,
257
- storage
265
+ storage,
266
+ options
258
267
  }) {
259
268
  let scope = (0, import_vue4.effectScope)();
260
269
  let scopeState;
@@ -274,7 +283,7 @@ function createReactiveRuleStatus({
274
283
  });
275
284
  const $message = (0, import_vue4.computed)(() => {
276
285
  let message = "";
277
- const customMessageRule = customMessages[ruleKey]?.message;
286
+ const customMessageRule = customMessages ? customMessages[ruleKey]?.message : void 0;
278
287
  if (customMessageRule) {
279
288
  if (typeof customMessageRule === "function") {
280
289
  message = customMessageRule(state.value, ...$params.value);
@@ -327,7 +336,6 @@ function createReactiveRuleStatus({
327
336
  $path
328
337
  };
329
338
  });
330
- $validate();
331
339
  }
332
340
  $watch();
333
341
  const $unwatchState = (0, import_vue4.watch)(scopeState.$params, $validate, {
@@ -360,7 +368,6 @@ function createReactiveRuleStatus({
360
368
  $unwatchState();
361
369
  scope.stop();
362
370
  scope = (0, import_vue4.effectScope)();
363
- scopeState = null;
364
371
  }
365
372
  return (0, import_vue4.reactive)({
366
373
  ...scopeState,
@@ -378,12 +385,14 @@ function createReactiveFieldStatus({
378
385
  rulesDef,
379
386
  customMessages,
380
387
  path,
381
- storage
388
+ storage,
389
+ options
382
390
  }) {
383
391
  let scope = (0, import_vue5.effectScope)();
384
392
  let scopeState;
385
393
  const $dirty = (0, import_vue5.ref)(false);
386
394
  const $anyDirty = (0, import_vue5.computed)(() => $dirty.value);
395
+ const triggerPunishment = (0, import_vue5.ref)(false);
387
396
  function createReactiveRulesResult() {
388
397
  const declaredRules = rulesDef.value;
389
398
  const storeResult = storage.checkRuleDeclEntry(path, declaredRules);
@@ -400,7 +409,8 @@ function createReactiveFieldStatus({
400
409
  ruleKey,
401
410
  state,
402
411
  path,
403
- storage
412
+ storage,
413
+ options
404
414
  })
405
415
  ];
406
416
  }
@@ -417,10 +427,14 @@ function createReactiveFieldStatus({
417
427
  storage.setDirtyEntry(path, $dirty.value);
418
428
  });
419
429
  const $unwatchState = (0, import_vue5.watch)(state, () => {
420
- if (!$dirty.value) {
421
- $dirty.value = true;
430
+ if ((0, import_vue5.unref)(options.autoDirty)) {
431
+ if (!$dirty.value) {
432
+ $dirty.value = true;
433
+ }
434
+ }
435
+ if (!(0, import_vue5.unref)(options.lazy)) {
436
+ $commit();
422
437
  }
423
- $validate();
424
438
  });
425
439
  function $unwatch() {
426
440
  if ($rules.value) {
@@ -433,9 +447,9 @@ function createReactiveFieldStatus({
433
447
  storage.setDirtyEntry(path, $dirty.value);
434
448
  }
435
449
  $unwatchState();
450
+ $unwatchValid();
436
451
  scope.stop();
437
452
  scope = (0, import_vue5.effectScope)();
438
- scopeState = null;
439
453
  }
440
454
  function $watch() {
441
455
  scopeState = scope.run(() => {
@@ -443,16 +457,30 @@ function createReactiveFieldStatus({
443
457
  return $invalid.value && !$pending.value && $dirty.value;
444
458
  });
445
459
  const $pending = (0, import_vue5.computed)(() => {
446
- return Object.entries($rules.value).some(([key, rule]) => {
447
- return rule.$pending;
448
- });
460
+ if (triggerPunishment.value || !(0, import_vue5.unref)(options.rewardEarly)) {
461
+ return Object.entries($rules.value).some(([key, ruleResult]) => {
462
+ return ruleResult.$pending;
463
+ });
464
+ }
465
+ return false;
449
466
  });
450
467
  const $invalid = (0, import_vue5.computed)(() => {
451
- return Object.entries($rules.value).some(([key, ruleResult]) => {
452
- return !ruleResult.$valid;
453
- });
468
+ if (triggerPunishment.value || !(0, import_vue5.unref)(options.rewardEarly)) {
469
+ return Object.entries($rules.value).some(([key, ruleResult]) => {
470
+ return !ruleResult.$valid;
471
+ });
472
+ }
473
+ return false;
474
+ });
475
+ const $valid = (0, import_vue5.computed)(() => {
476
+ if ((0, import_vue5.unref)(options.rewardEarly)) {
477
+ return Object.entries($rules.value).every(([key, ruleResult]) => {
478
+ return ruleResult.$valid;
479
+ });
480
+ } else {
481
+ return !$invalid.value;
482
+ }
454
483
  });
455
- const $valid = (0, import_vue5.computed)(() => !$invalid.value);
456
484
  return {
457
485
  $error,
458
486
  $pending,
@@ -463,15 +491,25 @@ function createReactiveFieldStatus({
463
491
  }
464
492
  const $rules = (0, import_vue5.ref)();
465
493
  createReactiveRulesResult();
494
+ const $unwatchValid = (0, import_vue5.watch)(scopeState.$valid, (valid) => {
495
+ if ((0, import_vue5.unref)(options.rewardEarly) && valid) {
496
+ triggerPunishment.value = false;
497
+ }
498
+ });
466
499
  function $reset() {
467
500
  $dirty.value = false;
468
501
  }
469
502
  function $touch() {
470
503
  $dirty.value = true;
471
- $validate();
504
+ }
505
+ function $commit() {
506
+ Object.entries($rules.value).map(([key, rule]) => {
507
+ return rule.$validate();
508
+ });
472
509
  }
473
510
  async function $validate() {
474
511
  try {
512
+ triggerPunishment.value = true;
475
513
  const results = await Promise.all(
476
514
  Object.entries($rules.value).map(([key, rule]) => {
477
515
  return rule.$validate();
@@ -499,14 +537,53 @@ function createReactiveFieldStatus({
499
537
  });
500
538
  }
501
539
 
540
+ // src/utils/randomId.ts
541
+ function randomId() {
542
+ const uint32 = window.crypto.getRandomValues(new Uint32Array(1))[0];
543
+ return uint32.toString(8);
544
+ }
545
+
502
546
  // src/core/useRegle/useStateProperties/createReactiveCollectionStatus.ts
503
- var import_vue6 = require("vue");
547
+ function createCollectionElement({
548
+ path,
549
+ index,
550
+ options,
551
+ storage,
552
+ value,
553
+ customMessages,
554
+ rules
555
+ }) {
556
+ const $path = `${path}.${index}`;
557
+ const $id = randomId();
558
+ if (!value[index].$id) {
559
+ Object.defineProperties(value[index], {
560
+ $id: {
561
+ value: $id
562
+ }
563
+ });
564
+ }
565
+ const $state = (0, import_vue6.toRefs)(value);
566
+ const $status = createReactiveChildrenStatus({
567
+ state: $state[index],
568
+ rulesDef: (0, import_vue6.toRef)(() => rules),
569
+ customMessages,
570
+ path: $path,
571
+ storage,
572
+ options
573
+ });
574
+ if ($status) {
575
+ $status.$id = value[index].$id ?? $id;
576
+ storage.addArrayStatus($status.$id, $status);
577
+ }
578
+ return $status;
579
+ }
504
580
  function createReactiveCollectionStatus({
505
581
  state,
506
582
  rulesDef,
507
583
  customMessages,
508
584
  path,
509
- storage
585
+ storage,
586
+ options
510
587
  }) {
511
588
  if (Array.isArray(state.value) && !rulesDef.value.$each) {
512
589
  return null;
@@ -523,16 +600,17 @@ function createReactiveCollectionStatus({
523
600
  rulesDef: (0, import_vue6.toRef)(() => otherFields),
524
601
  customMessages,
525
602
  path,
526
- storage
603
+ storage,
604
+ options
527
605
  });
528
606
  if (Array.isArray(state.value) && $each) {
529
607
  $eachStatus.value = state.value.map((value, index) => {
530
- const $path = `${path}.${index}`;
531
- return createReactiveChildrenStatus({
532
- state: (0, import_vue6.toRef)(() => value),
533
- rulesDef: (0, import_vue6.toRef)(() => $each),
534
- customMessages,
535
- path: $path,
608
+ return createCollectionElement({
609
+ path,
610
+ rules: $each,
611
+ value: state.value,
612
+ index,
613
+ options,
536
614
  storage
537
615
  });
538
616
  }).filter((f) => !!f);
@@ -540,6 +618,38 @@ function createReactiveCollectionStatus({
540
618
  $eachStatus.value = [];
541
619
  }
542
620
  }
621
+ function updateChildrenStatus() {
622
+ const { $each } = rulesDef.value;
623
+ if (Array.isArray(state.value) && $eachStatus.value && $each) {
624
+ state.value.forEach((value, index) => {
625
+ if (value.$id) {
626
+ const previousStatus = storage.getArrayStatus(value.$id);
627
+ if (previousStatus) {
628
+ $eachStatus.value[index] = previousStatus;
629
+ }
630
+ } else {
631
+ const newElement = createCollectionElement({
632
+ value: state.value,
633
+ rules: $each,
634
+ customMessages,
635
+ path,
636
+ storage,
637
+ options,
638
+ index
639
+ });
640
+ if (newElement) {
641
+ $eachStatus.value[index] = newElement;
642
+ }
643
+ }
644
+ });
645
+ }
646
+ if ($eachStatus.value) {
647
+ const deletedItems = $eachStatus.value.filter(($each2) => {
648
+ return Array.isArray(state.value) && !state.value.find((val) => val.$id === $each2.$id);
649
+ });
650
+ deletedItems.forEach((item) => item.$unwatch());
651
+ }
652
+ }
543
653
  function $unwatch() {
544
654
  if ($unwatchState) {
545
655
  $unwatchState();
@@ -557,16 +667,43 @@ function createReactiveCollectionStatus({
557
667
  $unwatchState = (0, import_vue6.watch)(
558
668
  state,
559
669
  () => {
560
- createStatus();
670
+ updateChildrenStatus();
561
671
  },
562
672
  { deep: true, flush: "sync" }
563
673
  );
564
674
  }
675
+ function $touch() {
676
+ $fieldStatus.value.$touch();
677
+ $eachStatus.value.forEach(($each) => {
678
+ $each.$touch();
679
+ });
680
+ }
681
+ function $reset() {
682
+ $fieldStatus.value.$reset();
683
+ $eachStatus.value.forEach(($each) => {
684
+ $each.$reset();
685
+ });
686
+ }
687
+ async function $validate() {
688
+ try {
689
+ const results = await Promise.all(
690
+ $eachStatus.value.map((rule) => {
691
+ return rule.$validate();
692
+ })
693
+ );
694
+ return results.every((value) => !!value);
695
+ } catch (e) {
696
+ return false;
697
+ }
698
+ }
565
699
  return (0, import_vue6.reactive)({
566
700
  ...$fieldStatus.value,
567
701
  $each: $eachStatus,
702
+ $validate,
568
703
  $unwatch,
569
- $watch
704
+ $watch,
705
+ $touch,
706
+ $reset
570
707
  });
571
708
  }
572
709
 
@@ -577,7 +714,8 @@ function createReactiveNestedStatus({
577
714
  customMessages,
578
715
  path = "",
579
716
  rootRules,
580
- storage
717
+ storage,
718
+ options
581
719
  }) {
582
720
  let scope = (0, import_vue7.effectScope)();
583
721
  let scopeState;
@@ -595,7 +733,8 @@ function createReactiveNestedStatus({
595
733
  rulesDef: statePropRulesRef,
596
734
  customMessages,
597
735
  path: path ? `${path}.${statePropKey}` : statePropKey,
598
- storage
736
+ storage,
737
+ options
599
738
  })
600
739
  ];
601
740
  }
@@ -689,11 +828,11 @@ function createReactiveNestedStatus({
689
828
  }
690
829
  scope.stop();
691
830
  scope = (0, import_vue7.effectScope)();
692
- scopeState = null;
693
831
  }
694
832
  return (0, import_vue7.reactive)({
695
833
  ...scopeState,
696
834
  $fields,
835
+ $value: state,
697
836
  $reset,
698
837
  $touch,
699
838
  $validate,
@@ -706,7 +845,8 @@ function createReactiveChildrenStatus({
706
845
  rulesDef,
707
846
  customMessages,
708
847
  path,
709
- storage
848
+ storage,
849
+ options
710
850
  }) {
711
851
  if (isCollectionRulesDef(rulesDef)) {
712
852
  return createReactiveCollectionStatus({
@@ -714,7 +854,8 @@ function createReactiveChildrenStatus({
714
854
  rulesDef,
715
855
  customMessages,
716
856
  path,
717
- storage
857
+ storage,
858
+ options
718
859
  });
719
860
  } else if (isNestedRulesDef(state, rulesDef) && isRefObject(state)) {
720
861
  return createReactiveNestedStatus({
@@ -722,7 +863,8 @@ function createReactiveChildrenStatus({
722
863
  state,
723
864
  customMessages,
724
865
  path,
725
- storage
866
+ storage,
867
+ options
726
868
  });
727
869
  } else if (isValidatorRulesDef(rulesDef)) {
728
870
  return createReactiveFieldStatus({
@@ -730,7 +872,8 @@ function createReactiveChildrenStatus({
730
872
  rulesDef,
731
873
  customMessages,
732
874
  path,
733
- storage
875
+ storage,
876
+ options
734
877
  });
735
878
  }
736
879
  return null;
@@ -746,6 +889,7 @@ function useStorage() {
746
889
  const collectionsStorage = (0, import_vue8.shallowRef)(/* @__PURE__ */ new Map());
747
890
  const dirtyStorage = (0, import_vue8.shallowRef)(/* @__PURE__ */ new Map());
748
891
  const ruleStatusStorage = (0, import_vue8.shallowRef)(/* @__PURE__ */ new Map());
892
+ const arrayStatusStorage = (0, import_vue8.shallowRef)(/* @__PURE__ */ new Map());
749
893
  function getFieldsEntry($path) {
750
894
  const existingFields = fieldsStorage.value.get($path);
751
895
  if (existingFields) {
@@ -766,6 +910,12 @@ function useStorage() {
766
910
  return $each;
767
911
  }
768
912
  }
913
+ function addArrayStatus($id, value) {
914
+ arrayStatusStorage.value.set($id, value);
915
+ }
916
+ function getArrayStatus($id) {
917
+ return arrayStatusStorage.value.get($id);
918
+ }
769
919
  function setDirtyEntry($path, dirty) {
770
920
  dirtyStorage.value.set($path, dirty);
771
921
  }
@@ -825,20 +975,23 @@ function useStorage() {
825
975
  getDirtyState,
826
976
  trySetRuleStatusRef,
827
977
  getFieldsEntry,
828
- getCollectionsEntry
978
+ getCollectionsEntry,
979
+ getArrayStatus,
980
+ addArrayStatus
829
981
  };
830
982
  }
831
983
 
832
984
  // src/core/useRegle/useStateProperties/useStateProperties.ts
833
- function useStateProperties(scopeRules, state, customRules) {
985
+ function useStateProperties(scopeRules, state, options, customRules) {
834
986
  const storage = useStorage();
835
987
  const $regle = (0, import_vue9.reactive)(
836
988
  createReactiveNestedStatus({
837
989
  rootRules: scopeRules,
838
990
  scopeRules,
839
991
  state,
840
- customMessages: customRules(),
841
- storage
992
+ customMessages: customRules?.(),
993
+ storage,
994
+ options
842
995
  })
843
996
  );
844
997
  const errors = useErrors($regle);
@@ -846,13 +999,23 @@ function useStateProperties(scopeRules, state, customRules) {
846
999
  }
847
1000
 
848
1001
  // src/core/useRegle/useRegle.ts
849
- function createUseRegleComposable(customRules) {
850
- function useRegle(state, rulesFactory) {
1002
+ function createUseRegleComposable(customRules, options) {
1003
+ const globalOptions = {
1004
+ autoDirty: options?.autoDirty ?? true,
1005
+ lazy: options?.lazy ?? false,
1006
+ rewardEarly: options?.rewardEarly ?? false
1007
+ };
1008
+ function useRegle2(state, rulesFactory, options2) {
851
1009
  const scopeRules = (0, import_vue10.isRef)(rulesFactory) ? rulesFactory : (0, import_vue10.computed)(rulesFactory);
1010
+ const resolvedOptions = {
1011
+ ...globalOptions,
1012
+ ...options2
1013
+ };
852
1014
  const initialState = (0, import_vue10.shallowRef)(structuredClone((0, import_vue10.toRaw)(state.value)));
853
1015
  const { $regle, errors } = useStateProperties(
854
1016
  scopeRules,
855
1017
  state,
1018
+ resolvedOptions,
856
1019
  customRules
857
1020
  );
858
1021
  function resetForm() {
@@ -861,7 +1024,11 @@ function createUseRegleComposable(customRules) {
861
1024
  }
862
1025
  async function validateForm() {
863
1026
  $regle.$touch();
864
- return await $regle.$validate();
1027
+ const result = await $regle.$validate();
1028
+ if (result) {
1029
+ return state.value;
1030
+ }
1031
+ return false;
865
1032
  }
866
1033
  return {
867
1034
  state,
@@ -871,19 +1038,22 @@ function createUseRegleComposable(customRules) {
871
1038
  validateForm
872
1039
  };
873
1040
  }
874
- return useRegle;
1041
+ return useRegle2;
875
1042
  }
1043
+ var useRegle = createUseRegleComposable();
876
1044
 
877
- // src/core/defineCustomValidators.ts
878
- function defineCustomValidators(customRules) {
879
- const useRegle = createUseRegleComposable(customRules);
880
- return {
881
- useRegle
882
- };
1045
+ // src/core/defineRegleOptions.ts
1046
+ function defineRegleOptions({
1047
+ rules,
1048
+ options
1049
+ }) {
1050
+ const useRegle2 = createUseRegleComposable(rules, options);
1051
+ return useRegle2;
883
1052
  }
884
1053
  // Annotate the CommonJS export names for ESM import in node:
885
1054
  0 && (module.exports = {
886
1055
  InternalRuleType,
887
1056
  createRule,
888
- defineCustomValidators
1057
+ defineRegleOptions,
1058
+ unwrapRuleParameters
889
1059
  });