@fgv/ts-utils 3.0.0-alpha.1 → 3.0.1-alpha.0
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/ts-utils.d.ts +317 -136
- package/lib/index.d.ts +2 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -1
- package/lib/index.js.map +1 -1
- package/lib/packlets/conversion/baseConverter.d.ts +124 -0
- package/lib/packlets/conversion/baseConverter.d.ts.map +1 -0
- package/lib/packlets/conversion/baseConverter.js +239 -0
- package/lib/packlets/conversion/baseConverter.js.map +1 -0
- package/lib/packlets/conversion/converter.d.ts +35 -103
- package/lib/packlets/conversion/converter.d.ts.map +1 -1
- package/lib/packlets/conversion/converter.js +0 -215
- package/lib/packlets/conversion/converter.js.map +1 -1
- package/lib/packlets/conversion/converters.d.ts +138 -124
- package/lib/packlets/conversion/converters.d.ts.map +1 -1
- package/lib/packlets/conversion/converters.js +111 -107
- package/lib/packlets/conversion/converters.js.map +1 -1
- package/lib/packlets/conversion/defaultingConverter.d.ts +90 -0
- package/lib/packlets/conversion/defaultingConverter.d.ts.map +1 -0
- package/lib/packlets/conversion/defaultingConverter.js +152 -0
- package/lib/packlets/conversion/defaultingConverter.js.map +1 -0
- package/lib/packlets/conversion/index.d.ts +2 -0
- package/lib/packlets/conversion/index.d.ts.map +1 -1
- package/lib/packlets/conversion/index.js +2 -0
- package/lib/packlets/conversion/index.js.map +1 -1
- package/lib/packlets/conversion/objectConverter.d.ts +5 -3
- package/lib/packlets/conversion/objectConverter.d.ts.map +1 -1
- package/lib/packlets/conversion/objectConverter.js +2 -2
- package/lib/packlets/conversion/objectConverter.js.map +1 -1
- package/lib/packlets/conversion/stringConverter.d.ts +2 -1
- package/lib/packlets/conversion/stringConverter.d.ts.map +1 -1
- package/lib/packlets/conversion/stringConverter.js +2 -2
- package/lib/packlets/conversion/stringConverter.js.map +1 -1
- package/lib/packlets/validation/convalidator.d.ts +22 -0
- package/lib/packlets/validation/convalidator.d.ts.map +1 -0
- package/lib/packlets/validation/convalidator.js +24 -0
- package/lib/packlets/validation/convalidator.js.map +1 -0
- package/lib/packlets/validation/genericValidator.d.ts +4 -0
- package/lib/packlets/validation/genericValidator.d.ts.map +1 -1
- package/lib/packlets/validation/genericValidator.js +10 -0
- package/lib/packlets/validation/genericValidator.js.map +1 -1
- package/lib/packlets/validation/index.d.ts +2 -1
- package/lib/packlets/validation/index.d.ts.map +1 -1
- package/lib/packlets/validation/index.js +3 -2
- package/lib/packlets/validation/index.js.map +1 -1
- package/lib/packlets/validation/validator.d.ts +8 -3
- package/lib/packlets/validation/validator.d.ts.map +1 -1
- package/lib/packlets/validation/validator.js.map +1 -1
- package/package.json +1 -1
package/dist/ts-utils.d.ts
CHANGED
|
@@ -9,16 +9,17 @@
|
|
|
9
9
|
export declare function allSucceed<T>(results: Iterable<Result<unknown>>, successValue: T): Result<T>;
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* A helper function to create a {@link Converter} which converts `unknown` to an array of `<T>`.
|
|
12
|
+
* A helper function to create a {@link Converter | Converter} which converts `unknown` to an array of `<T>`.
|
|
13
13
|
* @remarks
|
|
14
14
|
* If `onError` is `'failOnError'` (default), then the entire conversion fails if any element cannot
|
|
15
15
|
* be converted. If `onError` is `'ignoreErrors'`, then failing elements are silently ignored.
|
|
16
|
-
* @param converter - {@link Converter} used to convert each
|
|
16
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} used to convert each
|
|
17
|
+
* item in the array.
|
|
17
18
|
* @param ignoreErrors - Specifies treatment of unconvertible elements.
|
|
18
|
-
* @returns A {@link Converter} which returns an array of `<T>`.
|
|
19
|
+
* @returns A {@link Converter | Converter} which returns an array of `<T>`.
|
|
19
20
|
* @public
|
|
20
21
|
*/
|
|
21
|
-
declare function arrayOf<T, TC = undefined>(converter: Converter<T, TC>, onError?:
|
|
22
|
+
declare function arrayOf<T, TC = undefined>(converter: Converter<T, TC> | Validator<T, TC>, onError?: OnError): Converter<T[], TC>;
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
25
|
* Helper function to create a {@link Validation.Classes.ArrayValidator | ArrayValidator} which
|
|
@@ -114,6 +115,10 @@ declare class BaseConverter<T, TC = undefined> implements Converter<T, TC> {
|
|
|
114
115
|
* {@inheritdoc Converter.convert}
|
|
115
116
|
*/
|
|
116
117
|
convert(from: unknown, context?: TC): Result<T>;
|
|
118
|
+
/**
|
|
119
|
+
* {@inheritdoc Converter.convalidate}
|
|
120
|
+
*/
|
|
121
|
+
convalidate(from: unknown, context?: TC): Result<T>;
|
|
117
122
|
/**
|
|
118
123
|
* {@inheritdoc Converter.convertOptional}
|
|
119
124
|
*/
|
|
@@ -138,6 +143,10 @@ declare class BaseConverter<T, TC = undefined> implements Converter<T, TC> {
|
|
|
138
143
|
* {@inheritdoc Converter.mapConvertItems}
|
|
139
144
|
*/
|
|
140
145
|
mapConvertItems<TI>(mapConverter: Converter<TI, unknown>): Converter<TI[], TC>;
|
|
146
|
+
/**
|
|
147
|
+
* {@inheritdoc Converter.withAction}
|
|
148
|
+
*/
|
|
149
|
+
withAction<TI>(action: (result: Result<T>) => Result<TI>): Converter<TI, TC>;
|
|
141
150
|
/**
|
|
142
151
|
* {@inheritdoc Converter.withTypeGuard}
|
|
143
152
|
*/
|
|
@@ -154,6 +163,10 @@ declare class BaseConverter<T, TC = undefined> implements Converter<T, TC> {
|
|
|
154
163
|
* {@inheritdoc Converter.withBrand}
|
|
155
164
|
*/
|
|
156
165
|
withBrand<B extends string>(brand: B): Converter<Brand<T, B>, TC>;
|
|
166
|
+
/**
|
|
167
|
+
* {@inheritdoc Converter.withDefault}
|
|
168
|
+
*/
|
|
169
|
+
withDefault<TD = T>(defaultValue: TD): DefaultingConverter<T, TD, TC>;
|
|
157
170
|
/**
|
|
158
171
|
* @internal
|
|
159
172
|
*/
|
|
@@ -169,7 +182,7 @@ declare class BaseConverter<T, TC = undefined> implements Converter<T, TC> {
|
|
|
169
182
|
}
|
|
170
183
|
|
|
171
184
|
/**
|
|
172
|
-
* A {@link Converter} which converts `unknown` to `boolean`.
|
|
185
|
+
* A {@link Converter | Converter} which converts `unknown` to `boolean`.
|
|
173
186
|
* @remarks
|
|
174
187
|
* Boolean values or the case-insensitive strings `'true'` and `'false'` succeed.
|
|
175
188
|
* Anything else fails.
|
|
@@ -276,15 +289,39 @@ declare interface ConstraintOptions {
|
|
|
276
289
|
*/
|
|
277
290
|
declare type ConstraintTrait = FunctionConstraintTrait;
|
|
278
291
|
|
|
292
|
+
/**
|
|
293
|
+
* Helper interface to make it easy to use validators and converters interchangeably.
|
|
294
|
+
* @public
|
|
295
|
+
*/
|
|
296
|
+
declare interface Convalidator<T, TC = unknown> {
|
|
297
|
+
/**
|
|
298
|
+
* Indicates whether the corresponding property can be omitted.
|
|
299
|
+
*/
|
|
300
|
+
readonly isOptional: boolean;
|
|
301
|
+
/**
|
|
302
|
+
* Converts or validates from `unknown` to `<T>`. For objects and arrays, makes no
|
|
303
|
+
* guarantees wrt in-place validation or unrecognized properties.
|
|
304
|
+
* @param from - The `unknown` to be converted or validated.
|
|
305
|
+
* @param context - An optional conversion context of type `<TC>` to be used in
|
|
306
|
+
* the operation.
|
|
307
|
+
* @returns A {@link Result} with a {@link Success} and a value on success or an
|
|
308
|
+
* {@link Failure} with a a message on failure.
|
|
309
|
+
*/
|
|
310
|
+
convalidate(from: unknown, context?: TC): Result<T>;
|
|
311
|
+
}
|
|
312
|
+
|
|
279
313
|
declare namespace Conversion {
|
|
280
314
|
export {
|
|
281
315
|
Converters,
|
|
282
|
-
ConverterTraits,
|
|
283
|
-
ConstraintOptions,
|
|
284
|
-
Converter,
|
|
285
316
|
Infer,
|
|
286
317
|
ConvertedToType,
|
|
287
318
|
BaseConverter,
|
|
319
|
+
OnError,
|
|
320
|
+
ConverterTraits,
|
|
321
|
+
ConstraintOptions,
|
|
322
|
+
Converter,
|
|
323
|
+
DefaultingConverter,
|
|
324
|
+
GenericDefaultingConverter,
|
|
288
325
|
ObjectConverterOptions,
|
|
289
326
|
FieldConverters,
|
|
290
327
|
ObjectConverter,
|
|
@@ -307,7 +344,7 @@ declare type ConvertedToType<TCONV> = Infer<TCONV>;
|
|
|
307
344
|
* of optional templated type `<TC>` (default `undefined`).
|
|
308
345
|
* @public
|
|
309
346
|
*/
|
|
310
|
-
export declare interface Converter<T, TC = undefined> extends ConverterTraits {
|
|
347
|
+
export declare interface Converter<T, TC = undefined> extends ConverterTraits, Convalidator<T, TC> {
|
|
311
348
|
/**
|
|
312
349
|
* Indicates whether this element is explicitly optional.
|
|
313
350
|
*/
|
|
@@ -317,7 +354,8 @@ export declare interface Converter<T, TC = undefined> extends ConverterTraits {
|
|
|
317
354
|
*/
|
|
318
355
|
readonly brand?: string;
|
|
319
356
|
/**
|
|
320
|
-
* Converts from `unknown` to `<T>`.
|
|
357
|
+
* Converts from `unknown` to `<T>`. For objects and arrays, is guaranteed
|
|
358
|
+
* to return a new entity, with any unrecognized properties removed.
|
|
321
359
|
* @param from - The `unknown` to be converted
|
|
322
360
|
* @param context - An optional conversion context of type `<TC>` to be used in
|
|
323
361
|
* the conversion.
|
|
@@ -325,6 +363,10 @@ export declare interface Converter<T, TC = undefined> extends ConverterTraits {
|
|
|
325
363
|
* {@link Failure} with a a message on failure.
|
|
326
364
|
*/
|
|
327
365
|
convert(from: unknown, context?: TC): Result<T>;
|
|
366
|
+
/**
|
|
367
|
+
* {@inheritdoc Validation.Convalidator.convalidate}
|
|
368
|
+
*/
|
|
369
|
+
convalidate(from: unknown, context?: TC): Result<T>;
|
|
328
370
|
/**
|
|
329
371
|
* Converts from `unknown` to `<T>` or `undefined`, as appropriate.
|
|
330
372
|
*
|
|
@@ -399,6 +441,13 @@ export declare interface Converter<T, TC = undefined> extends ConverterTraits {
|
|
|
399
441
|
* @returns A new {@link Converter} returning `<TI[]>`.
|
|
400
442
|
*/
|
|
401
443
|
mapConvertItems<TI>(mapConverter: Converter<TI, unknown>): Converter<TI[], TC>;
|
|
444
|
+
/**
|
|
445
|
+
* Creates a {@link Converter | Converter} which applies a supplied action after
|
|
446
|
+
* conversion. The supplied action is always called regardless of success or failure
|
|
447
|
+
* of the base conversion and is allowed to mutate the return type.
|
|
448
|
+
* @param action - The action to be applied.
|
|
449
|
+
*/
|
|
450
|
+
withAction<T2>(action: (result: Result<T>) => Result<T2>): Converter<T2, TC>;
|
|
402
451
|
/**
|
|
403
452
|
* Creates a {@link Converter} which applies a supplied type guard to the conversion
|
|
404
453
|
* result.
|
|
@@ -438,6 +487,10 @@ export declare interface Converter<T, TC = undefined> extends ConverterTraits {
|
|
|
438
487
|
* @returns A {@link Converter} returning `Brand<T, B>`.
|
|
439
488
|
*/
|
|
440
489
|
withBrand<B extends string>(brand: B): Converter<Brand<T, B>, TC>;
|
|
490
|
+
/**
|
|
491
|
+
* Returns a Converter which always succeeds with a default value rather than failing.
|
|
492
|
+
*/
|
|
493
|
+
withDefault<TD = T>(dflt: TD): DefaultingConverter<T, TD, TC>;
|
|
441
494
|
}
|
|
442
495
|
|
|
443
496
|
declare namespace Converters {
|
|
@@ -463,7 +516,7 @@ declare namespace Converters {
|
|
|
463
516
|
discriminatedObject,
|
|
464
517
|
transform,
|
|
465
518
|
transformObject,
|
|
466
|
-
|
|
519
|
+
OnError,
|
|
467
520
|
string,
|
|
468
521
|
value,
|
|
469
522
|
number,
|
|
@@ -502,6 +555,27 @@ declare class Crc32Normalizer extends HashingNormalizer {
|
|
|
502
555
|
static crc32Hash(parts: string[]): string;
|
|
503
556
|
}
|
|
504
557
|
|
|
558
|
+
/**
|
|
559
|
+
* @public
|
|
560
|
+
*/
|
|
561
|
+
declare interface DefaultingConverter<T, TD = T, TC = undefined> extends Converter<T | TD, TC> {
|
|
562
|
+
/**
|
|
563
|
+
* Default value to use if the conversion fails.
|
|
564
|
+
*/
|
|
565
|
+
readonly defaultValue: TD;
|
|
566
|
+
/**
|
|
567
|
+
* Convert the supplied `unknown` to `Success<T>` or to the `Success` with the default value
|
|
568
|
+
* if conversion is not possible.
|
|
569
|
+
* @param from - the value to be converted.
|
|
570
|
+
* @param ctx - optional context for the conversion.
|
|
571
|
+
*/
|
|
572
|
+
convert(from: unknown, ctx?: TC): Success<T | TD>;
|
|
573
|
+
/**
|
|
574
|
+
* {@inheritdoc Converter.convalidate}
|
|
575
|
+
*/
|
|
576
|
+
convalidate(from: unknown, ctx?: TC): Success<T | TD>;
|
|
577
|
+
}
|
|
578
|
+
|
|
505
579
|
/**
|
|
506
580
|
* Default {@link Validation.ValidatorTraitValues | validation traits}.
|
|
507
581
|
* @public
|
|
@@ -509,12 +583,12 @@ declare class Crc32Normalizer extends HashingNormalizer {
|
|
|
509
583
|
declare const defaultValidatorTraits: ValidatorTraitValues;
|
|
510
584
|
|
|
511
585
|
/**
|
|
512
|
-
* Helper function to create a {@link Converter} which converts any `string` into an
|
|
586
|
+
* Helper function to create a {@link Converter | Converter} which converts any `string` into an
|
|
513
587
|
* array of `string`, by separating at a supplied delimiter.
|
|
514
588
|
* @remarks
|
|
515
589
|
* Delimiter may also be supplied as context at conversion time.
|
|
516
590
|
* @param delimiter - The delimiter at which to split.
|
|
517
|
-
* @returns A new {@link Converter} returning `string[]`.
|
|
591
|
+
* @returns A new {@link Converter | Converter} returning `string[]`.
|
|
518
592
|
* @public
|
|
519
593
|
*/
|
|
520
594
|
declare function delimitedString(delimiter: string, options?: 'filtered' | 'all'): Converter<string[], string>;
|
|
@@ -646,41 +720,41 @@ export declare class DetailedSuccess<T, TD> extends Success<T> {
|
|
|
646
720
|
export declare type DetailedSuccessContinuation<T, TD, TN> = (value: T, detail?: TD) => DetailedResult<TN, TD>;
|
|
647
721
|
|
|
648
722
|
/**
|
|
649
|
-
* Helper to create a {@link Converter} which converts a discriminated object without changing shape.
|
|
723
|
+
* Helper to create a {@link Converter | Converter} which converts a discriminated object without changing shape.
|
|
650
724
|
* @remarks
|
|
651
725
|
* Takes the name of the discriminator property and a
|
|
652
|
-
* {@link Converters.DiscriminatedObjectConverters | string-keyed Record of converters}. During conversion,
|
|
653
|
-
* the resulting {@link Converter} invokes the converter from `converters` that corresponds to the value of
|
|
726
|
+
* {@link Converters.DiscriminatedObjectConverters | string-keyed Record of converters and validators}. During conversion,
|
|
727
|
+
* the resulting {@link Converter | Converter} invokes the converter from `converters` that corresponds to the value of
|
|
654
728
|
* the discriminator property in the source object.
|
|
655
729
|
*
|
|
656
730
|
* If the source is not an object, the discriminator property is missing, or the discriminator has
|
|
657
|
-
* a value not present in the converters, conversion fails and returns {@link Failure} with more information.
|
|
731
|
+
* a value not present in the converters, conversion fails and returns {@link Failure | Failure} with more information.
|
|
658
732
|
* @param discriminatorProp - Name of the property used to discriminate types.
|
|
659
|
-
* @param converters - {@link Converters.DiscriminatedObjectConverters | String-keyed record of converters}
|
|
660
|
-
* invoke, where each key corresponds to a value of the discriminator property.
|
|
661
|
-
* @returns A {@link Converter} which converts the corresponding discriminated object.
|
|
733
|
+
* @param converters - {@link Converters.DiscriminatedObjectConverters | String-keyed record of converters and validators}
|
|
734
|
+
* to invoke, where each key corresponds to a value of the discriminator property.
|
|
735
|
+
* @returns A {@link Converter | Converter} which converts the corresponding discriminated object.
|
|
662
736
|
* @public
|
|
663
737
|
*/
|
|
664
738
|
declare function discriminatedObject<T, TD extends string = string, TC = unknown>(discriminatorProp: string, converters: DiscriminatedObjectConverters<T, TD>): Converter<T, TC>;
|
|
665
739
|
|
|
666
740
|
/**
|
|
667
|
-
* A string-keyed `Record<string, Converter>` which maps specific {@link Converter | converters}
|
|
668
|
-
* value of a discriminator property.
|
|
741
|
+
* A string-keyed `Record<string, Converter>` which maps specific {@link Converter | converters} or
|
|
742
|
+
* {@link Validator | Validators} to the value of a discriminator property.
|
|
669
743
|
* @public
|
|
670
744
|
*/
|
|
671
|
-
declare type DiscriminatedObjectConverters<T, TD extends string = string, TC = unknown> = Record<TD, Converter<T, TC>>;
|
|
745
|
+
declare type DiscriminatedObjectConverters<T, TD extends string = string, TC = unknown> = Record<TD, Converter<T, TC> | Validator<T, TC>>;
|
|
672
746
|
|
|
673
747
|
/**
|
|
674
|
-
* A helper function to create a {@link Converter} which extracts and converts an element from an array.
|
|
748
|
+
* A helper function to create a {@link Converter | Converter} which extracts and converts an element from an array.
|
|
675
749
|
* @remarks
|
|
676
|
-
* The returned {@link Converter} returns {@link Success} with the converted value if the element exists
|
|
677
|
-
* in the supplied array and can be converted. Returns {@link Failure} with an error message otherwise.
|
|
750
|
+
* The returned {@link Converter | Converter} returns {@link Success | Success} with the converted value if the element exists
|
|
751
|
+
* in the supplied array and can be converted. Returns {@link Failure | Failure} with an error message otherwise.
|
|
678
752
|
* @param index - The index of the element to be extracted.
|
|
679
|
-
* @param converter - A {@link Converter}
|
|
753
|
+
* @param converter - A {@link Converter | Converter} or {@link Validator | Validator} for the extracted element.
|
|
680
754
|
* @returns A {@link Converter | Converter<T>} which extracts the specified element from an array.
|
|
681
755
|
* @public
|
|
682
756
|
*/
|
|
683
|
-
declare function element<T, TC = undefined>(index: number, converter: Converter<T, TC>): Converter<T, TC>;
|
|
757
|
+
declare function element<T, TC = undefined>(index: number, converter: Converter<T, TC> | Validator<T, TC>): Converter<T, TC>;
|
|
684
758
|
|
|
685
759
|
/**
|
|
686
760
|
* @internal
|
|
@@ -688,13 +762,13 @@ declare function element<T, TC = undefined>(index: number, converter: Converter<
|
|
|
688
762
|
declare type Entry<T> = [string | number | symbol, T];
|
|
689
763
|
|
|
690
764
|
/**
|
|
691
|
-
* Helper function to create a {@link Converter} which converts `unknown` to one of a set of supplied
|
|
765
|
+
* Helper function to create a {@link Converter | Converter} which converts `unknown` to one of a set of supplied
|
|
692
766
|
* enumerated values. Anything else fails.
|
|
693
767
|
*
|
|
694
768
|
* @remarks
|
|
695
769
|
* Allowed enumerated values can also be supplied as context at conversion time.
|
|
696
770
|
* @param values - Array of allowed values.
|
|
697
|
-
* @returns A new {@link Converter} returning `<T>`.
|
|
771
|
+
* @returns A new {@link Converter | Converter} returning `<T>`.
|
|
698
772
|
* @public
|
|
699
773
|
*/
|
|
700
774
|
declare function enumeratedValue<T>(values: T[]): Converter<T, T[]>;
|
|
@@ -809,26 +883,27 @@ export declare type FailureContinuation<T> = (message: string) => Result<T>;
|
|
|
809
883
|
export declare function failWithDetail<T, TD>(message: string, detail: TD): DetailedFailure<T, TD>;
|
|
810
884
|
|
|
811
885
|
/**
|
|
812
|
-
* A helper function to create a {@link Converter} which extracts and convert a property specified
|
|
886
|
+
* A helper function to create a {@link Converter | Converter} which extracts and convert a property specified
|
|
813
887
|
* by name from an object.
|
|
814
888
|
* @remarks
|
|
815
|
-
* The resulting {@link Converter} returns {@link Success} with the converted value of the corresponding
|
|
816
|
-
* object property if the field exists and can be converted. Returns {@link Failure} with an error message
|
|
889
|
+
* The resulting {@link Converter | Converter} returns {@link Success | Success} with the converted value of the corresponding
|
|
890
|
+
* object property if the field exists and can be converted. Returns {@link Failure | Failure} with an error message
|
|
817
891
|
* otherwise.
|
|
818
892
|
* @param name - The name of the field to be extracted.
|
|
819
|
-
* @param converter - {@link Converter}
|
|
893
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} to use for the extracted
|
|
894
|
+
* field.
|
|
820
895
|
* @public
|
|
821
896
|
*/
|
|
822
|
-
declare function field<T, TC = undefined>(name: string, converter: Converter<T, TC>): Converter<T, TC>;
|
|
897
|
+
declare function field<T, TC = undefined>(name: string, converter: Converter<T, TC> | Validator<T, TC>): Converter<T, TC>;
|
|
823
898
|
|
|
824
899
|
/**
|
|
825
|
-
* Per-property converters for each of the properties in type T.
|
|
900
|
+
* Per-property converters or validators for each of the properties in type T.
|
|
826
901
|
* @remarks
|
|
827
902
|
* Used to construct a {@link Conversion.ObjectConverter | ObjectConverter}
|
|
828
903
|
* @public
|
|
829
904
|
*/
|
|
830
905
|
declare type FieldConverters<T, TC = unknown> = {
|
|
831
|
-
[key in keyof T]: Converter<T[key], TC>;
|
|
906
|
+
[key in keyof T]: Converter<T[key], TC> | Validator<T[key], TC>;
|
|
832
907
|
};
|
|
833
908
|
|
|
834
909
|
/**
|
|
@@ -848,13 +923,13 @@ export declare type FieldInitializers<T> = {
|
|
|
848
923
|
declare type FieldTransformers<TSRC, TDEST, TC = unknown> = {
|
|
849
924
|
[key in keyof TDEST]: {
|
|
850
925
|
/**
|
|
851
|
-
* The name of the property in the source object to be
|
|
926
|
+
* The name of the property in the source object to be converted.
|
|
852
927
|
*/
|
|
853
928
|
from: keyof TSRC;
|
|
854
929
|
/**
|
|
855
|
-
* The converter used to convert the property.
|
|
930
|
+
* The converter or validator used to convert the property.
|
|
856
931
|
*/
|
|
857
|
-
converter: Converter<TDEST[key], TC>;
|
|
932
|
+
converter: Converter<TDEST[key], TC> | Validator<TDEST[key], TC>;
|
|
858
933
|
/**
|
|
859
934
|
* If `true` then a missing source property is ignored. If `false` or omitted
|
|
860
935
|
* then a missing source property causes an error.
|
|
@@ -881,6 +956,94 @@ declare interface FunctionConstraintTrait {
|
|
|
881
956
|
type: 'function';
|
|
882
957
|
}
|
|
883
958
|
|
|
959
|
+
/**
|
|
960
|
+
* Generic {@link Conversion.DefaultingConverter | DefaultingConverter}, which wraps another converter
|
|
961
|
+
* to substitute a supplied default value for any errors returned by the inner converter.
|
|
962
|
+
* @public
|
|
963
|
+
*/
|
|
964
|
+
declare class GenericDefaultingConverter<T, TD = T, TC = undefined> implements DefaultingConverter<T, TD, TC> {
|
|
965
|
+
private _converter;
|
|
966
|
+
/**
|
|
967
|
+
* {@inheritdoc Conversion.DefaultingConverter.defaultValue}
|
|
968
|
+
*/
|
|
969
|
+
defaultValue: TD;
|
|
970
|
+
/**
|
|
971
|
+
* {@inheritdoc Converter.isOptional}
|
|
972
|
+
*/
|
|
973
|
+
get isOptional(): boolean;
|
|
974
|
+
/**
|
|
975
|
+
* {@inheritdoc Converter.isOptional}
|
|
976
|
+
*/
|
|
977
|
+
get brand(): string | undefined;
|
|
978
|
+
/**
|
|
979
|
+
* Constructs a new {@link Conversion.GenericDefaultingConverter | generic defaulting converter}.
|
|
980
|
+
* @param converter - inner {@link Converter | Converter} used for the base conversion.
|
|
981
|
+
* @param defaultValue - default value to be supplied if the inner conversion fails.
|
|
982
|
+
*/
|
|
983
|
+
constructor(converter: Converter<T, TC>, defaultValue: TD);
|
|
984
|
+
/**
|
|
985
|
+
* {@inheritdoc Converter.convert}
|
|
986
|
+
*/
|
|
987
|
+
convert(from: unknown, ctx?: TC | undefined): Success<T | TD>;
|
|
988
|
+
/**
|
|
989
|
+
* {@inheritdoc Converter.convalidate}
|
|
990
|
+
*/
|
|
991
|
+
convalidate(from: unknown, ctx?: TC | undefined): Success<T | TD>;
|
|
992
|
+
/**
|
|
993
|
+
* {@inheritdoc Converter.convertOptional}
|
|
994
|
+
*/
|
|
995
|
+
convertOptional(from: unknown, context?: TC | undefined, onError?: ('failOnError' | 'ignoreErrors') | undefined): Result<T | TD | undefined>;
|
|
996
|
+
/**
|
|
997
|
+
* {@inheritdoc Converter.optional}
|
|
998
|
+
*/
|
|
999
|
+
optional(onError?: ('failOnError' | 'ignoreErrors') | undefined): Converter<T | TD | undefined, TC>;
|
|
1000
|
+
/**
|
|
1001
|
+
* {@inheritdoc Converter.map}
|
|
1002
|
+
*/
|
|
1003
|
+
map<T2>(mapper: (from: T | TD) => Result<T2>): Converter<T2, TC>;
|
|
1004
|
+
/**
|
|
1005
|
+
* {@inheritdoc Converter.mapConvert}
|
|
1006
|
+
*/
|
|
1007
|
+
mapConvert<T2>(mapConverter: Converter<T2, unknown>): Converter<T2, TC>;
|
|
1008
|
+
/**
|
|
1009
|
+
* {@inheritdoc Converter.mapItems}
|
|
1010
|
+
*/
|
|
1011
|
+
mapItems<TI>(mapper: (from: unknown) => Result<TI>): Converter<TI[], TC>;
|
|
1012
|
+
/**
|
|
1013
|
+
* {@inheritdoc Converter.mapConvertItems}
|
|
1014
|
+
*/
|
|
1015
|
+
mapConvertItems<TI>(mapConverter: Converter<TI, unknown>): Converter<TI[], TC>;
|
|
1016
|
+
/**
|
|
1017
|
+
* {@inheritdoc Converter.withAction}
|
|
1018
|
+
*/
|
|
1019
|
+
withAction<T2>(action: (result: Result<T | TD>) => Result<T2>): Converter<T2, TC>;
|
|
1020
|
+
/**
|
|
1021
|
+
* {@inheritdoc Converter.withTypeGuard}
|
|
1022
|
+
*/
|
|
1023
|
+
withTypeGuard<TI>(guard: (from: unknown) => from is TI, message?: string | undefined): Converter<TI, TC>;
|
|
1024
|
+
/**
|
|
1025
|
+
* {@inheritdoc Converter.withItemTypeGuard}
|
|
1026
|
+
*/
|
|
1027
|
+
withItemTypeGuard<TI>(guard: (from: unknown) => from is TI, message?: string | undefined): Converter<TI[], TC>;
|
|
1028
|
+
/**
|
|
1029
|
+
* {@inheritdoc Converter.withConstraint}
|
|
1030
|
+
*/
|
|
1031
|
+
withConstraint(constraint: (val: T | TD) => boolean | Result<T | TD>, options?: ConstraintOptions | undefined): Converter<T | TD, TC>;
|
|
1032
|
+
/**
|
|
1033
|
+
* {@inheritdoc Converter.withBrand}
|
|
1034
|
+
*/
|
|
1035
|
+
withBrand<B extends string>(brand: B): Converter<Brand<T | TD, B>, TC>;
|
|
1036
|
+
/**
|
|
1037
|
+
* Returns a Converter which always succeeds with the supplied default value rather
|
|
1038
|
+
* than failing.
|
|
1039
|
+
*
|
|
1040
|
+
* Note that the supplied default value *overrides* the default value of this
|
|
1041
|
+
* {@link Conversion.DefaultingConverter | DefaultingConverter}.
|
|
1042
|
+
*/
|
|
1043
|
+
withDefault<TD2 = T>(dflt: TD2): DefaultingConverter<T, TD2, TC>;
|
|
1044
|
+
private _applyDefault;
|
|
1045
|
+
}
|
|
1046
|
+
|
|
884
1047
|
/**
|
|
885
1048
|
* Generic base implementation for an in-place {@link Validation.Validator | Validator}.
|
|
886
1049
|
* @public
|
|
@@ -916,6 +1079,10 @@ declare class GenericValidator<T, TC = undefined> implements Validator<T, TC> {
|
|
|
916
1079
|
* {@inheritdoc Validation.Validator.validate}
|
|
917
1080
|
*/
|
|
918
1081
|
validate(from: unknown, context?: TC): Result<T>;
|
|
1082
|
+
/**
|
|
1083
|
+
* {@inheritdoc Validation.Validator.convalidate}
|
|
1084
|
+
*/
|
|
1085
|
+
convalidate(from: unknown, context?: TC): Result<T>;
|
|
919
1086
|
/**
|
|
920
1087
|
* {@inheritdoc Validation.Validator.validateOptional}
|
|
921
1088
|
*/
|
|
@@ -1176,10 +1343,10 @@ export declare interface IResultLogger {
|
|
|
1176
1343
|
}
|
|
1177
1344
|
|
|
1178
1345
|
/**
|
|
1179
|
-
* Helper function to create a {@link Converter} from a supplied type guard function.
|
|
1346
|
+
* Helper function to create a {@link Converter | Converter} from a supplied type guard function.
|
|
1180
1347
|
* @param description - a description of the thing to be validated for use in error messages
|
|
1181
1348
|
* @param guard - a {@link Validation.TypeGuardWithContext} which performs the validation.
|
|
1182
|
-
* @returns A new {@link Converter} which validates the values using the supplied type guard
|
|
1349
|
+
* @returns A new {@link Converter | Converter} which validates the values using the supplied type guard
|
|
1183
1350
|
* and returns them in place.
|
|
1184
1351
|
* @public
|
|
1185
1352
|
*/
|
|
@@ -1207,7 +1374,7 @@ declare function isA_2<T, TC>(description: string, guard: TypeGuardWithContext<T
|
|
|
1207
1374
|
export declare function isKeyOf<T extends object>(key: string | number | symbol, item: T): key is keyof T;
|
|
1208
1375
|
|
|
1209
1376
|
/**
|
|
1210
|
-
* A {@link Converter} which converts an iso formatted string, a number or a `Date` object to
|
|
1377
|
+
* A {@link Converter | Converter} which converts an iso formatted string, a number or a `Date` object to
|
|
1211
1378
|
* a `Date` object.
|
|
1212
1379
|
* @public
|
|
1213
1380
|
*/
|
|
@@ -1231,7 +1398,7 @@ declare interface KeyedConverterOptions<T extends string = string, TC = undefine
|
|
|
1231
1398
|
* @remarks
|
|
1232
1399
|
* Can be used to coerce key names to supported values and/or strong types.
|
|
1233
1400
|
*/
|
|
1234
|
-
keyConverter?: Converter<T, TC>;
|
|
1401
|
+
keyConverter?: Converter<T, TC> | Validator<T, TC>;
|
|
1235
1402
|
}
|
|
1236
1403
|
|
|
1237
1404
|
/**
|
|
@@ -1241,10 +1408,10 @@ declare interface KeyedConverterOptions<T extends string = string, TC = undefine
|
|
|
1241
1408
|
declare type KeyedThingFactory<TS, TD, TK extends string = string> = (key: TK, thing: TS) => Result<TD>;
|
|
1242
1409
|
|
|
1243
1410
|
/**
|
|
1244
|
-
* Helper function to create a {@link Converter} which converts `unknown` to some supplied literal value. Succeeds with
|
|
1411
|
+
* Helper function to create a {@link Converter | Converter} which converts `unknown` to some supplied literal value. Succeeds with
|
|
1245
1412
|
* the supplied value if an identity comparison succeeds, fails otherwise.
|
|
1246
1413
|
* @param value - The value to be compared.
|
|
1247
|
-
* @returns A {@link Converter} which returns the supplied value on success.
|
|
1414
|
+
* @returns A {@link Converter | Converter} which returns the supplied value on success.
|
|
1248
1415
|
* @public
|
|
1249
1416
|
*/
|
|
1250
1417
|
declare function literal<T>(value: T): Converter<T, unknown>;
|
|
@@ -1326,48 +1493,53 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1326
1493
|
export declare function mapFailures<T>(results: Iterable<Result<T>>): string[];
|
|
1327
1494
|
|
|
1328
1495
|
/**
|
|
1329
|
-
* A helper function to create a {@link Converter} which converts the `string`-keyed properties
|
|
1330
|
-
* using a supplied {@link Converter | Converter<T>} to produce a
|
|
1496
|
+
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties
|
|
1497
|
+
* using a supplied {@link Converter | Converter<T>} or {@link Validator | Validator<T>} to produce a
|
|
1498
|
+
* `Map<string, T>`.
|
|
1331
1499
|
* @remarks
|
|
1332
1500
|
* The resulting converter fails conversion if any element cannot be converted.
|
|
1333
|
-
* @param converter - {@link Converter} used
|
|
1334
|
-
*
|
|
1501
|
+
* @param converter - {@link Converter | Converter} | {@link Validator | Validator} used for each item in
|
|
1502
|
+
* the source object.
|
|
1503
|
+
* @returns A {@link Converter | Converter} which returns `Map<string, T>`.
|
|
1335
1504
|
* {@label WITH_DEFAULT}
|
|
1336
1505
|
* @public
|
|
1337
1506
|
*/
|
|
1338
|
-
declare function mapOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>): Converter<Map<TK, T>, TC>;
|
|
1507
|
+
declare function mapOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>): Converter<Map<TK, T>, TC>;
|
|
1339
1508
|
|
|
1340
1509
|
/**
|
|
1341
|
-
* A helper function to create a {@link Converter} which converts the `string`-keyed properties
|
|
1342
|
-
* using a supplied {@link Converter | Converter<T>}
|
|
1343
|
-
* specified handling of elements that cannot be converted.
|
|
1510
|
+
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties
|
|
1511
|
+
* using a supplied {@link Converter | Converter<T>} or {@link Validator | Validator<T>} to produce a
|
|
1512
|
+
* `Map<string, T>` and specified handling of elements that cannot be converted.
|
|
1344
1513
|
* @remarks
|
|
1345
1514
|
* if `onError` is `'fail'` (default), then the entire conversion fails if any key or element
|
|
1346
1515
|
* cannot be converted. If `onError` is `'ignore'`, failing elements are silently ignored.
|
|
1347
|
-
* @param converter - {@link Converter
|
|
1348
|
-
*
|
|
1516
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} used for
|
|
1517
|
+
* each item in the source object.
|
|
1518
|
+
* @returns A {@link Converter | Converter} which returns `Map<string, T>`.
|
|
1349
1519
|
* {@label WITH_ON_ERROR}
|
|
1350
1520
|
* @public
|
|
1351
1521
|
*/
|
|
1352
|
-
declare function mapOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>, onError: 'fail' | 'ignore'): Converter<Map<TK, T>, TC>;
|
|
1522
|
+
declare function mapOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>, onError: 'fail' | 'ignore'): Converter<Map<TK, T>, TC>;
|
|
1353
1523
|
|
|
1354
1524
|
/**
|
|
1355
|
-
* A helper function to create a {@link Converter} which converts the `string`-keyed properties
|
|
1356
|
-
* using a supplied {@link Converter | Converter<T>}
|
|
1525
|
+
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties
|
|
1526
|
+
* using a supplied {@link Converter | Converter<T>} or {@link Validator | Validator<T>} to produce
|
|
1527
|
+
* a `Map<TK,T>`.
|
|
1357
1528
|
* @remarks
|
|
1358
1529
|
* If present, the supplied {@link Converters.KeyedConverterOptions | options} can provide a strongly-typed
|
|
1359
1530
|
* converter for keys and/or control the handling of elements that fail conversion.
|
|
1360
|
-
* @param converter - {@link Converter}
|
|
1531
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} used for each item
|
|
1532
|
+
* in the source object.
|
|
1361
1533
|
* @param options - Optional {@link Converters.KeyedConverterOptions | KeyedConverterOptions<TK, TC>} which
|
|
1362
1534
|
* supplies a key converter and/or error-handling options.
|
|
1363
|
-
* @returns A {@link Converter} which returns `Map<TK,
|
|
1535
|
+
* @returns A {@link Converter | Converter} which returns `Map<TK,T>`.
|
|
1364
1536
|
* {@label WITH_OPTIONS}
|
|
1365
1537
|
* @public
|
|
1366
1538
|
*/
|
|
1367
|
-
declare function mapOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>, options: KeyedConverterOptions<TK, TC>): Converter<Map<TK, T>, TC>;
|
|
1539
|
+
declare function mapOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>, options: KeyedConverterOptions<TK, TC>): Converter<Map<TK, T>, TC>;
|
|
1368
1540
|
|
|
1369
1541
|
/**
|
|
1370
|
-
* Helper function to create a {@link Converter} which converts `unknown` to one of a set of supplied enumerated
|
|
1542
|
+
* Helper function to create a {@link Converter | Converter} which converts `unknown` to one of a set of supplied enumerated
|
|
1371
1543
|
* values, mapping any of multiple supplied values to the enumeration.
|
|
1372
1544
|
* @remarks
|
|
1373
1545
|
* Enables mapping of multiple input values to a consistent internal representation (so e.g. `'y'`, `'yes'`,
|
|
@@ -1376,7 +1548,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1376
1548
|
* value, the second is the set of values that map to the result. Tuples are evaluated in the order
|
|
1377
1549
|
* supplied and are not checked for duplicates.
|
|
1378
1550
|
* @param message - An optional error message.
|
|
1379
|
-
* @returns A {@link Converter} which applies the mapping and yields `<T>` on success.
|
|
1551
|
+
* @returns A {@link Converter | Converter} which applies the mapping and yields `<T>` on success.
|
|
1380
1552
|
* @public
|
|
1381
1553
|
*/
|
|
1382
1554
|
declare function mappedEnumeratedValue<T>(map: [T, unknown[]][], message?: string): Converter<T, undefined>;
|
|
@@ -1458,7 +1630,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1458
1630
|
}
|
|
1459
1631
|
|
|
1460
1632
|
/**
|
|
1461
|
-
* A {@link Converter} which converts `unknown` to a `number`.
|
|
1633
|
+
* A {@link Converter | Converter} which converts `unknown` to a `number`.
|
|
1462
1634
|
* @remarks
|
|
1463
1635
|
* Numbers and strings with a numeric format succeed. Anything else fails.
|
|
1464
1636
|
* @public
|
|
@@ -1511,9 +1683,9 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1511
1683
|
* without changing shape, given a {@link Conversion.FieldConverters | FieldConverters<T>} and an optional
|
|
1512
1684
|
* {@link Conversion.ObjectConverterOptions | ObjectConverterOptions<T>} to further refine conversion behavior.
|
|
1513
1685
|
* @remarks
|
|
1514
|
-
* By default, if all of the requested fields exist and can be converted, returns {@link Success}
|
|
1686
|
+
* By default, if all of the requested fields exist and can be converted, returns {@link Success | Success}
|
|
1515
1687
|
* with a new object that contains the converted values under the original key names. If any required properties
|
|
1516
|
-
* do not exist or cannot be converted, the entire conversion fails, returning {@link Failure} with additional
|
|
1688
|
+
* do not exist or cannot be converted, the entire conversion fails, returning {@link Failure | Failure} with additional
|
|
1517
1689
|
* error information.
|
|
1518
1690
|
*
|
|
1519
1691
|
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
@@ -1533,9 +1705,9 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1533
1705
|
* without changing shape, given a {@link Conversion.FieldConverters | FieldConverters<T>} and a set of
|
|
1534
1706
|
* optional properties.
|
|
1535
1707
|
* @remarks
|
|
1536
|
-
* By default, if all of the requested fields exist and can be converted, returns {@link Success}
|
|
1708
|
+
* By default, if all of the requested fields exist and can be converted, returns {@link Success | Success}
|
|
1537
1709
|
* with a new object that contains the converted values under the original key names. If any required properties
|
|
1538
|
-
* do not exist or cannot be converted, the entire conversion fails, returning {@link Failure} with additional
|
|
1710
|
+
* do not exist or cannot be converted, the entire conversion fails, returning {@link Failure | Failure} with additional
|
|
1539
1711
|
* error information.
|
|
1540
1712
|
*
|
|
1541
1713
|
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
@@ -1767,22 +1939,24 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1767
1939
|
export declare function omit<T extends object, K extends keyof T>(from: T, exclude: K[]): Omit<T, K>;
|
|
1768
1940
|
|
|
1769
1941
|
/**
|
|
1770
|
-
* A helper function to create a {@link Converter} for polymorphic values.
|
|
1771
|
-
* converter which
|
|
1772
|
-
* result. Returns an error if none of the supplied converters can
|
|
1942
|
+
* A helper function to create a {@link Converter | Converter} for polymorphic values.
|
|
1943
|
+
* Returns a converter which invokes the wrapped converters in sequence, returning the
|
|
1944
|
+
* first successful result. Returns an error if none of the supplied converters can
|
|
1945
|
+
* convert the value.
|
|
1773
1946
|
* @remarks
|
|
1774
1947
|
* If `onError` is `ignoreErrors` (default), then errors from any of the
|
|
1775
1948
|
* converters are ignored provided that some converter succeeds. If
|
|
1776
1949
|
* onError is `failOnError`, then an error from any converter fails the entire
|
|
1777
1950
|
* conversion.
|
|
1778
1951
|
*
|
|
1779
|
-
* @param converters - An ordered list of {@link Converter | converters}
|
|
1952
|
+
* @param converters - An ordered list of {@link Converter | converters} or {@link Validator | validators}
|
|
1953
|
+
* to be considered.
|
|
1780
1954
|
* @param onError - Specifies treatment of unconvertible elements.
|
|
1781
|
-
* @returns A new {@link Converter} which yields a value from the union of the types returned
|
|
1955
|
+
* @returns A new {@link Converter | Converter} which yields a value from the union of the types returned
|
|
1782
1956
|
* by the wrapped converters.
|
|
1783
1957
|
* @public
|
|
1784
1958
|
*/
|
|
1785
|
-
declare function oneOf<T, TC = unknown>(converters: Array<Converter<T, TC>>, onError?:
|
|
1959
|
+
declare function oneOf<T, TC = unknown>(converters: Array<Converter<T, TC> | Validator<T, TC>>, onError?: OnError): Converter<T, TC>;
|
|
1786
1960
|
|
|
1787
1961
|
/**
|
|
1788
1962
|
* Helper function to create a {@link Validation.Validator | Validator} which validates one
|
|
@@ -1832,51 +2006,49 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1832
2006
|
validators: Validator<T, TC>[];
|
|
1833
2007
|
}
|
|
1834
2008
|
|
|
1835
|
-
declare type OnError = 'failOnError' | 'ignoreErrors';
|
|
1836
|
-
|
|
1837
2009
|
/**
|
|
1838
2010
|
* Action to take on conversion failures.
|
|
1839
2011
|
* @public
|
|
1840
2012
|
*/
|
|
1841
|
-
declare type
|
|
2013
|
+
declare type OnError = 'failOnError' | 'ignoreErrors';
|
|
1842
2014
|
|
|
1843
2015
|
/**
|
|
1844
|
-
* A {@link Converter} to convert an optional `boolean` value.
|
|
2016
|
+
* A {@link Converter | Converter} to convert an optional `boolean` value.
|
|
1845
2017
|
* @remarks
|
|
1846
2018
|
* Values of type `boolean` or strings that match (case-insensitive) `'true'`
|
|
1847
|
-
* or `'false'` are converted and returned. Anything else returns {@link Success}
|
|
2019
|
+
* or `'false'` are converted and returned. Anything else returns {@link Success | Success}
|
|
1848
2020
|
* with value `undefined`.
|
|
1849
2021
|
* @public
|
|
1850
2022
|
*/
|
|
1851
2023
|
declare const optionalBoolean: Converter<boolean | undefined>;
|
|
1852
2024
|
|
|
1853
2025
|
/**
|
|
1854
|
-
* A helper function to create a {@link Converter} which extracts and converts an optional element from an array.
|
|
2026
|
+
* A helper function to create a {@link Converter | Converter} which extracts and converts an optional element from an array.
|
|
1855
2027
|
* @remarks
|
|
1856
|
-
* The resulting {@link Converter} returns {@link Success} with the converted value if the element exists
|
|
1857
|
-
* in the supplied array and can be converted. Returns {@link Success} with value `undefined` if the parameter
|
|
1858
|
-
* is an array but the index is out of range. Returns {@link Failure} with a message if the supplied parameter
|
|
2028
|
+
* The resulting {@link Converter | Converter} returns {@link Success | Success} with the converted value if the element exists
|
|
2029
|
+
* in the supplied array and can be converted. Returns {@link Success | Success} with value `undefined` if the parameter
|
|
2030
|
+
* is an array but the index is out of range. Returns {@link Failure | Failure} with a message if the supplied parameter
|
|
1859
2031
|
* is not an array, if the requested index is negative, or if the element cannot be converted.
|
|
1860
2032
|
* @param index - The index of the element to be extracted.
|
|
1861
|
-
* @param converter - A {@link Converter} used
|
|
2033
|
+
* @param converter - A {@link Converter | Converter} or {@link Validator | Validator} used for the extracted element.
|
|
1862
2034
|
* @returns A {@link Converter | Converter<T>} which extracts the specified element from an array.
|
|
1863
2035
|
* @public
|
|
1864
2036
|
*/
|
|
1865
|
-
declare function optionalElement<T, TC = undefined>(index: number, converter: Converter<T, TC>): Converter<T | undefined, TC>;
|
|
2037
|
+
declare function optionalElement<T, TC = undefined>(index: number, converter: Converter<T, TC> | Validator<T, TC>): Converter<T | undefined, TC>;
|
|
1866
2038
|
|
|
1867
2039
|
/**
|
|
1868
|
-
* A helper function to create a {@link Converter} which extracts and convert a property specified
|
|
2040
|
+
* A helper function to create a {@link Converter | Converter} which extracts and convert a property specified
|
|
1869
2041
|
* by name from an object.
|
|
1870
2042
|
* @remarks
|
|
1871
|
-
* The resulting {@link Converter} returns {@link Success} with the converted value of
|
|
1872
|
-
* object property if the field exists and can be converted. Returns {@link Success
|
|
1873
|
-
* if the supplied parameter is an object but the named field is not present.
|
|
1874
|
-
* an error message otherwise.
|
|
2043
|
+
* The resulting {@link Converter | Converter} returns {@link Success | Success} with the converted value of
|
|
2044
|
+
* the corresponding object property if the field exists and can be converted. Returns {@link Success | Success}
|
|
2045
|
+
* with `undefined` if the supplied parameter is an object but the named field is not present.
|
|
2046
|
+
* Returns {@link Failure | Failure} with an error message otherwise.
|
|
1875
2047
|
* @param name - The name of the field to be extracted.
|
|
1876
|
-
* @param converter - {@link Converter}
|
|
2048
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} to use for the extracted field.
|
|
1877
2049
|
* @public
|
|
1878
2050
|
*/
|
|
1879
|
-
declare function optionalField<T, TC = undefined>(name: string, converter: Converter<T, TC>): Converter<T | undefined, TC>;
|
|
2051
|
+
declare function optionalField<T, TC = undefined>(name: string, converter: Converter<T, TC> | Validator<T, TC>): Converter<T | undefined, TC>;
|
|
1880
2052
|
|
|
1881
2053
|
/**
|
|
1882
2054
|
* Applies a factory method to convert an optional `ReadonlyMap<string, TS>` into a `Record<string, TD>`
|
|
@@ -1899,10 +2071,10 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1899
2071
|
export declare function optionalMapToRecord<TS, TD, TK extends string = string>(src: ReadonlyMap<TK, TS> | undefined, factory: KeyedThingFactory<TS, TD, TK>): Result<Record<TK, TD> | undefined>;
|
|
1900
2072
|
|
|
1901
2073
|
/**
|
|
1902
|
-
* A {@link Converter} which converts an optional `number` value.
|
|
2074
|
+
* A {@link Converter | Converter} which converts an optional `number` value.
|
|
1903
2075
|
* @remarks
|
|
1904
2076
|
* Values of type `number` or numeric strings are converted and returned.
|
|
1905
|
-
* Anything else returns {@link Success} with value `undefined`.
|
|
2077
|
+
* Anything else returns {@link Success | Success} with value `undefined`.
|
|
1906
2078
|
* @public
|
|
1907
2079
|
*/
|
|
1908
2080
|
declare const optionalNumber: Converter<number | undefined>;
|
|
@@ -1928,8 +2100,8 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1928
2100
|
export declare function optionalRecordToPossiblyEmptyMap<TS, TD, TK extends string = string>(src: Record<TK, TS> | undefined, factory: KeyedThingFactory<TS, TD, TK>): Result<Map<TK, TD>>;
|
|
1929
2101
|
|
|
1930
2102
|
/**
|
|
1931
|
-
* A {@link Converter} which converts an optional `string` value. Values of type
|
|
1932
|
-
* `string` are returned. Anything else returns {@link Success} with value `undefined`.
|
|
2103
|
+
* A {@link Converter | Converter} which converts an optional `string` value. Values of type
|
|
2104
|
+
* `string` are returned. Anything else returns {@link Success | Success} with value `undefined`.
|
|
1933
2105
|
* @public
|
|
1934
2106
|
*/
|
|
1935
2107
|
declare const optionalString: Converter<string | undefined, unknown>;
|
|
@@ -2006,45 +2178,49 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2006
2178
|
export declare function propagateWithDetail<T, TD>(result: Result<T>, detail: TD, successDetail?: TD): DetailedResult<T, TD>;
|
|
2007
2179
|
|
|
2008
2180
|
/**
|
|
2009
|
-
* A helper function to create a {@link Converter} which converts the `string`-keyed
|
|
2010
|
-
* using a supplied {@link Converter | Converter<T>}
|
|
2181
|
+
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed
|
|
2182
|
+
* properties using a supplied {@link Converter | Converter<T>} or {@link Validator | Validator<T>} to
|
|
2183
|
+
* produce a `Record<string, T>`.
|
|
2011
2184
|
* @remarks
|
|
2012
2185
|
* The resulting converter fails conversion if any element cannot be converted.
|
|
2013
|
-
* @param converter - {@link Converter}
|
|
2014
|
-
*
|
|
2186
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} used for each
|
|
2187
|
+
* item in the source object.
|
|
2188
|
+
* @returns A {@link Converter | Converter} which returns `Record<string, T>`.
|
|
2015
2189
|
* {@label WITH_DEFAULT}
|
|
2016
2190
|
* @public
|
|
2017
2191
|
*/
|
|
2018
|
-
declare function recordOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>): Converter<Record<TK, T>, TC>;
|
|
2192
|
+
declare function recordOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>): Converter<Record<TK, T>, TC>;
|
|
2019
2193
|
|
|
2020
2194
|
/**
|
|
2021
|
-
* A helper function to create a {@link Converter} which converts the `string`-keyed properties
|
|
2022
|
-
* using a supplied {@link Converter | Converter<T>}
|
|
2023
|
-
* specified handling of elements that cannot be converted.
|
|
2195
|
+
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties
|
|
2196
|
+
* using a supplied {@link Converter | Converter<T>} or {@link Validator | Validator<T>} to produce a
|
|
2197
|
+
* `Record<string, T>` and optionally specified handling of elements that cannot be converted.
|
|
2024
2198
|
* @remarks
|
|
2025
2199
|
* if `onError` is `'fail'` (default), then the entire conversion fails if any key or element
|
|
2026
2200
|
* cannot be converted. If `onError` is `'ignore'`, failing elements are silently ignored.
|
|
2027
|
-
* @param converter - {@link Converter}
|
|
2028
|
-
*
|
|
2201
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} for each item in
|
|
2202
|
+
* the source object.
|
|
2203
|
+
* @returns A {@link Converter | Converter} which returns `Record<string, T>`.
|
|
2029
2204
|
* {@label WITH_ON_ERROR}
|
|
2030
2205
|
* @public
|
|
2031
2206
|
*/
|
|
2032
|
-
declare function recordOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>, onError: 'fail' | 'ignore'): Converter<Record<TK, T>, TC>;
|
|
2207
|
+
declare function recordOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>, onError: 'fail' | 'ignore'): Converter<Record<TK, T>, TC>;
|
|
2033
2208
|
|
|
2034
2209
|
/**
|
|
2035
|
-
* A helper function to create a {@link Converter} which converts the `string`-keyed properties
|
|
2036
|
-
* using a supplied {@link Converter | Converter<T>} to produce a
|
|
2210
|
+
* A helper function to create a {@link Converter | Converter} or which converts the `string`-keyed properties
|
|
2211
|
+
* using a supplied {@link Converter | Converter<T>} or {@link Validator | Validator<T>} to produce a
|
|
2212
|
+
* `Record<TK, T>`.
|
|
2037
2213
|
* @remarks
|
|
2038
2214
|
* If present, the supplied {@link Converters.KeyedConverterOptions | options} can provide a strongly-typed
|
|
2039
2215
|
* converter for keys and/or control the handling of elements that fail conversion.
|
|
2040
|
-
* @param converter - {@link Converter} used
|
|
2216
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} used for each item in the source object.
|
|
2041
2217
|
* @param options - Optional {@link Converters.KeyedConverterOptions | KeyedConverterOptions<TK, TC>} which
|
|
2042
2218
|
* supplies a key converter and/or error-handling options.
|
|
2043
|
-
* @returns A {@link Converter} which returns `Record<TK, T>`.
|
|
2219
|
+
* @returns A {@link Converter | Converter} which returns `Record<TK, T>`.
|
|
2044
2220
|
* {@label WITH_OPTIONS}
|
|
2045
2221
|
* @public
|
|
2046
2222
|
*/
|
|
2047
|
-
declare function recordOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>, options: KeyedConverterOptions<TK, TC>): Converter<Record<TK, T>, TC>;
|
|
2223
|
+
declare function recordOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>, options: KeyedConverterOptions<TK, TC>): Converter<Record<TK, T>, TC>;
|
|
2048
2224
|
|
|
2049
2225
|
/**
|
|
2050
2226
|
* Applies a factory method to convert a `Record<TK, TS>` into a `Map<TK, TD>`.
|
|
@@ -2138,10 +2314,10 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2138
2314
|
declare const string_2: Validator<string>;
|
|
2139
2315
|
|
|
2140
2316
|
/**
|
|
2141
|
-
* {@link Converter} to convert an `unknown` to an array of `string`.
|
|
2317
|
+
* {@link Converter | Converter} to convert an `unknown` to an array of `string`.
|
|
2142
2318
|
* @remarks
|
|
2143
|
-
* Returns {@link Success} with the the supplied value if it as an array
|
|
2144
|
-
* of strings, returns {@link Failure} with an error message otherwise.
|
|
2319
|
+
* Returns {@link Success | Success} with the the supplied value if it as an array
|
|
2320
|
+
* of strings, returns {@link Failure | Failure} with an error message otherwise.
|
|
2145
2321
|
* @public
|
|
2146
2322
|
*/
|
|
2147
2323
|
declare const stringArray: Converter<string[]>;
|
|
@@ -2152,7 +2328,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2152
2328
|
* helper methods.
|
|
2153
2329
|
* @public
|
|
2154
2330
|
*/
|
|
2155
|
-
declare class StringConverter<T extends string = string, TC = unknown> extends BaseConverter<T, TC> {
|
|
2331
|
+
export declare class StringConverter<T extends string = string, TC = unknown> extends BaseConverter<T, TC> {
|
|
2156
2332
|
/**
|
|
2157
2333
|
* Construct a new {@link Conversion.StringConverter | StringConverter}.
|
|
2158
2334
|
* @param defaultContext - Optional context used by the conversion.
|
|
@@ -2352,19 +2528,19 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2352
2528
|
* @remarks
|
|
2353
2529
|
* Template conversions are applied using `mustache` syntax.
|
|
2354
2530
|
* @param defaultContext - Optional default context to use for template values.
|
|
2355
|
-
* @returns A new {@link Converter} returning `string`.
|
|
2531
|
+
* @returns A new {@link Converter | Converter} returning `string`.
|
|
2356
2532
|
* @public
|
|
2357
2533
|
*/
|
|
2358
2534
|
declare function templateString(defaultContext?: unknown): StringConverter<string, unknown>;
|
|
2359
2535
|
|
|
2360
2536
|
/**
|
|
2361
|
-
* Helper to create a {@link Converter} which converts a source object to a new object with a
|
|
2537
|
+
* Helper to create a {@link Converter | Converter} which converts a source object to a new object with a
|
|
2362
2538
|
* different shape.
|
|
2363
2539
|
*
|
|
2364
2540
|
* @remarks
|
|
2365
|
-
* On successful conversion, the resulting {@link Converter} returns {@link Success} with a new
|
|
2541
|
+
* On successful conversion, the resulting {@link Converter | Converter} returns {@link Success | Success} with a new
|
|
2366
2542
|
* object, which contains the converted values under the key names specified at initialization time.
|
|
2367
|
-
* It returns {@link Failure} with an error message if any fields to be extracted do not exist
|
|
2543
|
+
* It returns {@link Failure | Failure} with an error message if any fields to be extracted do not exist
|
|
2368
2544
|
* or cannot be converted.
|
|
2369
2545
|
*
|
|
2370
2546
|
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
@@ -2373,29 +2549,29 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2373
2549
|
* @param properties - An object with key names that correspond to the target object and an
|
|
2374
2550
|
* appropriate {@link Conversion.FieldConverters | FieldConverter} which extracts and converts
|
|
2375
2551
|
* a single filed from the source object.
|
|
2376
|
-
* @returns A {@link Converter} with the specified conversion behavior.
|
|
2552
|
+
* @returns A {@link Converter | Converter} with the specified conversion behavior.
|
|
2377
2553
|
* @public
|
|
2378
2554
|
*/
|
|
2379
2555
|
declare function transform<T, TC = unknown>(properties: FieldConverters<T, TC>): Converter<T, TC>;
|
|
2380
2556
|
|
|
2381
2557
|
/**
|
|
2382
|
-
* Helper to create a strongly-typed {@link Converter} which converts a source object to a
|
|
2558
|
+
* Helper to create a strongly-typed {@link Converter | Converter} which converts a source object to a
|
|
2383
2559
|
* new object with a different shape.
|
|
2384
2560
|
*
|
|
2385
2561
|
* @remarks
|
|
2386
|
-
* On successful conversion, the resulting {@link Converter} returns {@link Success} with a new
|
|
2562
|
+
* On successful conversion, the resulting {@link Converter | Converter} returns {@link Success | Success} with a new
|
|
2387
2563
|
* object, which contains the converted values under the key names specified at initialization time.
|
|
2388
2564
|
*
|
|
2389
|
-
* It returns {@link Failure} with an error message if any fields to be extracted do not exist
|
|
2565
|
+
* It returns {@link Failure | Failure} with an error message if any fields to be extracted do not exist
|
|
2390
2566
|
* or cannot be converted.
|
|
2391
2567
|
*
|
|
2392
2568
|
* @param destinationFields - An object with key names that correspond to the target object and an
|
|
2393
2569
|
* appropriate {@link Converters.FieldTransformers | FieldTransformers} which specifies the name
|
|
2394
|
-
* of the corresponding property in the source object, the converter used
|
|
2395
|
-
* property and any configuration to guide the conversion.
|
|
2570
|
+
* of the corresponding property in the source object, the converter or validator used for each source
|
|
2571
|
+
* property and any other configuration to guide the conversion.
|
|
2396
2572
|
* @param options - Options which affect the transformation.
|
|
2397
2573
|
*
|
|
2398
|
-
* @returns A {@link Converter} with the specified conversion behavior.
|
|
2574
|
+
* @returns A {@link Converter | Converter} with the specified conversion behavior.
|
|
2399
2575
|
* @public
|
|
2400
2576
|
*/
|
|
2401
2577
|
declare function transformObject<TSRC, TDEST, TC = unknown>(destinationFields: FieldTransformers<TSRC, TDEST, TC>, options?: TransformObjectOptions<TSRC>): Converter<TDEST, TC>;
|
|
@@ -2469,19 +2645,19 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2469
2645
|
declare type TypeGuardWithContext<T, TC = unknown> = (from: unknown, context?: TC) => from is T;
|
|
2470
2646
|
|
|
2471
2647
|
/**
|
|
2472
|
-
* Helper function to create a {@link Converter} from any {@link Validation.Validator}
|
|
2648
|
+
* Helper function to create a {@link Converter | Converter} from any {@link Validation.Validator}
|
|
2473
2649
|
* @param validator - the validator to be wrapped
|
|
2474
|
-
* @returns A {@link Converter} which uses the supplied validator.
|
|
2650
|
+
* @returns A {@link Converter | Converter} which uses the supplied validator.
|
|
2475
2651
|
* @public
|
|
2476
2652
|
*/
|
|
2477
2653
|
declare function validated<T, TC = unknown>(validator: Validator<T, TC>): Converter<T, TC>;
|
|
2478
2654
|
|
|
2479
2655
|
/**
|
|
2480
|
-
* Helper function to create a {@link Converter} which validates that a supplied value is
|
|
2656
|
+
* Helper function to create a {@link Converter | Converter} which validates that a supplied value is
|
|
2481
2657
|
* of a type validated by a supplied validator function and returns it.
|
|
2482
2658
|
* @remarks
|
|
2483
|
-
* If `validator` succeeds, this {@link Converter} returns {@link Success} with the supplied
|
|
2484
|
-
* value of `from` coerced to type `<T>`. Returns a {@link Failure} with additional
|
|
2659
|
+
* If `validator` succeeds, this {@link Converter | Converter} returns {@link Success | Success} with the supplied
|
|
2660
|
+
* value of `from` coerced to type `<T>`. Returns a {@link Failure | Failure} with additional
|
|
2485
2661
|
* information otherwise.
|
|
2486
2662
|
* @param validator - A validator function to determine if the converted value is valid.
|
|
2487
2663
|
* @param description - A description of the validated type for use in error messages.
|
|
@@ -2496,6 +2672,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2496
2672
|
Classes,
|
|
2497
2673
|
Validators,
|
|
2498
2674
|
TypeGuardWithContext,
|
|
2675
|
+
Convalidator,
|
|
2499
2676
|
FunctionConstraintTrait,
|
|
2500
2677
|
ConstraintTrait,
|
|
2501
2678
|
ValidatorTraitValues,
|
|
@@ -2513,7 +2690,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2513
2690
|
* required characteristics (type, values, etc).
|
|
2514
2691
|
* @public
|
|
2515
2692
|
*/
|
|
2516
|
-
export declare interface Validator<T, TC = undefined> {
|
|
2693
|
+
export declare interface Validator<T, TC = undefined> extends Convalidator<T, TC> {
|
|
2517
2694
|
/**
|
|
2518
2695
|
* {@link Validation.ValidatorTraits | Traits} describing this validation.
|
|
2519
2696
|
*/
|
|
@@ -2527,14 +2704,18 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2527
2704
|
*/
|
|
2528
2705
|
readonly brand: string | undefined;
|
|
2529
2706
|
/**
|
|
2530
|
-
* Tests to see if a supplied `unknown` value matches this
|
|
2531
|
-
*
|
|
2707
|
+
* Tests to see if a supplied `unknown` value matches this validation. All
|
|
2708
|
+
* validate calls are guaranteed to return the entity passed in on Success.
|
|
2532
2709
|
* @param from - The `unknown` value to be tested.
|
|
2533
2710
|
* @param context - Optional validation context.
|
|
2534
2711
|
* @returns {@link Success} with the typed, validated value,
|
|
2535
2712
|
* or {@link Failure} with an error message if validation fails.
|
|
2536
2713
|
*/
|
|
2537
2714
|
validate(from: unknown, context?: TC): Result<T>;
|
|
2715
|
+
/**
|
|
2716
|
+
* {@inheritdoc Validation.Convalidator.convalidate}
|
|
2717
|
+
*/
|
|
2718
|
+
convalidate(from: unknown, context?: TC): Result<T>;
|
|
2538
2719
|
/**
|
|
2539
2720
|
* Tests to see if a supplied `unknown` value matches this
|
|
2540
2721
|
* validation. Accepts `undefined`.
|