@fgv/ts-utils 4.1.0 → 4.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.json +30 -0
- package/CHANGELOG.md +17 -1
- package/dist/ts-utils.d.ts +599 -54
- package/lib/index.d.ts +3 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +6 -1
- package/lib/index.js.map +1 -1
- package/lib/packlets/collections/common.d.ts +6 -0
- package/lib/packlets/collections/common.d.ts.map +1 -0
- package/lib/packlets/collections/common.js +24 -0
- package/lib/packlets/collections/common.js.map +1 -0
- package/lib/packlets/collections/index.d.ts +8 -0
- package/lib/packlets/collections/index.d.ts.map +1 -0
- package/lib/packlets/collections/index.js +68 -0
- package/lib/packlets/collections/index.js.map +1 -0
- package/lib/packlets/collections/readonlyResultMap.d.ts +58 -0
- package/lib/packlets/collections/readonlyResultMap.d.ts.map +1 -0
- package/lib/packlets/collections/readonlyResultMap.js +24 -0
- package/lib/packlets/collections/readonlyResultMap.js.map +1 -0
- package/lib/packlets/collections/resultMap.d.ts +180 -0
- package/lib/packlets/collections/resultMap.d.ts.map +1 -0
- package/lib/packlets/collections/resultMap.js +224 -0
- package/lib/packlets/collections/resultMap.js.map +1 -0
- package/lib/packlets/collections/resultMapValidator.d.ts +94 -0
- package/lib/packlets/collections/resultMapValidator.d.ts.map +1 -0
- package/lib/packlets/collections/resultMapValidator.js +126 -0
- package/lib/packlets/collections/resultMapValidator.js.map +1 -0
- package/lib/packlets/collections/utils.d.ts +69 -0
- package/lib/packlets/collections/utils.d.ts.map +1 -0
- package/lib/packlets/collections/utils.js +116 -0
- package/lib/packlets/collections/utils.js.map +1 -0
- package/lib/packlets/collections/validatingResultMap.d.ts +73 -0
- package/lib/packlets/collections/validatingResultMap.d.ts.map +1 -0
- package/lib/packlets/collections/validatingResultMap.js +102 -0
- package/lib/packlets/collections/validatingResultMap.js.map +1 -0
- package/lib/packlets/conversion/baseConverter.d.ts +7 -3
- package/lib/packlets/conversion/baseConverter.d.ts.map +1 -1
- package/lib/packlets/conversion/baseConverter.js +10 -0
- package/lib/packlets/conversion/baseConverter.js.map +1 -1
- package/lib/packlets/conversion/converter.d.ts +19 -3
- package/lib/packlets/conversion/converter.d.ts.map +1 -1
- package/lib/packlets/conversion/converter.js.map +1 -1
- package/lib/packlets/conversion/converters.d.ts +42 -42
- package/lib/packlets/conversion/converters.d.ts.map +1 -1
- package/lib/packlets/conversion/converters.js +9 -9
- package/lib/packlets/conversion/converters.js.map +1 -1
- package/lib/packlets/conversion/defaultingConverter.d.ts +6 -2
- package/lib/packlets/conversion/defaultingConverter.d.ts.map +1 -1
- package/lib/packlets/conversion/defaultingConverter.js +7 -0
- package/lib/packlets/conversion/defaultingConverter.js.map +1 -1
- package/lib/packlets/conversion/objectConverter.d.ts +3 -3
- package/lib/packlets/conversion/objectConverter.d.ts.map +1 -1
- package/lib/packlets/conversion/objectConverter.js.map +1 -1
- package/lib/packlets/validation/field.d.ts +1 -1
- package/lib/packlets/validation/field.d.ts.map +1 -1
- package/lib/packlets/validation/field.js.map +1 -1
- package/lib/packlets/validation/genericValidator.d.ts +6 -2
- package/lib/packlets/validation/genericValidator.d.ts.map +1 -1
- package/lib/packlets/validation/genericValidator.js +19 -2
- package/lib/packlets/validation/genericValidator.js.map +1 -1
- package/lib/packlets/validation/validator.d.ts +18 -1
- package/lib/packlets/validation/validator.d.ts.map +1 -1
- package/lib/packlets/validation/validator.js.map +1 -1
- package/lib/packlets/validation/validatorBase.d.ts +1 -1
- package/lib/packlets/validation/validatorBase.d.ts.map +1 -1
- package/lib/packlets/validation/validatorBase.js.map +1 -1
- package/package.json +1 -1
package/dist/ts-utils.d.ts
CHANGED
|
@@ -22,7 +22,7 @@ export declare function allSucceed<T>(results: Iterable<Result<unknown>>, succes
|
|
|
22
22
|
* @returns A {@link Converter | Converter} which returns an array of `<T>`.
|
|
23
23
|
* @public
|
|
24
24
|
*/
|
|
25
|
-
declare function arrayOf<T, TC =
|
|
25
|
+
declare function arrayOf<T, TC = unknown>(converter: Converter<T, TC> | Validator<T, TC>, onError?: OnError): Converter<T[], TC>;
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
28
|
* Helper function to create a {@link Validation.Classes.ArrayValidator | ArrayValidator} which
|
|
@@ -84,7 +84,7 @@ declare namespace Base {
|
|
|
84
84
|
* Base templated wrapper to simplify creation of new {@link Converter}s.
|
|
85
85
|
* @public
|
|
86
86
|
*/
|
|
87
|
-
declare class BaseConverter<T, TC =
|
|
87
|
+
declare class BaseConverter<T, TC = unknown> implements Converter<T, TC> {
|
|
88
88
|
/**
|
|
89
89
|
* @internal
|
|
90
90
|
*/
|
|
@@ -170,6 +170,10 @@ declare class BaseConverter<T, TC = undefined> implements Converter<T, TC> {
|
|
|
170
170
|
* @internal
|
|
171
171
|
*/
|
|
172
172
|
protected _context(supplied?: TC): TC | undefined;
|
|
173
|
+
/**
|
|
174
|
+
* {@inheritdoc Converter.withFormattedError}
|
|
175
|
+
*/
|
|
176
|
+
withFormattedError(formatter: ConversionErrorFormatter<TC>): Converter<T, TC>;
|
|
173
177
|
/**
|
|
174
178
|
* @internal
|
|
175
179
|
*/
|
|
@@ -187,7 +191,7 @@ declare class BaseConverter<T, TC = undefined> implements Converter<T, TC> {
|
|
|
187
191
|
* Anything else fails.
|
|
188
192
|
* @public
|
|
189
193
|
*/
|
|
190
|
-
declare const boolean: Converter<boolean,
|
|
194
|
+
declare const boolean: Converter<boolean, unknown>;
|
|
191
195
|
|
|
192
196
|
/**
|
|
193
197
|
* A {@link Validation.Classes.BooleanValidator | BooleanValidator} which validates a boolean in place.
|
|
@@ -261,6 +265,26 @@ declare namespace Classes {
|
|
|
261
265
|
}
|
|
262
266
|
}
|
|
263
267
|
|
|
268
|
+
declare namespace Collections {
|
|
269
|
+
export {
|
|
270
|
+
Utils,
|
|
271
|
+
KeyValueEntry,
|
|
272
|
+
ResultMapResultDetail,
|
|
273
|
+
ResultMapForEachCb,
|
|
274
|
+
IReadOnlyResultMap,
|
|
275
|
+
IResultMapConstructorParams,
|
|
276
|
+
ResultMapValueFactory,
|
|
277
|
+
ResultMap,
|
|
278
|
+
IReadOnlyResultMapValidator,
|
|
279
|
+
IResultMapValidatorCreateParams,
|
|
280
|
+
ResultMapValidator,
|
|
281
|
+
IReadOnlyValidatingResultMap,
|
|
282
|
+
IValidatingResultMapConstructorParams,
|
|
283
|
+
ValidatingResultMap
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
export { Collections }
|
|
287
|
+
|
|
264
288
|
/**
|
|
265
289
|
* A {@link Validation.Constraint | Constraint<T>} function returns
|
|
266
290
|
* `true` if the supplied value meets the constraint. Can return
|
|
@@ -296,6 +320,7 @@ declare namespace Conversion {
|
|
|
296
320
|
BaseConverter,
|
|
297
321
|
OnError,
|
|
298
322
|
ConverterTraits,
|
|
323
|
+
ConversionErrorFormatter,
|
|
299
324
|
ConstraintOptions,
|
|
300
325
|
Converter,
|
|
301
326
|
DefaultingConverter,
|
|
@@ -309,6 +334,16 @@ declare namespace Conversion {
|
|
|
309
334
|
}
|
|
310
335
|
export { Conversion }
|
|
311
336
|
|
|
337
|
+
/**
|
|
338
|
+
* Formats an incoming error message and value that failed validation.
|
|
339
|
+
* @param val - The value that failed validation.
|
|
340
|
+
* @param message - The default error message, if any.
|
|
341
|
+
* @param context - Optional validation context.
|
|
342
|
+
* @returns The formatted error message.
|
|
343
|
+
* @public
|
|
344
|
+
*/
|
|
345
|
+
declare type ConversionErrorFormatter<TC = unknown> = (val: unknown, message?: string, context?: TC) => string;
|
|
346
|
+
|
|
312
347
|
/**
|
|
313
348
|
* Deprecated name for Infer<T> retained for compatibility
|
|
314
349
|
* @deprecated use @see Infer instead
|
|
@@ -322,7 +357,7 @@ declare type ConvertedToType<TCONV> = Infer<TCONV>;
|
|
|
322
357
|
* of optional templated type `<TC>` (default `undefined`).
|
|
323
358
|
* @public
|
|
324
359
|
*/
|
|
325
|
-
export declare interface Converter<T, TC =
|
|
360
|
+
export declare interface Converter<T, TC = unknown> extends ConverterTraits {
|
|
326
361
|
/**
|
|
327
362
|
* Indicates whether this element is explicitly optional.
|
|
328
363
|
*/
|
|
@@ -390,7 +425,7 @@ export declare interface Converter<T, TC = undefined> extends ConverterTraits {
|
|
|
390
425
|
* converted result from this {@link Converter}.
|
|
391
426
|
* @returns A new {@link Converter} returning `<T2>`.
|
|
392
427
|
*/
|
|
393
|
-
mapConvert<T2>(mapConverter: Converter<T2>): Converter<T2, TC>;
|
|
428
|
+
mapConvert<T2>(mapConverter: Converter<T2, unknown>): Converter<T2, TC>;
|
|
394
429
|
/**
|
|
395
430
|
* Creates a {@link Converter} which maps the individual items of a collection
|
|
396
431
|
* resulting from this {@link Converter} using the supplied map function.
|
|
@@ -454,6 +489,13 @@ export declare interface Converter<T, TC = undefined> extends ConverterTraits {
|
|
|
454
489
|
* @returns A new {@link Converter} returning `<T>`.
|
|
455
490
|
*/
|
|
456
491
|
withConstraint(constraint: (val: T) => boolean | Result<T>, options?: ConstraintOptions): Converter<T, TC>;
|
|
492
|
+
/**
|
|
493
|
+
* Creates a new {@link Converter} which is derived from this one but which returns an
|
|
494
|
+
* error message formatted by the supplied formatter if the conversion fails.
|
|
495
|
+
* @param formatter - The formatter to be applied.
|
|
496
|
+
* @returns A new {@link Converter} returning `<T>`.
|
|
497
|
+
*/
|
|
498
|
+
withFormattedError(formatter: ConversionErrorFormatter<TC>): Converter<T, TC>;
|
|
457
499
|
/**
|
|
458
500
|
* returns a converter which adds a brand to the type to prevent mismatched usage
|
|
459
501
|
* of simple types.
|
|
@@ -530,7 +572,7 @@ declare class Crc32Normalizer extends HashingNormalizer {
|
|
|
530
572
|
/**
|
|
531
573
|
* @public
|
|
532
574
|
*/
|
|
533
|
-
declare interface DefaultingConverter<T, TD = T, TC =
|
|
575
|
+
declare interface DefaultingConverter<T, TD = T, TC = unknown> extends Converter<T | TD, TC> {
|
|
534
576
|
/**
|
|
535
577
|
* Default value to use if the conversion fails.
|
|
536
578
|
*/
|
|
@@ -719,10 +761,10 @@ declare type DiscriminatedObjectConverters<T, TD extends string = string, TC = u
|
|
|
719
761
|
* in the supplied array and can be converted. Returns {@link Failure | Failure} with an error message otherwise.
|
|
720
762
|
* @param index - The index of the element to be extracted.
|
|
721
763
|
* @param converter - A {@link Converter | Converter} or {@link Validator | Validator} for the extracted element.
|
|
722
|
-
* @returns A {@link Converter | Converter<T>} which extracts the specified element from an array.
|
|
764
|
+
* @returns A {@link Converter | Converter<T, TC>} which extracts the specified element from an array.
|
|
723
765
|
* @public
|
|
724
766
|
*/
|
|
725
|
-
declare function element<T, TC =
|
|
767
|
+
declare function element<T, TC = unknown>(index: number, converter: Converter<T, TC> | Validator<T, TC>): Converter<T, TC>;
|
|
726
768
|
|
|
727
769
|
/**
|
|
728
770
|
* @internal
|
|
@@ -866,7 +908,7 @@ export declare function failWithDetail<T, TD>(message: string, detail: TD): Deta
|
|
|
866
908
|
* field.
|
|
867
909
|
* @public
|
|
868
910
|
*/
|
|
869
|
-
declare function field<T, TC =
|
|
911
|
+
declare function field<T, TC = unknown>(name: string, converter: Converter<T, TC> | Validator<T, TC>): Converter<T, TC>;
|
|
870
912
|
|
|
871
913
|
/**
|
|
872
914
|
* Per-property converters or validators for each of the properties in type T.
|
|
@@ -933,7 +975,7 @@ declare interface FunctionConstraintTrait {
|
|
|
933
975
|
* to substitute a supplied default value for any errors returned by the inner converter.
|
|
934
976
|
* @public
|
|
935
977
|
*/
|
|
936
|
-
declare class GenericDefaultingConverter<T, TD = T, TC =
|
|
978
|
+
declare class GenericDefaultingConverter<T, TD = T, TC = unknown> implements DefaultingConverter<T, TD, TC> {
|
|
937
979
|
private _converter;
|
|
938
980
|
/**
|
|
939
981
|
* {@inheritdoc Conversion.DefaultingConverter.defaultValue}
|
|
@@ -1001,6 +1043,10 @@ declare class GenericDefaultingConverter<T, TD = T, TC = undefined> implements D
|
|
|
1001
1043
|
* {@inheritdoc Converter.withBrand}
|
|
1002
1044
|
*/
|
|
1003
1045
|
withBrand<B extends string>(brand: B): Converter<Brand<T | TD, B>, TC>;
|
|
1046
|
+
/**
|
|
1047
|
+
* {@inheritdoc Converter.withFormattedError}
|
|
1048
|
+
*/
|
|
1049
|
+
withFormattedError(formatter: ConversionErrorFormatter<TC>): Converter<T | TD, TC>;
|
|
1004
1050
|
/**
|
|
1005
1051
|
* Returns a Converter which always succeeds with the supplied default value rather
|
|
1006
1052
|
* than failing.
|
|
@@ -1016,7 +1062,7 @@ declare class GenericDefaultingConverter<T, TD = T, TC = undefined> implements D
|
|
|
1016
1062
|
* Generic base implementation for an in-place {@link Validation.Validator | Validator}.
|
|
1017
1063
|
* @public
|
|
1018
1064
|
*/
|
|
1019
|
-
declare class GenericValidator<T, TC =
|
|
1065
|
+
declare class GenericValidator<T, TC = unknown> implements Validator<T, TC> {
|
|
1020
1066
|
/**
|
|
1021
1067
|
* {@inheritdoc Validation.Validator.traits}
|
|
1022
1068
|
*/
|
|
@@ -1071,6 +1117,10 @@ declare class GenericValidator<T, TC = undefined> implements Validator<T, TC> {
|
|
|
1071
1117
|
* {@inheritdoc Validation.Validator.brand}
|
|
1072
1118
|
*/
|
|
1073
1119
|
withBrand<B extends string>(brand: B): Validator<Brand<T, B>, TC>;
|
|
1120
|
+
/**
|
|
1121
|
+
* {@inheritdoc Validation.Validator.withFormattedError}
|
|
1122
|
+
*/
|
|
1123
|
+
withFormattedError(formatter: ValidationErrorFormatter<TC>): Validator<T, TC>;
|
|
1074
1124
|
/**
|
|
1075
1125
|
* Gets a default or explicit context.
|
|
1076
1126
|
* @param explicitContext - Optional explicit context.
|
|
@@ -1189,7 +1239,7 @@ export declare interface IMessageAggregator {
|
|
|
1189
1239
|
* @example `Infer<typeof Converters.mapOf(Converters.stringArray)>` is `Map<string, string[]>`
|
|
1190
1240
|
* @beta
|
|
1191
1241
|
*/
|
|
1192
|
-
declare type Infer<TCONV> = TCONV extends Converter<infer TTO> ? InnerInferredType<TTO> : never;
|
|
1242
|
+
declare type Infer<TCONV> = TCONV extends Converter<infer TTO, unknown> ? InnerInferredType<TTO> : never;
|
|
1193
1243
|
|
|
1194
1244
|
/**
|
|
1195
1245
|
* @public
|
|
@@ -1210,6 +1260,86 @@ declare class InMemoryLogger extends LoggerBase {
|
|
|
1210
1260
|
*/
|
|
1211
1261
|
declare type InnerInferredType<TCONV> = TCONV extends Converter<infer TTO> ? TTO extends Array<infer TTOELEM> ? InnerInferredType<TTOELEM>[] : TTO : TCONV extends Array<infer TELEM> ? InnerInferredType<TELEM>[] : TCONV;
|
|
1212
1262
|
|
|
1263
|
+
/**
|
|
1264
|
+
* A readonly `ReadonlyMap<TK, TV>`-like object which reports success or failure
|
|
1265
|
+
* with additional details using the
|
|
1266
|
+
* {@link https://github.com/ErikFortune/fgv/tree/main/libraries/ts-utils#the-result-pattern | result pattern}.
|
|
1267
|
+
* @public
|
|
1268
|
+
*/
|
|
1269
|
+
export declare interface IReadOnlyResultMap<TK extends string = string, TV = unknown> {
|
|
1270
|
+
/**
|
|
1271
|
+
* {@inheritdoc Collections.ResultMap.inner}
|
|
1272
|
+
*/
|
|
1273
|
+
readonly inner: ReadonlyMap<TK, TV>;
|
|
1274
|
+
/**
|
|
1275
|
+
* {@inheritdoc Collections.ResultMap.size}
|
|
1276
|
+
*/
|
|
1277
|
+
readonly size: number;
|
|
1278
|
+
/**
|
|
1279
|
+
* {@inheritdoc Collections.ResultMap.entries}
|
|
1280
|
+
*/
|
|
1281
|
+
entries(): MapIterator<KeyValueEntry<TK, TV>>;
|
|
1282
|
+
/**
|
|
1283
|
+
* {@inheritdoc Collections.ResultMap.forEach}
|
|
1284
|
+
*/
|
|
1285
|
+
forEach(cb: ResultMapForEachCb, arg?: unknown): void;
|
|
1286
|
+
/**
|
|
1287
|
+
* {@inheritdoc Collections.ResultMap.get}
|
|
1288
|
+
*/
|
|
1289
|
+
get(key: TK): DetailedResult<TV, ResultMapResultDetail>;
|
|
1290
|
+
/**
|
|
1291
|
+
* {@inheritdoc Collections.ResultMap.has}
|
|
1292
|
+
*/
|
|
1293
|
+
has(key: TK): boolean;
|
|
1294
|
+
/**
|
|
1295
|
+
* {@inheritdoc Collections.ResultMap.keys}
|
|
1296
|
+
*/
|
|
1297
|
+
keys(): MapIterator<TK>;
|
|
1298
|
+
/**
|
|
1299
|
+
* {@inheritdoc Collections.ResultMap.values}
|
|
1300
|
+
*/
|
|
1301
|
+
values(): MapIterator<TV>;
|
|
1302
|
+
/**
|
|
1303
|
+
* Gets an iterator over the map entries.
|
|
1304
|
+
* @returns An iterator over the map entries.
|
|
1305
|
+
*/
|
|
1306
|
+
[Symbol.iterator](): IterableIterator<KeyValueEntry<TK, TV>>;
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
/**
|
|
1310
|
+
* A read-only interface exposing non-mutating methods of a {@link Collections.ResultMapValidator | ResultMapValidator}.
|
|
1311
|
+
* @public
|
|
1312
|
+
*/
|
|
1313
|
+
declare interface IReadOnlyResultMapValidator<TK extends string = string, TV = unknown> {
|
|
1314
|
+
/**
|
|
1315
|
+
* {@inheritdoc Collections.ResultMapValidator.validators}
|
|
1316
|
+
*/
|
|
1317
|
+
readonly validators: KeyValueValidators<TK, TV>;
|
|
1318
|
+
/**
|
|
1319
|
+
* {@inheritdoc Collections.ResultMapValidator.map}
|
|
1320
|
+
*/
|
|
1321
|
+
readonly map: IReadOnlyResultMap<TK, TV>;
|
|
1322
|
+
/**
|
|
1323
|
+
* {@inheritdoc Collections.ResultMap.get}
|
|
1324
|
+
*/
|
|
1325
|
+
get(key: string): DetailedResult<TV, ResultMapResultDetail>;
|
|
1326
|
+
/**
|
|
1327
|
+
* {@inheritdoc Collections.ResultMap.has}
|
|
1328
|
+
*/
|
|
1329
|
+
has(key: string): boolean;
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
/**
|
|
1333
|
+
* A read-only interface exposing non-mutating methods of a {@link Collections.ValidatingResultMap | ValidatingResultMap}.
|
|
1334
|
+
* @public
|
|
1335
|
+
*/
|
|
1336
|
+
declare interface IReadOnlyValidatingResultMap<TK extends string = string, TV = unknown> extends IReadOnlyResultMap<TK, TV> {
|
|
1337
|
+
/**
|
|
1338
|
+
* {@inheritdoc Collections.ValidatingResultMap.validate}
|
|
1339
|
+
*/
|
|
1340
|
+
readonly validate: IReadOnlyResultMapValidator<TK, TV>;
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1213
1343
|
/**
|
|
1214
1344
|
* Represents the result of some operation of sequence of operations.
|
|
1215
1345
|
* @remarks
|
|
@@ -1352,6 +1482,23 @@ export declare interface IResultLogger {
|
|
|
1352
1482
|
error(message: string): void;
|
|
1353
1483
|
}
|
|
1354
1484
|
|
|
1485
|
+
/**
|
|
1486
|
+
* Parameters for constructing a {@link Collections.ResultMap | ResultMap}.
|
|
1487
|
+
* @public
|
|
1488
|
+
*/
|
|
1489
|
+
declare interface IResultMapConstructorParams<TK extends string = string, TV = unknown> {
|
|
1490
|
+
entries?: Iterable<KeyValueEntry<TK, TV>>;
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
/**
|
|
1494
|
+
* Parameters for constructing a {@link Collections.ResultMapValidator | ResultMapValidator}.
|
|
1495
|
+
* @public
|
|
1496
|
+
*/
|
|
1497
|
+
declare interface IResultMapValidatorCreateParams<TK extends string = string, TV = unknown> {
|
|
1498
|
+
map: ResultMap<TK, TV>;
|
|
1499
|
+
validators: KeyValueValidators<TK, TV>;
|
|
1500
|
+
}
|
|
1501
|
+
|
|
1355
1502
|
/**
|
|
1356
1503
|
* Helper function to create a {@link Converter | Converter} from a supplied type guard function.
|
|
1357
1504
|
* @param description - a description of the thing to be validated for use in error messages
|
|
@@ -1373,6 +1520,14 @@ declare function isA<T, TC = unknown>(description: string, guard: TypeGuardWithC
|
|
|
1373
1520
|
*/
|
|
1374
1521
|
declare function isA_2<T, TC>(description: string, guard: TypeGuardWithContext<T, TC>, params?: Omit<TypeGuardValidatorConstructorParams<T, TC>, 'description' | 'guard'>): TypeGuardValidator<T, TC>;
|
|
1375
1522
|
|
|
1523
|
+
/**
|
|
1524
|
+
* Determines if a supplied value is an iterable object or some other type.
|
|
1525
|
+
* @param value - The value to be tested.
|
|
1526
|
+
* @returns `true` if the value is an iterable object, `false` otherwise.
|
|
1527
|
+
* @public
|
|
1528
|
+
*/
|
|
1529
|
+
declare function isIterable<TE = unknown, TI extends Iterable<TE> = Iterable<TE>, TO = unknown>(value: TI | TO): value is TI;
|
|
1530
|
+
|
|
1376
1531
|
/**
|
|
1377
1532
|
* Helper type-guard function to report whether a specified key is present in
|
|
1378
1533
|
* a supplied object.
|
|
@@ -1383,13 +1538,22 @@ declare function isA_2<T, TC>(description: string, guard: TypeGuardWithContext<T
|
|
|
1383
1538
|
*/
|
|
1384
1539
|
export declare function isKeyOf<T extends object>(key: string | number | symbol, item: T): key is keyof T;
|
|
1385
1540
|
|
|
1541
|
+
/**
|
|
1542
|
+
* Parameters for constructing a {@link Collections.ResultMap | ResultMap}.
|
|
1543
|
+
* @public
|
|
1544
|
+
*/
|
|
1545
|
+
declare interface IValidatingResultMapConstructorParams<TK extends string = string, TV = unknown> {
|
|
1546
|
+
entries?: Iterable<KeyValueEntry<string, unknown>>;
|
|
1547
|
+
validators: KeyValueValidators<TK, TV>;
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1386
1550
|
/**
|
|
1387
1551
|
* Options for {@link Converters.(recordOf:3) | Converters.recordOf} and
|
|
1388
1552
|
* {@link Converters.(mapOf:3) | Converters.mapOf}
|
|
1389
1553
|
* helper functions.
|
|
1390
1554
|
* @public
|
|
1391
1555
|
*/
|
|
1392
|
-
declare interface KeyedConverterOptions<T extends string = string, TC =
|
|
1556
|
+
declare interface KeyedConverterOptions<T extends string = string, TC = unknown> {
|
|
1393
1557
|
/**
|
|
1394
1558
|
* if `onError` is `'fail'` (default), then the entire conversion fails if any key or element
|
|
1395
1559
|
* cannot be converted. If `onError` is `'ignore'`, failing elements are silently ignored.
|
|
@@ -1410,6 +1574,69 @@ declare interface KeyedConverterOptions<T extends string = string, TC = undefine
|
|
|
1410
1574
|
*/
|
|
1411
1575
|
declare type KeyedThingFactory<TS, TD, TK extends string = string> = (key: TK, thing: TS) => Result<TD>;
|
|
1412
1576
|
|
|
1577
|
+
/**
|
|
1578
|
+
* Generic key-value entry.
|
|
1579
|
+
* @public
|
|
1580
|
+
*/
|
|
1581
|
+
declare type KeyValueEntry<TK extends string = string, TV = unknown> = [TK, TV];
|
|
1582
|
+
|
|
1583
|
+
/**
|
|
1584
|
+
* Helper class for validating key-value pairs.
|
|
1585
|
+
* @public
|
|
1586
|
+
*/
|
|
1587
|
+
declare class KeyValueValidators<TK extends string = string, TV = unknown> {
|
|
1588
|
+
/**
|
|
1589
|
+
* Required key validator or converter.
|
|
1590
|
+
*/
|
|
1591
|
+
readonly key: Validator<TK, unknown> | Converter<TK, unknown>;
|
|
1592
|
+
/**
|
|
1593
|
+
* Required value validator or converter.
|
|
1594
|
+
*/
|
|
1595
|
+
readonly value: Validator<TV, unknown> | Converter<TV, unknown>;
|
|
1596
|
+
/**
|
|
1597
|
+
* Optional entry validator or converter. If no entry validator is provided,
|
|
1598
|
+
* an entry is considered valid if both key and value are valid.
|
|
1599
|
+
*/
|
|
1600
|
+
readonly entry?: Validator<KeyValueEntry<TK, TV>, unknown> | Converter<KeyValueEntry<TK, TV>, unknown>;
|
|
1601
|
+
/**
|
|
1602
|
+
* Constructs a new key-value validator.
|
|
1603
|
+
* @param key - Required key validator or converter.
|
|
1604
|
+
* @param value - Required value validator or converter.
|
|
1605
|
+
* @param entry - Optional entry validator or converter. If no entry validator is provided,
|
|
1606
|
+
* an entry is considered valid if both key and value are valid.
|
|
1607
|
+
*/
|
|
1608
|
+
constructor(key: Validator<TK, unknown> | Converter<TK, unknown>, value: Validator<TV, unknown> | Converter<TV, unknown>, entry?: Validator<KeyValueEntry<TK, TV>, unknown> | Converter<KeyValueEntry<TK, TV>, unknown>);
|
|
1609
|
+
/**
|
|
1610
|
+
* Validates a supplied unknown as a valid key value.
|
|
1611
|
+
* @param key - The unknown to be validated.
|
|
1612
|
+
* @returns `Success` with the validated key value and 'success' detail if the key is valid,
|
|
1613
|
+
* or `Failure` with an error message and 'invalid-key' detail if the key is invalid.
|
|
1614
|
+
*/
|
|
1615
|
+
validateKey(key: unknown): DetailedResult<TK, ResultMapResultDetail>;
|
|
1616
|
+
/**
|
|
1617
|
+
* Validates a supplied unknown as a valid value.
|
|
1618
|
+
* @param key - The unknown to be validated.
|
|
1619
|
+
* @returns `Success` with the validated value and 'success' detail if the value is valid,
|
|
1620
|
+
* or `Failure` with an error message and 'invalid-value' detail if the value is invalid.
|
|
1621
|
+
*/
|
|
1622
|
+
validateValue(key: unknown): DetailedResult<TV, ResultMapResultDetail>;
|
|
1623
|
+
/**
|
|
1624
|
+
* Validates a supplied unknown as a valid key-value pair.
|
|
1625
|
+
* @param entry - The unknown to be validated.
|
|
1626
|
+
* @returns `Success` with the validated key-value pair and 'success' detail if the entry
|
|
1627
|
+
* is valid, or `Failure` with an error message and 'invalid-key' or 'invalid-value' detail if
|
|
1628
|
+
* the entry is invalid
|
|
1629
|
+
*/
|
|
1630
|
+
validateEntry(entry: unknown): DetailedResult<KeyValueEntry<TK, TV>, ResultMapResultDetail>;
|
|
1631
|
+
/**
|
|
1632
|
+
* Validates a supplied iterable of unknowns as valid key-value pairs.
|
|
1633
|
+
* @param entries - The iterable of unknowns to be validated.
|
|
1634
|
+
* @returns `Success` with an array of validated key-value pairs if all entries are valid,
|
|
1635
|
+
* or `Failure` with an error message if any entry is invalid.
|
|
1636
|
+
*/
|
|
1637
|
+
validateEntries(entries: Iterable<unknown>): Result<KeyValueEntry<TK, TV>[]>;
|
|
1638
|
+
}
|
|
1639
|
+
|
|
1413
1640
|
/**
|
|
1414
1641
|
* Helper function to create a {@link Converter | Converter} which converts `unknown` to some supplied literal value. Succeeds with
|
|
1415
1642
|
* the supplied value if an identity comparison succeeds, fails otherwise.
|
|
@@ -1417,7 +1644,7 @@ declare type KeyedThingFactory<TS, TD, TK extends string = string> = (key: TK, t
|
|
|
1417
1644
|
* @returns A {@link Converter | Converter} which returns the supplied value on success.
|
|
1418
1645
|
* @public
|
|
1419
1646
|
*/
|
|
1420
|
-
declare function literal<T>(value: T): Converter<T,
|
|
1647
|
+
declare function literal<T, TC = unknown>(value: T): Converter<T, TC>;
|
|
1421
1648
|
|
|
1422
1649
|
/**
|
|
1423
1650
|
* Helper function to create a {@link Validation.Validator} which validates a literal value.
|
|
@@ -1501,7 +1728,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1501
1728
|
|
|
1502
1729
|
/**
|
|
1503
1730
|
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties
|
|
1504
|
-
* using a supplied {@link Converter | Converter<T>} or {@link Validator | Validator<T>} to produce a
|
|
1731
|
+
* using a supplied {@link Converter | Converter<T, TC>} or {@link Validator | Validator<T>} to produce a
|
|
1505
1732
|
* `Map<string, T>`.
|
|
1506
1733
|
* @remarks
|
|
1507
1734
|
* The resulting converter fails conversion if any element cannot be converted.
|
|
@@ -1511,11 +1738,11 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1511
1738
|
* {@label WITH_DEFAULT}
|
|
1512
1739
|
* @public
|
|
1513
1740
|
*/
|
|
1514
|
-
declare function mapOf<T, TC =
|
|
1741
|
+
declare function mapOf<T, TC = unknown, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>): Converter<Map<TK, T>, TC>;
|
|
1515
1742
|
|
|
1516
1743
|
/**
|
|
1517
1744
|
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties
|
|
1518
|
-
* using a supplied {@link Converter | Converter<T>} or {@link Validator | Validator<T>} to produce a
|
|
1745
|
+
* using a supplied {@link Converter | Converter<T, TC>} or {@link Validator | Validator<T>} to produce a
|
|
1519
1746
|
* `Map<string, T>` and specified handling of elements that cannot be converted.
|
|
1520
1747
|
* @remarks
|
|
1521
1748
|
* if `onError` is `'fail'` (default), then the entire conversion fails if any key or element
|
|
@@ -1526,11 +1753,11 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1526
1753
|
* {@label WITH_ON_ERROR}
|
|
1527
1754
|
* @public
|
|
1528
1755
|
*/
|
|
1529
|
-
declare function mapOf<T, TC =
|
|
1756
|
+
declare function mapOf<T, TC = unknown, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>, onError: 'fail' | 'ignore'): Converter<Map<TK, T>, TC>;
|
|
1530
1757
|
|
|
1531
1758
|
/**
|
|
1532
1759
|
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties
|
|
1533
|
-
* using a supplied {@link Converter | Converter<T>} or {@link Validator | Validator<T>} to produce
|
|
1760
|
+
* using a supplied {@link Converter | Converter<T, TC>} or {@link Validator | Validator<T>} to produce
|
|
1534
1761
|
* a `Map<TK,T>`.
|
|
1535
1762
|
* @remarks
|
|
1536
1763
|
* If present, the supplied {@link Converters.KeyedConverterOptions | options} can provide a strongly-typed
|
|
@@ -1543,7 +1770,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1543
1770
|
* {@label WITH_OPTIONS}
|
|
1544
1771
|
* @public
|
|
1545
1772
|
*/
|
|
1546
|
-
declare function mapOf<T, TC =
|
|
1773
|
+
declare function mapOf<T, TC = unknown, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>, options: KeyedConverterOptions<TK, TC>): Converter<Map<TK, T>, TC>;
|
|
1547
1774
|
|
|
1548
1775
|
/**
|
|
1549
1776
|
* Helper function to create a {@link Converter | Converter} which converts `unknown` to one of a set of supplied enumerated
|
|
@@ -1558,7 +1785,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1558
1785
|
* @returns A {@link Converter | Converter} which applies the mapping and yields `<T>` on success.
|
|
1559
1786
|
* @public
|
|
1560
1787
|
*/
|
|
1561
|
-
declare function mappedEnumeratedValue<T>(map: [T, unknown[]][], message?: string): Converter<T,
|
|
1788
|
+
declare function mappedEnumeratedValue<T, TC = unknown>(map: [T, unknown[]][], message?: string): Converter<T, TC>;
|
|
1562
1789
|
|
|
1563
1790
|
/**
|
|
1564
1791
|
* Aggregates successful result values from a collection of {@link Result | Result<T>}.
|
|
@@ -1701,7 +1928,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1701
1928
|
* Numbers and strings with a numeric format succeed. Anything else fails.
|
|
1702
1929
|
* @public
|
|
1703
1930
|
*/
|
|
1704
|
-
declare const number: Converter<number,
|
|
1931
|
+
declare const number: Converter<number, unknown>;
|
|
1705
1932
|
|
|
1706
1933
|
/**
|
|
1707
1934
|
* A {@link Validation.Classes.NumberValidator | NumberValidator} which validates a number in place.
|
|
@@ -1716,7 +1943,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1716
1943
|
* of numbers, returns {@link Failure | Failure} with an error message otherwise.
|
|
1717
1944
|
* @public
|
|
1718
1945
|
*/
|
|
1719
|
-
declare const numberArray: Converter<number[]>;
|
|
1946
|
+
declare const numberArray: Converter<number[], unknown>;
|
|
1720
1947
|
|
|
1721
1948
|
/**
|
|
1722
1949
|
* An in-place {@link Validation.Validator | Validator} for `number` values.
|
|
@@ -1745,8 +1972,8 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1745
1972
|
declare type NumberValidatorConstructorParams<T extends number = number, TC = unknown> = GenericValidatorConstructorParams<T, TC>;
|
|
1746
1973
|
|
|
1747
1974
|
/**
|
|
1748
|
-
* Helper function to create a {@link Conversion.ObjectConverter | ObjectConverter<T>} which converts an object
|
|
1749
|
-
* without changing shape, given a {@link Conversion.FieldConverters | FieldConverters<T>} and an optional
|
|
1975
|
+
* Helper function to create a {@link Conversion.ObjectConverter | ObjectConverter<T, TC>} which converts an object
|
|
1976
|
+
* without changing shape, given a {@link Conversion.FieldConverters | FieldConverters<T, TC>} and an optional
|
|
1750
1977
|
* {@link Conversion.ObjectConverterOptions | ObjectConverterOptions<T>} to further refine conversion behavior.
|
|
1751
1978
|
* @remarks
|
|
1752
1979
|
* By default, if all of the requested fields exist and can be converted, returns {@link Success | Success}
|
|
@@ -1756,7 +1983,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1756
1983
|
*
|
|
1757
1984
|
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
1758
1985
|
* fail the conversion.
|
|
1759
|
-
* @param properties - An {@link Conversion.FieldConverters | FieldConverters<T>} defining the shape of the
|
|
1986
|
+
* @param properties - An {@link Conversion.FieldConverters | FieldConverters<T, TC>} defining the shape of the
|
|
1760
1987
|
* source object and {@link Converter | converters} to be applied to each properties.
|
|
1761
1988
|
* @param options - An {@link Conversion.ObjectConverterOptions | ObjectConverterOptions<T>} containing options
|
|
1762
1989
|
* for the object converter.
|
|
@@ -1764,11 +1991,11 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1764
1991
|
* {@label WITH_OPTIONS}
|
|
1765
1992
|
* @public
|
|
1766
1993
|
*/
|
|
1767
|
-
declare function object<T>(properties: FieldConverters<T>, options?: ObjectConverterOptions<T>): ObjectConverter<T>;
|
|
1994
|
+
declare function object<T, TC = unknown>(properties: FieldConverters<T, TC>, options?: ObjectConverterOptions<T>): ObjectConverter<T, TC>;
|
|
1768
1995
|
|
|
1769
1996
|
/**
|
|
1770
|
-
* Helper function to create a {@link Conversion.ObjectConverter | ObjectConverter<T>} which converts an object
|
|
1771
|
-
* without changing shape, given a {@link Conversion.FieldConverters | FieldConverters<T>} and a set of
|
|
1997
|
+
* Helper function to create a {@link Conversion.ObjectConverter | ObjectConverter<T, TC>} which converts an object
|
|
1998
|
+
* without changing shape, given a {@link Conversion.FieldConverters | FieldConverters<T, TC>} and a set of
|
|
1772
1999
|
* optional properties.
|
|
1773
2000
|
* @remarks
|
|
1774
2001
|
* By default, if all of the requested fields exist and can be converted, returns {@link Success | Success}
|
|
@@ -1778,7 +2005,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1778
2005
|
*
|
|
1779
2006
|
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
1780
2007
|
* fail the conversion.
|
|
1781
|
-
* @param properties - An {@link Conversion.FieldConverters | FieldConverters<T>} defining the shape of the
|
|
2008
|
+
* @param properties - An {@link Conversion.FieldConverters | FieldConverters<T, TC>} defining the shape of the
|
|
1782
2009
|
* source object and {@link Converter | converters} to be applied to each properties.
|
|
1783
2010
|
* @param optional - An array of `(keyof T)` listing the keys to be considered optional.
|
|
1784
2011
|
* {@label WITH_KEYS}
|
|
@@ -1786,7 +2013,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1786
2013
|
* @public
|
|
1787
2014
|
* @deprecated Use {@link Converters.(object:1) | Converters.object(fields, options)} instead.
|
|
1788
2015
|
*/
|
|
1789
|
-
declare function object<T>(properties: FieldConverters<T>, optional: (keyof T)[]): ObjectConverter<T>;
|
|
2016
|
+
declare function object<T, TC = unknown>(properties: FieldConverters<T, TC>, optional: (keyof T)[]): ObjectConverter<T, TC>;
|
|
1790
2017
|
|
|
1791
2018
|
/**
|
|
1792
2019
|
* Helper function to create a {@link Validation.Classes.ObjectValidator | ObjectValidator} which validates
|
|
@@ -1815,13 +2042,13 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1815
2042
|
/**
|
|
1816
2043
|
* Fields converted by this {@link Conversion.ObjectConverter | ObjectConverter}.
|
|
1817
2044
|
*/
|
|
1818
|
-
readonly fields: FieldConverters<T>;
|
|
2045
|
+
readonly fields: FieldConverters<T, TC>;
|
|
1819
2046
|
/**
|
|
1820
2047
|
* Options used to initialize this {@link Conversion.ObjectConverter | ObjectConverter}.
|
|
1821
2048
|
*/
|
|
1822
2049
|
readonly options: ObjectConverterOptions<T>;
|
|
1823
2050
|
/**
|
|
1824
|
-
* Constructs a new {@link Conversion.ObjectConverter | ObjectConverter<T>} using options
|
|
2051
|
+
* Constructs a new {@link Conversion.ObjectConverter | ObjectConverter<T, TC>} using options
|
|
1825
2052
|
* supplied in a {@link Conversion.ObjectConverterOptions | ObjectConverterOptions<T>}.
|
|
1826
2053
|
* @param fields - A {@link Conversion.FieldConverters | FieldConverters<T>} containing
|
|
1827
2054
|
* a {@link Converter} for each field
|
|
@@ -1830,7 +2057,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
1830
2057
|
*/
|
|
1831
2058
|
constructor(fields: FieldConverters<T, TC>, options?: ObjectConverterOptions<T>);
|
|
1832
2059
|
/**
|
|
1833
|
-
* Constructs a new {@link Conversion.ObjectConverter | ObjectConverter<T>} with optional
|
|
2060
|
+
* Constructs a new {@link Conversion.ObjectConverter | ObjectConverter<T, TC>} with optional
|
|
1834
2061
|
* properties specified as an array of `keyof T`.
|
|
1835
2062
|
* @param fields - A {@link Conversion.FieldConverters | FieldConverters<T>} containing
|
|
1836
2063
|
* a {@link Converter} for each field.
|
|
@@ -2086,7 +2313,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2086
2313
|
* with value `undefined`.
|
|
2087
2314
|
* @public
|
|
2088
2315
|
*/
|
|
2089
|
-
declare const optionalBoolean: Converter<boolean | undefined>;
|
|
2316
|
+
declare const optionalBoolean: Converter<boolean | undefined, unknown>;
|
|
2090
2317
|
|
|
2091
2318
|
/**
|
|
2092
2319
|
* A helper function to create a {@link Converter | Converter} which extracts and converts an optional element from an array.
|
|
@@ -2097,10 +2324,10 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2097
2324
|
* is not an array, if the requested index is negative, or if the element cannot be converted.
|
|
2098
2325
|
* @param index - The index of the element to be extracted.
|
|
2099
2326
|
* @param converter - A {@link Converter | Converter} or {@link Validator | Validator} used for the extracted element.
|
|
2100
|
-
* @returns A {@link Converter | Converter<T>} which extracts the specified element from an array.
|
|
2327
|
+
* @returns A {@link Converter | Converter<T, TC>} which extracts the specified element from an array.
|
|
2101
2328
|
* @public
|
|
2102
2329
|
*/
|
|
2103
|
-
declare function optionalElement<T, TC =
|
|
2330
|
+
declare function optionalElement<T, TC = unknown>(index: number, converter: Converter<T, TC> | Validator<T, TC>): Converter<T | undefined, TC>;
|
|
2104
2331
|
|
|
2105
2332
|
/**
|
|
2106
2333
|
* A helper function to create a {@link Converter | Converter} which extracts and convert a property specified
|
|
@@ -2114,7 +2341,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2114
2341
|
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} to use for the extracted field.
|
|
2115
2342
|
* @public
|
|
2116
2343
|
*/
|
|
2117
|
-
declare function optionalField<T, TC =
|
|
2344
|
+
declare function optionalField<T, TC = unknown>(name: string, converter: Converter<T, TC> | Validator<T, TC>): Converter<T | undefined, TC>;
|
|
2118
2345
|
|
|
2119
2346
|
/**
|
|
2120
2347
|
* Applies a factory method to convert an optional `ReadonlyMap<string, TS>` into a `Record<string, TD>`
|
|
@@ -2143,7 +2370,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2143
2370
|
* Anything else returns {@link Success | Success} with value `undefined`.
|
|
2144
2371
|
* @public
|
|
2145
2372
|
*/
|
|
2146
|
-
declare const optionalNumber: Converter<number | undefined>;
|
|
2373
|
+
declare const optionalNumber: Converter<number | undefined, unknown>;
|
|
2147
2374
|
|
|
2148
2375
|
/**
|
|
2149
2376
|
* Applies a factory method to convert an optional `Record<TK, TS>` into a `Map<TK, TD>`, or `undefined`.
|
|
@@ -2249,7 +2476,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2249
2476
|
|
|
2250
2477
|
/**
|
|
2251
2478
|
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed
|
|
2252
|
-
* properties using a supplied {@link Converter | Converter<T>} or {@link Validator | Validator<T>} to
|
|
2479
|
+
* properties using a supplied {@link Converter | Converter<T, TC>} or {@link Validator | Validator<T>} to
|
|
2253
2480
|
* produce a `Record<string, T>`.
|
|
2254
2481
|
* @remarks
|
|
2255
2482
|
* The resulting converter fails conversion if any element cannot be converted.
|
|
@@ -2259,11 +2486,11 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2259
2486
|
* {@label WITH_DEFAULT}
|
|
2260
2487
|
* @public
|
|
2261
2488
|
*/
|
|
2262
|
-
declare function recordOf<T, TC =
|
|
2489
|
+
declare function recordOf<T, TC = unknown, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>): Converter<Record<TK, T>, TC>;
|
|
2263
2490
|
|
|
2264
2491
|
/**
|
|
2265
2492
|
* A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties
|
|
2266
|
-
* using a supplied {@link Converter | Converter<T>} or {@link Validator | Validator<T>} to produce a
|
|
2493
|
+
* using a supplied {@link Converter | Converter<T, TC>} or {@link Validator | Validator<T>} to produce a
|
|
2267
2494
|
* `Record<string, T>` and optionally specified handling of elements that cannot be converted.
|
|
2268
2495
|
* @remarks
|
|
2269
2496
|
* if `onError` is `'fail'` (default), then the entire conversion fails if any key or element
|
|
@@ -2274,11 +2501,11 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2274
2501
|
* {@label WITH_ON_ERROR}
|
|
2275
2502
|
* @public
|
|
2276
2503
|
*/
|
|
2277
|
-
declare function recordOf<T, TC =
|
|
2504
|
+
declare function recordOf<T, TC = unknown, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>, onError: 'fail' | 'ignore'): Converter<Record<TK, T>, TC>;
|
|
2278
2505
|
|
|
2279
2506
|
/**
|
|
2280
2507
|
* A helper function to create a {@link Converter | Converter} or which converts the `string`-keyed properties
|
|
2281
|
-
* using a supplied {@link Converter | Converter<T>} or {@link Validator | Validator<T>} to produce a
|
|
2508
|
+
* using a supplied {@link Converter | Converter<T, TC>} or {@link Validator | Validator<T>} to produce a
|
|
2282
2509
|
* `Record<TK, T>`.
|
|
2283
2510
|
* @remarks
|
|
2284
2511
|
* If present, the supplied {@link Converters.KeyedConverterOptions | options} can provide a strongly-typed
|
|
@@ -2290,7 +2517,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2290
2517
|
* {@label WITH_OPTIONS}
|
|
2291
2518
|
* @public
|
|
2292
2519
|
*/
|
|
2293
|
-
declare function recordOf<T, TC =
|
|
2520
|
+
declare function recordOf<T, TC = unknown, TK extends string = string>(converter: Converter<T, TC> | Validator<T, TC>, options: KeyedConverterOptions<TK, TC>): Converter<Record<TK, T>, TC>;
|
|
2294
2521
|
|
|
2295
2522
|
/**
|
|
2296
2523
|
* Applies a factory method to convert a `Record<TK, TS>` into a `Map<TK, TD>`.
|
|
@@ -2317,6 +2544,249 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2317
2544
|
*/
|
|
2318
2545
|
export declare type ResultDetailType<T> = T extends DetailedResult<unknown, infer TD> ? TD : never;
|
|
2319
2546
|
|
|
2547
|
+
/**
|
|
2548
|
+
* A {@link Collections.ResultMap | ResultMap} class as a `Map<TK, TV>`-like object which
|
|
2549
|
+
* reports success or failure with additional details using the
|
|
2550
|
+
* {@link https://github.com/ErikFortune/fgv/tree/main/libraries/ts-utils#the-result-pattern | result pattern}.
|
|
2551
|
+
* @public
|
|
2552
|
+
*/
|
|
2553
|
+
export declare class ResultMap<TK extends string = string, TV = unknown> implements IReadOnlyResultMap<TK, TV> {
|
|
2554
|
+
/**
|
|
2555
|
+
* Readonly raw access to the inner `Map<TK, TV>` object.
|
|
2556
|
+
*/
|
|
2557
|
+
get inner(): ReadonlyMap<TK, TV>;
|
|
2558
|
+
/**
|
|
2559
|
+
* Protected raw access to the inner `Map<TK, TV>` object.
|
|
2560
|
+
* @public
|
|
2561
|
+
*/
|
|
2562
|
+
protected readonly _inner: Map<TK, TV>;
|
|
2563
|
+
/**
|
|
2564
|
+
* Constructs a new {@link Collections.ResultMap | ResultMap}.
|
|
2565
|
+
* @param iterable - An iterable to initialize the map.
|
|
2566
|
+
*/
|
|
2567
|
+
constructor(iterable?: Iterable<KeyValueEntry<TK, TV>>);
|
|
2568
|
+
/**
|
|
2569
|
+
* Constructs a new {@link Collections.ResultMap | ResultMap}.
|
|
2570
|
+
* @param params - An optional set of parameters to configure the map.
|
|
2571
|
+
*/
|
|
2572
|
+
constructor(params: IResultMapConstructorParams);
|
|
2573
|
+
/**
|
|
2574
|
+
* Creates a new {@link Collections.ResultMap | ResultMap}.
|
|
2575
|
+
* @param elements - An optional iterable to initialize the map.
|
|
2576
|
+
* @returns `Success` with the new map, or `Failure` with error details
|
|
2577
|
+
* if an error occurred.
|
|
2578
|
+
* @public
|
|
2579
|
+
*/
|
|
2580
|
+
static create<TK extends string = string, TV = unknown>(elements: Iterable<KeyValueEntry<TK, TV>>): Result<ResultMap<TK, TV>>;
|
|
2581
|
+
/**
|
|
2582
|
+
* Creates a new {@link Collections.ResultMap | ResultMap}.
|
|
2583
|
+
* @param params - An optional set of parameters to configure the map.
|
|
2584
|
+
* @returns `Success` with the new map, or `Failure` with error details
|
|
2585
|
+
* if an error occurred.
|
|
2586
|
+
* @public
|
|
2587
|
+
*/
|
|
2588
|
+
static create<TK extends string = string, TV = unknown>(params?: IResultMapConstructorParams<TK, TV>): Result<ResultMap<TK, TV>>;
|
|
2589
|
+
/**
|
|
2590
|
+
* Sets a key/value pair in the map if the key does not already exist.
|
|
2591
|
+
* @param key - The key to set.
|
|
2592
|
+
* @param value - The value to set.
|
|
2593
|
+
* @returns `Success` with the value and detail `added` if the key was added,
|
|
2594
|
+
* `Failure` with detail `exists` if the key already exists. Fails with detail
|
|
2595
|
+
* 'invalid-key' or 'invalid-value' and an error message if either is invalid.
|
|
2596
|
+
*/
|
|
2597
|
+
add(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;
|
|
2598
|
+
/**
|
|
2599
|
+
* Clears the map.
|
|
2600
|
+
*/
|
|
2601
|
+
clear(): void;
|
|
2602
|
+
/**
|
|
2603
|
+
* Deletes a key from the map.
|
|
2604
|
+
* @param key - The key to delete.
|
|
2605
|
+
* @returns `Success` with the previous value and the detail 'deleted'
|
|
2606
|
+
* if the key was found and deleted, `Failure` with detail 'not-found'
|
|
2607
|
+
* if the key was not found, or with detail 'invalid-key' if the key is invalid.
|
|
2608
|
+
*/
|
|
2609
|
+
delete(key: TK): DetailedResult<TV, ResultMapResultDetail>;
|
|
2610
|
+
/**
|
|
2611
|
+
* Returns an iterator over the map entries.
|
|
2612
|
+
* @returns An iterator over the map entries.
|
|
2613
|
+
*/
|
|
2614
|
+
entries(): MapIterator<KeyValueEntry<TK, TV>>;
|
|
2615
|
+
/**
|
|
2616
|
+
* Calls a function for each entry in the map.
|
|
2617
|
+
* @param cb - The function to call for each entry.
|
|
2618
|
+
* @param arg - An optional argument to pass to the callback.
|
|
2619
|
+
*/
|
|
2620
|
+
forEach(cb: ResultMapForEachCb<TK, TV>, arg?: unknown): void;
|
|
2621
|
+
/**
|
|
2622
|
+
* Gets a value from the map.
|
|
2623
|
+
* @param key - The key to retrieve.
|
|
2624
|
+
* @returns `Success` with the value and detail `exists` if the key was found,
|
|
2625
|
+
* `Failure` with detail `not-found` if the key was not found or with detail
|
|
2626
|
+
* `invalid-key` if the key is invalid.
|
|
2627
|
+
*/
|
|
2628
|
+
get(key: TK): DetailedResult<TV, ResultMapResultDetail>;
|
|
2629
|
+
/**
|
|
2630
|
+
* Gets a value from the map, or adds a supplied value it if it does not exist.
|
|
2631
|
+
* @param key - The key to be retrieved or created.
|
|
2632
|
+
* @param value - The value to add if the key does not exist.
|
|
2633
|
+
* @returns `Success` with the value and detail `exists` if the key was found,
|
|
2634
|
+
* `Success` with the value and detail `added` if the key was not found and added.
|
|
2635
|
+
* Fails with detail 'invalid-key' or 'invalid-value' and an error message if either
|
|
2636
|
+
* is invalid.
|
|
2637
|
+
* {@label WITH_VALUE}
|
|
2638
|
+
*/
|
|
2639
|
+
getOrAdd(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;
|
|
2640
|
+
/**
|
|
2641
|
+
* Gets a value from the map, or adds a value created by a factory function if it does not exist.
|
|
2642
|
+
* @param key - The key of the element to be retrieved or created.
|
|
2643
|
+
* @param factory - A {@link Collections.ResultMapValueFactory | factory function} to create the value if
|
|
2644
|
+
* the key does not exist.
|
|
2645
|
+
* @returns `Success` with the value and detail `exists` if the key was found, `Success` with
|
|
2646
|
+
* the value and detail `added` if the key was not found and added. Fails with detail 'invalid-key'
|
|
2647
|
+
* or 'invalid-value' and an error message if either is invalid.
|
|
2648
|
+
* {@label WITH_FACTORY}
|
|
2649
|
+
*/
|
|
2650
|
+
getOrAdd(key: TK, factory: ResultMapValueFactory<TK, TV>): DetailedResult<TV, ResultMapResultDetail>;
|
|
2651
|
+
/**
|
|
2652
|
+
* Returns `true` if the map contains a key.
|
|
2653
|
+
* @param key - The key to check.
|
|
2654
|
+
* @returns `true` if the key exists, `false` otherwise.
|
|
2655
|
+
*/
|
|
2656
|
+
has(key: TK): boolean;
|
|
2657
|
+
/**
|
|
2658
|
+
* Returns an iterator over the map keys.
|
|
2659
|
+
* @returns An iterator over the map keys.
|
|
2660
|
+
*/
|
|
2661
|
+
keys(): MapIterator<TK>;
|
|
2662
|
+
/**
|
|
2663
|
+
* Sets a key/value pair in the map.
|
|
2664
|
+
* @param key - The key to set.
|
|
2665
|
+
* @param value - The value to set.
|
|
2666
|
+
* @returns `Success` with the new value and the detail `updated` if the
|
|
2667
|
+
* key was found and updated, `Success` with the new value and detail
|
|
2668
|
+
* `added` if the key was not found and added. Fails with detail
|
|
2669
|
+
* 'invalid-key' or 'invalid-value' and an error message if either is invalid.
|
|
2670
|
+
*/
|
|
2671
|
+
set(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;
|
|
2672
|
+
/**
|
|
2673
|
+
* Returns the number of entries in the map.
|
|
2674
|
+
*/
|
|
2675
|
+
get size(): number;
|
|
2676
|
+
/**
|
|
2677
|
+
* Updates an existing key in the map - the map is not updated if the key does
|
|
2678
|
+
* not exist.
|
|
2679
|
+
* @param key - The key to update.
|
|
2680
|
+
* @param value - The value to set.
|
|
2681
|
+
* @returns `Success` with the value and detail 'exists' if the key was found
|
|
2682
|
+
* and the value updated, `Failure` an error message and with detail `not-found`
|
|
2683
|
+
* if the key was not found, or with detail 'invalid-key' or 'invalid-value'
|
|
2684
|
+
* if either is invalid.
|
|
2685
|
+
*/
|
|
2686
|
+
update(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;
|
|
2687
|
+
/**
|
|
2688
|
+
* Returns an iterator over the map values.
|
|
2689
|
+
* @returns An iterator over the map values.
|
|
2690
|
+
*/
|
|
2691
|
+
values(): MapIterator<TV>;
|
|
2692
|
+
/**
|
|
2693
|
+
* Gets an iterator over the map entries.
|
|
2694
|
+
* @returns An iterator over the map entries.
|
|
2695
|
+
*/
|
|
2696
|
+
[Symbol.iterator](): IterableIterator<KeyValueEntry<TK, TV>>;
|
|
2697
|
+
/**
|
|
2698
|
+
* Gets a readonly version of this map.
|
|
2699
|
+
* @returns A readonly version of this map.
|
|
2700
|
+
*/
|
|
2701
|
+
toReadOnly(): IReadOnlyResultMap<TK, TV>;
|
|
2702
|
+
/**
|
|
2703
|
+
* Determines if a value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory}.
|
|
2704
|
+
* @param value - The value to check.
|
|
2705
|
+
* @returns `true` if the value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory},
|
|
2706
|
+
* `false` otherwise.
|
|
2707
|
+
* @public
|
|
2708
|
+
*/
|
|
2709
|
+
protected _isResultMapValueFactory<TK extends string, TV>(value: TV | ResultMapValueFactory<TK, TV>): value is ResultMapValueFactory<TK, TV>;
|
|
2710
|
+
}
|
|
2711
|
+
|
|
2712
|
+
/**
|
|
2713
|
+
* Callback for {@link Collections.ResultMap | ResultMap} `forEach` method.
|
|
2714
|
+
* @public
|
|
2715
|
+
*/
|
|
2716
|
+
declare type ResultMapForEachCb<TK extends string = string, TE = unknown> = (value: TE, key: TK, map: IReadOnlyResultMap<TK, TE>, thisArg?: unknown) => void;
|
|
2717
|
+
|
|
2718
|
+
/**
|
|
2719
|
+
* Additional success or failure details for {@link Collections.ResultMap | ResultMap} calls.
|
|
2720
|
+
* @public
|
|
2721
|
+
*/
|
|
2722
|
+
declare type ResultMapResultDetail = 'added' | 'deleted' | 'exists' | 'invalid-key' | 'invalid-value' | 'not-found' | 'success' | 'updated';
|
|
2723
|
+
|
|
2724
|
+
/**
|
|
2725
|
+
* A {@link Collections.ResultMap | ResultMap} wrapper which validates weakly-typed keys
|
|
2726
|
+
* before calling the wrapped result map.
|
|
2727
|
+
* @public
|
|
2728
|
+
*/
|
|
2729
|
+
declare class ResultMapValidator<TK extends string = string, TV = unknown> implements IReadOnlyResultMapValidator<TK, TV> {
|
|
2730
|
+
readonly validators: KeyValueValidators<TK, TV>;
|
|
2731
|
+
get map(): IReadOnlyResultMap<TK, TV>;
|
|
2732
|
+
protected _map: ResultMap<TK, TV>;
|
|
2733
|
+
/**
|
|
2734
|
+
* Constructs a new {@link Collections.ValidatingResultMap | ValidatingResultMap}.
|
|
2735
|
+
* @param params - Required parameters for constructing the map.
|
|
2736
|
+
*/
|
|
2737
|
+
constructor(params: IResultMapValidatorCreateParams<TK, TV>);
|
|
2738
|
+
/**
|
|
2739
|
+
* {@inheritdoc Collections.ResultMap.add}
|
|
2740
|
+
*/
|
|
2741
|
+
add(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail>;
|
|
2742
|
+
/**
|
|
2743
|
+
* {@inheritdoc Collections.ResultMap.delete}
|
|
2744
|
+
*/
|
|
2745
|
+
delete(key: string): DetailedResult<TV, ResultMapResultDetail>;
|
|
2746
|
+
/**
|
|
2747
|
+
* {@inheritdoc Collections.ResultMap.get}
|
|
2748
|
+
*/
|
|
2749
|
+
get(key: string): DetailedResult<TV, ResultMapResultDetail>;
|
|
2750
|
+
/**
|
|
2751
|
+
* {@inheritdoc Collections.ResultMap.(getOrAdd:1)}
|
|
2752
|
+
*/
|
|
2753
|
+
getOrAdd(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail>;
|
|
2754
|
+
/**
|
|
2755
|
+
* {@inheritdoc Collections.ResultMap.(getOrAdd:2)}
|
|
2756
|
+
*/
|
|
2757
|
+
getOrAdd(key: string, factory: ResultMapValueFactory<TK, TV>): DetailedResult<TV, ResultMapResultDetail>;
|
|
2758
|
+
/**
|
|
2759
|
+
* {@inheritdoc Collections.ResultMap.has}
|
|
2760
|
+
*/
|
|
2761
|
+
has(key: string): boolean;
|
|
2762
|
+
/**
|
|
2763
|
+
* {@inheritdoc Collections.ResultMap.set}
|
|
2764
|
+
*/
|
|
2765
|
+
set(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail>;
|
|
2766
|
+
/**
|
|
2767
|
+
* {@inheritdoc Collections.ResultMap.update}
|
|
2768
|
+
*/
|
|
2769
|
+
update(key: string, value: unknown): DetailedResult<TV, ResultMapResultDetail>;
|
|
2770
|
+
/**
|
|
2771
|
+
* Gets a read-only version of this validator.
|
|
2772
|
+
*/
|
|
2773
|
+
toReadOnly(): IReadOnlyResultMapValidator<TK, TV>;
|
|
2774
|
+
/**
|
|
2775
|
+
* Determines if a value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory}.
|
|
2776
|
+
* @param value - The value to check.
|
|
2777
|
+
* @returns `true` if the value is a {@link Collections.ResultMapValueFactory | ResultMapValueFactory},
|
|
2778
|
+
* `false` otherwise.
|
|
2779
|
+
* @public
|
|
2780
|
+
*/
|
|
2781
|
+
protected _isResultMapValueFactory<TK extends string, TV>(value: TV | ResultMapValueFactory<TK, TV>): value is ResultMapValueFactory<TK, TV>;
|
|
2782
|
+
}
|
|
2783
|
+
|
|
2784
|
+
/**
|
|
2785
|
+
* Deferred constructor for the {@link Collections.ResultMap.(getOrAdd:2) | getOrAdd} method.
|
|
2786
|
+
* @public
|
|
2787
|
+
*/
|
|
2788
|
+
declare type ResultMapValueFactory<TK extends string = string, TV = unknown> = (key: TK) => Result<TV>;
|
|
2789
|
+
|
|
2320
2790
|
/**
|
|
2321
2791
|
* Type inference to determine the result type of an {@link Result}.
|
|
2322
2792
|
* @beta
|
|
@@ -2325,7 +2795,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2325
2795
|
|
|
2326
2796
|
/**
|
|
2327
2797
|
* Helper function to create a {@link Conversion.ObjectConverter | ObjectConverter} which converts an object
|
|
2328
|
-
* without changing shape, a {@link Conversion.FieldConverters | FieldConverters<T>} and an optional
|
|
2798
|
+
* without changing shape, a {@link Conversion.FieldConverters | FieldConverters<T, TC>} and an optional
|
|
2329
2799
|
* {@link Converters.StrictObjectConverterOptions | StrictObjectConverterOptions<T>} to further refine
|
|
2330
2800
|
* conversion behavior.
|
|
2331
2801
|
*
|
|
@@ -2341,11 +2811,11 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2341
2811
|
* {@label WITH_OPTIONS}
|
|
2342
2812
|
* @public
|
|
2343
2813
|
*/
|
|
2344
|
-
declare function strictObject<T>(properties: FieldConverters<T>, options?: StrictObjectConverterOptions<T>): ObjectConverter<T>;
|
|
2814
|
+
declare function strictObject<T, TC = unknown>(properties: FieldConverters<T, TC>, options?: StrictObjectConverterOptions<T>): ObjectConverter<T, TC>;
|
|
2345
2815
|
|
|
2346
2816
|
/**
|
|
2347
2817
|
* Helper function to create a {@link Conversion.ObjectConverter | ObjectConverter} which converts an object
|
|
2348
|
-
* without changing shape, a {@link Conversion.FieldConverters | FieldConverters<T>} and an optional
|
|
2818
|
+
* without changing shape, a {@link Conversion.FieldConverters | FieldConverters<T, TC>} and an optional
|
|
2349
2819
|
* {@link Converters.StrictObjectConverterOptions | StrictObjectConverterOptions<T>} to further refine
|
|
2350
2820
|
* conversion behavior.
|
|
2351
2821
|
*
|
|
@@ -2362,7 +2832,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2362
2832
|
* @deprecated Use {@link Converters.(strictObject:1) | Converters.strictObject(options)} instead.
|
|
2363
2833
|
* @public
|
|
2364
2834
|
*/
|
|
2365
|
-
declare function strictObject<T>(properties: FieldConverters<T>, optional: (keyof T)[]): ObjectConverter<T>;
|
|
2835
|
+
declare function strictObject<T, TC = unknown>(properties: FieldConverters<T, TC>, optional: (keyof T)[]): ObjectConverter<T, TC>;
|
|
2366
2836
|
|
|
2367
2837
|
/**
|
|
2368
2838
|
* Options for the {@link Converters.(strictObject:1)} helper function.
|
|
@@ -2390,7 +2860,7 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2390
2860
|
* of strings, returns {@link Failure | Failure} with an error message otherwise.
|
|
2391
2861
|
* @public
|
|
2392
2862
|
*/
|
|
2393
|
-
declare const stringArray: Converter<string[]>;
|
|
2863
|
+
declare const stringArray: Converter<string[], unknown>;
|
|
2394
2864
|
|
|
2395
2865
|
/**
|
|
2396
2866
|
* The {@link Conversion.StringConverter | StringConverter} class extends
|
|
@@ -2706,6 +3176,13 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2706
3176
|
*/
|
|
2707
3177
|
declare type TypeGuardWithContext<T, TC = unknown> = (from: unknown, context?: TC) => from is T;
|
|
2708
3178
|
|
|
3179
|
+
declare namespace Utils {
|
|
3180
|
+
export {
|
|
3181
|
+
isIterable,
|
|
3182
|
+
KeyValueValidators
|
|
3183
|
+
}
|
|
3184
|
+
}
|
|
3185
|
+
|
|
2709
3186
|
/**
|
|
2710
3187
|
* Helper function to create a {@link Converter | Converter} from any {@link Validation.Validator}
|
|
2711
3188
|
* @param validator - the validator to be wrapped
|
|
@@ -2723,10 +3200,59 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2723
3200
|
* information otherwise.
|
|
2724
3201
|
* @param validator - A validator function to determine if the converted value is valid.
|
|
2725
3202
|
* @param description - A description of the validated type for use in error messages.
|
|
2726
|
-
* @returns A new {@link Converter | Converter<T>} which applies the supplied validation.
|
|
3203
|
+
* @returns A new {@link Converter | Converter<T, TC>} which applies the supplied validation.
|
|
2727
3204
|
* @public
|
|
2728
3205
|
*/
|
|
2729
|
-
declare function validateWith<T, TC =
|
|
3206
|
+
declare function validateWith<T, TC = unknown>(validator: (from: unknown) => from is T, description?: string): Converter<T, TC>;
|
|
3207
|
+
|
|
3208
|
+
/**
|
|
3209
|
+
* A {@link Collections.ResultMap | ResultMap} with a {@link Collections.ResultMapValidator | validator}
|
|
3210
|
+
* property that enables validated use of the underlying map with weakly-typed keys and values.
|
|
3211
|
+
* @public
|
|
3212
|
+
*/
|
|
3213
|
+
export declare class ValidatingResultMap<TK extends string = string, TV = unknown> extends ResultMap<TK, TV> implements IReadOnlyValidatingResultMap<TK, TV> {
|
|
3214
|
+
/**
|
|
3215
|
+
* A {@link Collections.ResultMapValidator | ResultMapValidator} which validates keys and values
|
|
3216
|
+
* before inserting them into this collection.
|
|
3217
|
+
*/
|
|
3218
|
+
readonly validate: ResultMapValidator<TK, TV>;
|
|
3219
|
+
/**
|
|
3220
|
+
* Constructs a new {@link Collections.ValidatingResultMap | ValidatingResultMap}.
|
|
3221
|
+
* @param params - Required parameters for constructing the map.
|
|
3222
|
+
*/
|
|
3223
|
+
constructor(params: IValidatingResultMapConstructorParams<TK, TV>);
|
|
3224
|
+
/**
|
|
3225
|
+
* Creates a new {@link Collections.ValidatingResultMap | ValidatingResultMap} instance.
|
|
3226
|
+
* @param params - Required parameters for constructing the map.
|
|
3227
|
+
* @returns `Success` with the new map if successful, `Failure` otherwise.
|
|
3228
|
+
* @public
|
|
3229
|
+
*/
|
|
3230
|
+
static createValidating<TK extends string = string, TV = unknown>(params: IValidatingResultMapConstructorParams<TK, TV>): Result<ValidatingResultMap<TK, TV>>;
|
|
3231
|
+
/**
|
|
3232
|
+
* {@inheritdoc Collections.ResultMap.add}
|
|
3233
|
+
*/
|
|
3234
|
+
add(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;
|
|
3235
|
+
/**
|
|
3236
|
+
* {@inheritdoc Collections.ResultMap.(getOrAdd:1)}
|
|
3237
|
+
*/
|
|
3238
|
+
getOrAdd(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;
|
|
3239
|
+
/**
|
|
3240
|
+
* {@inheritdoc Collections.ResultMap.(getOrAdd:2)}
|
|
3241
|
+
*/
|
|
3242
|
+
getOrAdd(key: TK, factory: ResultMapValueFactory<TK, TV>): DetailedResult<TV, ResultMapResultDetail>;
|
|
3243
|
+
/**
|
|
3244
|
+
* {@inheritdoc Collections.ResultMap.set}
|
|
3245
|
+
*/
|
|
3246
|
+
set(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;
|
|
3247
|
+
/**
|
|
3248
|
+
* {@inheritdoc Collections.ResultMap.update}
|
|
3249
|
+
*/
|
|
3250
|
+
update(key: TK, value: TV): DetailedResult<TV, ResultMapResultDetail>;
|
|
3251
|
+
/**
|
|
3252
|
+
* Gets a read-only version of this map.
|
|
3253
|
+
*/
|
|
3254
|
+
toReadOnly(): IReadOnlyValidatingResultMap<TK, TV>;
|
|
3255
|
+
}
|
|
2730
3256
|
|
|
2731
3257
|
declare namespace Validation {
|
|
2732
3258
|
export {
|
|
@@ -2741,17 +3267,28 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2741
3267
|
ValidatorTraits,
|
|
2742
3268
|
ValidatorOptions,
|
|
2743
3269
|
Constraint,
|
|
3270
|
+
ValidationErrorFormatter,
|
|
2744
3271
|
Validator
|
|
2745
3272
|
}
|
|
2746
3273
|
}
|
|
2747
3274
|
export { Validation }
|
|
2748
3275
|
|
|
3276
|
+
/**
|
|
3277
|
+
* Formats an incoming error message and value that failed validation.
|
|
3278
|
+
* @param val - The value that failed validation.
|
|
3279
|
+
* @param message - The default error message, if any.
|
|
3280
|
+
* @param context - Optional validation context.
|
|
3281
|
+
* @returns The formatted error message.
|
|
3282
|
+
* @public
|
|
3283
|
+
*/
|
|
3284
|
+
declare type ValidationErrorFormatter<TC = unknown> = (val: unknown, message?: string, context?: TC) => string;
|
|
3285
|
+
|
|
2749
3286
|
/**
|
|
2750
3287
|
* In-place validation that a supplied unknown matches some
|
|
2751
3288
|
* required characteristics (type, values, etc).
|
|
2752
3289
|
* @public
|
|
2753
3290
|
*/
|
|
2754
|
-
export declare interface Validator<T, TC =
|
|
3291
|
+
export declare interface Validator<T, TC = unknown> {
|
|
2755
3292
|
/**
|
|
2756
3293
|
* {@link Validation.ValidatorTraits | Traits} describing this validation.
|
|
2757
3294
|
*/
|
|
@@ -2818,13 +3355,21 @@ declare type LogLevel = 'detail' | 'info' | 'warning' | 'error' | 'silent';
|
|
|
2818
3355
|
* @param brand - The brand to be applied.
|
|
2819
3356
|
*/
|
|
2820
3357
|
withBrand<B extends string>(brand: B): Validator<Brand<T, B>, TC>;
|
|
3358
|
+
/**
|
|
3359
|
+
* Creates a new {@link Validation.Validator | in-place validator} which
|
|
3360
|
+
* is derived from this one but which returns an error message supplied
|
|
3361
|
+
* by the provided formatter if an error occurs.
|
|
3362
|
+
* @param formatter - The error message formatter to be applied.
|
|
3363
|
+
* @returns A new {@link Validation.Validator | Validator}.
|
|
3364
|
+
*/
|
|
3365
|
+
withFormattedError(formatter: ValidationErrorFormatter<TC>): Validator<T, TC>;
|
|
2821
3366
|
}
|
|
2822
3367
|
|
|
2823
3368
|
/**
|
|
2824
3369
|
* Abstract base helper class for specific validator implementations
|
|
2825
3370
|
* @internal
|
|
2826
3371
|
*/
|
|
2827
|
-
declare abstract class ValidatorBase<T, TC =
|
|
3372
|
+
declare abstract class ValidatorBase<T, TC = unknown> extends GenericValidator<T, TC> {
|
|
2828
3373
|
/**
|
|
2829
3374
|
* Inner constructor
|
|
2830
3375
|
* @param params - Initialization params.
|