@regle/core 0.0.5 → 0.0.6

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.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as vue from 'vue';
2
- import { MaybeRef, Ref } from 'vue';
3
- import { PartialDeep } from 'type-fest';
2
+ import { MaybeRef, Ref, ComputedRef } from 'vue';
3
+ import { DeepMaybeRef as DeepMaybeRef$1 } from 'types/utils';
4
4
 
5
5
  type ArrayElement<T> = T extends Array<infer U> ? U : never;
6
6
  type ExcludeFromTuple<T extends readonly any[], E> = T extends [infer F, ...infer R] ? [NonNullable<F>] extends [E] ? ExcludeFromTuple<R, E> : [Exclude<F, E>, ...ExcludeFromTuple<R, E>] : [];
@@ -185,7 +185,21 @@ type RegleCollectionErrors<TRule extends RegleFormPropertyType<any, any> | undef
185
185
  readonly $errors: string[];
186
186
  readonly $each: RegleValidationErrors<TRule>[];
187
187
  };
188
- type PossibleRegleErrors = RegleCollectionErrors<any> | string[] | RegleErrorTree<any>;
188
+ /**
189
+ * @internal
190
+ */
191
+ type $InternalRegleErrors = RegleCollectionErrors<any> | string[] | RegleErrorTree<any>;
192
+ type RegleExternalErrorTree<TState extends Record<string, any> = Record<string, any>> = {
193
+ [K in keyof TState]?: RegleExternalValidationErrors<TState[K]>;
194
+ };
195
+ type RegleExternalValidationErrors<TValue extends any> = [NonNullable<TValue>] extends [
196
+ never
197
+ ] ? string[] : TValue extends Array<infer U> ? RegleExternalCollectionErrors<U> : NonNullable<TValue> extends Date ? string[] : NonNullable<TValue> extends File ? string[] : TValue extends Record<string, any> ? RegleExternalErrorTree<TValue> : string[];
198
+ type RegleExternalCollectionErrors<TValue extends any = any> = {
199
+ $errors?: string[];
200
+ $each?: RegleExternalValidationErrors<TValue>[];
201
+ };
202
+ type $InternalExternalRegleErrors = RegleExternalCollectionErrors<any> | string[] | RegleExternalErrorTree<any>;
189
203
  type DataType = string | number | Record<string, any> | File | Array<any> | Date | null | undefined;
190
204
 
191
205
  /**
@@ -219,6 +233,7 @@ type $InternalRegleStatusType = $InternalRegleCollectionStatus | RegleCommonStat
219
233
  */
220
234
  interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, Partial<AllRulesDeclarations>> = Record<string, any>, TState extends Record<PropertyKey, any> = any, TKey extends PropertyKey = string> extends RegleCommonStatus<TState> {
221
235
  $value: TState[TKey];
236
+ readonly $externalErrors?: string[];
222
237
  readonly $rules: {
223
238
  readonly [TRuleKey in keyof TRules]: RegleRuleStatus<TState[TKey], TRules[TRuleKey] extends RegleRuleDefinition<any, infer TParams> ? TParams : []>;
224
239
  };
@@ -230,6 +245,7 @@ interface RegleFieldStatus<TRules extends RegleFormPropertyType<any, Partial<All
230
245
  interface $InternalRegleFieldStatus extends RegleCommonStatus {
231
246
  $value: any;
232
247
  $rules: Record<string, $InternalRegleRuleStatus>;
248
+ $externalErrors?: string[];
233
249
  }
234
250
  /**
235
251
  * @public
@@ -248,6 +264,7 @@ interface RegleCommonStatus<TValue = any> {
248
264
  $validate(): Promise<boolean>;
249
265
  $unwatch(): void;
250
266
  $watch(): void;
267
+ $clearExternalErrors(): void;
251
268
  }
252
269
  /**
253
270
  * @public
@@ -269,17 +286,19 @@ type RegleRuleStatus<TValue = any, TParams extends any[] = any[]> = {
269
286
  * @reference {@link RegleRuleStatus}
270
287
  */
271
288
  interface $InternalRegleRuleStatus {
272
- readonly $type: string;
273
- readonly $message: string;
274
- readonly $active: boolean;
275
- readonly $valid: boolean;
276
- readonly $pending: boolean;
277
- readonly $path: string;
289
+ $type: string;
290
+ $message: string;
291
+ $active: boolean;
292
+ $valid: boolean;
293
+ $pending: boolean;
294
+ $path: string;
295
+ $externalErrors?: string[];
296
+ $params?: any[];
278
297
  $validator(value: any, ...args: any[]): boolean | Promise<boolean>;
279
298
  $validate(): Promise<boolean>;
280
299
  $unwatch(): void;
281
300
  $watch(): void;
282
- readonly $params?: any[];
301
+ $clearExternalErrors(): void;
283
302
  }
284
303
  /**
285
304
  * @public
@@ -302,13 +321,249 @@ interface $InternalRegleCollectionStatus extends Omit<$InternalRegleStatus, '$fi
302
321
  };
303
322
  }
304
323
 
324
+ /**
325
+ Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
326
+
327
+ @category Type
328
+ */
329
+ type Primitive =
330
+ | null
331
+ | undefined
332
+ | string
333
+ | number
334
+ | boolean
335
+ | symbol
336
+ | bigint;
337
+
338
+ declare global {
339
+ // eslint-disable-next-line @typescript-eslint/consistent-type-definitions -- It has to be an `interface` so that it can be merged.
340
+ interface SymbolConstructor {
341
+ readonly observable: symbol;
342
+ }
343
+ }
344
+
345
+ /**
346
+ Matches any primitive, `Date`, or `RegExp` value.
347
+ */
348
+ type BuiltIns = Primitive | Date | RegExp;
349
+
350
+ /**
351
+ Test if the given function has multiple call signatures.
352
+
353
+ Needed to handle the case of a single call signature with properties.
354
+
355
+ Multiple call signatures cannot currently be supported due to a TypeScript limitation.
356
+ @see https://github.com/microsoft/TypeScript/issues/29732
357
+ */
358
+ type HasMultipleCallSignatures<T extends (...arguments_: any[]) => unknown> =
359
+ T extends {(...arguments_: infer A): unknown; (...arguments_: any[]): unknown}
360
+ ? unknown[] extends A
361
+ ? false
362
+ : true
363
+ : false;
364
+
365
+ /**
366
+ @see PartialDeep
367
+ */
368
+ type PartialDeepOptions = {
369
+ /**
370
+ Whether to affect the individual elements of arrays and tuples.
371
+
372
+ @default false
373
+ */
374
+ readonly recurseIntoArrays?: boolean;
375
+ };
376
+
377
+ /**
378
+ Create a type from another type with all keys and nested keys set to optional.
379
+
380
+ Use-cases:
381
+ - Merging a default settings/config object with another object, the second object would be a deep partial of the default object.
382
+ - Mocking and testing complex entities, where populating an entire object with its keys would be redundant in terms of the mock or test.
383
+
384
+ @example
385
+ ```
386
+ import type {PartialDeep} from 'type-fest';
387
+
388
+ const settings: Settings = {
389
+ textEditor: {
390
+ fontSize: 14;
391
+ fontColor: '#000000';
392
+ fontWeight: 400;
393
+ }
394
+ autocomplete: false;
395
+ autosave: true;
396
+ };
397
+
398
+ const applySavedSettings = (savedSettings: PartialDeep<Settings>) => {
399
+ return {...settings, ...savedSettings};
400
+ }
401
+
402
+ settings = applySavedSettings({textEditor: {fontWeight: 500}});
403
+ ```
404
+
405
+ By default, this does not affect elements in array and tuple types. You can change this by passing `{recurseIntoArrays: true}` as the second type argument:
406
+
407
+ ```
408
+ import type {PartialDeep} from 'type-fest';
409
+
410
+ interface Settings {
411
+ languages: string[];
412
+ }
413
+
414
+ const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true}> = {
415
+ languages: [undefined]
416
+ };
417
+ ```
418
+
419
+ @category Object
420
+ @category Array
421
+ @category Set
422
+ @category Map
423
+ */
424
+ type PartialDeep<T, Options extends PartialDeepOptions = {}> = T extends BuiltIns
425
+ ? T
426
+ : T extends Map<infer KeyType, infer ValueType>
427
+ ? PartialMapDeep<KeyType, ValueType, Options>
428
+ : T extends Set<infer ItemType>
429
+ ? PartialSetDeep<ItemType, Options>
430
+ : T extends ReadonlyMap<infer KeyType, infer ValueType>
431
+ ? PartialReadonlyMapDeep<KeyType, ValueType, Options>
432
+ : T extends ReadonlySet<infer ItemType>
433
+ ? PartialReadonlySetDeep<ItemType, Options>
434
+ : T extends ((...arguments_: any[]) => unknown)
435
+ ? T | undefined
436
+ : T extends object
437
+ ? T extends ReadonlyArray<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
438
+ ? Options['recurseIntoArrays'] extends true
439
+ ? ItemType[] extends T // Test for arrays (non-tuples) specifically
440
+ ? readonly ItemType[] extends T // Differentiate readonly and mutable arrays
441
+ ? ReadonlyArray<PartialDeep<ItemType | undefined, Options>>
442
+ : Array<PartialDeep<ItemType | undefined, Options>>
443
+ : PartialObjectDeep<T, Options> // Tuples behave properly
444
+ : T // If they don't opt into array testing, just use the original type
445
+ : PartialObjectDeep<T, Options>
446
+ : unknown;
447
+
448
+ /**
449
+ Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.
450
+ */
451
+ type PartialMapDeep<KeyType, ValueType, Options extends PartialDeepOptions> = {} & Map<PartialDeep<KeyType, Options>, PartialDeep<ValueType, Options>>;
452
+
453
+ /**
454
+ Same as `PartialDeep`, but accepts only `Set`s as inputs. Internal helper for `PartialDeep`.
455
+ */
456
+ type PartialSetDeep<T, Options extends PartialDeepOptions> = {} & Set<PartialDeep<T, Options>>;
457
+
458
+ /**
459
+ Same as `PartialDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `PartialDeep`.
460
+ */
461
+ type PartialReadonlyMapDeep<KeyType, ValueType, Options extends PartialDeepOptions> = {} & ReadonlyMap<PartialDeep<KeyType, Options>, PartialDeep<ValueType, Options>>;
462
+
463
+ /**
464
+ Same as `PartialDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `PartialDeep`.
465
+ */
466
+ type PartialReadonlySetDeep<T, Options extends PartialDeepOptions> = {} & ReadonlySet<PartialDeep<T, Options>>;
467
+
468
+ /**
469
+ Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
470
+ */
471
+ type PartialObjectDeep<ObjectType extends object, Options extends PartialDeepOptions> = {
472
+ [KeyType in keyof ObjectType]?: PartialDeep<ObjectType[KeyType], Options>
473
+ };
474
+
475
+ type ExcludeUndefined<T> = Exclude<T, undefined>;
476
+
477
+ /**
478
+ Create a type from another type with all keys and nested keys set to required.
479
+
480
+ Use-cases:
481
+ - Creating optional configuration interfaces where the underlying implementation still requires all options to be fully specified.
482
+ - Modeling the resulting type after a deep merge with a set of defaults.
483
+
484
+ @example
485
+ ```
486
+ import type {RequiredDeep} from 'type-fest';
487
+
488
+ type Settings = {
489
+ textEditor?: {
490
+ fontSize?: number | undefined;
491
+ fontColor?: string | undefined;
492
+ fontWeight?: number | undefined;
493
+ }
494
+ autocomplete?: boolean | undefined;
495
+ autosave?: boolean | undefined;
496
+ };
497
+
498
+ type RequiredSettings = RequiredDeep<Settings>;
499
+ // type RequiredSettings = {
500
+ // textEditor: {
501
+ // fontSize: number;
502
+ // fontColor: string;
503
+ // fontWeight: number;
504
+ // }
505
+ // autocomplete: boolean;
506
+ // autosave: boolean;
507
+ // }
508
+ ```
509
+
510
+ Note that types containing overloaded functions are not made deeply required due to a [TypeScript limitation](https://github.com/microsoft/TypeScript/issues/29732).
511
+
512
+ @category Utilities
513
+ @category Object
514
+ @category Array
515
+ @category Set
516
+ @category Map
517
+ */
518
+ type RequiredDeep<T, E extends ExcludeUndefined<T> = ExcludeUndefined<T>> = E extends BuiltIns
519
+ ? E
520
+ : E extends Map<infer KeyType, infer ValueType>
521
+ ? Map<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
522
+ : E extends Set<infer ItemType>
523
+ ? Set<RequiredDeep<ItemType>>
524
+ : E extends ReadonlyMap<infer KeyType, infer ValueType>
525
+ ? ReadonlyMap<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
526
+ : E extends ReadonlySet<infer ItemType>
527
+ ? ReadonlySet<RequiredDeep<ItemType>>
528
+ : E extends WeakMap<infer KeyType, infer ValueType>
529
+ ? WeakMap<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
530
+ : E extends WeakSet<infer ItemType>
531
+ ? WeakSet<RequiredDeep<ItemType>>
532
+ : E extends Promise<infer ValueType>
533
+ ? Promise<RequiredDeep<ValueType>>
534
+ : E extends (...arguments_: any[]) => unknown
535
+ ? {} extends RequiredObjectDeep<E>
536
+ ? E
537
+ : HasMultipleCallSignatures<E> extends true
538
+ ? E
539
+ : ((...arguments_: Parameters<E>) => ReturnType<E>) & RequiredObjectDeep<E>
540
+ : E extends object
541
+ ? E extends Array<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
542
+ ? ItemType[] extends E // Test for arrays (non-tuples) specifically
543
+ ? Array<RequiredDeep<ItemType>> // Recreate relevant array type to prevent eager evaluation of circular reference
544
+ : RequiredObjectDeep<E> // Tuples behave properly
545
+ : RequiredObjectDeep<E>
546
+ : unknown;
547
+
548
+ type RequiredObjectDeep<ObjectType extends object> = {
549
+ [KeyType in keyof ObjectType]-?: RequiredDeep<ObjectType[KeyType]>
550
+ };
551
+
305
552
  interface Regle<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree>> {
306
- state: Ref<PartialDeep<TState>>;
553
+ $state: Ref<PartialDeep<TState>>;
307
554
  $regle: RegleStatus<TState, TRules>;
308
- errors: RegleErrorTree<TRules>;
555
+ /** Show active errors based on your behaviour options (lazy, autoDirty)
556
+ * It allow you to skip scouting the `$regle` object
557
+ */
558
+ $errors: RegleErrorTree<TRules>;
559
+ $valid: ComputedRef<boolean>;
560
+ $invalid: ComputedRef<boolean>;
309
561
  resetForm: () => void;
310
562
  validateForm: () => Promise<false | DeepSafeFormState<TState, TRules>>;
311
563
  }
564
+ type DeepReactiveState<T extends Record<string, any>> = {
565
+ [K in keyof T]: MaybeRef<T[K]>;
566
+ };
312
567
  type DeepSafeFormState<TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, CustomRulesDeclarationTree>> = {
313
568
  [K in keyof TState as [SafeProperty<TState[K], TRules[K]>] extends [never] ? K : never]?: [
314
569
  SafeProperty<TState[K], TRules[K]>
@@ -335,6 +590,10 @@ interface RegleBehaviourOptions {
335
590
  */
336
591
  rewardEarly?: boolean;
337
592
  }
593
+ interface LocalRegleBehaviourOptions<TState extends Record<string, any>> {
594
+ $externalErrors?: MaybeRef<RegleExternalErrorTree<TState>>;
595
+ }
596
+ type ResolvedRegleBehaviourOptions = DeepMaybeRef$1<RequiredDeep<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<Record<string, any>>;
338
597
 
339
598
  declare function createRule<TValue extends any, TParams extends any[] = [], TType extends string = string>(definition: RegleRuleInit<TValue, TParams, TType>): InferRegleRule<TValue, TParams>;
340
599
 
@@ -354,6 +613,6 @@ declare function unwrapRuleParameters<TParams extends any[]>(params: ParamDecl[]
354
613
  declare function defineRegleOptions<TCustomRules extends Partial<AllRulesDeclarations>>({ rules, options, }: {
355
614
  rules?: () => TCustomRules;
356
615
  options?: RegleBehaviourOptions;
357
- }): <TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, Partial<AllRulesDeclarations> & TCustomRules>>(state: vue.Ref<TState>, rulesFactory: vue.ComputedRef<TRules> | (() => TRules), options?: Partial<DeepMaybeRef<Required<RegleBehaviourOptions>>> | undefined) => Regle<TState, TRules>;
616
+ }): <TState extends Record<string, any>, TRules extends ReglePartialValidationTree<TState, Partial<AllRulesDeclarations> & TCustomRules>>(state: vue.Ref<TState> | DeepReactiveState<TState>, rulesFactory: vue.ComputedRef<TRules> | (() => TRules), options?: (Partial<DeepMaybeRef<RegleBehaviourOptions>> & LocalRegleBehaviourOptions<TState>) | undefined) => Regle<TState, TRules>;
358
617
 
359
- export { $InternalFormPropertyTypes, $InternalRegleCollectionRuleDecl, $InternalRegleCollectionStatus, $InternalRegleFieldStatus, $InternalReglePartialValidationTree, $InternalRegleRuleDecl, $InternalRegleRuleStatus, $InternalRegleStatus, $InternalRegleStatusType, AllRulesDeclarations, ArrayElement, CustomRulesDeclarationTree, DataType, DeepMaybeRef, ExcludeFromTuple, FormRuleDeclaration, InferRegleRule, InferRegleStatusType, InlineRuleDeclaration, InternalRuleType, Maybe, MaybeNull, ParamDecl, PossibleRegleErrors, Regle, RegleBehaviourOptions, RegleCollectionErrors, RegleCollectionRuleDecl, RegleCollectionRuleDefinition, RegleCollectionStatus, RegleCommonStatus, RegleErrorTree, RegleFieldStatus, RegleFormPropertyType, RegleInternalRuleDefs, ReglePartialValidationTree, RegleRuleDecl, RegleRuleDefinition, RegleRuleDefinitionProcessor, RegleRuleInit, RegleRuleRaw, RegleRuleStatus, RegleRuleWithParamsDefinition, RegleStatus, RegleUniversalParams, RegleValidationErrors, UnrefTuple, UnwrapRegleUniversalParams, createRule, defineRegleOptions, unwrapRuleParameters };
618
+ export { $InternalExternalRegleErrors, $InternalFormPropertyTypes, $InternalRegleCollectionRuleDecl, $InternalRegleCollectionStatus, $InternalRegleErrors, $InternalRegleFieldStatus, $InternalReglePartialValidationTree, $InternalRegleRuleDecl, $InternalRegleRuleStatus, $InternalRegleStatus, $InternalRegleStatusType, AllRulesDeclarations, ArrayElement, CustomRulesDeclarationTree, DataType, DeepMaybeRef, ExcludeFromTuple, FormRuleDeclaration, InferRegleRule, InferRegleStatusType, InlineRuleDeclaration, InternalRuleType, LocalRegleBehaviourOptions, Maybe, MaybeNull, ParamDecl, Regle, RegleBehaviourOptions, RegleCollectionErrors, RegleCollectionRuleDecl, RegleCollectionRuleDefinition, RegleCollectionStatus, RegleCommonStatus, RegleErrorTree, RegleExternalCollectionErrors, RegleExternalErrorTree, RegleExternalValidationErrors, RegleFieldStatus, RegleFormPropertyType, RegleInternalRuleDefs, ReglePartialValidationTree, RegleRuleDecl, RegleRuleDefinition, RegleRuleDefinitionProcessor, RegleRuleInit, RegleRuleRaw, RegleRuleStatus, RegleRuleWithParamsDefinition, RegleStatus, RegleUniversalParams, RegleValidationErrors, ResolvedRegleBehaviourOptions, UnrefTuple, UnwrapRegleUniversalParams, createRule, defineRegleOptions, unwrapRuleParameters };