@regle/core 0.5.6 → 0.5.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/README.md +1 -1
- package/dist/regle-core.d.cts +94 -2
- package/dist/regle-core.d.ts +94 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ It's heavily inspired by Vuelidate.
|
|
|
11
11
|
|
|
12
12
|
## 📚 Documentation
|
|
13
13
|
|
|
14
|
-
[](https://
|
|
14
|
+
[](https://reglejs.dev/)
|
|
15
15
|
|
|
16
16
|
## 🎮 Play with it
|
|
17
17
|
|
package/dist/regle-core.d.cts
CHANGED
|
@@ -423,12 +423,38 @@ type IsPropertyOutputRequired<TState, TRule extends RegleFormPropertyType<any, a
|
|
|
423
423
|
type SafeFieldProperty<TState, TRule extends RegleFormPropertyType<any, any> | undefined = never> = TRule extends RegleRuleDecl<any, any> ? unknown extends TRule['required'] ? Maybe<TState> : TRule['required'] extends undefined ? never : TRule['required'] extends RegleRuleDefinition<any, infer Params, any, any, any> ? Params extends never[] ? Maybe<TState> : Maybe<TState> : Maybe<TState> : Maybe<TState>;
|
|
424
424
|
|
|
425
425
|
type useRegleFn<TCustomRules extends Partial<AllRulesDeclarations>, TShortcuts extends RegleShortcutDefinition<any> = never> = <TState extends Record<string, any>, TRules extends ReglePartialRuleTree<Unwrap<TState>, Partial<AllRulesDeclarations> & TCustomRules> & TValid, TValidationGroups extends Record<string, RegleValidationGroupEntry[]>, TValid = isDeepExact<NoInferLegacy<TRules>, ReglePartialRuleTree<Unwrap<TState>, Partial<AllRulesDeclarations> & TCustomRules>> extends true ? {} : MismatchInfo<NoInferLegacy<TRules>, ReglePartialRuleTree<Unwrap<TState>, Partial<AllRulesDeclarations> & TCustomRules>>>(state: MaybeRef<TState> | DeepReactiveState<TState>, rulesFactory: MaybeRefOrGetter<TRules>, options?: Partial<DeepMaybeRef<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<Unwrap<TState>, TRules, TValidationGroups>) => Regle<Unwrap<TState>, TRules, TValidationGroups, TShortcuts>;
|
|
426
|
+
/**
|
|
427
|
+
* useRegle serves as the foundation for validation logic.
|
|
428
|
+
*
|
|
429
|
+
* It accepts the following inputs:
|
|
430
|
+
*
|
|
431
|
+
* @param state - This can be a plain object, a ref, a reactive object, or a structure containing nested refs.
|
|
432
|
+
* @param rules - These should align with the structure of your state.
|
|
433
|
+
* @param modifiers - Customize computed beahviour
|
|
434
|
+
*
|
|
435
|
+
* ```ts
|
|
436
|
+
* import { useRegle } from '@regle/core';
|
|
437
|
+
import { required } from '@regle/rules';
|
|
438
|
+
|
|
439
|
+
const { r$ } = useRegle({ email: '' }, {
|
|
440
|
+
email: { required }
|
|
441
|
+
})
|
|
442
|
+
* ```
|
|
443
|
+
* Docs: {@link https://reglejs.dev/core-concepts/}
|
|
444
|
+
*/
|
|
426
445
|
declare const useRegle: useRegleFn<Partial<AllRulesDeclarations>, RegleShortcutDefinition<any>>;
|
|
427
446
|
|
|
428
447
|
interface inferRulesFn<TCustomRules extends Partial<AllRulesDeclarations>> {
|
|
429
448
|
<TState extends Record<string, any>, TRules extends ReglePartialRuleTree<Unwrap<TState>, Partial<AllRulesDeclarations> & TCustomRules> & TValid, TValid = isDeepExact<NoInferLegacy<TRules>, ReglePartialRuleTree<Unwrap<TState>, Partial<AllRulesDeclarations> & TCustomRules>> extends true ? {} : never>(state: MaybeRef<TState> | DeepReactiveState<TState> | undefined, rulesFactory: TRules): NoInferLegacy<TRules>;
|
|
430
449
|
<TState extends PrimitiveTypes, TRules extends RegleRuleDecl>(state: MaybeRef<TState>, rulesFactory: TRules): NoInferLegacy<TRules>;
|
|
431
450
|
}
|
|
451
|
+
/**
|
|
452
|
+
* Rule type helper to provide autocomplete and typecheck to your form rules or part of your form rules
|
|
453
|
+
* It will just return the rules without any processing.
|
|
454
|
+
*
|
|
455
|
+
* @param state - The state reference
|
|
456
|
+
* @param rules - Your rule tree
|
|
457
|
+
*/
|
|
432
458
|
declare const inferRules: inferRulesFn<Partial<AllRulesDeclarations>>;
|
|
433
459
|
|
|
434
460
|
declare function useRootStorage({ initialState, options, scopeRules, state, customRules, shortcuts, }: {
|
|
@@ -1209,6 +1235,9 @@ type $InternalRegleResult = {
|
|
|
1209
1235
|
* @public
|
|
1210
1236
|
*/
|
|
1211
1237
|
type RegleRoot<TState extends Record<string, any> = Record<string, any>, TRules extends ReglePartialRuleTree<TState> = Record<string, any>, TValidationGroups extends Record<string, RegleValidationGroupEntry[]> = never, TShortcuts extends RegleShortcutDefinition = {}> = RegleStatus<TState, TRules, TShortcuts> & ([TValidationGroups] extends [never] ? {} : {
|
|
1238
|
+
/**
|
|
1239
|
+
* Collection of validation groups used declared with the `validationGroups` modifier
|
|
1240
|
+
*/
|
|
1212
1241
|
$groups: {
|
|
1213
1242
|
readonly [TKey in keyof TValidationGroups]: RegleValidationGroupOutput;
|
|
1214
1243
|
};
|
|
@@ -1217,12 +1246,17 @@ type RegleRoot<TState extends Record<string, any> = Record<string, any>, TRules
|
|
|
1217
1246
|
* @public
|
|
1218
1247
|
*/
|
|
1219
1248
|
type RegleStatus<TState extends Record<string, any> | undefined = Record<string, any>, TRules extends ReglePartialRuleTree<NonNullable<TState>> = Record<string, any>, TShortcuts extends RegleShortcutDefinition = {}> = RegleCommonStatus<TState> & {
|
|
1249
|
+
/** Represents all the children of your object. You can access any nested child at any depth to get the relevant data you need for your form. */
|
|
1220
1250
|
readonly $fields: {
|
|
1221
1251
|
readonly [TKey in keyof TState]: InferRegleStatusType<NonNullable<TRules[TKey]>, NonNullable<TState>, TKey, TShortcuts>;
|
|
1222
1252
|
} & {
|
|
1223
1253
|
readonly [TKey in keyof TState as TRules[TKey] extends NonNullable<TRules[TKey]> ? TKey : never]-?: InferRegleStatusType<NonNullable<TRules[TKey]>, NonNullable<TState>, TKey, TShortcuts>;
|
|
1224
1254
|
};
|
|
1255
|
+
/** Collection of all the error messages, collected for all children properties and nested forms.
|
|
1256
|
+
*
|
|
1257
|
+
* Only contains errors from properties where $dirty equals true. */
|
|
1225
1258
|
readonly $errors: RegleErrorTree<TState>;
|
|
1259
|
+
/** Collection of all the error messages, collected for all children properties. */
|
|
1226
1260
|
readonly $silentErrors: RegleErrorTree<TState>;
|
|
1227
1261
|
$extractDirtyFields: (filterNullishValues?: boolean) => PartialDeep<TState>;
|
|
1228
1262
|
$validate: () => Promise<RegleResult<TState, TRules>>;
|
|
@@ -1255,14 +1289,23 @@ type $InternalRegleStatusType = $InternalRegleCollectionStatus | $InternalRegleS
|
|
|
1255
1289
|
* @public
|
|
1256
1290
|
*/
|
|
1257
1291
|
type RegleFieldStatus<TState extends any = any, TRules extends RegleFormPropertyType<any, Partial<AllRulesDeclarations>> = Record<string, any>, TShortcuts extends RegleShortcutDefinition = never> = Omit<RegleCommonStatus<TState>, '$value'> & {
|
|
1292
|
+
/** A reference to the original validated model. It can be used to bind your form with v-model.*/
|
|
1258
1293
|
$value: Maybe<UnwrapNestedRefs<TState>>;
|
|
1294
|
+
/** $value variant that will not "touch" the field and update the value silently, running only the rules, so you can easily swap values without impacting user interaction. */
|
|
1259
1295
|
$silentValue: Maybe<UnwrapNestedRefs<TState>>;
|
|
1296
|
+
/** Collection of all the error messages, collected for all children properties and nested forms.
|
|
1297
|
+
*
|
|
1298
|
+
* Only contains errors from properties where $dirty equals true. */
|
|
1260
1299
|
readonly $errors: string[];
|
|
1300
|
+
/** Collection of all the error messages, collected for all children properties and nested forms. */
|
|
1261
1301
|
readonly $silentErrors: string[];
|
|
1262
1302
|
readonly $externalErrors: string[];
|
|
1263
1303
|
readonly $tooltips: string[];
|
|
1304
|
+
/** Will return a copy of your state with only the fields that are dirty. By default it will filter out nullish values or objects, but you can override it with the first parameter $extractDirtyFields(false). */
|
|
1264
1305
|
$extractDirtyFields: (filterNullishValues?: boolean) => Maybe<TState>;
|
|
1306
|
+
/** Sets all properties as dirty, triggering all rules. It returns a promise that will either resolve to false or a type safe copy of your form state. Values that had the required rule will be transformed into a non-nullable value (type only). */
|
|
1265
1307
|
$validate: () => Promise<RegleResult<TState, TRules>>;
|
|
1308
|
+
/** This is reactive tree containing all the declared rules of your field. To know more about the rule properties check the rules properties section */
|
|
1266
1309
|
readonly $rules: {
|
|
1267
1310
|
readonly [TRuleKey in keyof Omit<TRules, '$each' | keyof FieldRegleBehaviourOptions>]: RegleRuleStatus<TState, TRules[TRuleKey] extends RegleRuleDefinition<any, infer TParams, any> ? TParams : [], TRules[TRuleKey] extends RegleRuleDefinition<any, any, any, infer TMetadata> ? TMetadata : TRules[TRuleKey] extends InlineRuleDeclaration<any, any[], infer TMetadata> ? TMetadata extends Promise<infer P> ? P : TMetadata : any>;
|
|
1268
1311
|
};
|
|
@@ -1291,35 +1334,58 @@ interface RegleCommonStatus<TValue = any> {
|
|
|
1291
1334
|
readonly $invalid: boolean;
|
|
1292
1335
|
readonly $dirty: boolean;
|
|
1293
1336
|
readonly $anyDirty: boolean;
|
|
1337
|
+
/** Indicates if any async rule for the field is currently running. Always false for synchronous rules. */
|
|
1294
1338
|
readonly $pending: boolean;
|
|
1339
|
+
/** Convenience flag to easily decide if a message should be displayed. Equivalent to $dirty && !$pending && $invalid. */
|
|
1295
1340
|
readonly $error: boolean;
|
|
1341
|
+
/** Indicates whether the field is ready for submission. Equivalent to !$invalid && !$pending. */
|
|
1296
1342
|
readonly $ready: boolean;
|
|
1343
|
+
/** Return the current key name of the field. */
|
|
1297
1344
|
readonly $name: string;
|
|
1345
|
+
/** Id used to track collections items */
|
|
1298
1346
|
$id?: string;
|
|
1347
|
+
/** A reference to the original validated model. It can be used to bind your form with v-model.*/
|
|
1299
1348
|
$value: UnwrapNestedRefs<TValue>;
|
|
1349
|
+
/** $value variant that will not "touch" the field and update the value silently, running only the rules, so you can easily swap values without impacting user interaction. */
|
|
1300
1350
|
$silentValue: UnwrapNestedRefs<TValue>;
|
|
1351
|
+
/** Marks the field and all nested properties as $dirty. */
|
|
1301
1352
|
$touch(runCommit?: boolean, withConditions?: boolean): void;
|
|
1353
|
+
/** Resets the $dirty state on all nested properties of a form. */
|
|
1302
1354
|
$reset(): void;
|
|
1355
|
+
/** Will reset both your validation state and your form state to their initial values. */
|
|
1303
1356
|
$resetAll: () => void;
|
|
1357
|
+
/** Clears the $externalResults state back to an empty object. */
|
|
1358
|
+
$clearExternalErrors(): void;
|
|
1359
|
+
/** @interal */
|
|
1304
1360
|
$unwatch(): void;
|
|
1361
|
+
/** @interal */
|
|
1305
1362
|
$watch(): void;
|
|
1306
|
-
$clearExternalErrors(): void;
|
|
1307
1363
|
}
|
|
1308
1364
|
/**
|
|
1309
1365
|
* @public
|
|
1310
1366
|
*/
|
|
1311
1367
|
type RegleRuleStatus<TValue = any, TParams extends any[] = any[], TMetadata extends RegleRuleMetadataDefinition = any> = {
|
|
1368
|
+
/** The name of the rule type. */
|
|
1312
1369
|
readonly $type: string;
|
|
1370
|
+
/** Returns the computed error message or messages for the current rule. */
|
|
1313
1371
|
readonly $message: string | string[];
|
|
1314
1372
|
readonly $tooltip: string | string[];
|
|
1373
|
+
/** Indicates whether or not the rule is enabled (for rules like requiredIf) */
|
|
1315
1374
|
readonly $active: boolean;
|
|
1375
|
+
/** Indicates the state of validation for this validator. */
|
|
1316
1376
|
readonly $valid: boolean;
|
|
1377
|
+
/** If the rule is async, indicates if it's currently pending. Always false if it's synchronous. */
|
|
1317
1378
|
readonly $pending: boolean;
|
|
1379
|
+
/** Returns the current path of the rule (used internally for tracking) */
|
|
1318
1380
|
readonly $path: string;
|
|
1381
|
+
/** Contains the metadata returned by the validator function. */
|
|
1319
1382
|
readonly $metadata: TMetadata;
|
|
1320
|
-
|
|
1383
|
+
/** Run the rule validator and compute its properties like $message and $active */
|
|
1321
1384
|
$validate(): Promise<boolean>;
|
|
1385
|
+
/** Reset the $valid, $metadata and $pending states */
|
|
1322
1386
|
$reset(): void;
|
|
1387
|
+
/** Returns the original rule validator function. */
|
|
1388
|
+
$validator: ((value: Maybe<TValue>, ...args: [TParams] extends [never[]] ? [] : [unknown[]] extends [TParams] ? any[] : TParams) => RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>) & ((value: TValue, ...args: [TParams] extends [never[]] ? [] : [unknown[]] extends [TParams] ? any[] : TParams) => RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>);
|
|
1323
1389
|
} & ([TParams] extends [never[]] ? {} : [unknown[]] extends [TParams] ? {
|
|
1324
1390
|
readonly $params?: any[];
|
|
1325
1391
|
} : {
|
|
@@ -1352,13 +1418,23 @@ interface $InternalRegleRuleStatus {
|
|
|
1352
1418
|
* @public
|
|
1353
1419
|
*/
|
|
1354
1420
|
type RegleCollectionStatus<TState extends any[] = any[], TRules extends ReglePartialRuleTree<ArrayElement<TState>> = Record<string, any>, TFieldRule extends RegleCollectionRuleDecl<any, any> = never, TShortcuts extends RegleShortcutDefinition = {}> = Omit<RegleCommonStatus<TState>, '$value'> & {
|
|
1421
|
+
/** A reference to the original validated model. It can be used to bind your form with v-model.*/
|
|
1355
1422
|
$value: Maybe<TState>;
|
|
1423
|
+
/** $value variant that will not "touch" the field and update the value silently, running only the rules, so you can easily swap values without impacting user interaction. */
|
|
1356
1424
|
$silentValue: Maybe<TState>;
|
|
1425
|
+
/** Collection of status of every item in your collection. Each item will be a field you can access, or map on it to display your elements. */
|
|
1357
1426
|
readonly $each: Array<InferRegleStatusType<NonNullable<TRules>, NonNullable<TState>, number, TShortcuts>>;
|
|
1427
|
+
/** Represents the status of the collection itself. You can have validation rules on the array like minLength, this field represents the isolated status of the collection. */
|
|
1358
1428
|
readonly $field: RegleFieldStatus<TState, TFieldRule, TShortcuts>;
|
|
1429
|
+
/** Collection of all the error messages, collected for all children properties and nested forms.
|
|
1430
|
+
*
|
|
1431
|
+
* Only contains errors from properties where $dirty equals true. */
|
|
1359
1432
|
readonly $errors: RegleCollectionErrors<TState>;
|
|
1433
|
+
/** Collection of all the error messages, collected for all children properties and nested forms. */
|
|
1360
1434
|
readonly $silentErrors: RegleCollectionErrors<TState>;
|
|
1435
|
+
/** Will return a copy of your state with only the fields that are dirty. By default it will filter out nullish values or objects, but you can override it with the first parameter $extractDirtyFields(false). */
|
|
1361
1436
|
$extractDirtyFields: (filterNullishValues?: boolean) => PartialDeep<TState>;
|
|
1437
|
+
/** Sets all properties as dirty, triggering all rules. It returns a promise that will either resolve to false or a type safe copy of your form state. Values that had the required rule will be transformed into a non-nullable value (type only). */
|
|
1362
1438
|
$validate: () => Promise<RegleResult<TState, TRules>>;
|
|
1363
1439
|
} & ([TShortcuts['collections']] extends [never] ? {} : {
|
|
1364
1440
|
[K in keyof TShortcuts['collections']]: ReturnType<NonNullable<TShortcuts['collections']>[K]>;
|
|
@@ -1389,6 +1465,7 @@ interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fi
|
|
|
1389
1465
|
* @typeParam TValue - The input value the rule should receive
|
|
1390
1466
|
* @typeParam TParams - Tuple declaration of the rule parameters (if any)
|
|
1391
1467
|
*
|
|
1468
|
+
*
|
|
1392
1469
|
* @param definition - The rule processors object
|
|
1393
1470
|
*
|
|
1394
1471
|
* @returns A rule definition that can be callable depending on params presence
|
|
@@ -1408,7 +1485,10 @@ interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fi
|
|
|
1408
1485
|
* },
|
|
1409
1486
|
* message: "The value should be 'foo'"
|
|
1410
1487
|
* })
|
|
1488
|
+
*
|
|
1411
1489
|
* ```
|
|
1490
|
+
*
|
|
1491
|
+
* Docs: {@link https://reglejs.dev/core-concepts/rules/reusable-rules}
|
|
1412
1492
|
*/
|
|
1413
1493
|
declare function createRule<TValue extends any, TParams extends any[], TReturn extends RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>, TMetadata extends RegleRuleMetadataDefinition = TReturn extends Promise<infer M> ? M : TReturn, TAsync extends boolean = TReturn extends Promise<any> ? true : false>(definition: RegleRuleInit<TValue, TParams, TReturn, TMetadata, TAsync>): InferRegleRule<TValue, TParams, TAsync, TMetadata>;
|
|
1414
1494
|
|
|
@@ -1418,6 +1498,18 @@ declare function createRule<TValue extends any, TParams extends any[], TReturn e
|
|
|
1418
1498
|
*/
|
|
1419
1499
|
declare function unwrapRuleParameters<TParams extends any[]>(params: MaybeRefOrGetter[]): TParams;
|
|
1420
1500
|
|
|
1501
|
+
/**
|
|
1502
|
+
* Define a global regle configuration, where you can:
|
|
1503
|
+
* - Customize buil-in rules messages
|
|
1504
|
+
* - Add your custom rules
|
|
1505
|
+
* - Define global modifiers
|
|
1506
|
+
* - Define shortcuts
|
|
1507
|
+
*
|
|
1508
|
+
* It will return:
|
|
1509
|
+
*
|
|
1510
|
+
* - a `useRegle` composable that can typecheck your custom rules
|
|
1511
|
+
* - an `inferRules` helper that can typecheck your custom rules
|
|
1512
|
+
*/
|
|
1421
1513
|
declare function defineRegleConfig<TShortcuts extends RegleShortcutDefinition<TCustomRules>, TCustomRules extends Partial<AllRulesDeclarations> = EmptyObject>({ rules, modifiers, shortcuts, }: {
|
|
1422
1514
|
rules?: () => TCustomRules;
|
|
1423
1515
|
modifiers?: RegleBehaviourOptions;
|
package/dist/regle-core.d.ts
CHANGED
|
@@ -423,12 +423,38 @@ type IsPropertyOutputRequired<TState, TRule extends RegleFormPropertyType<any, a
|
|
|
423
423
|
type SafeFieldProperty<TState, TRule extends RegleFormPropertyType<any, any> | undefined = never> = TRule extends RegleRuleDecl<any, any> ? unknown extends TRule['required'] ? Maybe<TState> : TRule['required'] extends undefined ? never : TRule['required'] extends RegleRuleDefinition<any, infer Params, any, any, any> ? Params extends never[] ? Maybe<TState> : Maybe<TState> : Maybe<TState> : Maybe<TState>;
|
|
424
424
|
|
|
425
425
|
type useRegleFn<TCustomRules extends Partial<AllRulesDeclarations>, TShortcuts extends RegleShortcutDefinition<any> = never> = <TState extends Record<string, any>, TRules extends ReglePartialRuleTree<Unwrap<TState>, Partial<AllRulesDeclarations> & TCustomRules> & TValid, TValidationGroups extends Record<string, RegleValidationGroupEntry[]>, TValid = isDeepExact<NoInferLegacy<TRules>, ReglePartialRuleTree<Unwrap<TState>, Partial<AllRulesDeclarations> & TCustomRules>> extends true ? {} : MismatchInfo<NoInferLegacy<TRules>, ReglePartialRuleTree<Unwrap<TState>, Partial<AllRulesDeclarations> & TCustomRules>>>(state: MaybeRef<TState> | DeepReactiveState<TState>, rulesFactory: MaybeRefOrGetter<TRules>, options?: Partial<DeepMaybeRef<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<Unwrap<TState>, TRules, TValidationGroups>) => Regle<Unwrap<TState>, TRules, TValidationGroups, TShortcuts>;
|
|
426
|
+
/**
|
|
427
|
+
* useRegle serves as the foundation for validation logic.
|
|
428
|
+
*
|
|
429
|
+
* It accepts the following inputs:
|
|
430
|
+
*
|
|
431
|
+
* @param state - This can be a plain object, a ref, a reactive object, or a structure containing nested refs.
|
|
432
|
+
* @param rules - These should align with the structure of your state.
|
|
433
|
+
* @param modifiers - Customize computed beahviour
|
|
434
|
+
*
|
|
435
|
+
* ```ts
|
|
436
|
+
* import { useRegle } from '@regle/core';
|
|
437
|
+
import { required } from '@regle/rules';
|
|
438
|
+
|
|
439
|
+
const { r$ } = useRegle({ email: '' }, {
|
|
440
|
+
email: { required }
|
|
441
|
+
})
|
|
442
|
+
* ```
|
|
443
|
+
* Docs: {@link https://reglejs.dev/core-concepts/}
|
|
444
|
+
*/
|
|
426
445
|
declare const useRegle: useRegleFn<Partial<AllRulesDeclarations>, RegleShortcutDefinition<any>>;
|
|
427
446
|
|
|
428
447
|
interface inferRulesFn<TCustomRules extends Partial<AllRulesDeclarations>> {
|
|
429
448
|
<TState extends Record<string, any>, TRules extends ReglePartialRuleTree<Unwrap<TState>, Partial<AllRulesDeclarations> & TCustomRules> & TValid, TValid = isDeepExact<NoInferLegacy<TRules>, ReglePartialRuleTree<Unwrap<TState>, Partial<AllRulesDeclarations> & TCustomRules>> extends true ? {} : never>(state: MaybeRef<TState> | DeepReactiveState<TState> | undefined, rulesFactory: TRules): NoInferLegacy<TRules>;
|
|
430
449
|
<TState extends PrimitiveTypes, TRules extends RegleRuleDecl>(state: MaybeRef<TState>, rulesFactory: TRules): NoInferLegacy<TRules>;
|
|
431
450
|
}
|
|
451
|
+
/**
|
|
452
|
+
* Rule type helper to provide autocomplete and typecheck to your form rules or part of your form rules
|
|
453
|
+
* It will just return the rules without any processing.
|
|
454
|
+
*
|
|
455
|
+
* @param state - The state reference
|
|
456
|
+
* @param rules - Your rule tree
|
|
457
|
+
*/
|
|
432
458
|
declare const inferRules: inferRulesFn<Partial<AllRulesDeclarations>>;
|
|
433
459
|
|
|
434
460
|
declare function useRootStorage({ initialState, options, scopeRules, state, customRules, shortcuts, }: {
|
|
@@ -1209,6 +1235,9 @@ type $InternalRegleResult = {
|
|
|
1209
1235
|
* @public
|
|
1210
1236
|
*/
|
|
1211
1237
|
type RegleRoot<TState extends Record<string, any> = Record<string, any>, TRules extends ReglePartialRuleTree<TState> = Record<string, any>, TValidationGroups extends Record<string, RegleValidationGroupEntry[]> = never, TShortcuts extends RegleShortcutDefinition = {}> = RegleStatus<TState, TRules, TShortcuts> & ([TValidationGroups] extends [never] ? {} : {
|
|
1238
|
+
/**
|
|
1239
|
+
* Collection of validation groups used declared with the `validationGroups` modifier
|
|
1240
|
+
*/
|
|
1212
1241
|
$groups: {
|
|
1213
1242
|
readonly [TKey in keyof TValidationGroups]: RegleValidationGroupOutput;
|
|
1214
1243
|
};
|
|
@@ -1217,12 +1246,17 @@ type RegleRoot<TState extends Record<string, any> = Record<string, any>, TRules
|
|
|
1217
1246
|
* @public
|
|
1218
1247
|
*/
|
|
1219
1248
|
type RegleStatus<TState extends Record<string, any> | undefined = Record<string, any>, TRules extends ReglePartialRuleTree<NonNullable<TState>> = Record<string, any>, TShortcuts extends RegleShortcutDefinition = {}> = RegleCommonStatus<TState> & {
|
|
1249
|
+
/** Represents all the children of your object. You can access any nested child at any depth to get the relevant data you need for your form. */
|
|
1220
1250
|
readonly $fields: {
|
|
1221
1251
|
readonly [TKey in keyof TState]: InferRegleStatusType<NonNullable<TRules[TKey]>, NonNullable<TState>, TKey, TShortcuts>;
|
|
1222
1252
|
} & {
|
|
1223
1253
|
readonly [TKey in keyof TState as TRules[TKey] extends NonNullable<TRules[TKey]> ? TKey : never]-?: InferRegleStatusType<NonNullable<TRules[TKey]>, NonNullable<TState>, TKey, TShortcuts>;
|
|
1224
1254
|
};
|
|
1255
|
+
/** Collection of all the error messages, collected for all children properties and nested forms.
|
|
1256
|
+
*
|
|
1257
|
+
* Only contains errors from properties where $dirty equals true. */
|
|
1225
1258
|
readonly $errors: RegleErrorTree<TState>;
|
|
1259
|
+
/** Collection of all the error messages, collected for all children properties. */
|
|
1226
1260
|
readonly $silentErrors: RegleErrorTree<TState>;
|
|
1227
1261
|
$extractDirtyFields: (filterNullishValues?: boolean) => PartialDeep<TState>;
|
|
1228
1262
|
$validate: () => Promise<RegleResult<TState, TRules>>;
|
|
@@ -1255,14 +1289,23 @@ type $InternalRegleStatusType = $InternalRegleCollectionStatus | $InternalRegleS
|
|
|
1255
1289
|
* @public
|
|
1256
1290
|
*/
|
|
1257
1291
|
type RegleFieldStatus<TState extends any = any, TRules extends RegleFormPropertyType<any, Partial<AllRulesDeclarations>> = Record<string, any>, TShortcuts extends RegleShortcutDefinition = never> = Omit<RegleCommonStatus<TState>, '$value'> & {
|
|
1292
|
+
/** A reference to the original validated model. It can be used to bind your form with v-model.*/
|
|
1258
1293
|
$value: Maybe<UnwrapNestedRefs<TState>>;
|
|
1294
|
+
/** $value variant that will not "touch" the field and update the value silently, running only the rules, so you can easily swap values without impacting user interaction. */
|
|
1259
1295
|
$silentValue: Maybe<UnwrapNestedRefs<TState>>;
|
|
1296
|
+
/** Collection of all the error messages, collected for all children properties and nested forms.
|
|
1297
|
+
*
|
|
1298
|
+
* Only contains errors from properties where $dirty equals true. */
|
|
1260
1299
|
readonly $errors: string[];
|
|
1300
|
+
/** Collection of all the error messages, collected for all children properties and nested forms. */
|
|
1261
1301
|
readonly $silentErrors: string[];
|
|
1262
1302
|
readonly $externalErrors: string[];
|
|
1263
1303
|
readonly $tooltips: string[];
|
|
1304
|
+
/** Will return a copy of your state with only the fields that are dirty. By default it will filter out nullish values or objects, but you can override it with the first parameter $extractDirtyFields(false). */
|
|
1264
1305
|
$extractDirtyFields: (filterNullishValues?: boolean) => Maybe<TState>;
|
|
1306
|
+
/** Sets all properties as dirty, triggering all rules. It returns a promise that will either resolve to false or a type safe copy of your form state. Values that had the required rule will be transformed into a non-nullable value (type only). */
|
|
1265
1307
|
$validate: () => Promise<RegleResult<TState, TRules>>;
|
|
1308
|
+
/** This is reactive tree containing all the declared rules of your field. To know more about the rule properties check the rules properties section */
|
|
1266
1309
|
readonly $rules: {
|
|
1267
1310
|
readonly [TRuleKey in keyof Omit<TRules, '$each' | keyof FieldRegleBehaviourOptions>]: RegleRuleStatus<TState, TRules[TRuleKey] extends RegleRuleDefinition<any, infer TParams, any> ? TParams : [], TRules[TRuleKey] extends RegleRuleDefinition<any, any, any, infer TMetadata> ? TMetadata : TRules[TRuleKey] extends InlineRuleDeclaration<any, any[], infer TMetadata> ? TMetadata extends Promise<infer P> ? P : TMetadata : any>;
|
|
1268
1311
|
};
|
|
@@ -1291,35 +1334,58 @@ interface RegleCommonStatus<TValue = any> {
|
|
|
1291
1334
|
readonly $invalid: boolean;
|
|
1292
1335
|
readonly $dirty: boolean;
|
|
1293
1336
|
readonly $anyDirty: boolean;
|
|
1337
|
+
/** Indicates if any async rule for the field is currently running. Always false for synchronous rules. */
|
|
1294
1338
|
readonly $pending: boolean;
|
|
1339
|
+
/** Convenience flag to easily decide if a message should be displayed. Equivalent to $dirty && !$pending && $invalid. */
|
|
1295
1340
|
readonly $error: boolean;
|
|
1341
|
+
/** Indicates whether the field is ready for submission. Equivalent to !$invalid && !$pending. */
|
|
1296
1342
|
readonly $ready: boolean;
|
|
1343
|
+
/** Return the current key name of the field. */
|
|
1297
1344
|
readonly $name: string;
|
|
1345
|
+
/** Id used to track collections items */
|
|
1298
1346
|
$id?: string;
|
|
1347
|
+
/** A reference to the original validated model. It can be used to bind your form with v-model.*/
|
|
1299
1348
|
$value: UnwrapNestedRefs<TValue>;
|
|
1349
|
+
/** $value variant that will not "touch" the field and update the value silently, running only the rules, so you can easily swap values without impacting user interaction. */
|
|
1300
1350
|
$silentValue: UnwrapNestedRefs<TValue>;
|
|
1351
|
+
/** Marks the field and all nested properties as $dirty. */
|
|
1301
1352
|
$touch(runCommit?: boolean, withConditions?: boolean): void;
|
|
1353
|
+
/** Resets the $dirty state on all nested properties of a form. */
|
|
1302
1354
|
$reset(): void;
|
|
1355
|
+
/** Will reset both your validation state and your form state to their initial values. */
|
|
1303
1356
|
$resetAll: () => void;
|
|
1357
|
+
/** Clears the $externalResults state back to an empty object. */
|
|
1358
|
+
$clearExternalErrors(): void;
|
|
1359
|
+
/** @interal */
|
|
1304
1360
|
$unwatch(): void;
|
|
1361
|
+
/** @interal */
|
|
1305
1362
|
$watch(): void;
|
|
1306
|
-
$clearExternalErrors(): void;
|
|
1307
1363
|
}
|
|
1308
1364
|
/**
|
|
1309
1365
|
* @public
|
|
1310
1366
|
*/
|
|
1311
1367
|
type RegleRuleStatus<TValue = any, TParams extends any[] = any[], TMetadata extends RegleRuleMetadataDefinition = any> = {
|
|
1368
|
+
/** The name of the rule type. */
|
|
1312
1369
|
readonly $type: string;
|
|
1370
|
+
/** Returns the computed error message or messages for the current rule. */
|
|
1313
1371
|
readonly $message: string | string[];
|
|
1314
1372
|
readonly $tooltip: string | string[];
|
|
1373
|
+
/** Indicates whether or not the rule is enabled (for rules like requiredIf) */
|
|
1315
1374
|
readonly $active: boolean;
|
|
1375
|
+
/** Indicates the state of validation for this validator. */
|
|
1316
1376
|
readonly $valid: boolean;
|
|
1377
|
+
/** If the rule is async, indicates if it's currently pending. Always false if it's synchronous. */
|
|
1317
1378
|
readonly $pending: boolean;
|
|
1379
|
+
/** Returns the current path of the rule (used internally for tracking) */
|
|
1318
1380
|
readonly $path: string;
|
|
1381
|
+
/** Contains the metadata returned by the validator function. */
|
|
1319
1382
|
readonly $metadata: TMetadata;
|
|
1320
|
-
|
|
1383
|
+
/** Run the rule validator and compute its properties like $message and $active */
|
|
1321
1384
|
$validate(): Promise<boolean>;
|
|
1385
|
+
/** Reset the $valid, $metadata and $pending states */
|
|
1322
1386
|
$reset(): void;
|
|
1387
|
+
/** Returns the original rule validator function. */
|
|
1388
|
+
$validator: ((value: Maybe<TValue>, ...args: [TParams] extends [never[]] ? [] : [unknown[]] extends [TParams] ? any[] : TParams) => RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>) & ((value: TValue, ...args: [TParams] extends [never[]] ? [] : [unknown[]] extends [TParams] ? any[] : TParams) => RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>);
|
|
1323
1389
|
} & ([TParams] extends [never[]] ? {} : [unknown[]] extends [TParams] ? {
|
|
1324
1390
|
readonly $params?: any[];
|
|
1325
1391
|
} : {
|
|
@@ -1352,13 +1418,23 @@ interface $InternalRegleRuleStatus {
|
|
|
1352
1418
|
* @public
|
|
1353
1419
|
*/
|
|
1354
1420
|
type RegleCollectionStatus<TState extends any[] = any[], TRules extends ReglePartialRuleTree<ArrayElement<TState>> = Record<string, any>, TFieldRule extends RegleCollectionRuleDecl<any, any> = never, TShortcuts extends RegleShortcutDefinition = {}> = Omit<RegleCommonStatus<TState>, '$value'> & {
|
|
1421
|
+
/** A reference to the original validated model. It can be used to bind your form with v-model.*/
|
|
1355
1422
|
$value: Maybe<TState>;
|
|
1423
|
+
/** $value variant that will not "touch" the field and update the value silently, running only the rules, so you can easily swap values without impacting user interaction. */
|
|
1356
1424
|
$silentValue: Maybe<TState>;
|
|
1425
|
+
/** Collection of status of every item in your collection. Each item will be a field you can access, or map on it to display your elements. */
|
|
1357
1426
|
readonly $each: Array<InferRegleStatusType<NonNullable<TRules>, NonNullable<TState>, number, TShortcuts>>;
|
|
1427
|
+
/** Represents the status of the collection itself. You can have validation rules on the array like minLength, this field represents the isolated status of the collection. */
|
|
1358
1428
|
readonly $field: RegleFieldStatus<TState, TFieldRule, TShortcuts>;
|
|
1429
|
+
/** Collection of all the error messages, collected for all children properties and nested forms.
|
|
1430
|
+
*
|
|
1431
|
+
* Only contains errors from properties where $dirty equals true. */
|
|
1359
1432
|
readonly $errors: RegleCollectionErrors<TState>;
|
|
1433
|
+
/** Collection of all the error messages, collected for all children properties and nested forms. */
|
|
1360
1434
|
readonly $silentErrors: RegleCollectionErrors<TState>;
|
|
1435
|
+
/** Will return a copy of your state with only the fields that are dirty. By default it will filter out nullish values or objects, but you can override it with the first parameter $extractDirtyFields(false). */
|
|
1361
1436
|
$extractDirtyFields: (filterNullishValues?: boolean) => PartialDeep<TState>;
|
|
1437
|
+
/** Sets all properties as dirty, triggering all rules. It returns a promise that will either resolve to false or a type safe copy of your form state. Values that had the required rule will be transformed into a non-nullable value (type only). */
|
|
1362
1438
|
$validate: () => Promise<RegleResult<TState, TRules>>;
|
|
1363
1439
|
} & ([TShortcuts['collections']] extends [never] ? {} : {
|
|
1364
1440
|
[K in keyof TShortcuts['collections']]: ReturnType<NonNullable<TShortcuts['collections']>[K]>;
|
|
@@ -1389,6 +1465,7 @@ interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fi
|
|
|
1389
1465
|
* @typeParam TValue - The input value the rule should receive
|
|
1390
1466
|
* @typeParam TParams - Tuple declaration of the rule parameters (if any)
|
|
1391
1467
|
*
|
|
1468
|
+
*
|
|
1392
1469
|
* @param definition - The rule processors object
|
|
1393
1470
|
*
|
|
1394
1471
|
* @returns A rule definition that can be callable depending on params presence
|
|
@@ -1408,7 +1485,10 @@ interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fi
|
|
|
1408
1485
|
* },
|
|
1409
1486
|
* message: "The value should be 'foo'"
|
|
1410
1487
|
* })
|
|
1488
|
+
*
|
|
1411
1489
|
* ```
|
|
1490
|
+
*
|
|
1491
|
+
* Docs: {@link https://reglejs.dev/core-concepts/rules/reusable-rules}
|
|
1412
1492
|
*/
|
|
1413
1493
|
declare function createRule<TValue extends any, TParams extends any[], TReturn extends RegleRuleMetadataDefinition | Promise<RegleRuleMetadataDefinition>, TMetadata extends RegleRuleMetadataDefinition = TReturn extends Promise<infer M> ? M : TReturn, TAsync extends boolean = TReturn extends Promise<any> ? true : false>(definition: RegleRuleInit<TValue, TParams, TReturn, TMetadata, TAsync>): InferRegleRule<TValue, TParams, TAsync, TMetadata>;
|
|
1414
1494
|
|
|
@@ -1418,6 +1498,18 @@ declare function createRule<TValue extends any, TParams extends any[], TReturn e
|
|
|
1418
1498
|
*/
|
|
1419
1499
|
declare function unwrapRuleParameters<TParams extends any[]>(params: MaybeRefOrGetter[]): TParams;
|
|
1420
1500
|
|
|
1501
|
+
/**
|
|
1502
|
+
* Define a global regle configuration, where you can:
|
|
1503
|
+
* - Customize buil-in rules messages
|
|
1504
|
+
* - Add your custom rules
|
|
1505
|
+
* - Define global modifiers
|
|
1506
|
+
* - Define shortcuts
|
|
1507
|
+
*
|
|
1508
|
+
* It will return:
|
|
1509
|
+
*
|
|
1510
|
+
* - a `useRegle` composable that can typecheck your custom rules
|
|
1511
|
+
* - an `inferRules` helper that can typecheck your custom rules
|
|
1512
|
+
*/
|
|
1421
1513
|
declare function defineRegleConfig<TShortcuts extends RegleShortcutDefinition<TCustomRules>, TCustomRules extends Partial<AllRulesDeclarations> = EmptyObject>({ rules, modifiers, shortcuts, }: {
|
|
1422
1514
|
rules?: () => TCustomRules;
|
|
1423
1515
|
modifiers?: RegleBehaviourOptions;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@regle/core",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.7",
|
|
4
4
|
"description": "Typescript-first model-based form validation library for Vue 3",
|
|
5
5
|
"peerDependencies": {
|
|
6
6
|
"vue": "^3.3.0"
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"publishConfig": {
|
|
63
63
|
"access": "public"
|
|
64
64
|
},
|
|
65
|
-
"homepage": "https://
|
|
65
|
+
"homepage": "https://reglejs.dev/",
|
|
66
66
|
"repository": {
|
|
67
67
|
"type": "git",
|
|
68
68
|
"url": "git+https://github.com/victorgarciaesgi/regle.git"
|