@regle/core 1.13.1 → 1.14.0-beta.1
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/regle-core.d.ts +333 -80
- package/dist/regle-core.js +359 -85
- package/dist/regle-core.min.js +1 -1
- package/package.json +1 -1
package/dist/regle-core.d.ts
CHANGED
|
@@ -51,34 +51,68 @@ interface useRegleFn<TCustomRules extends Partial<ExtendedRulesDeclarations>, TS
|
|
|
51
51
|
};
|
|
52
52
|
}
|
|
53
53
|
/**
|
|
54
|
-
* useRegle serves as the foundation for validation logic.
|
|
54
|
+
* `useRegle` serves as the foundation for validation logic.
|
|
55
|
+
* It transforms your data and validation rules into a powerful, reactive validation system.
|
|
55
56
|
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
* @param
|
|
59
|
-
* @
|
|
60
|
-
* @param modifiers - Customize regle behaviour
|
|
57
|
+
* @param state - Your form data (plain object, ref, reactive object, or structure with nested refs)
|
|
58
|
+
* @param rules - Validation rules that should align with the structure of your state
|
|
59
|
+
* @param modifiers - Optional configuration to customize regle behavior
|
|
60
|
+
* @returns An object containing `r$` - the reactive validation state
|
|
61
61
|
*
|
|
62
|
+
* @example
|
|
62
63
|
* ```ts
|
|
63
64
|
* import { useRegle } from '@regle/core';
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
* import { required, email, minLength } from '@regle/rules';
|
|
66
|
+
*
|
|
67
|
+
* const { r$ } = useRegle(
|
|
68
|
+
* { name: '', email: '' },
|
|
69
|
+
* {
|
|
70
|
+
* name: { required, minLength: minLength(2) },
|
|
71
|
+
* email: { required, email }
|
|
72
|
+
* }
|
|
73
|
+
* );
|
|
74
|
+
*
|
|
75
|
+
* // Access validation state
|
|
76
|
+
* r$.$valid // Whether all validations pass
|
|
77
|
+
* r$.$value // The current form values
|
|
78
|
+
* r$.name.$errors // Errors for the name field
|
|
79
|
+
*
|
|
80
|
+
* // Trigger validation
|
|
81
|
+
* const result = await r$.$validate();
|
|
69
82
|
* ```
|
|
70
|
-
*
|
|
83
|
+
*
|
|
84
|
+
* @see {@link https://reglejs.dev/core-concepts/ Documentation}
|
|
71
85
|
*/
|
|
72
86
|
declare const useRegle: useRegleFn<Partial<ExtendedRulesDeclarations>, RegleShortcutDefinition<any>, {}, {}>;
|
|
73
87
|
interface inferRulesFn<TCustomRules extends Partial<ExtendedRulesDeclarations>> {
|
|
74
88
|
<TState$1 extends Record<string, any> | MaybeInput<PrimitiveTypes>, TRules$1 extends DeepExact<TRules$1, ReglePartialRuleTree<Unwrap<TState$1 extends Record<string, any> ? TState$1 : {}>, Partial<DefaultValidatorsTree> & TCustomRules>>, TDecl extends RegleRuleDecl<NonNullable<TState$1>, Partial<DefaultValidatorsTree> & TCustomRules>>(state: MaybeRef<TState$1> | DeepReactiveState<TState$1> | undefined, rulesFactory: TState$1 extends MaybeInput<PrimitiveTypes> ? TDecl : TState$1 extends Record<string, any> ? TRules$1 : {}): NonNullable<TState$1> extends PrimitiveTypes ? TDecl : TRules$1;
|
|
75
89
|
}
|
|
76
90
|
/**
|
|
77
|
-
*
|
|
78
|
-
* It
|
|
91
|
+
* Type helper to provide autocomplete and type-checking for your form rules.
|
|
92
|
+
* It returns the rules without any processing - useful with computed rules.
|
|
79
93
|
*
|
|
80
94
|
* @param state - The state reference
|
|
81
95
|
* @param rules - Your rule tree
|
|
96
|
+
* @returns The rules object (passthrough)
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* import { inferRules, useRegle } from '@regle/core';
|
|
101
|
+
* import { required, minLength } from '@regle/rules';
|
|
102
|
+
*
|
|
103
|
+
* const state = ref({ name: '' });
|
|
104
|
+
*
|
|
105
|
+
* // inferRules preserves TypeScript autocompletion
|
|
106
|
+
* const rules = computed(() => {
|
|
107
|
+
* return inferRules(state, {
|
|
108
|
+
* name: { required, minLength: minLength(2) }
|
|
109
|
+
* })
|
|
110
|
+
* });
|
|
111
|
+
*
|
|
112
|
+
* const { r$ } = useRegle(state, rules);
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* @see {@link https://reglejs.dev/core-concepts/#dynamic-rules-object Documentation}
|
|
82
116
|
*/
|
|
83
117
|
declare const inferRules: inferRulesFn<Partial<ExtendedRulesDeclarations>>;
|
|
84
118
|
declare function useRootStorage({
|
|
@@ -127,21 +161,34 @@ interface useRulesFn<TCustomRules extends Partial<ExtendedRulesDeclarations>, TS
|
|
|
127
161
|
};
|
|
128
162
|
}
|
|
129
163
|
/**
|
|
130
|
-
* useRules is a
|
|
164
|
+
* `useRules` is a variant of `useRegle` that doesn't require you to provide initial state.
|
|
165
|
+
* It creates an empty state based on your rules structure and implements the Standard Schema spec.
|
|
131
166
|
*
|
|
132
|
-
*
|
|
167
|
+
* This is useful when you want to define validation rules first and infer the state type from them.
|
|
133
168
|
*
|
|
134
|
-
* @param rules - Your rules object
|
|
135
|
-
* @param modifiers -
|
|
169
|
+
* @param rules - Your validation rules object
|
|
170
|
+
* @param modifiers - Optional configuration to customize regle behavior
|
|
171
|
+
* @returns The reactive validation state (implements StandardSchemaV1)
|
|
136
172
|
*
|
|
173
|
+
* @example
|
|
137
174
|
* ```ts
|
|
138
|
-
* import { useRules } from '@regle/core';
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
}
|
|
175
|
+
* import { useRules, type InferInput } from '@regle/core';
|
|
176
|
+
* import { required, string, email } from '@regle/rules';
|
|
177
|
+
*
|
|
178
|
+
* const r$ = useRules({
|
|
179
|
+
* name: { required, string },
|
|
180
|
+
* email: { required, email }
|
|
181
|
+
* });
|
|
182
|
+
*
|
|
183
|
+
* // State is automatically created and typed
|
|
184
|
+
* r$.$value.name // string | null
|
|
185
|
+
* r$.$value.email // string | null
|
|
186
|
+
*
|
|
187
|
+
* // Can be used with Standard Schema compatible libraries
|
|
188
|
+
* const result = await r$['~standard'].validate({ name: '', email: '' });
|
|
144
189
|
* ```
|
|
190
|
+
*
|
|
191
|
+
* @see {@link https://reglejs.dev/common-usage/standard-schema#userules Documentation}
|
|
145
192
|
*/
|
|
146
193
|
declare const useRules: useRulesFn<Partial<ExtendedRulesDeclarations>, RegleShortcutDefinition<any>>;
|
|
147
194
|
/**
|
|
@@ -1290,35 +1337,57 @@ interface SuperCompatibleRegleCollectionStatus extends Omit<SuperCompatibleRegle
|
|
|
1290
1337
|
type SuperCompatibleRegleCollectionErrors = $InternalRegleCollectionErrors;
|
|
1291
1338
|
type SuperCompatibleRegleCollectionIssues = $InternalRegleCollectionIssues;
|
|
1292
1339
|
/**
|
|
1293
|
-
* Create a typed custom rule that can be used like
|
|
1294
|
-
*
|
|
1340
|
+
* Create a typed custom rule that can be used like built-in rules.
|
|
1341
|
+
* The created rule can be declared in global options or used directly.
|
|
1295
1342
|
*
|
|
1296
|
-
*
|
|
1343
|
+
* Features:
|
|
1344
|
+
* - Automatically detects if the rule is async
|
|
1345
|
+
* - Supports parameters for configurable rules
|
|
1346
|
+
* - Full TypeScript type inference
|
|
1347
|
+
* - Custom metadata support
|
|
1297
1348
|
*
|
|
1349
|
+
* @param definition - The rule definition object containing:
|
|
1350
|
+
* - `validator`: The validation function
|
|
1351
|
+
* - `message`: Error message (string or function)
|
|
1352
|
+
* - `type`: Optional rule type identifier
|
|
1353
|
+
* - `active`: Optional function to conditionally activate the rule
|
|
1354
|
+
* - `tooltip`: Optional tooltip message
|
|
1298
1355
|
*
|
|
1299
|
-
* @
|
|
1300
|
-
*
|
|
1301
|
-
* @returns A rule definition that can be callable depending on params presence
|
|
1302
|
-
*
|
|
1303
|
-
* @exemple
|
|
1356
|
+
* @returns A rule definition that is callable if it accepts parameters
|
|
1304
1357
|
*
|
|
1358
|
+
* @example
|
|
1305
1359
|
* ```ts
|
|
1306
|
-
*
|
|
1307
|
-
* import {isFilled} from '@regle/rules';
|
|
1360
|
+
* import { createRule } from '@regle/core';
|
|
1361
|
+
* import { isFilled } from '@regle/rules';
|
|
1308
1362
|
*
|
|
1363
|
+
* // Simple rule without params
|
|
1309
1364
|
* export const isFoo = createRule({
|
|
1310
1365
|
* validator(value: Maybe<string>) {
|
|
1311
|
-
*
|
|
1312
|
-
*
|
|
1313
|
-
*
|
|
1314
|
-
*
|
|
1366
|
+
* if (isFilled(value)) {
|
|
1367
|
+
* return value === 'foo';
|
|
1368
|
+
* }
|
|
1369
|
+
* return true;
|
|
1315
1370
|
* },
|
|
1316
1371
|
* message: "The value should be 'foo'"
|
|
1317
|
-
* })
|
|
1372
|
+
* });
|
|
1318
1373
|
*
|
|
1374
|
+
* // Rule with parameters
|
|
1375
|
+
* export const minCustom = createRule({
|
|
1376
|
+
* validator(value: Maybe<number>, min: number) {
|
|
1377
|
+
* if (isFilled(value)) {
|
|
1378
|
+
* return value >= min;
|
|
1379
|
+
* }
|
|
1380
|
+
* return true;
|
|
1381
|
+
* },
|
|
1382
|
+
* message: ({ $params: [min] }) => `Value must be at least ${min}`
|
|
1383
|
+
* });
|
|
1384
|
+
*
|
|
1385
|
+
* // Usage
|
|
1386
|
+
* useRegle({ name: '' }, { name: { isFoo } });
|
|
1387
|
+
* useRegle({ count: 0 }, { count: { minCustom: minCustom(5) } });
|
|
1319
1388
|
* ```
|
|
1320
1389
|
*
|
|
1321
|
-
*
|
|
1390
|
+
* @see {@link https://reglejs.dev/core-concepts/rules/reusable-rules Documentation}
|
|
1322
1391
|
*/
|
|
1323
1392
|
declare function createRule<TValue$1 extends any, TParams$1 extends any[], TReturn extends RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>, TMetadata$1 extends RegleRuleMetadataDefinition = (TReturn extends Promise<infer M> ? M : TReturn), TAsync$1 extends boolean = (TReturn extends Promise<any> ? true : false)>(definition: RegleRuleInit<TValue$1, TParams$1, TReturn, TMetadata$1, TAsync$1>): InferRegleRule<TValue$1, TParams$1, TAsync$1, TMetadata$1>;
|
|
1324
1393
|
/**
|
|
@@ -1327,16 +1396,43 @@ declare function createRule<TValue$1 extends any, TParams$1 extends any[], TRetu
|
|
|
1327
1396
|
*/
|
|
1328
1397
|
declare function unwrapRuleParameters<TParams$1 extends any[]>(params: MaybeRefOrGetter[]): TParams$1;
|
|
1329
1398
|
/**
|
|
1330
|
-
* Define a global
|
|
1399
|
+
* Define a global Regle configuration to customize the validation behavior across your application.
|
|
1400
|
+
*
|
|
1401
|
+
* Features:
|
|
1331
1402
|
* - Customize built-in rules messages
|
|
1332
|
-
* - Add your custom rules
|
|
1333
|
-
* - Define global modifiers
|
|
1334
|
-
* - Define shortcuts
|
|
1403
|
+
* - Add your custom rules with full type inference
|
|
1404
|
+
* - Define global modifiers (lazy, rewardEarly, etc.)
|
|
1405
|
+
* - Define shortcuts for common validation patterns
|
|
1335
1406
|
*
|
|
1336
|
-
*
|
|
1407
|
+
* @param options - Configuration options
|
|
1408
|
+
* @param options.rules - Factory function returning custom rules
|
|
1409
|
+
* @param options.modifiers - Global behavior modifiers
|
|
1410
|
+
* @param options.shortcuts - Reusable validation shortcuts
|
|
1411
|
+
* @returns Object containing typed `useRegle`, `inferRules`, and `useRules` functions
|
|
1412
|
+
*
|
|
1413
|
+
* @example
|
|
1414
|
+
* ```ts
|
|
1415
|
+
* import { defineRegleConfig } from '@regle/core';
|
|
1416
|
+
* import { required, withMessage } from '@regle/rules';
|
|
1417
|
+
*
|
|
1418
|
+
* export const { useRegle, inferRules, useRules } = defineRegleConfig({
|
|
1419
|
+
* rules: () => ({
|
|
1420
|
+
* // Override default required message
|
|
1421
|
+
* required: withMessage(required, 'This field cannot be empty'),
|
|
1422
|
+
* // Add custom rule
|
|
1423
|
+
* myCustomRule: createRule({
|
|
1424
|
+
* validator: (value) => value === 'valid',
|
|
1425
|
+
* message: 'Invalid value'
|
|
1426
|
+
* })
|
|
1427
|
+
* }),
|
|
1428
|
+
* modifiers: {
|
|
1429
|
+
* lazy: true,
|
|
1430
|
+
* rewardEarly: true
|
|
1431
|
+
* }
|
|
1432
|
+
* });
|
|
1433
|
+
* ```
|
|
1337
1434
|
*
|
|
1338
|
-
*
|
|
1339
|
-
* - an `inferRules` helper that can typecheck your custom rules
|
|
1435
|
+
* @see {@link https://reglejs.dev/advanced-usage/global-config Documentation}
|
|
1340
1436
|
*/
|
|
1341
1437
|
declare function defineRegleConfig<TShortcuts extends RegleShortcutDefinition<TCustomRules>, TCustomRules extends Partial<ExtendedRulesDeclarations>>({
|
|
1342
1438
|
rules,
|
|
@@ -1352,12 +1448,31 @@ declare function defineRegleConfig<TShortcuts extends RegleShortcutDefinition<TC
|
|
|
1352
1448
|
useRules: useRulesFn<TCustomRules, TShortcuts>;
|
|
1353
1449
|
};
|
|
1354
1450
|
/**
|
|
1355
|
-
* Extend an already created custom `useRegle`
|
|
1451
|
+
* Extend an already created custom `useRegle` configuration with additional rules, modifiers, or shortcuts.
|
|
1356
1452
|
*
|
|
1357
|
-
*
|
|
1453
|
+
* @param regle - The existing useRegle function to extend
|
|
1454
|
+
* @param options - Additional configuration to merge
|
|
1455
|
+
* @param options.rules - Additional custom rules
|
|
1456
|
+
* @param options.modifiers - Additional modifiers to merge
|
|
1457
|
+
* @param options.shortcuts - Additional shortcuts to merge
|
|
1458
|
+
* @returns Object containing the extended `useRegle` and `inferRules` functions
|
|
1358
1459
|
*
|
|
1359
|
-
*
|
|
1360
|
-
*
|
|
1460
|
+
* @example
|
|
1461
|
+
* ```ts
|
|
1462
|
+
* import { extendRegleConfig } from '@regle/core';
|
|
1463
|
+
* import { baseUseRegle } from './base-config';
|
|
1464
|
+
*
|
|
1465
|
+
* export const { useRegle, inferRules } = extendRegleConfig(baseUseRegle, {
|
|
1466
|
+
* rules: () => ({
|
|
1467
|
+
* additionalRule: myNewRule
|
|
1468
|
+
* }),
|
|
1469
|
+
* modifiers: {
|
|
1470
|
+
* rewardEarly: true
|
|
1471
|
+
* }
|
|
1472
|
+
* });
|
|
1473
|
+
* ```
|
|
1474
|
+
*
|
|
1475
|
+
* @see {@link https://reglejs.dev/advanced-usage/global-config Documentation}
|
|
1361
1476
|
*/
|
|
1362
1477
|
declare function extendRegleConfig<TRootCustomRules extends Partial<ExtendedRulesDeclarations>, TRootShortcuts extends RegleShortcutDefinition<{}>, TShortcuts extends RegleShortcutDefinition<Merge<TRootCustomRules, TCustomRules>>, TCustomRules extends Partial<ExtendedRulesDeclarations>>(regle: useRegleFn<TRootCustomRules, TRootShortcuts>, {
|
|
1363
1478
|
rules,
|
|
@@ -1429,6 +1544,44 @@ type MergedReglesResult<TRegles extends Record<string, SuperCompatibleRegleRoot>
|
|
|
1429
1544
|
errors: EmptyObject;
|
|
1430
1545
|
issues: EmptyObject;
|
|
1431
1546
|
};
|
|
1547
|
+
/**
|
|
1548
|
+
* Merge multiple Regle instances into a single validation state.
|
|
1549
|
+
* Useful for combining multiple forms or validation scopes.
|
|
1550
|
+
*
|
|
1551
|
+
* @param regles - An object containing named Regle instances to merge
|
|
1552
|
+
* @param _scoped - Internal flag for scoped validation (default: false)
|
|
1553
|
+
* @returns A merged validation state with all instances' properties combined
|
|
1554
|
+
*
|
|
1555
|
+
* @example
|
|
1556
|
+
* ```ts
|
|
1557
|
+
* import { useRegle, mergeRegles } from '@regle/core';
|
|
1558
|
+
* import { required } from '@regle/rules';
|
|
1559
|
+
*
|
|
1560
|
+
* // Create separate validation instances
|
|
1561
|
+
* const { r$: personalInfo } = useRegle(
|
|
1562
|
+
* { name: '', email: '' },
|
|
1563
|
+
* { name: { required }, email: { required } }
|
|
1564
|
+
* );
|
|
1565
|
+
*
|
|
1566
|
+
* const { r$: address } = useRegle(
|
|
1567
|
+
* { street: '', city: '' },
|
|
1568
|
+
* { street: { required }, city: { required } }
|
|
1569
|
+
* );
|
|
1570
|
+
*
|
|
1571
|
+
* // Merge them together
|
|
1572
|
+
* const merged$ = mergeRegles({
|
|
1573
|
+
* personalInfo,
|
|
1574
|
+
* address
|
|
1575
|
+
* });
|
|
1576
|
+
*
|
|
1577
|
+
* // Access combined state
|
|
1578
|
+
* merged$.$valid // true when ALL forms are valid
|
|
1579
|
+
* merged$.$errors // { personalInfo: {...}, address: {...} }
|
|
1580
|
+
* await merged$.$validate() // Validates all forms
|
|
1581
|
+
* ```
|
|
1582
|
+
*
|
|
1583
|
+
* @see {@link https://reglejs.dev/advanced-usage/merge-regles Documentation}
|
|
1584
|
+
*/
|
|
1432
1585
|
declare function mergeRegles<TRegles extends Record<string, SuperCompatibleRegleRoot>, TScoped extends boolean = false>(regles: TRegles, _scoped?: TScoped): TScoped extends false ? MergedRegles<TRegles> : MergedScopedRegles;
|
|
1433
1586
|
type useCollectScopeFn<TNamedScoped extends boolean = false> = TNamedScoped extends true ? <const TValue$1 extends Record<string, Record<string, any>>>(namespace?: MaybeRefOrGetter<string | string[]>) => {
|
|
1434
1587
|
r$: MergedRegles<{ [K in keyof TValue$1]: RegleRoot<TValue$1[K]> & SuperCompatibleRegleRoot }>;
|
|
@@ -1460,6 +1613,35 @@ type CreateScopedUseRegleOptions<TCustomRegle extends useRegleFn<any, any>, TAsR
|
|
|
1460
1613
|
*/
|
|
1461
1614
|
asRecord?: TAsRecord;
|
|
1462
1615
|
};
|
|
1616
|
+
/**
|
|
1617
|
+
* Create a scoped validation system for collecting and validating multiple form instances.
|
|
1618
|
+
* Useful for dynamic forms, multi-step wizards, or component-based form architectures.
|
|
1619
|
+
*
|
|
1620
|
+
* @param options - Configuration options
|
|
1621
|
+
* @param options.customUseRegle - Custom useRegle instance with your global config
|
|
1622
|
+
* @param options.customStore - External ref to store collected instances
|
|
1623
|
+
* @param options.asRecord - If true, collect instances in a Record (requires `id` param in useScopedRegle)
|
|
1624
|
+
* @returns Object containing `useScopedRegle` and `useCollectScope` functions
|
|
1625
|
+
*
|
|
1626
|
+
* @example
|
|
1627
|
+
* ```ts
|
|
1628
|
+
* // scoped-config.ts
|
|
1629
|
+
* import { createScopedUseRegle } from '@regle/core';
|
|
1630
|
+
*
|
|
1631
|
+
* export const { useScopedRegle, useCollectScope } = createScopedUseRegle();
|
|
1632
|
+
*
|
|
1633
|
+
* // ChildComponent.vue
|
|
1634
|
+
* const { r$ } = useScopedRegle(state, rules, {
|
|
1635
|
+
* namespace: 'myForm'
|
|
1636
|
+
* });
|
|
1637
|
+
*
|
|
1638
|
+
* // ParentComponent.vue
|
|
1639
|
+
* const { r$: collectedR$ } = useCollectScope('myForm');
|
|
1640
|
+
* await collectedR$.$validate(); // Validates all child forms
|
|
1641
|
+
* ```
|
|
1642
|
+
*
|
|
1643
|
+
* @see {@link https://reglejs.dev/advanced-usage/scoped-validation Documentation}
|
|
1644
|
+
*/
|
|
1463
1645
|
declare function createScopedUseRegle<TCustomRegle extends useRegleFn<any, any> = useRegleFn<Partial<ExtendedRulesDeclarations>>, TAsRecord extends boolean = false, TReturnedRegle extends useRegleFn<any, any, any, any> = (TCustomRegle extends useRegleFn<infer A, infer B> ? useRegleFn<A, B, {
|
|
1464
1646
|
dispose: () => void;
|
|
1465
1647
|
register: () => void;
|
|
@@ -1479,34 +1661,64 @@ declare const useCollectScope: <TValue$1 extends Record<string, unknown>[] = Rec
|
|
|
1479
1661
|
namespace?: vue0.MaybeRefOrGetter<string>;
|
|
1480
1662
|
}>;
|
|
1481
1663
|
/**
|
|
1482
|
-
*
|
|
1664
|
+
* Create variant-based validation rules that depend on a discriminant field value.
|
|
1665
|
+
* Useful for union types where different fields are required based on a type discriminant.
|
|
1666
|
+
*
|
|
1667
|
+
* Note: Autocomplete may not fully work due to TypeScript limitations.
|
|
1483
1668
|
*
|
|
1484
|
-
*
|
|
1669
|
+
* @param root - The reactive state object
|
|
1670
|
+
* @param discriminantKey - The key used to discriminate between variants
|
|
1671
|
+
* @param variants - Array of variant rule definitions using `literal` for type matching
|
|
1672
|
+
* @returns A computed ref containing the currently active variant rules
|
|
1485
1673
|
*
|
|
1674
|
+
* @example
|
|
1486
1675
|
* ```ts
|
|
1487
|
-
*
|
|
1488
|
-
*
|
|
1489
|
-
*
|
|
1490
|
-
*
|
|
1491
|
-
*
|
|
1492
|
-
*
|
|
1493
|
-
*
|
|
1494
|
-
*
|
|
1495
|
-
*
|
|
1496
|
-
*
|
|
1497
|
-
*
|
|
1498
|
-
*
|
|
1676
|
+
* import { useRegle, createVariant } from '@regle/core';
|
|
1677
|
+
* import { required, email, literal } from '@regle/rules';
|
|
1678
|
+
*
|
|
1679
|
+
* const state = ref({
|
|
1680
|
+
* type: 'EMAIL' as 'EMAIL' | 'GITHUB',
|
|
1681
|
+
* email: '',
|
|
1682
|
+
* username: ''
|
|
1683
|
+
* });
|
|
1684
|
+
*
|
|
1685
|
+
* // ⚠️ Use getter syntax for your rules
|
|
1686
|
+
* const { r$ } = useRegle(state, () => {
|
|
1687
|
+
* const variant = createVariant(state, 'type', [
|
|
1688
|
+
* { type: { literal: literal('EMAIL') }, email: { required, email } },
|
|
1689
|
+
* { type: { literal: literal('GITHUB') }, username: { required } },
|
|
1690
|
+
* { type: { required } }, // Default case
|
|
1691
|
+
* ]);
|
|
1692
|
+
*
|
|
1693
|
+
* return {
|
|
1694
|
+
* ...variant.value,
|
|
1695
|
+
* };
|
|
1696
|
+
* });
|
|
1499
1697
|
* ```
|
|
1698
|
+
*
|
|
1699
|
+
* @see {@link https://reglejs.dev/advanced-usage/variants Documentation}
|
|
1500
1700
|
*/
|
|
1501
1701
|
declare function createVariant<TForm extends Record<string, any>, TDiscriminant extends keyof JoinDiscriminatedUnions<TForm>, TVariants extends VariantTuple<JoinDiscriminatedUnions<TForm>, TDiscriminant>>(root: MaybeRefOrGetter<TForm> | DeepReactiveState<TForm>, discriminantKey: TDiscriminant, variants: [...TVariants]): Ref<TVariants[number]>;
|
|
1502
1702
|
/**
|
|
1503
|
-
*
|
|
1703
|
+
* Type guard to narrow a variant field to a specific discriminated value.
|
|
1704
|
+
* Enables type-safe access to variant-specific fields.
|
|
1504
1705
|
*
|
|
1706
|
+
* @param root - The Regle status object
|
|
1707
|
+
* @param discriminantKey - The key used to discriminate between variants
|
|
1708
|
+
* @param discriminantValue - The specific value to narrow to
|
|
1709
|
+
* @returns `true` if the discriminant matches, with TypeScript narrowing the type
|
|
1710
|
+
*
|
|
1711
|
+
* @example
|
|
1505
1712
|
* ```ts
|
|
1713
|
+
* import { narrowVariant } from '@regle/core';
|
|
1714
|
+
*
|
|
1506
1715
|
* if (narrowVariant(r$, 'type', 'EMAIL')) {
|
|
1507
|
-
*
|
|
1716
|
+
* // TypeScript knows r$.email exists here
|
|
1717
|
+
* r$.email.$value = 'user@example.com';
|
|
1508
1718
|
* }
|
|
1509
1719
|
* ```
|
|
1720
|
+
*
|
|
1721
|
+
* @see {@link https://reglejs.dev/advanced-usage/variants Documentation}
|
|
1510
1722
|
*/
|
|
1511
1723
|
declare function narrowVariant<TRoot extends {
|
|
1512
1724
|
[x: string]: unknown;
|
|
@@ -1518,13 +1730,28 @@ declare function narrowVariant<TRoot extends {
|
|
|
1518
1730
|
$value: infer V;
|
|
1519
1731
|
} ? V : unknown)>(root: TRoot | undefined, discriminantKey: TKey$1, discriminantValue: TValue$1): root is NarrowVariant<TRoot, TKey$1, TValue$1>;
|
|
1520
1732
|
/**
|
|
1521
|
-
*
|
|
1733
|
+
* Create a reactive reference to a narrowed variant.
|
|
1734
|
+
* Useful in templates or when you need a stable ref to the narrowed type.
|
|
1735
|
+
*
|
|
1736
|
+
* @param root - The Regle status object (can be a ref)
|
|
1737
|
+
* @param discriminantKey - The key used to discriminate between variants
|
|
1738
|
+
* @param discriminantValue - The specific value to narrow to
|
|
1739
|
+
* @param options - Optional `{ unsafeAssertion: true }` to assert the variant always exists
|
|
1740
|
+
* @returns A ref containing the narrowed variant (or undefined if not matching)
|
|
1522
1741
|
*
|
|
1742
|
+
* @example
|
|
1523
1743
|
* ```vue
|
|
1524
1744
|
* <script setup lang="ts">
|
|
1525
|
-
*
|
|
1745
|
+
* import { variantToRef } from '@regle/core';
|
|
1746
|
+
*
|
|
1747
|
+
* const emailR$ = variantToRef(r$, 'type', 'EMAIL');
|
|
1748
|
+
*
|
|
1749
|
+
* // In template:
|
|
1750
|
+
* // <input v-if="emailR$" v-model="emailR$.$value.email" />
|
|
1526
1751
|
* </script>
|
|
1527
1752
|
* ```
|
|
1753
|
+
*
|
|
1754
|
+
* @see {@link https://reglejs.dev/advanced-usage/variants Documentation}
|
|
1528
1755
|
*/
|
|
1529
1756
|
declare function variantToRef<TRoot extends RegleStatus<{}, any, any>, const TKey$1 extends keyof TRoot['$fields'], const TValue$1 extends (LazyJoinDiscriminatedUnions<Exclude<TRoot['$fields'][TKey$1], RegleCollectionStatus<any, any, any> | RegleStatus<any, any, any> | NarrowVariantExtracts[keyof NarrowVariantExtracts]>> extends {
|
|
1530
1757
|
$value: infer V;
|
|
@@ -1538,29 +1765,55 @@ declare function variantToRef<TRoot extends RegleStatus<{}, any, any>, const TKe
|
|
|
1538
1765
|
$value: infer V;
|
|
1539
1766
|
} ? V : unknown)>(root: MaybeRef<TRoot>, discriminantKey: TKey$1, discriminantValue: TValue$1): Ref<NarrowVariant<TRoot, TKey$1, TValue$1> | undefined>;
|
|
1540
1767
|
/**
|
|
1541
|
-
* Helper method to wrap
|
|
1768
|
+
* Helper method to wrap a raw rules object with type inference.
|
|
1769
|
+
* Provides autocomplete and type-checking without processing.
|
|
1542
1770
|
*
|
|
1543
|
-
*
|
|
1771
|
+
* @param rules - The rules object to wrap
|
|
1772
|
+
* @returns The same rules object (passthrough)
|
|
1544
1773
|
*
|
|
1774
|
+
* @example
|
|
1545
1775
|
* ```ts
|
|
1546
|
-
*
|
|
1776
|
+
* import { defineRules } from '@regle/core';
|
|
1777
|
+
* import { required, string } from '@regle/rules';
|
|
1778
|
+
*
|
|
1779
|
+
* // defineRules helps catch structure errors
|
|
1780
|
+
* const rules = defineRules({
|
|
1781
|
+
* firstName: { required, string },
|
|
1782
|
+
* lastName: { required, string }
|
|
1783
|
+
* });
|
|
1547
1784
|
* ```
|
|
1785
|
+
*
|
|
1786
|
+
* @see {@link https://reglejs.dev/common-usage/standard-schema Documentation}
|
|
1548
1787
|
*/
|
|
1549
1788
|
declare function defineRules<TRules$1 extends RegleUnknownRulesTree>(rules: TRules$1): TRules$1;
|
|
1550
1789
|
/**
|
|
1551
|
-
* Refine a
|
|
1790
|
+
* Refine a rules object to add rules that depend on the state values.
|
|
1791
|
+
* Inspired by Zod's `refine`, this allows writing dynamic rules while maintaining type safety.
|
|
1552
1792
|
*
|
|
1553
|
-
* @
|
|
1793
|
+
* @param rules - The base rules object
|
|
1794
|
+
* @param refinement - A function that receives the typed state and returns additional rules
|
|
1795
|
+
* @returns A function that, given the state ref, returns the merged rules
|
|
1554
1796
|
*
|
|
1797
|
+
* @example
|
|
1555
1798
|
* ```ts
|
|
1799
|
+
* import { refineRules, type InferInput } from '@regle/core';
|
|
1800
|
+
* import { required, string, sameAs } from '@regle/rules';
|
|
1801
|
+
*
|
|
1556
1802
|
* const rules = refineRules({
|
|
1557
|
-
*
|
|
1558
|
-
* }, (state) => {
|
|
1559
|
-
*
|
|
1560
|
-
*
|
|
1561
|
-
*
|
|
1562
|
-
*
|
|
1803
|
+
* password: { required, string },
|
|
1804
|
+
* }, (state) => ({
|
|
1805
|
+
* // state is typed based on the base rules
|
|
1806
|
+
* confirmPassword: {
|
|
1807
|
+
* required,
|
|
1808
|
+
* sameAs: sameAs(() => state.value.password)
|
|
1809
|
+
* }
|
|
1810
|
+
* }));
|
|
1811
|
+
*
|
|
1812
|
+
* type State = InferInput<typeof rules>;
|
|
1813
|
+
* // { password: string; confirmPassword: string }
|
|
1563
1814
|
* ```
|
|
1815
|
+
*
|
|
1816
|
+
* @see {@link https://reglejs.dev/common-usage/standard-schema#refinerules Documentation}
|
|
1564
1817
|
*/
|
|
1565
1818
|
declare function refineRules<TRules$1 extends RegleUnknownRulesTree, TRefinement extends ReglePartialRuleTree<InferInput<TRules$1>> & RegleUnknownRulesTree>(rules: TRules$1, refinement: (state: Ref<InferInput<TRules$1>>) => TRefinement): (state: Ref<InferInput<TRules$1>>) => Merge<TRules$1, TRefinement>;
|
|
1566
1819
|
declare const RegleVuePlugin: Plugin;
|