Skip to content

Commit 2e06931

Browse files
fix: fixed shortcuts not being persisted when using extendRegleConfig
1 parent ce3bf4c commit 2e06931

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

packages/core/src/core/defineRegleConfig.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ export function extendRegleConfig<
6969
} {
7070
const rootConfig = regle.__config ?? {};
7171
const newRules = () => ({ ...rootConfig.rules?.(), ...rules?.() }) as TCustomRules;
72-
const newModifiers = rootConfig.modifiers && modifiers ? merge(rootConfig.modifiers, modifiers) : modifiers;
73-
const newShortcuts = rootConfig.shortcuts && shortcuts ? merge(rootConfig.shortcuts, shortcuts) : shortcuts;
72+
const newModifiers =
73+
rootConfig.modifiers && modifiers ? merge(rootConfig.modifiers, modifiers) : (rootConfig.modifiers ?? modifiers);
74+
const newShortcuts =
75+
rootConfig.shortcuts && shortcuts ? merge(rootConfig.shortcuts, shortcuts) : (rootConfig.shortcuts ?? shortcuts);
7476

7577
const useRegle = createUseRegleComposable<TCustomRules, TShortcuts>(newRules, newModifiers, newShortcuts as any);
76-
useRegle.__config = { rules: newRules, modifiers: newModifiers, shortcuts: newShortcuts };
78+
useRegle.__config = { rules: newRules, modifiers: newModifiers, shortcuts: newShortcuts as any };
7779

7880
const inferRules = createInferRuleHelper<TCustomRules>();
7981

tests/unit/useRegle/global-config/global-rules.spec.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { defineRegleConfig, extendRegleConfig, useRegle, type Maybe, type RegleComputedRules } from '@regle/core';
1+
import { defineRegleConfig, extendRegleConfig, type Maybe } from '@regle/core';
2+
import { and, applyIf, minValue, not, or, required, sameAs, withMessage } from '@regle/rules';
23
import { ref } from 'vue';
34
import { ruleMockIsEven, ruleMockMetadata } from '../../../fixtures';
4-
import { and, applyIf, minValue, not, or, required, sameAs, withMessage } from '@regle/rules';
55
import { createRegleComponent } from '../../../utils/test.utils';
66

77
const { useRegle: useCustomRegle } = defineRegleConfig({
@@ -12,6 +12,11 @@ const { useRegle: useCustomRegle } = defineRegleConfig({
1212
}),
1313
rule: withMessage(ruleMockIsEven, 'Patched rule'),
1414
}),
15+
shortcuts: {
16+
fields: {
17+
$isRequired: (field) => field.$rules.required?.$active ?? false,
18+
},
19+
},
1520
});
1621
const form = ref({
1722
withApply: 1,
@@ -115,7 +120,7 @@ describe('extendRegleConfig', () => {
115120
withAnd: { rule: and(ruleMockIsEven, minValue(1)) },
116121
withOr: { rule: or(ruleMockIsEven, minValue(5)) },
117122
withNot: { rule: not(ruleMockIsEven) },
118-
level0: { rule: ruleMockIsEven },
123+
level0: { required, rule: ruleMockIsEven },
119124
level1: {
120125
child: { rule: ruleMockIsEven },
121126
level2: {
@@ -137,7 +142,9 @@ describe('extendRegleConfig', () => {
137142

138143
await vm.$nextTick();
139144

145+
expect(vm.r$.$fields.password.$isRequired).toBe(false);
140146
expect(vm.r$.level0.$errors).toStrictEqual(['Patched rule']);
147+
expect(vm.r$.level0.$isRequired).toBe(true);
141148
expect(vm.r$.level1.child.$errors).toStrictEqual(['Patched rule']);
142149
expect(vm.r$.level1.level2.child.$errors).toStrictEqual(['Patched min:3', 'Re-patched rule']);
143150

0 commit comments

Comments
 (0)