vuelidify 2.1.0 → 2.1.2
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 +51 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,6 +11,8 @@ Powerful and typed model-based validation for Vue 3
|
|
|
11
11
|
|
|
12
12
|
[Custom Validators](#custom-validators)
|
|
13
13
|
|
|
14
|
+
[Utility Functions](#utility-functions)
|
|
15
|
+
|
|
14
16
|
[Throttle Functions](#throttle-functions)
|
|
15
17
|
|
|
16
18
|
[Technical Details](#technical-details)
|
|
@@ -166,7 +168,7 @@ type BaseValidationReturn<F> = {
|
|
|
166
168
|
// Make sure your names are unique between your validators.
|
|
167
169
|
name?: string,
|
|
168
170
|
// the unique identifier for this validation result. Assigned internally.
|
|
169
|
-
// you can use this
|
|
171
|
+
// you can use this to identify your DOM elements that display error messages.
|
|
170
172
|
id? string,
|
|
171
173
|
// required for determining whether or not this validator passed
|
|
172
174
|
isValid: boolean,
|
|
@@ -404,7 +406,7 @@ There aren't many validators provided by this library on purpose. If you feel a
|
|
|
404
406
|
# Custom Validators
|
|
405
407
|
This section guides you to create your own generic validators. Validators were designed to be easy to create and easier to use.
|
|
406
408
|
|
|
407
|
-
Here is a breakdown of one of the
|
|
409
|
+
Here is a breakdown of one of the provided validators (expanded to make comments more readable):
|
|
408
410
|
|
|
409
411
|
```ts
|
|
410
412
|
// always provide a header comment to explain what the validator does!
|
|
@@ -436,8 +438,8 @@ export function isEmailSync<
|
|
|
436
438
|
// Strongly type the expected params object to have intellisense
|
|
437
439
|
params: ValidatorParams<T, K, V, A>
|
|
438
440
|
) => {
|
|
439
|
-
// you can do whatever
|
|
440
|
-
//
|
|
441
|
+
// you can do whatever a normal function can do here.
|
|
442
|
+
// However, you must return undefined, an array of validators, or a validation result.
|
|
441
443
|
// In this case, we're checking the value of the property against an email regex.
|
|
442
444
|
return {
|
|
443
445
|
isValid: params.value ? RegExp(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/).test(params.value) : false,
|
|
@@ -464,15 +466,55 @@ export function isEmailSync<T extends string | undefined | null>(): SyncValidato
|
|
|
464
466
|
This validator is effectively: `SyncValidator<string | undefined | null, unknown, unknown, unknown, unknown>`
|
|
465
467
|
|
|
466
468
|
## Throttle Functions
|
|
467
|
-
Vuelidify provides several throttling functions for limiting how often a function can be invoked.
|
|
469
|
+
Vuelidify provides several throttling functions for limiting how often a function can be invoked. We figured these would be useful outside of just validation. Functions like `debounce` and `throttle` are common examples exported by lodash. However, lodash's implementations are often hard to use because they don't return control back to the caller (i.e. they don't return a promise). The functions Vuelidify provides strongly type themselves to the function you provide, and always return a promise when it makes sense to. Here is a list of the available throttling functions:
|
|
468
470
|
|
|
469
|
-
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
471
|
+
- ```ts
|
|
472
|
+
bufferAsync<F extends (...args: any[]) => any>(
|
|
473
|
+
func: F,
|
|
474
|
+
): (...params: Parameters<F>) => Promise<Awaited<ReturnType<F>> | typeof IGNORE_RESULT>
|
|
475
|
+
```
|
|
476
|
+
`bufferAsync` ensures that the provided function has only one instance executing at a time. Calls to the buffered function will return a promise to execute when the current instance returns. Only the latest promise will execute the function next, all other promises will resolve to `IGNORE_RESULT` once the current instance returns. This function is very useful for only invoking resource-heavy functions while guaranteeing each execution uses the most up-to-date parameters.
|
|
477
|
+
|
|
478
|
+
- ```ts
|
|
479
|
+
throttleBufferAsync<F extends (...args: any[]) => any>(
|
|
480
|
+
func: F,
|
|
481
|
+
delayMs: number,
|
|
482
|
+
): (...params: Parameters<F>) => Promise<Awaited<ReturnType<F>> | typeof IGNORE_RESULT>
|
|
483
|
+
```
|
|
484
|
+
`throttleBufferAsync` behaves very similarly to `bufferAsync`, but instead of waiting for the current instance to return, it waits for a throttle duration to expire. Once the throttle expires the latest buffered promise will execute the function and all others will resolve to `IGNORE_RESULT`. This means multiple instances of the function could be running at the same time, depending on the throttle and how long the function takes to return.
|
|
485
|
+
|
|
486
|
+
- ```ts
|
|
487
|
+
throttleAsync<F extends (...args: any) => any>(
|
|
488
|
+
func: F,
|
|
489
|
+
delayMs: number,
|
|
490
|
+
): {
|
|
491
|
+
isThrottled: Ref<boolean>;
|
|
492
|
+
throttledFunc: (...params: Parameters<F>) => ReturnType<F> | typeof IGNORE_RESULT;
|
|
493
|
+
}
|
|
494
|
+
```
|
|
495
|
+
`throttleAsync` ensures a function can only be invoked once every throttle period. Does not use buffering. Returns two objects, a ref indicating if the function is in its throttle period and the throttled function. Useful for hard limiting invocation of a function (e.g. forgot password form submission). Calls during the throttle period return `IGNORE_RESULT`.
|
|
496
|
+
|
|
497
|
+
- ```ts
|
|
498
|
+
trailingDebounceAsync<F extends (...args: any) => any>(
|
|
499
|
+
func: F,
|
|
500
|
+
delayMs: number,
|
|
501
|
+
): (...params: Parameters<F>) => Promise<Awaited<ReturnType<F>> | typeof IGNORE_RESULT>
|
|
502
|
+
```
|
|
503
|
+
`trailingDebounceAsync` ensures a function will only be invoked after a delay has passed since the last call. Guarantees the latest parameters will be executed. All calls prior to the latest will return `IGNORE_RESULT`.
|
|
473
504
|
|
|
474
505
|
`IGNORE_RESULT` is a constant unique symbol exported by Vuelidify that helps you to identify when invocations are ignored by a throttle function. This was done to make sure this unique state doesn't conflict with possible returns from your own functions.
|
|
475
506
|
|
|
507
|
+
## Utility Functions
|
|
508
|
+
Vuelidify provides a utility function you are free to use as well.
|
|
509
|
+
|
|
510
|
+
- ```ts
|
|
511
|
+
reduceUndefined<T, K = NonNullable<T>>(
|
|
512
|
+
array: T[],
|
|
513
|
+
getter: (value: T) => K | undefined | null,
|
|
514
|
+
): K[]
|
|
515
|
+
```
|
|
516
|
+
Used internally when collecting error messages from validator results. Removes undefined or null values from a mapped array. The getter defaults to selecting every element in the array, but you can provide your own to perform your own mapping.
|
|
517
|
+
|
|
476
518
|
## Technical Details
|
|
477
519
|
For those interested in the inner workings of the library without looking at the code:
|
|
478
520
|
|