@fgv/ts-utils 1.2.0-alpha.2 → 1.3.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/LICENSE +0 -0
- package/README.md +4 -0
- package/brand.d.ts +2 -0
- package/brand.d.ts.map +1 -0
- package/brand.js +1 -1
- package/converter.d.ts +185 -73
- package/converter.d.ts.map +1 -0
- package/converter.js +104 -51
- package/converters.d.ts +440 -182
- package/converters.d.ts.map +1 -0
- package/converters.js +358 -209
- package/csvHelpers.d.ts +7 -0
- package/csvHelpers.d.ts.map +1 -0
- package/csvHelpers.js +14 -4
- package/extendedArray.d.ts +46 -0
- package/extendedArray.d.ts.map +1 -0
- package/extendedArray.js +53 -8
- package/formatter.d.ts +53 -0
- package/formatter.d.ts.map +1 -0
- package/formatter.js +27 -4
- package/hash.d.ts +51 -0
- package/hash.d.ts.map +1 -0
- package/hash.js +168 -0
- package/index.d.ts +6 -2
- package/index.d.ts.map +1 -0
- package/index.js +15 -5
- package/logger.d.ts +3 -2
- package/logger.d.ts.map +1 -0
- package/logger.js +12 -12
- package/package.json +24 -20
- package/rangeOf.d.ts +96 -0
- package/rangeOf.d.ts.map +1 -0
- package/rangeOf.js +81 -2
- package/result.d.ts +391 -39
- package/result.d.ts.map +1 -0
- package/result.js +257 -49
- package/ts-utils.d.ts +2451 -0
- package/tsdoc-metadata.json +11 -0
- package/utils.d.ts +49 -27
- package/utils.d.ts.map +1 -0
- package/utils.js +53 -36
- package/validation/boolean.d.ts +27 -0
- package/validation/boolean.d.ts.map +1 -0
- package/validation/boolean.js +59 -0
- package/validation/classes.d.ts +5 -1
- package/validation/classes.d.ts.map +1 -0
- package/validation/classes.js +9 -2
- package/validation/field.d.ts +30 -0
- package/validation/field.d.ts.map +1 -0
- package/validation/field.js +23 -5
- package/validation/genericValidator.d.ts +41 -22
- package/validation/genericValidator.d.ts.map +1 -0
- package/validation/genericValidator.js +25 -26
- package/validation/index.d.ts +5 -3
- package/validation/index.d.ts.map +1 -0
- package/validation/index.js +15 -8
- package/validation/number.d.ts +20 -0
- package/validation/number.d.ts.map +1 -0
- package/validation/number.js +17 -2
- package/validation/object.d.ts +76 -11
- package/validation/object.d.ts.map +1 -0
- package/validation/object.js +52 -14
- package/validation/string.d.ts +20 -0
- package/validation/string.d.ts.map +1 -0
- package/validation/string.js +17 -2
- package/validation/traits.d.ts +50 -1
- package/validation/traits.d.ts.map +1 -0
- package/validation/traits.js +15 -2
- package/validation/validator.d.ts +44 -23
- package/validation/validator.d.ts.map +1 -0
- package/validation/validator.js +1 -1
- package/validation/validatorBase.d.ts +16 -1
- package/validation/validatorBase.d.ts.map +1 -0
- package/validation/validatorBase.js +7 -1
- package/validation/validators.d.ts +26 -2
- package/validation/validators.d.ts.map +1 -0
- package/validation/validators.js +26 -8
package/ts-utils.d.ts
ADDED
|
@@ -0,0 +1,2451 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Determines if an iterable collection of {@link Result | Result<T>} were all successful.
|
|
3
|
+
* @param results - The collection of {@link Result | Result<T>} to be tested.
|
|
4
|
+
* @returns Returns {@link Success} with `true` if all {@link Result | results} are successful.
|
|
5
|
+
* If any are unsuccessful, returns {@link Failure} with a concatenated summary the error
|
|
6
|
+
* messages from all failed elements.
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export declare function allSucceed<T>(results: Iterable<Result<unknown>>, successValue: T): Result<T>;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A helper function to create a {@link Converter} which converts `unknown` to an array of `<T>`.
|
|
13
|
+
* @remarks
|
|
14
|
+
* If `onError` is `'failOnError'` (default), then the entire conversion fails if any element cannot
|
|
15
|
+
* be converted. If `onError` is `'ignoreErrors'`, then failing elements are silently ignored.
|
|
16
|
+
* @param converter - {@link Converter} used to convert each item in the array.
|
|
17
|
+
* @param ignoreErrors - Specifies treatment of unconvertable elements.
|
|
18
|
+
* @returns A {@link Converter} which returns an array of `<T>`.
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
declare function arrayOf<T, TC = undefined>(converter: Converter<T, TC>, onError?: OnError_2): Converter<T[], TC>;
|
|
22
|
+
|
|
23
|
+
declare namespace Base {
|
|
24
|
+
export {
|
|
25
|
+
ValidatorFunc,
|
|
26
|
+
GenericValidatorConstructorParams,
|
|
27
|
+
GenericValidator
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Base templated wrapper to simplify creation of new {@link Converter}s.
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
export declare class BaseConverter<T, TC = undefined> implements Converter<T, TC> {
|
|
36
|
+
/**
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
39
|
+
protected readonly _defaultContext?: TC;
|
|
40
|
+
/**
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
protected _isOptional: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
protected _brand?: string;
|
|
48
|
+
private readonly _converter;
|
|
49
|
+
/**
|
|
50
|
+
* Constructs a new {@link Converter} which uses the supplied function to perform the conversion.
|
|
51
|
+
* @param converter - The conversion function to be applied.
|
|
52
|
+
* @param defaultContext - Optional conversion context to be used by default.
|
|
53
|
+
* @param traits - Optional {@link ConverterTraits | traits} to be assigned to the resulting
|
|
54
|
+
* converter.
|
|
55
|
+
*/
|
|
56
|
+
constructor(converter: (from: unknown, self: Converter<T, TC>, context?: TC) => Result<T>, defaultContext?: TC, traits?: ConverterTraits);
|
|
57
|
+
/**
|
|
58
|
+
* {@inheritdoc Converter.isOptional}
|
|
59
|
+
*/
|
|
60
|
+
get isOptional(): boolean;
|
|
61
|
+
/**
|
|
62
|
+
* {@inheritdoc Converter.brand}
|
|
63
|
+
*/
|
|
64
|
+
get brand(): string | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* {@inheritdoc Converter.convert}
|
|
67
|
+
*/
|
|
68
|
+
convert(from: unknown, context?: TC): Result<T>;
|
|
69
|
+
/**
|
|
70
|
+
* {@inheritdoc Converter.convertOptional}
|
|
71
|
+
*/
|
|
72
|
+
convertOptional(from: unknown, context?: TC, onError?: OnError): Result<T | undefined>;
|
|
73
|
+
/**
|
|
74
|
+
* {@inheritdoc Converter.optional}
|
|
75
|
+
*/
|
|
76
|
+
optional(onError?: OnError): Converter<T | undefined, TC>;
|
|
77
|
+
/**
|
|
78
|
+
* {@inheritdoc Converter.map}
|
|
79
|
+
*/
|
|
80
|
+
map<T2>(mapper: (from: T) => Result<T2>): Converter<T2, TC>;
|
|
81
|
+
/**
|
|
82
|
+
* {@inheritdoc Converter.mapConvert}
|
|
83
|
+
*/
|
|
84
|
+
mapConvert<T2>(mapConverter: Converter<T2>): Converter<T2, TC>;
|
|
85
|
+
/**
|
|
86
|
+
* {@inheritdoc Converter.mapItems}
|
|
87
|
+
*/
|
|
88
|
+
mapItems<TI>(mapper: (from: unknown) => Result<TI>): Converter<TI[], TC>;
|
|
89
|
+
/**
|
|
90
|
+
* {@inheritdoc Converter.mapConvertItems}
|
|
91
|
+
*/
|
|
92
|
+
mapConvertItems<TI>(mapConverter: Converter<TI, unknown>): Converter<TI[], TC>;
|
|
93
|
+
/**
|
|
94
|
+
* {@inheritdoc Converter.withTypeGuard}
|
|
95
|
+
*/
|
|
96
|
+
withTypeGuard<TI>(guard: (from: unknown) => from is TI, message?: string): Converter<TI, TC>;
|
|
97
|
+
/**
|
|
98
|
+
* {@inheritdoc Converter.withItemTypeGuard}
|
|
99
|
+
*/
|
|
100
|
+
withItemTypeGuard<TI>(guard: (from: unknown) => from is TI, message?: string): Converter<TI[], TC>;
|
|
101
|
+
/**
|
|
102
|
+
* {@inheritdoc Converter.withConstraint}
|
|
103
|
+
*/
|
|
104
|
+
withConstraint(constraint: (val: T) => boolean | Result<T>, options?: ConstraintOptions): Converter<T, TC>;
|
|
105
|
+
/**
|
|
106
|
+
* {@inheritdoc Converter.withBrand}
|
|
107
|
+
*/
|
|
108
|
+
withBrand<B extends string>(brand: B): Converter<Brand<T, B>, TC>;
|
|
109
|
+
/**
|
|
110
|
+
* @internal
|
|
111
|
+
*/
|
|
112
|
+
protected _context(supplied?: TC): TC | undefined;
|
|
113
|
+
/**
|
|
114
|
+
* @internal
|
|
115
|
+
*/
|
|
116
|
+
protected _traits(traits?: Partial<ConverterTraits>): ConverterTraits;
|
|
117
|
+
/**
|
|
118
|
+
* @internal
|
|
119
|
+
*/
|
|
120
|
+
protected _with(traits: Partial<ConverterTraits>): this;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* A {@link Converter} which converts `unknown` to `boolean`.
|
|
125
|
+
* @remarks
|
|
126
|
+
* Boolean values or the case-insensitive strings `'true'` and `'false'` succeed.
|
|
127
|
+
* Anything else fails.
|
|
128
|
+
* @public
|
|
129
|
+
*/
|
|
130
|
+
declare const boolean: BaseConverter<boolean, undefined>;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* A {@link Validation.Classes.BooleanValidator | BooleanValidator} which validates a boolean in place.
|
|
134
|
+
* @public
|
|
135
|
+
*/
|
|
136
|
+
declare const boolean_2: BooleanValidator<unknown>;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* An in-place {@link Validation.Validator | Validator} for `boolean` values.
|
|
140
|
+
* @public
|
|
141
|
+
*/
|
|
142
|
+
declare class BooleanValidator<TC = unknown> extends GenericValidator<boolean, TC> {
|
|
143
|
+
/**
|
|
144
|
+
* Constructs a new {@link Validation.Classes.BooleanValidator | BooleanValidator}.
|
|
145
|
+
* @param params - Optional {@link Validation.Classes.BooleanValidatorConstructorParams | init params} for the
|
|
146
|
+
* new {@link Validation.Classes.BooleanValidator | BooleanValidator}.
|
|
147
|
+
*/
|
|
148
|
+
constructor(params?: BooleanValidatorConstructorParams<TC>);
|
|
149
|
+
/**
|
|
150
|
+
* Static method which validates that a supplied `unknown` value is a `boolean`.
|
|
151
|
+
* @param from - The `unknown` value to be tested.
|
|
152
|
+
* @returns Returns `true` if `from` is a `boolean`, or {@link Failure} with an error
|
|
153
|
+
* message if not.
|
|
154
|
+
*/
|
|
155
|
+
static validateBoolean(from: unknown): boolean | Failure<boolean>;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Parameters used to construct a {@link Validation.Classes.BooleanValidator | BooleanValidator}.
|
|
160
|
+
* @public
|
|
161
|
+
*/
|
|
162
|
+
declare type BooleanValidatorConstructorParams<TC = unknown> = GenericValidatorConstructorParams<boolean, TC>;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Helper type to brand a simple type to prevent inappropriate use
|
|
166
|
+
* @public
|
|
167
|
+
*/
|
|
168
|
+
export declare type Brand<T, B> = T & {
|
|
169
|
+
__brand: B;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Wraps a function which might throw to convert exception results
|
|
174
|
+
* to {@link Failure}.
|
|
175
|
+
* @param func - The function to be captured.
|
|
176
|
+
* @returns Returns {@link Success} with a value of type `<T>` on
|
|
177
|
+
* success , or {@link Failure} with the thrown error message if
|
|
178
|
+
* `func` throws an `Error`.
|
|
179
|
+
* @public
|
|
180
|
+
*/
|
|
181
|
+
export declare function captureResult<T>(func: () => T): Result<T>;
|
|
182
|
+
|
|
183
|
+
declare namespace Classes {
|
|
184
|
+
export {
|
|
185
|
+
StringValidator,
|
|
186
|
+
StringValidatorConstructorParams,
|
|
187
|
+
BooleanValidator,
|
|
188
|
+
BooleanValidatorConstructorParams,
|
|
189
|
+
NumberValidator,
|
|
190
|
+
NumberValidatorConstructorParams,
|
|
191
|
+
FieldValidators,
|
|
192
|
+
ObjectValidator,
|
|
193
|
+
ObjectValidatorConstructorParams,
|
|
194
|
+
ObjectValidatorOptions
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Computes an md5 hash from an array of strings. Not secure and not intended to be secure.
|
|
200
|
+
* @param parts - The strings to be hashed
|
|
201
|
+
* @returns An md5 hash of the parts
|
|
202
|
+
* @public
|
|
203
|
+
*/
|
|
204
|
+
declare function computeHash(parts: string[]): string;
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* A {@link Validation.Constraint | Constraint<T>} function returns
|
|
208
|
+
* `true` if the supplied value meets the constraint. Can return
|
|
209
|
+
* {@link Failure} with an error message or simply return `false`
|
|
210
|
+
* for a default message.
|
|
211
|
+
* @public
|
|
212
|
+
*/
|
|
213
|
+
declare type Constraint<T> = (val: T) => boolean | Failure<T>;
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Options for {@link Converter.withConstraint}.
|
|
217
|
+
* @public
|
|
218
|
+
*/
|
|
219
|
+
export declare interface ConstraintOptions {
|
|
220
|
+
/**
|
|
221
|
+
* Optional description for error messages when constraint
|
|
222
|
+
* function returns false.
|
|
223
|
+
*/
|
|
224
|
+
readonly description: string;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Union of all supported constraint traits.
|
|
229
|
+
* @public
|
|
230
|
+
*/
|
|
231
|
+
declare type ConstraintTrait = FunctionConstraintTrait;
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Deprecated name for Infer<T> retained for compatibility
|
|
235
|
+
* @deprecated use @see Infer instead
|
|
236
|
+
* @internal
|
|
237
|
+
*/
|
|
238
|
+
export declare type ConvertedToType<TCONV> = Infer<TCONV>;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Generic converter to convert unknown to a templated type `<T>`, using
|
|
242
|
+
* intrinsic rules or as modified by an optional conversion context
|
|
243
|
+
* of optional templated type `<TC>` (default `undefined`).
|
|
244
|
+
* @public
|
|
245
|
+
*/
|
|
246
|
+
export declare interface Converter<T, TC = undefined> extends ConverterTraits {
|
|
247
|
+
/**
|
|
248
|
+
* Indicates whether this element is explicitly optional.
|
|
249
|
+
*/
|
|
250
|
+
readonly isOptional: boolean;
|
|
251
|
+
/**
|
|
252
|
+
* Returns the brand for a branded type.
|
|
253
|
+
*/
|
|
254
|
+
readonly brand?: string;
|
|
255
|
+
/**
|
|
256
|
+
* Converts from `unknown` to `<T>`.
|
|
257
|
+
* @param from - The `unknown` to be converted
|
|
258
|
+
* @param context - An optional conversion context of type `<TC>` to be used in
|
|
259
|
+
* the conversion.
|
|
260
|
+
* @returns A {@link Result} with a {@link Success} and a value on success or an
|
|
261
|
+
* {@link Failure} with a a message on failure.
|
|
262
|
+
*/
|
|
263
|
+
convert(from: unknown, context?: TC): Result<T>;
|
|
264
|
+
/**
|
|
265
|
+
* Converts from `unknown` to `<T>` or `undefined`, as appropriate.
|
|
266
|
+
*
|
|
267
|
+
* @remarks
|
|
268
|
+
* If `onError` is `failOnError`, the converter succeeds for
|
|
269
|
+
* `undefined` or any convertible value, but reports an error
|
|
270
|
+
* if it encounters a value that cannot be converted.
|
|
271
|
+
*
|
|
272
|
+
* If `onError` is `ignoreErrors` (default) then values that
|
|
273
|
+
* cannot be converted result in a successful return of `undefined`.
|
|
274
|
+
* @param from - The `unknown` to be converted
|
|
275
|
+
* @param context - An optional conversion context of type `<TC>` to be used in
|
|
276
|
+
* the conversion.
|
|
277
|
+
* @param onError - Specifies handling of values that cannot be converted (default `ignoreErrors`).
|
|
278
|
+
* @returns A {@link Result} with a {@link Success} and a value on success or an
|
|
279
|
+
* {@link Failure} with a a message on failure.
|
|
280
|
+
*/
|
|
281
|
+
convertOptional(from: unknown, context?: TC, onError?: OnError): Result<T | undefined>;
|
|
282
|
+
/**
|
|
283
|
+
* Creates a {@link Converter} for an optional value.
|
|
284
|
+
*
|
|
285
|
+
* @remarks
|
|
286
|
+
* If `onError` is `failOnError`, the resulting converter will accept `undefined`
|
|
287
|
+
* or a convertible value, but report an error if it encounters a value that cannot be
|
|
288
|
+
* converted.
|
|
289
|
+
*
|
|
290
|
+
* If `onError` is `ignoreErrors` (default) then values that cannot be converted will
|
|
291
|
+
* result in a successful return of `undefined`.
|
|
292
|
+
*
|
|
293
|
+
* @param onError - Specifies handling of values that cannot be converted (default `ignoreErrors`).
|
|
294
|
+
* @returns A new {@link Converter} returning `<T|undefined>`.
|
|
295
|
+
* */
|
|
296
|
+
optional(onError?: OnError): Converter<T | undefined, TC>;
|
|
297
|
+
/**
|
|
298
|
+
* Creates a {@link Converter} which applies a (possibly) mapping conversion to
|
|
299
|
+
* the converted value of this {@link Converter}.
|
|
300
|
+
* @param mapper - A function which maps from the the result type `<T>` of this
|
|
301
|
+
* converter to a new result type `<T2>`.
|
|
302
|
+
* @returns A new {@link Converter} returning `<T2>`.
|
|
303
|
+
*/
|
|
304
|
+
map<T2>(mapper: (from: T) => Result<T2>): Converter<T2, TC>;
|
|
305
|
+
/**
|
|
306
|
+
* Creates a {@link Converter} which applies an additional supplied
|
|
307
|
+
* converter to the result of this converter.
|
|
308
|
+
*
|
|
309
|
+
* @param mapConverter - The {@link Converter} to be applied to the
|
|
310
|
+
* converted result from this {@link Converter}.
|
|
311
|
+
* @returns A new {@link Converter} returning `<T2>`.
|
|
312
|
+
*/
|
|
313
|
+
mapConvert<T2>(mapConverter: Converter<T2>): Converter<T2, TC>;
|
|
314
|
+
/**
|
|
315
|
+
* Creates a {@link Converter} which maps the individual items of a collection
|
|
316
|
+
* resulting from this {@link Converter} using the supplied map fuction.
|
|
317
|
+
*
|
|
318
|
+
* @remarks
|
|
319
|
+
* Fails if `from` is not an array.
|
|
320
|
+
*
|
|
321
|
+
* @param mapper - The map function to be applied to each element of the
|
|
322
|
+
* result of this {@link Converter}.
|
|
323
|
+
* @returns A new {@link Converter} returning `<TI[]>`.
|
|
324
|
+
*/
|
|
325
|
+
mapItems<TI>(mapper: (from: unknown) => Result<TI>): Converter<TI[], TC>;
|
|
326
|
+
/**
|
|
327
|
+
* Creates a {@link Converter} which maps the individual items of a collection
|
|
328
|
+
* resulting from this {@link Converter} using the supplied {@link Converter}.
|
|
329
|
+
*
|
|
330
|
+
* @remarks
|
|
331
|
+
* Fails if `from` is not an array.
|
|
332
|
+
*
|
|
333
|
+
* @param mapConverter - The {@link Converter} to be applied to each element of the
|
|
334
|
+
* result of this {@link Converter}.
|
|
335
|
+
* @returns A new {@link Converter} returning `<TI[]>`.
|
|
336
|
+
*/
|
|
337
|
+
mapConvertItems<TI>(mapConverter: Converter<TI, unknown>): Converter<TI[], TC>;
|
|
338
|
+
/**
|
|
339
|
+
* Creates a {@link Converter} which applies a supplied type guard to the conversion
|
|
340
|
+
* result.
|
|
341
|
+
* @param guard - The type guard function to apply.
|
|
342
|
+
* @param message - Optional message to be reported if the type guard fails.
|
|
343
|
+
* @returns A new {@link Converter} returning `<TI>`.
|
|
344
|
+
*/
|
|
345
|
+
withTypeGuard<TI>(guard: (from: unknown) => from is TI, message?: string): Converter<TI, TC>;
|
|
346
|
+
/**
|
|
347
|
+
* Creates a {@link Converter} which applies a supplied type guard to each member of
|
|
348
|
+
* the conversion result from this converter.
|
|
349
|
+
*
|
|
350
|
+
* @remarks
|
|
351
|
+
* Fails if the conversion result is not an array or if any member fails the
|
|
352
|
+
* type guard.
|
|
353
|
+
* @param guard - The type guard function to apply to each element.
|
|
354
|
+
* @param message - Optional message to be reported if the type guard fails.
|
|
355
|
+
* @returns A new {@link Converter} returning `<TI>`.
|
|
356
|
+
*/
|
|
357
|
+
withItemTypeGuard<TI>(guard: (from: unknown) => from is TI, message?: string): Converter<TI[], TC>;
|
|
358
|
+
/**
|
|
359
|
+
* Creates a {@link Converter} which applies an optional constraint to the result
|
|
360
|
+
* of this conversion. If this {@link Converter} (the base converter) succeeds, the new
|
|
361
|
+
* converter calls a supplied constraint evaluation function with the conversion, which
|
|
362
|
+
* fails the entire conversion if the constraint function returns either `false` or
|
|
363
|
+
* {@link Failure | Failure<T>}.
|
|
364
|
+
*
|
|
365
|
+
* @param constraint - Constraint evaluation function.
|
|
366
|
+
* @param options - {@link ConstraintOptions | Options} for constraint evaluation.
|
|
367
|
+
* @returns A new {@link Converter} returning `<T>`.
|
|
368
|
+
*/
|
|
369
|
+
withConstraint(constraint: (val: T) => boolean | Result<T>, options?: ConstraintOptions): Converter<T, TC>;
|
|
370
|
+
/**
|
|
371
|
+
* returns a converter which adds a brand to the type to prevent mismatched usage
|
|
372
|
+
* of simple types.
|
|
373
|
+
* @param brand - The brand to be applied to the result value.
|
|
374
|
+
* @returns A {@link Converter} returning `Brand<T, B>`.
|
|
375
|
+
*/
|
|
376
|
+
withBrand<B extends string>(brand: B): Converter<Brand<T, B>, TC>;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
declare namespace Converters {
|
|
380
|
+
export {
|
|
381
|
+
templateString,
|
|
382
|
+
enumeratedValue,
|
|
383
|
+
mappedEnumeratedValue,
|
|
384
|
+
literal,
|
|
385
|
+
delimitedString,
|
|
386
|
+
oneOf,
|
|
387
|
+
arrayOf,
|
|
388
|
+
extendedArrayOf,
|
|
389
|
+
recordOf,
|
|
390
|
+
mapOf,
|
|
391
|
+
validateWith,
|
|
392
|
+
element,
|
|
393
|
+
optionalElement,
|
|
394
|
+
field,
|
|
395
|
+
optionalField,
|
|
396
|
+
object,
|
|
397
|
+
strictObject,
|
|
398
|
+
discriminatedObject,
|
|
399
|
+
transform,
|
|
400
|
+
rangeTypeOf,
|
|
401
|
+
rangeOf,
|
|
402
|
+
StringMatchOptions,
|
|
403
|
+
StringConverter,
|
|
404
|
+
string,
|
|
405
|
+
value,
|
|
406
|
+
number,
|
|
407
|
+
boolean,
|
|
408
|
+
optionalString,
|
|
409
|
+
isoDate,
|
|
410
|
+
optionalNumber,
|
|
411
|
+
optionalBoolean,
|
|
412
|
+
stringArray,
|
|
413
|
+
numberArray,
|
|
414
|
+
KeyedConverterOptions,
|
|
415
|
+
ObjectConverterOptions,
|
|
416
|
+
FieldConverters,
|
|
417
|
+
ObjectConverter,
|
|
418
|
+
StrictObjectConverterOptions,
|
|
419
|
+
DiscriminatedObjectConverters
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
export { Converters }
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Converter traits.
|
|
426
|
+
* @public
|
|
427
|
+
*/
|
|
428
|
+
export declare interface ConverterTraits {
|
|
429
|
+
readonly isOptional: boolean;
|
|
430
|
+
readonly brand?: string;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
declare namespace Csv {
|
|
434
|
+
export {
|
|
435
|
+
readCsvFileSync
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
export { Csv }
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Default {@link RangeOfFormats | formats} to use for both
|
|
442
|
+
* open-ended and complete {@link RangeOf | RangeOf<T>}.
|
|
443
|
+
* @public
|
|
444
|
+
*/
|
|
445
|
+
export declare const DEFAULT_RANGEOF_FORMATS: {
|
|
446
|
+
minOnly: string;
|
|
447
|
+
maxOnly: string;
|
|
448
|
+
minMax: string;
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* Default {@link Validation.ValidatorTraitValues | validation traits}.
|
|
453
|
+
* @public
|
|
454
|
+
*/
|
|
455
|
+
declare const defaultValidatorTraits: ValidatorTraitValues;
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Helper function to create a {@link Converter} which converts any `string` into an
|
|
459
|
+
* array of `string`, by separating at a supplied delimiter.
|
|
460
|
+
* @remarks
|
|
461
|
+
* Delimeter may also be supplied as context at conversion time.
|
|
462
|
+
* @param delimiter - The delimiter at which to split.
|
|
463
|
+
* @returns A new {@link Converter} returning `string[]`.
|
|
464
|
+
* @public
|
|
465
|
+
*/
|
|
466
|
+
declare function delimitedString(delimiter: string, options?: 'filtered' | 'all'): Converter<string[], string>;
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* A {@link DetailedFailure} extends {@link Failure} to report optional failure details in
|
|
470
|
+
* addition to the error message.
|
|
471
|
+
* @public
|
|
472
|
+
*/
|
|
473
|
+
export declare class DetailedFailure<T, TD> extends Failure<T> {
|
|
474
|
+
/**
|
|
475
|
+
* @internal
|
|
476
|
+
*/
|
|
477
|
+
protected _detail: TD;
|
|
478
|
+
/**
|
|
479
|
+
* Constructs a new {@link DetailedFailure | DetailedFailure<T, TD>} with the supplied
|
|
480
|
+
* message and detail.
|
|
481
|
+
* @param message - The message to be returned.
|
|
482
|
+
* @param detail - The error detail to be returned.
|
|
483
|
+
*/
|
|
484
|
+
constructor(message: string, detail: TD);
|
|
485
|
+
/**
|
|
486
|
+
* The error detail associated with this {@link DetailedFailure}.
|
|
487
|
+
*/
|
|
488
|
+
get detail(): TD;
|
|
489
|
+
/**
|
|
490
|
+
* Reports that this {@link DetailedFailure} is a failure.
|
|
491
|
+
* @remarks
|
|
492
|
+
* Always true for {@link DetailedFailure} but can be used as type guard
|
|
493
|
+
* to discriminate {@link DetailedSuccess} from {@link DetailedFailure} in
|
|
494
|
+
* a {@link DetailedResult}.
|
|
495
|
+
* @returns `true`
|
|
496
|
+
*/
|
|
497
|
+
isFailure(): this is DetailedFailure<T, TD>;
|
|
498
|
+
/**
|
|
499
|
+
* Propagates the error message and detail from this result.
|
|
500
|
+
* @remarks
|
|
501
|
+
* Mutates the success type as the success callback would have, but does not
|
|
502
|
+
* call the success callback.
|
|
503
|
+
* @param _cb - {@link DetailedSuccessContinuation | Success callback} to be called
|
|
504
|
+
* on a {@link DetailedResult} in case of success (ignored).
|
|
505
|
+
* @returns A new {@link DetailedFailure | DetailedFailure<TN, TD>} which contains
|
|
506
|
+
* the error message and detail from this one.
|
|
507
|
+
*/
|
|
508
|
+
onSuccess<TN>(_cb: DetailedSuccessContinuation<T, TD, TN>): DetailedResult<TN, TD>;
|
|
509
|
+
/**
|
|
510
|
+
* Invokes the supplied {@link DetailedFailureContinuation | failure callback} and propagates
|
|
511
|
+
* its returned {@link DetailedResult | DetailedResult<T, TD>}.
|
|
512
|
+
* @param cb - The {@link DetailedFailureContinuation | failure callback} to be invoked.
|
|
513
|
+
* @returns The {@link DetailedResult | DetailedResult<T, TD>} returned by the failure callback.
|
|
514
|
+
*/
|
|
515
|
+
onFailure(cb: DetailedFailureContinuation<T, TD>): DetailedResult<T, TD>;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Callback to be called when a {@link DetailedResult} encounters a failure.
|
|
520
|
+
* @remarks
|
|
521
|
+
* A failure callback can change {@link Failure} to {@link Success} (e.g. by returning a default value)
|
|
522
|
+
* or it can change or embellish the error message, but it cannot change the success return type.
|
|
523
|
+
* @public
|
|
524
|
+
*/
|
|
525
|
+
export declare type DetailedFailureContinuation<T, TD> = (message: string, detail: TD) => DetailedResult<T, TD>;
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Type inference to determine the result type `T` of a {@link DetailedResult | DetailedResult<T, TD>}.
|
|
529
|
+
* @beta
|
|
530
|
+
*/
|
|
531
|
+
export declare type DetailedResult<T, TD> = DetailedSuccess<T, TD> | DetailedFailure<T, TD>;
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* A {@link DetailedSuccess} extends {@link Success} to report optional success details in
|
|
535
|
+
* addition to the error message.
|
|
536
|
+
* @public
|
|
537
|
+
*/
|
|
538
|
+
export declare class DetailedSuccess<T, TD> extends Success<T> {
|
|
539
|
+
/**
|
|
540
|
+
* @internal
|
|
541
|
+
*/
|
|
542
|
+
protected _detail?: TD;
|
|
543
|
+
/**
|
|
544
|
+
* Constructs a new {@link DetailedSuccess | DetailedSuccess<T, TD>} with the supplied
|
|
545
|
+
* value and detail.
|
|
546
|
+
* @param value - The value to be returned.
|
|
547
|
+
* @param detail - An optional successful detail to be returned. If omitted, detail
|
|
548
|
+
* will be `undefined`.
|
|
549
|
+
*/
|
|
550
|
+
constructor(value: T, detail?: TD);
|
|
551
|
+
/**
|
|
552
|
+
* The success detail associated with this {@link DetailedSuccess}, or `undefined` if
|
|
553
|
+
* no detail was supplied.
|
|
554
|
+
*/
|
|
555
|
+
get detail(): TD | undefined;
|
|
556
|
+
/**
|
|
557
|
+
* Reports that this {@link DetailedSuccess} is a success.
|
|
558
|
+
* @remarks
|
|
559
|
+
* Always true for {@link DetailedSuccess} but can be used as type guard
|
|
560
|
+
* to discriminate {@link DetailedSuccess} from {@link DetailedFailure} in
|
|
561
|
+
* a {@link DetailedResult}.
|
|
562
|
+
* @returns `true`
|
|
563
|
+
*/
|
|
564
|
+
isSuccess(): this is DetailedSuccess<T, TD>;
|
|
565
|
+
/**
|
|
566
|
+
* Invokes the supplied {@link DetailedSuccessContinuation | success callback} and propagates
|
|
567
|
+
* its returned {@link DetailedResult | DetailedResult<TN, TD>}.
|
|
568
|
+
* @remarks
|
|
569
|
+
* The success callback mutates the return type from `<T>` to `<TN>`.
|
|
570
|
+
* @param cb - The {@link DetailedSuccessContinuation | success callback} to be invoked.
|
|
571
|
+
* @returns The {@link DetailedResult | DetailedResult<T, TD>} returned by the success callback.
|
|
572
|
+
*/
|
|
573
|
+
onSuccess<TN>(cb: DetailedSuccessContinuation<T, TD, TN>): DetailedResult<TN, TD>;
|
|
574
|
+
/**
|
|
575
|
+
* Propagates this {@link DetailedSuccess}.
|
|
576
|
+
* @remarks
|
|
577
|
+
* Failure does not mutate return type so we can return this event directly.
|
|
578
|
+
* @param _cb - {@link DetailedFailureContinuation | Failure callback} to be called
|
|
579
|
+
* on a {@link DetailedResult} in case of failure (ignored).
|
|
580
|
+
* @returns `this`
|
|
581
|
+
*/
|
|
582
|
+
onFailure(_cb: DetailedFailureContinuation<T, TD>): DetailedResult<T, TD>;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* Callback to be called when a {@link DetailedResult} encounters success.
|
|
587
|
+
* @remarks
|
|
588
|
+
* A success callback can return a different result type than it receives, allowing
|
|
589
|
+
* success results to chain through intermediate result types.
|
|
590
|
+
* @public
|
|
591
|
+
*/
|
|
592
|
+
export declare type DetailedSuccessContinuation<T, TD, TN> = (value: T, detail?: TD) => DetailedResult<TN, TD>;
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* Helper to create a {@link Converter} whhich converts a discriminated object without changing shape.
|
|
596
|
+
* @remarks
|
|
597
|
+
* Takes the name of the discriminator property and a
|
|
598
|
+
* {@link Converters.DiscriminatedObjectConverters | string-keyed Record of converters}. During conversion,
|
|
599
|
+
* the resulting {@link Converter} invokes the converter from `converters` that corresponds to the value of
|
|
600
|
+
* the discriminator property in the source object.
|
|
601
|
+
*
|
|
602
|
+
* If the source is not an object, the discriminator property is missing, or the discriminator has
|
|
603
|
+
* a value not present in the converters, conversion fails and returns {@link Failure} with more information.
|
|
604
|
+
* @param discriminatorProp - Name of the property used to discriminate types.
|
|
605
|
+
* @param converters - {@link Converters.DiscriminatedObjectConverters | String-keyed record of converters} to
|
|
606
|
+
* invoke, where each key corresponds to a value of the discriminator property.
|
|
607
|
+
* @returns A {@link Converter} which converts the corresponding discriminated object.
|
|
608
|
+
* @public
|
|
609
|
+
*/
|
|
610
|
+
declare function discriminatedObject<T, TD extends string = string, TC = unknown>(discriminatorProp: string, converters: DiscriminatedObjectConverters<T, TD>): Converter<T, TC>;
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* A string-keyed `Record<string, Converter>` which maps specific {@link Converter | converters} to the
|
|
614
|
+
* value of a discriminator property.
|
|
615
|
+
* @public
|
|
616
|
+
*/
|
|
617
|
+
declare type DiscriminatedObjectConverters<T, TD extends string = string, TC = unknown> = Record<TD, Converter<T, TC>>;
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* A helper function to create a {@link Converter} which extracts and converts an element from an array.
|
|
621
|
+
* @remarks
|
|
622
|
+
* The returned {@link Converter} returns {@link Success} with the converted value if the element exists
|
|
623
|
+
* in the supplied array and can be converted. Returns {@link Failure} with an error message otherwise.
|
|
624
|
+
* @param index - The index of the element to be extracted.
|
|
625
|
+
* @param converter - A {@link Converter} used to convert the extracted element.
|
|
626
|
+
* @returns A {@link Converter | Converter<T>} which extracts the specified element from an array.
|
|
627
|
+
* @public
|
|
628
|
+
*/
|
|
629
|
+
declare function element<T, TC = undefined>(index: number, converter: Converter<T, TC>): Converter<T, TC>;
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* Helper function to create a {@link Converter} which converts `unknown` to one of a set of supplied
|
|
633
|
+
* enumerated values. Anything else fails.
|
|
634
|
+
*
|
|
635
|
+
* @remarks
|
|
636
|
+
* Allowed enumerated values can also be supplied as context at conversion time.
|
|
637
|
+
* @param values - Array of allowed values.
|
|
638
|
+
* @returns A new {@link Converter} returning `<T>`.
|
|
639
|
+
* @public
|
|
640
|
+
*/
|
|
641
|
+
declare function enumeratedValue<T>(values: T[]): Converter<T, T[]>;
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* An experimental array template which extend built-in `Array` to include a handful
|
|
645
|
+
* of predicates which return {@link Result | Result<T>}.
|
|
646
|
+
* @beta
|
|
647
|
+
*/
|
|
648
|
+
export declare class ExtendedArray<T> extends Array<T> {
|
|
649
|
+
readonly itemDescription: string;
|
|
650
|
+
/**
|
|
651
|
+
* Constructs an {@link ExtendedArray}.
|
|
652
|
+
* @param itemDescription - Brief description of the type of each item in this array.
|
|
653
|
+
* @param items - The initial contents of the array.
|
|
654
|
+
*/
|
|
655
|
+
constructor(itemDescription: string, ...items: T[]);
|
|
656
|
+
/**
|
|
657
|
+
* Type guard to determine if some arbitrary array is an
|
|
658
|
+
* {@link ExtendedArray}
|
|
659
|
+
* @param a - The `Array` to be tested.
|
|
660
|
+
* @returns Returns `true` if `a` is an {@link ExtendedArray},
|
|
661
|
+
* `false` otherwise.
|
|
662
|
+
*/
|
|
663
|
+
static isExtendedArray<T>(a?: T[]): a is ExtendedArray<T>;
|
|
664
|
+
/**
|
|
665
|
+
* Determines if this array contains exactly one element which matches
|
|
666
|
+
* a supplied predicate.
|
|
667
|
+
* @param predicate - The predicate function to be applied.
|
|
668
|
+
* @returns Returns {@link Success | Success<T>} with the single matching
|
|
669
|
+
* result if exactly one item matches `predicate`. Returns {@link Failure}
|
|
670
|
+
* with an error message if there are no matches or more than one match.
|
|
671
|
+
*/
|
|
672
|
+
single(predicate?: (item: T) => boolean): Result<T>;
|
|
673
|
+
/**
|
|
674
|
+
* Returns the first element of an {@link ExtendedArray}. Fails with an
|
|
675
|
+
* error message if the array is empty.
|
|
676
|
+
* @param failMessage - Optional message to be displayed in the event of failure.
|
|
677
|
+
* @returns Returns {@link Success | Success<T>} with the value of the first element
|
|
678
|
+
* in the array, or {@link Failure} with an error message if the array is empty.
|
|
679
|
+
*/
|
|
680
|
+
first(failMessage?: string): Result<T>;
|
|
681
|
+
/**
|
|
682
|
+
* Returns an array containing all elements of an {@link ExtendedArray}. Fails with
|
|
683
|
+
* an error message if the array is empty.
|
|
684
|
+
* @param failMessage - Optional message to be displayed in the event of failure.
|
|
685
|
+
* @returns Returns {@link Success | Success<T[]>} with a new (non-extended) `Array`
|
|
686
|
+
* containing the elements of this array, or {@link Failure} with an error message
|
|
687
|
+
* if the array is empty.
|
|
688
|
+
*/
|
|
689
|
+
atLeastOne(failMessage?: string): Result<T[]>;
|
|
690
|
+
/**
|
|
691
|
+
* Gets a new (non-extended) `Array` containing all of the elements from this
|
|
692
|
+
* {@link ExtendedArray}.
|
|
693
|
+
* @returns A new (non-extended) `Array<T>`.
|
|
694
|
+
*/
|
|
695
|
+
all(): T[];
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
/**
|
|
699
|
+
* A helper function to create a {@link Converter} which converts `unknown` to {@link ExtendedArray | ExtendedArray<T>}.
|
|
700
|
+
* @remarks
|
|
701
|
+
* If `onError` is `'failOnError'` (default), then the entire conversion fails if any element cannot
|
|
702
|
+
* be converted. If `onError` is `'ignoreErrors'`, then failing elements are silently ignored.
|
|
703
|
+
* @param converter - {@link Converter} used to convert each item in the array
|
|
704
|
+
* @param ignoreErrors - Specifies treatment of unconvertable elements
|
|
705
|
+
* @beta
|
|
706
|
+
*/
|
|
707
|
+
declare function extendedArrayOf<T, TC = undefined>(label: string, converter: Converter<T, TC>, onError?: OnError_2): Converter<ExtendedArray<T>, TC>;
|
|
708
|
+
|
|
709
|
+
/**
|
|
710
|
+
* Returns {@link Failure | Failure<T>} with the supplied error message.
|
|
711
|
+
* @param message - Error message to be returned.
|
|
712
|
+
* @public
|
|
713
|
+
*/
|
|
714
|
+
declare function fail_2<T>(message: string): Failure<T>;
|
|
715
|
+
export { fail_2 as fail }
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* Reports a failed {@link IResult | result} from some operation, with an error message.
|
|
719
|
+
* @public
|
|
720
|
+
*/
|
|
721
|
+
export declare class Failure<T> implements IResult<T> {
|
|
722
|
+
/**
|
|
723
|
+
* {@inheritdoc IResult.success}
|
|
724
|
+
*/
|
|
725
|
+
readonly success = false;
|
|
726
|
+
/**
|
|
727
|
+
* @internal
|
|
728
|
+
*/
|
|
729
|
+
private readonly _message;
|
|
730
|
+
/**
|
|
731
|
+
* Constructs a {@link Failure} with the supplied message.
|
|
732
|
+
* @param message - Error message to be reported.
|
|
733
|
+
*/
|
|
734
|
+
constructor(message: string);
|
|
735
|
+
/**
|
|
736
|
+
* Gets the error message associated with this error.
|
|
737
|
+
*/
|
|
738
|
+
get message(): string;
|
|
739
|
+
/**
|
|
740
|
+
* {@inheritdoc IResult.isSuccess}
|
|
741
|
+
*/
|
|
742
|
+
isSuccess(): this is Success<T>;
|
|
743
|
+
/**
|
|
744
|
+
* {@inheritdoc IResult.isFailure}
|
|
745
|
+
*/
|
|
746
|
+
isFailure(): this is Failure<T>;
|
|
747
|
+
/**
|
|
748
|
+
* {@inheritdoc IResult.getValueOrThrow}
|
|
749
|
+
*/
|
|
750
|
+
getValueOrThrow(logger?: IResultLogger): never;
|
|
751
|
+
/**
|
|
752
|
+
* {@inheritdoc IResult.getValueOrDefault}
|
|
753
|
+
*/
|
|
754
|
+
getValueOrDefault(dflt?: T): T | undefined;
|
|
755
|
+
/**
|
|
756
|
+
* {@inheritdoc IResult.onSuccess}
|
|
757
|
+
*/
|
|
758
|
+
onSuccess<TN>(_: SuccessContinuation<T, TN>): Result<TN>;
|
|
759
|
+
/**
|
|
760
|
+
* {@inheritdoc IResult.onFailure}
|
|
761
|
+
*/
|
|
762
|
+
onFailure(cb: FailureContinuation<T>): Result<T>;
|
|
763
|
+
/**
|
|
764
|
+
* {@inheritdoc IResult.withFailureDetail}
|
|
765
|
+
*/
|
|
766
|
+
withFailureDetail<TD>(detail: TD): DetailedResult<T, TD>;
|
|
767
|
+
/**
|
|
768
|
+
* {@inheritdoc IResult.withDetail}
|
|
769
|
+
*/
|
|
770
|
+
withDetail<TD>(detail: TD, _successDetail?: TD): DetailedResult<T, TD>;
|
|
771
|
+
/**
|
|
772
|
+
* Get a 'friendly' string representation of this object.
|
|
773
|
+
* @remarks
|
|
774
|
+
* The string representation of a {@link Failure} value is the error message.
|
|
775
|
+
* @returns A string representing this object.
|
|
776
|
+
*/
|
|
777
|
+
toString(): string;
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
/**
|
|
781
|
+
* Continuation callback to be called in the event that an
|
|
782
|
+
* {@link Result} fails.
|
|
783
|
+
* @public
|
|
784
|
+
*/
|
|
785
|
+
export declare type FailureContinuation<T> = (message: string) => Result<T>;
|
|
786
|
+
|
|
787
|
+
/**
|
|
788
|
+
* Returns {@link DetailedFailure | DetailedFailure<T, TD>} with a supplied error message and detail.
|
|
789
|
+
* @param message - The error message to be returned.
|
|
790
|
+
* @param detail - The event detail to be returned.
|
|
791
|
+
* @returns An {@link DetailedFailure | DetailedFailure<T, TD>} with the supplied error
|
|
792
|
+
* message and detail.
|
|
793
|
+
* @public
|
|
794
|
+
*/
|
|
795
|
+
export declare function failWithDetail<T, TD>(message: string, detail: TD): DetailedFailure<T, TD>;
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* A helper function to create a {@link Converter} which extracts and convert a property specified
|
|
799
|
+
* by name from an object.
|
|
800
|
+
* @remarks
|
|
801
|
+
* The resulting {@link Converter} returns {@link Success} with the converted value of the correpsonding
|
|
802
|
+
* object property if the field exists and can be converted. Returns {@link Failure} with an error message
|
|
803
|
+
* otherwise.
|
|
804
|
+
* @param name - The name of the field to be extracted.
|
|
805
|
+
* @param converter - {@link Converter} used to convert the extracted field.
|
|
806
|
+
* @public
|
|
807
|
+
*/
|
|
808
|
+
declare function field<T, TC = undefined>(name: string, converter: Converter<T, TC>): Converter<T, TC>;
|
|
809
|
+
|
|
810
|
+
/**
|
|
811
|
+
* Per-property converters for each of the properties in type T.
|
|
812
|
+
* @remarks
|
|
813
|
+
* Used to construct a {@link Converters.ObjectConverter | ObjectConverter}
|
|
814
|
+
* @public
|
|
815
|
+
*/
|
|
816
|
+
declare type FieldConverters<T, TC = unknown> = {
|
|
817
|
+
[key in keyof T]: Converter<T[key], TC>;
|
|
818
|
+
};
|
|
819
|
+
|
|
820
|
+
/**
|
|
821
|
+
* String-keyed record of initialization functions to be passed to {@link populateObject}.
|
|
822
|
+
* @public
|
|
823
|
+
*/
|
|
824
|
+
export declare type FieldInitializers<T> = {
|
|
825
|
+
[key in keyof T]: (state: Partial<T>) => Result<T[key]>;
|
|
826
|
+
};
|
|
827
|
+
|
|
828
|
+
/**
|
|
829
|
+
* Per-property {@link Validation.Validator | validators} for each of the properties in `<T>`.
|
|
830
|
+
* @public
|
|
831
|
+
*/
|
|
832
|
+
declare type FieldValidators<T, TC = unknown> = {
|
|
833
|
+
[key in keyof T]: Validator<T[key], TC>;
|
|
834
|
+
};
|
|
835
|
+
|
|
836
|
+
/**
|
|
837
|
+
* Formats a list of items using the supplied template and formatter, one result
|
|
838
|
+
* per output line.
|
|
839
|
+
* @param format - A mustache template used to format each item.
|
|
840
|
+
* @param items - The items to be formatted.
|
|
841
|
+
* @param itemFormatter - The {@link Formatter | Formatter<T>} used to format each item.
|
|
842
|
+
* @returns The resulting string.
|
|
843
|
+
* @beta
|
|
844
|
+
*/
|
|
845
|
+
export declare function formatList<T>(format: string, items: T[], itemFormatter: Formatter<T>): Result<string>;
|
|
846
|
+
|
|
847
|
+
/**
|
|
848
|
+
* Interface for an object that can be formatted.
|
|
849
|
+
* @beta
|
|
850
|
+
*/
|
|
851
|
+
export declare interface Formattable {
|
|
852
|
+
/**
|
|
853
|
+
* Formats an object using the supplied mustache template.
|
|
854
|
+
* @param format - A mustache template used to format the object.
|
|
855
|
+
* @returns {@link Success} with the resulting string, or {@link Failure}
|
|
856
|
+
* with an error message if an error occurs.
|
|
857
|
+
*/
|
|
858
|
+
format(format: string): Result<string>;
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
/**
|
|
862
|
+
* Base class which adds common formatting.
|
|
863
|
+
* @beta
|
|
864
|
+
*/
|
|
865
|
+
export declare class FormattableBase {
|
|
866
|
+
/**
|
|
867
|
+
* Helper enables derived classes to add named details to a formatted presentation.
|
|
868
|
+
* @param details - An array of detail description strings.
|
|
869
|
+
* @param label - Label to use for the new detail.
|
|
870
|
+
* @param value - Value to use for the new detail.
|
|
871
|
+
* @internal
|
|
872
|
+
*/
|
|
873
|
+
protected static _tryAddDetail(details: string[], label: string, value: string | undefined): void;
|
|
874
|
+
/**
|
|
875
|
+
* {@inheritdoc Formattable.format}
|
|
876
|
+
*/
|
|
877
|
+
format(template: string): Result<string>;
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
/**
|
|
881
|
+
* Destination format for some formatted string.
|
|
882
|
+
* @beta
|
|
883
|
+
*/
|
|
884
|
+
export declare type FormatTargets = 'text' | 'markdown' | 'embed';
|
|
885
|
+
|
|
886
|
+
/**
|
|
887
|
+
* Type definition for a formatting function, which takes a `string` and an
|
|
888
|
+
* item and returns {@link Result | Result<string>}.
|
|
889
|
+
* @beta
|
|
890
|
+
*/
|
|
891
|
+
export declare type Formatter<T> = (format: string, item: T) => Result<string>;
|
|
892
|
+
|
|
893
|
+
/**
|
|
894
|
+
* A collection of {@link Formatter | formatters} indexed by target name, to enable
|
|
895
|
+
* different format methods per output target.
|
|
896
|
+
* @beta
|
|
897
|
+
*/
|
|
898
|
+
export declare type FormattersByExtendedTarget<TFT extends FormatTargets, T> = Record<TFT, Formatter<T>>;
|
|
899
|
+
|
|
900
|
+
/**
|
|
901
|
+
* A collection of {@link Formatter | formatters} indexed by the {@link FormatTargets | default supported
|
|
902
|
+
* target formats}.
|
|
903
|
+
* @beta
|
|
904
|
+
*/
|
|
905
|
+
export declare type FormattersByTarget<T> = FormattersByExtendedTarget<FormatTargets, T>;
|
|
906
|
+
|
|
907
|
+
/**
|
|
908
|
+
* A {@link Validation.ConstraintTrait | ConstraintTrait} indicating that
|
|
909
|
+
* a {@link Validation.Constraint | Constraint<T>} function provides an
|
|
910
|
+
* additional constraint implementation.
|
|
911
|
+
* @public
|
|
912
|
+
*/
|
|
913
|
+
declare interface FunctionConstraintTrait {
|
|
914
|
+
type: 'function';
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
/**
|
|
918
|
+
* Generic base implementation for an in-place {@link Validation.Validator | Validator}.
|
|
919
|
+
* @public
|
|
920
|
+
*/
|
|
921
|
+
declare class GenericValidator<T, TC = undefined> implements Validator<T, TC> {
|
|
922
|
+
/**
|
|
923
|
+
* {@inheritdoc Validation.Validator.traits}
|
|
924
|
+
*/
|
|
925
|
+
readonly traits: ValidatorTraits;
|
|
926
|
+
/**
|
|
927
|
+
* @internal
|
|
928
|
+
*/
|
|
929
|
+
protected readonly _validator: ValidatorFunc<T, TC>;
|
|
930
|
+
/**
|
|
931
|
+
* @internal
|
|
932
|
+
*/
|
|
933
|
+
protected readonly _options: ValidatorOptions<TC>;
|
|
934
|
+
/**
|
|
935
|
+
* Constructs a new {@link Validation.Base.GenericValidator | GenericValidator<T>}.
|
|
936
|
+
* @param params - The {@link Validation.Base.GenericValidatorConstructorParams | constructor params}
|
|
937
|
+
* used to configure validation.
|
|
938
|
+
*/
|
|
939
|
+
constructor(params: Partial<GenericValidatorConstructorParams<T, TC>>);
|
|
940
|
+
/**
|
|
941
|
+
* {@inheritdoc Validation.Validator.isOptional}
|
|
942
|
+
*/
|
|
943
|
+
get isOptional(): boolean;
|
|
944
|
+
/**
|
|
945
|
+
* {@inheritdoc Validation.Validator.brand}
|
|
946
|
+
*/
|
|
947
|
+
get brand(): string | undefined;
|
|
948
|
+
/**
|
|
949
|
+
* {@inheritdoc Validation.Validator.validate}
|
|
950
|
+
*/
|
|
951
|
+
validate(from: unknown, context?: TC): Result<T>;
|
|
952
|
+
/**
|
|
953
|
+
* {@inheritdoc Validation.Validator.validateOptional}
|
|
954
|
+
*/
|
|
955
|
+
validateOptional(from: unknown, context?: TC): Result<T | undefined>;
|
|
956
|
+
/**
|
|
957
|
+
* {@inheritdoc Validation.Validator.guard}
|
|
958
|
+
*/
|
|
959
|
+
guard(from: unknown, context?: TC): from is T;
|
|
960
|
+
/**
|
|
961
|
+
* {@inheritdoc Validation.Validator.optional}
|
|
962
|
+
*/
|
|
963
|
+
optional(): Validator<T | undefined, TC>;
|
|
964
|
+
/**
|
|
965
|
+
* {@inheritdoc Validation.Validator.withConstraint}
|
|
966
|
+
*/
|
|
967
|
+
withConstraint(constraint: Constraint<T>, trait?: ConstraintTrait): Validator<T, TC>;
|
|
968
|
+
/**
|
|
969
|
+
* {@inheritdoc Validation.Validator.brand}
|
|
970
|
+
*/
|
|
971
|
+
withBrand<B extends string>(brand: B): Validator<Brand<T, B>, TC>;
|
|
972
|
+
/**
|
|
973
|
+
* Gets a default or explicit context.
|
|
974
|
+
* @param explicitContext - Optional explicit context.
|
|
975
|
+
* @returns The appropriate context to use.
|
|
976
|
+
* @internal
|
|
977
|
+
*/
|
|
978
|
+
protected _context(explicitContext?: TC): TC | undefined;
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
/**
|
|
982
|
+
* Options used to initialize a {@link Validation.Base.GenericValidator | GenericValidator}.
|
|
983
|
+
* @public
|
|
984
|
+
*/
|
|
985
|
+
declare interface GenericValidatorConstructorParams<T, TC> {
|
|
986
|
+
options?: ValidatorOptions<TC>;
|
|
987
|
+
traits?: Partial<ValidatorTraits>;
|
|
988
|
+
validator?: ValidatorFunc<T, TC>;
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
declare namespace Hash {
|
|
992
|
+
export {
|
|
993
|
+
computeHash,
|
|
994
|
+
Normalizer
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
export { Hash }
|
|
998
|
+
|
|
999
|
+
/**
|
|
1000
|
+
* Infers the type that will be returned by an intstantiated converter. Works
|
|
1001
|
+
* for complex as well as simple types.
|
|
1002
|
+
* @example `Infer<typeof Converters.mapOf(Converters.stringArray)>` is `Map<string, string[]>`
|
|
1003
|
+
* @beta
|
|
1004
|
+
*/
|
|
1005
|
+
export declare type Infer<TCONV> = TCONV extends Converter<infer TTO> ? InnerInferredType<TTO> : never;
|
|
1006
|
+
|
|
1007
|
+
/**
|
|
1008
|
+
* internal
|
|
1009
|
+
*/
|
|
1010
|
+
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);
|
|
1011
|
+
|
|
1012
|
+
/**
|
|
1013
|
+
* Represents the result of some operation of sequence of operations.
|
|
1014
|
+
* @remarks
|
|
1015
|
+
* This common contract enables comingled discriminated usage of {@link Success | Success<T>}
|
|
1016
|
+
* and {@link Failure | Failure<T>}.
|
|
1017
|
+
* @public
|
|
1018
|
+
*/
|
|
1019
|
+
export declare interface IResult<T> {
|
|
1020
|
+
/**
|
|
1021
|
+
* Indicates whether the operation was successful.
|
|
1022
|
+
*/
|
|
1023
|
+
readonly success: boolean;
|
|
1024
|
+
/**
|
|
1025
|
+
* Indicates whether this operation was successful. Functions
|
|
1026
|
+
* as a type guard for {@link Success | Success<T>}.
|
|
1027
|
+
*/
|
|
1028
|
+
isSuccess(): this is Success<T>;
|
|
1029
|
+
/**
|
|
1030
|
+
* Indicates whether this operation failed. Functions
|
|
1031
|
+
* as a type guard for {@link Failure | Failure<T>}.
|
|
1032
|
+
*/
|
|
1033
|
+
isFailure(): this is Failure<T>;
|
|
1034
|
+
/**
|
|
1035
|
+
* Gets the value associated with a successful {@link IResult | result},
|
|
1036
|
+
* or throws the error message if the corresponding operation failed.
|
|
1037
|
+
* @param logger - An optional {@link IResultLogger | logger} to which the
|
|
1038
|
+
* error will also be reported.
|
|
1039
|
+
* @returns The return value, if the operation was successful.
|
|
1040
|
+
* @throws The error message if the operation failed.
|
|
1041
|
+
*/
|
|
1042
|
+
getValueOrThrow(logger?: IResultLogger): T;
|
|
1043
|
+
/**
|
|
1044
|
+
* Gets the value associated with a successful {@link IResult | result},
|
|
1045
|
+
* or a default value if the corresponding operation failed.
|
|
1046
|
+
* @param dflt - The value to be returned if the operation failed (default is
|
|
1047
|
+
* `undefined`).
|
|
1048
|
+
* @returns The return value, if the operation was successful. Returns
|
|
1049
|
+
* the supplied default value or `undefined` if no default is supplied.
|
|
1050
|
+
*/
|
|
1051
|
+
getValueOrDefault(dflt?: T): T | undefined;
|
|
1052
|
+
/**
|
|
1053
|
+
* Calls a supplied {@link SuccessContinuation | success continuation} if
|
|
1054
|
+
* the operation was a success.
|
|
1055
|
+
* @remarks
|
|
1056
|
+
* The {@link SuccessContinuation | success continuation} might return a
|
|
1057
|
+
* different result type than {@link IResult} on which it is invoked. This
|
|
1058
|
+
* enables chaining of operations with heterogenous return types.
|
|
1059
|
+
*
|
|
1060
|
+
* @param cb - The {@link SuccessContinuation | success continuation} to
|
|
1061
|
+
* be called in the event of success.
|
|
1062
|
+
* @returns If this operation was successful, returns the value returned
|
|
1063
|
+
* by the {@link SuccessContinuation | success continuation}. If this result
|
|
1064
|
+
* failed, propagates the error message from this failure.
|
|
1065
|
+
*/
|
|
1066
|
+
onSuccess<TN>(cb: SuccessContinuation<T, TN>): Result<TN>;
|
|
1067
|
+
/**
|
|
1068
|
+
* Calls a supplied {@link FailureContinuation | failed continuation} if
|
|
1069
|
+
* the operation failed.
|
|
1070
|
+
* @param cb - The {@link FailureContinuation | failure continuation} to
|
|
1071
|
+
* be called in the event of failure.
|
|
1072
|
+
* @returns If this operation failed, returns the value returned by the
|
|
1073
|
+
* {@link FailureContinuation | failure continuation}. If this result
|
|
1074
|
+
* was successful, propagates the result value from the successful event.
|
|
1075
|
+
*/
|
|
1076
|
+
onFailure(cb: FailureContinuation<T>): Result<T>;
|
|
1077
|
+
/**
|
|
1078
|
+
* Converts a {@link IResult | IResult<T>} to a {@link DetailedResult | DetailedResult<T, TD>},
|
|
1079
|
+
* adding a supplied detail if the operation failed.
|
|
1080
|
+
* @param detail - The detail to be added if this operation failed.
|
|
1081
|
+
* @returns A new {@link DetailedResult | DetailedResult<T, TD>} with either
|
|
1082
|
+
* the success result or the error message from this {@link IResult}, with
|
|
1083
|
+
* the supplied detail (if this event failed) or detail `undefined` (if
|
|
1084
|
+
* this result succeeded).
|
|
1085
|
+
*/
|
|
1086
|
+
withFailureDetail<TD>(detail: TD): DetailedResult<T, TD>;
|
|
1087
|
+
/**
|
|
1088
|
+
* Converts a {@link IResult | IResult<T>} to a {@link DetailedResult | DetailedResult<T, TD>},
|
|
1089
|
+
* adding supplied details.
|
|
1090
|
+
* @param detail - The default detail to be added to the new {@link DetailedResult}.
|
|
1091
|
+
* @param successDetail - An optional detail to be added if this result was successful.
|
|
1092
|
+
* @returns A new {@link DetailedResult | DetailedResult<T, TD>} with either
|
|
1093
|
+
* the success result or the error message from this {@link IResult} and the
|
|
1094
|
+
* appopriate added detail.
|
|
1095
|
+
*/
|
|
1096
|
+
withDetail<TD>(detail: TD, successDetail?: TD): DetailedResult<T, TD>;
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
/**
|
|
1100
|
+
* Simple logger interface used by {@link IResult.getValueOrThrow}.
|
|
1101
|
+
* @public
|
|
1102
|
+
*/
|
|
1103
|
+
export declare interface IResultLogger {
|
|
1104
|
+
/**
|
|
1105
|
+
* Log an error message.
|
|
1106
|
+
* @param message - The message to be logged.
|
|
1107
|
+
*/
|
|
1108
|
+
error(message: string): void;
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
/**
|
|
1112
|
+
* Helper type-guard function to report whether a specified key is present in
|
|
1113
|
+
* a supplied object.
|
|
1114
|
+
* @param key - The key to be tested.
|
|
1115
|
+
* @param item - The object to be tested.
|
|
1116
|
+
* @returns Returns `true` if the key is present, `false` otherwise.
|
|
1117
|
+
* @public
|
|
1118
|
+
*/
|
|
1119
|
+
export declare function isKeyOf<T extends object>(key: string | number | symbol, item: T): key is keyof T;
|
|
1120
|
+
|
|
1121
|
+
/**
|
|
1122
|
+
* A {@link Converter} which converts an iso formatted string, a number or a `Date` object to
|
|
1123
|
+
* a `Date` object.
|
|
1124
|
+
* @public
|
|
1125
|
+
*/
|
|
1126
|
+
declare const isoDate: BaseConverter<Date, undefined>;
|
|
1127
|
+
|
|
1128
|
+
/**
|
|
1129
|
+
* Options for {@link Converters.(recordOf:withOptions)} and {@link Converters.(mapOf:withOptions)}
|
|
1130
|
+
* helper functions.
|
|
1131
|
+
* @public
|
|
1132
|
+
*/
|
|
1133
|
+
declare interface KeyedConverterOptions<T extends string = string, TC = undefined> {
|
|
1134
|
+
/**
|
|
1135
|
+
* if `onError` is `'fail'` (default), then the entire conversion fails if any key or element
|
|
1136
|
+
* cannot be converted. If `onError` is `'ignore'`, failing elements are silently ignored.
|
|
1137
|
+
*/
|
|
1138
|
+
onError?: 'fail' | 'ignore';
|
|
1139
|
+
/**
|
|
1140
|
+
* If present, `keyConverter` is used to convert the source object property names to
|
|
1141
|
+
* keys in the resulting map or record.
|
|
1142
|
+
* @remarks
|
|
1143
|
+
* Can be used to coerce key names to supported values and/or strong types.
|
|
1144
|
+
*/
|
|
1145
|
+
keyConverter?: Converter<T, TC>;
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
/**
|
|
1149
|
+
* Type for factory methods which convert a key-value pair to a new unique value.
|
|
1150
|
+
* @public
|
|
1151
|
+
*/
|
|
1152
|
+
declare type KeyedThingFactory<TS, TD, TK extends string = string> = (key: TK, thing: TS) => Result<TD>;
|
|
1153
|
+
|
|
1154
|
+
/**
|
|
1155
|
+
* Helper function to create a {@link Converter} which converts `unknown` to some supplied literal value. Succeeds with
|
|
1156
|
+
* the supplied value if an identity comparison succeeds, fails otherwise.
|
|
1157
|
+
* @param value - The value to be compared.
|
|
1158
|
+
* @returns A {@link Converter} which returns the supplied value on success.
|
|
1159
|
+
* @public
|
|
1160
|
+
*/
|
|
1161
|
+
declare function literal<T>(value: T): Converter<T, unknown>;
|
|
1162
|
+
|
|
1163
|
+
/**
|
|
1164
|
+
* Aggregates sucessful results from a collection of {@link DetailedResult | DetailedResult<T, TD>},
|
|
1165
|
+
* optionally ignoring certain error details.
|
|
1166
|
+
* @param results - The collection of {@link DetailedResult | DetailedResult<T, TD>} to be mapped.
|
|
1167
|
+
* @param ignore - An array of error detail values (of type `<TD>`) that should be ignored.
|
|
1168
|
+
* @returns {@link Success} with an array containing all successful results if all results either
|
|
1169
|
+
* suceeded or returned error details listed in `ignore`. If any results failed with details
|
|
1170
|
+
* that cannot be ignored, returns {@link Failure} with an concatenated summary of all non-ignorable
|
|
1171
|
+
* error mesasges.
|
|
1172
|
+
* @public
|
|
1173
|
+
*/
|
|
1174
|
+
export declare function mapDetailedResults<T, TD>(results: Iterable<DetailedResult<T, TD>>, ignore: TD[]): Result<T[]>;
|
|
1175
|
+
|
|
1176
|
+
/**
|
|
1177
|
+
* Aggregates error messages from a collection of {@link Result | Result<T>}.
|
|
1178
|
+
* @param results - An interable collection of {@link Result | Result<T>} for which
|
|
1179
|
+
* error messages are aggregated.
|
|
1180
|
+
* @returns An array of strings consisting of all error messages returned by
|
|
1181
|
+
* {@link Result | results} in the source collection. Ignores {@link Success}
|
|
1182
|
+
* results and returns an empty array if there were no errors.
|
|
1183
|
+
* @public
|
|
1184
|
+
*/
|
|
1185
|
+
export declare function mapFailures<T>(results: Iterable<Result<T>>): string[];
|
|
1186
|
+
|
|
1187
|
+
/**
|
|
1188
|
+
* A helper function to create a {@link Converter} which converts the `string`-keyed properties
|
|
1189
|
+
* using a supplied {@link Converter | Converter<T>} to produce a `Map<string, T>`.
|
|
1190
|
+
* @remarks
|
|
1191
|
+
* The resulting converter fails conversion if any element cannot be converted.
|
|
1192
|
+
* @param converter - {@link Converter} used to convert each item in the source object.
|
|
1193
|
+
* @returns A {@link Converter} which returns `Map<string, T>`.
|
|
1194
|
+
* {@label default}
|
|
1195
|
+
* @public
|
|
1196
|
+
*/
|
|
1197
|
+
declare function mapOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>): Converter<Map<TK, T>, TC>;
|
|
1198
|
+
|
|
1199
|
+
/**
|
|
1200
|
+
* A helper function to create a {@link Converter} which converts the `string`-keyed properties
|
|
1201
|
+
* using a supplied {@link Converter | Converter<T>} to produce a `Map<string, T>` and optionally
|
|
1202
|
+
* specified handling of elements that cannot be converted.
|
|
1203
|
+
* @remarks
|
|
1204
|
+
* if `onError` is `'fail'` (default), then the entire conversion fails if any key or element
|
|
1205
|
+
* cannot be converted. If `onError` is `'ignore'`, failing elements are silently ignored.
|
|
1206
|
+
* @param converter - {@link Converter} used to convert each item in the source object.
|
|
1207
|
+
* @returns A {@link Converter} which returns `Map<string, T>`.
|
|
1208
|
+
* {@label withOnError}
|
|
1209
|
+
* @public
|
|
1210
|
+
*/
|
|
1211
|
+
declare function mapOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>, onError: 'fail' | 'ignore'): Converter<Map<TK, T>, TC>;
|
|
1212
|
+
|
|
1213
|
+
/**
|
|
1214
|
+
* A helper function to create a {@link Converter} which converts the `string`-keyed properties
|
|
1215
|
+
* using a supplied {@link Converter | Converter<T>} to produce a `Map<TK, T>`.
|
|
1216
|
+
* @remarks
|
|
1217
|
+
* If present, the supplied {@link Converters.KeyedConverterOptions | options} can provide a strongly-typed
|
|
1218
|
+
* converter for keys and/or control the handling of elements that fail conversion.
|
|
1219
|
+
* @param converter - {@link Converter} used to convert each item in the source object.
|
|
1220
|
+
* @param options - Optional {@link Converters.KeyedConverterOptions | KeyedConverterOptions<TK, TC>} which
|
|
1221
|
+
* supplies a key converter and/or error-handling options.
|
|
1222
|
+
* @returns A {@link Converter} which returns `Map<TK, T>`.
|
|
1223
|
+
* {@label withOptions}
|
|
1224
|
+
* @public
|
|
1225
|
+
*/
|
|
1226
|
+
declare function mapOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>, options: KeyedConverterOptions<TK, TC>): Converter<Map<TK, T>, TC>;
|
|
1227
|
+
|
|
1228
|
+
/**
|
|
1229
|
+
* Helper function to create a {@link Converter} which converts `unknown` to one of a set of supplied enumerated
|
|
1230
|
+
* values, mapping any of multiple supplied values to the enumeration.
|
|
1231
|
+
* @remarks
|
|
1232
|
+
* Enables mapping of multiple input values to a consistent internal representation (so e.g. `'y'`, `'yes'`,
|
|
1233
|
+
* `'true'`, `1` and `true` can all map to boolean `true`)
|
|
1234
|
+
* @param map - An array of tuples describing the mapping. The first element of each tuple is the result
|
|
1235
|
+
* value, the second is the set of values that map to the result. Tuples are evaluated in the order
|
|
1236
|
+
* supplied and are not checked for duplicates.
|
|
1237
|
+
* @param message - An optional error message.
|
|
1238
|
+
* @returns A {@link Converter} which applies the mapping and yields `<T>` on success.
|
|
1239
|
+
* @public
|
|
1240
|
+
*/
|
|
1241
|
+
declare function mappedEnumeratedValue<T>(map: [T, unknown[]][], message?: string): Converter<T, undefined>;
|
|
1242
|
+
|
|
1243
|
+
/**
|
|
1244
|
+
* Aggregates sucessful result values from a collection of {@link Result | Result<T>}.
|
|
1245
|
+
* @param results - The collection of {@link Result | Result<T>} to be mapped.
|
|
1246
|
+
* @returns If all {@link Result | results} are successful, returns {@link Success} with an
|
|
1247
|
+
* array containing all returned values. If any {@link Result | results} failed, returns
|
|
1248
|
+
* {@link Failure} with a concatenated summary of all error messages.
|
|
1249
|
+
* @public
|
|
1250
|
+
*/
|
|
1251
|
+
export declare function mapResults<T>(results: Iterable<Result<T>>): Result<T[]>;
|
|
1252
|
+
|
|
1253
|
+
/**
|
|
1254
|
+
* Aggregates successful results from a a collection of {@link Result | Result<T>}.
|
|
1255
|
+
* @param results - An `Iterable` of {@link Result | Result<T>} from which success
|
|
1256
|
+
* results are to be aggregated.
|
|
1257
|
+
* @returns {@link Success} with an array of `<T>` if any results were successful. If
|
|
1258
|
+
* all {@link Result | results} failed, returns {@link Failure} with a concatenated
|
|
1259
|
+
* summary of all error messages.
|
|
1260
|
+
* @public
|
|
1261
|
+
*/
|
|
1262
|
+
export declare function mapSuccess<T>(results: Iterable<Result<T>>): Result<T[]>;
|
|
1263
|
+
|
|
1264
|
+
/**
|
|
1265
|
+
* Applies a factory method to convert a `Map<TK, TS>` into a `Record<TK, TD>`.
|
|
1266
|
+
* @param src - The `Map` object to be converted.
|
|
1267
|
+
* @param factory - The factory method used to convert elements.
|
|
1268
|
+
* @returns {@link Success} with the resulting `Record<TK, TD>` if conversion succeeds, or
|
|
1269
|
+
* {@link Failure} with an error message if an error occurs.
|
|
1270
|
+
* @public
|
|
1271
|
+
*/
|
|
1272
|
+
export declare function mapToRecord<TS, TD, TK extends string = string>(src: Map<TK, TS>, factory: KeyedThingFactory<TS, TD, TK>): Result<Record<TK, TD>>;
|
|
1273
|
+
|
|
1274
|
+
/**
|
|
1275
|
+
* Computes a normalized hash for an arbitrary javascript value.
|
|
1276
|
+
* @public
|
|
1277
|
+
*/
|
|
1278
|
+
declare class Normalizer {
|
|
1279
|
+
/**
|
|
1280
|
+
* Computes a normalized md5 hash from an arbitrary supplied object. Not secure and not
|
|
1281
|
+
* intended to be secure. Also not fast and not intended to be fast.
|
|
1282
|
+
*
|
|
1283
|
+
* Normalization just sorts Maps, Sets and object keys by hash so that differences in order
|
|
1284
|
+
* do not affect the hash.
|
|
1285
|
+
*
|
|
1286
|
+
* @param from - The arbitrary `unknown` to be hashed.
|
|
1287
|
+
* @returns A normalized md5 hash for the supplied value.
|
|
1288
|
+
*/
|
|
1289
|
+
computeHash(from: unknown): Result<string>;
|
|
1290
|
+
/**
|
|
1291
|
+
* Compares two property names from some object being normalized.
|
|
1292
|
+
* @param k1 - First key to be compared.
|
|
1293
|
+
* @param k2 - Second key to be compared.
|
|
1294
|
+
* @returns `1` if `k1` is greater, `-1` if `k2` is greater and
|
|
1295
|
+
* `0` if they are equal.
|
|
1296
|
+
* @internal
|
|
1297
|
+
*/
|
|
1298
|
+
protected _compareKeys(k1: unknown, k2: unknown): number;
|
|
1299
|
+
/**
|
|
1300
|
+
* Normalizes an array of object property entries (e.g. as returned by `Object.entries()`).
|
|
1301
|
+
* @remarks
|
|
1302
|
+
* Converts property names (entry key) to string and then sorts as string.
|
|
1303
|
+
* @param entries - The entries to be normalized.
|
|
1304
|
+
* @returns A normalized sorted array of entries.
|
|
1305
|
+
* @internal
|
|
1306
|
+
*/
|
|
1307
|
+
protected _normalizeEntries(entries: Iterable<[unknown, unknown]>): [unknown, unknown][];
|
|
1308
|
+
/**
|
|
1309
|
+
* Constructs a normalized string representation of some literal value.
|
|
1310
|
+
* @param from - The literal value to be normalized.
|
|
1311
|
+
* @returns A normalized string representation of the literal.
|
|
1312
|
+
* @internal
|
|
1313
|
+
*/
|
|
1314
|
+
protected _normalizeLiteral(from: string | number | bigint | boolean | symbol | undefined | Date | RegExp | null): Result<string>;
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
/**
|
|
1318
|
+
* A {@link Converter} which converts `unknown` to a `number`.
|
|
1319
|
+
* @remarks
|
|
1320
|
+
* Numbers and strings with a numeric format succeed. Anything else fails.
|
|
1321
|
+
* @public
|
|
1322
|
+
*/
|
|
1323
|
+
declare const number: BaseConverter<number, undefined>;
|
|
1324
|
+
|
|
1325
|
+
/**
|
|
1326
|
+
* A {@link Validation.Classes.NumberValidator | NumberValidator} which validates a number in place.
|
|
1327
|
+
* @public
|
|
1328
|
+
*/
|
|
1329
|
+
declare const number_2: NumberValidator<number, unknown>;
|
|
1330
|
+
|
|
1331
|
+
/**
|
|
1332
|
+
* {@link Converter} to convert an `unknown` to an array of `number`.
|
|
1333
|
+
* @remarks
|
|
1334
|
+
* Returns {@link Success} with the the supplied value if it as an array
|
|
1335
|
+
* of numbers, returns {@link Failure} with an error message otherwise.
|
|
1336
|
+
* @public
|
|
1337
|
+
*/
|
|
1338
|
+
declare const numberArray: Converter<number[], undefined>;
|
|
1339
|
+
|
|
1340
|
+
/**
|
|
1341
|
+
* An in-place {@link Validation.Validator | Validator} for `number` values.
|
|
1342
|
+
* @public
|
|
1343
|
+
*/
|
|
1344
|
+
declare class NumberValidator<T extends number = number, TC = unknown> extends GenericValidator<T, TC> {
|
|
1345
|
+
/**
|
|
1346
|
+
* Constructs a new {@link Validation.Classes.NumberValidator | NumberValidator}.
|
|
1347
|
+
* @param params - Optional {@link Validation.Classes.NumberValidatorConstructorParams | init params} for the
|
|
1348
|
+
* new {@link Validation.Classes.NumberValidator | NumberValidator}.
|
|
1349
|
+
*/
|
|
1350
|
+
constructor(params?: NumberValidatorConstructorParams<T, TC>);
|
|
1351
|
+
/**
|
|
1352
|
+
* Static method which validates that a supplied `unknown` value is a `number`.
|
|
1353
|
+
* @param from - The `unknown` value to be tested.
|
|
1354
|
+
* @returns Returns `true` if `from` is a `number`, or {@link Failure} with an error
|
|
1355
|
+
* message if not.
|
|
1356
|
+
*/
|
|
1357
|
+
static validateNumber<T extends number>(from: unknown): boolean | Failure<T>;
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
/**
|
|
1361
|
+
* Parameters used to construct a {@link Validation.Classes.NumberValidator | NumberValidator}.
|
|
1362
|
+
* @public
|
|
1363
|
+
*/
|
|
1364
|
+
declare type NumberValidatorConstructorParams<T extends number = number, TC = unknown> = GenericValidatorConstructorParams<T, TC>;
|
|
1365
|
+
|
|
1366
|
+
/**
|
|
1367
|
+
* Helper function to create a {@link Converters.ObjectConverter | ObjectConverter<T>} which converts an object
|
|
1368
|
+
* without changing shape, given a {@link Converters.FieldConverters | FieldConverters<T>} and an optional
|
|
1369
|
+
* {@link Converters.ObjectConverterOptions | ObjectConverterOptions<T>} to further refine conversion beavior.
|
|
1370
|
+
* @remarks
|
|
1371
|
+
* By default, if all of the requested fields exist and can be converted, returns {@link Success}
|
|
1372
|
+
* with a new object that contains the converted values under the original key names. If any requried properties
|
|
1373
|
+
* do not exist or cannot be converted, the entire conversion fails, returning {@link Failure} with additional
|
|
1374
|
+
* error information.
|
|
1375
|
+
*
|
|
1376
|
+
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
1377
|
+
* fail the conversion.
|
|
1378
|
+
* @param properties - An {@link Converters.FieldConverters | FieldConverters<T>} defining the shape of the
|
|
1379
|
+
* source object and {@link Converter | converters} to be applied to each properties.
|
|
1380
|
+
* @param options - An {@link Converters.ObjectConverterOptions | ObjectConverterOptions<T>} containing options
|
|
1381
|
+
* for the object converter.
|
|
1382
|
+
* @returns A new {@link Converters.ObjectConverter | ObjectConverter} which applies the specified conversions.
|
|
1383
|
+
* {@label withOptions}
|
|
1384
|
+
* @public
|
|
1385
|
+
*/
|
|
1386
|
+
declare function object<T>(properties: FieldConverters<T>, options?: ObjectConverterOptions<T>): ObjectConverter<T>;
|
|
1387
|
+
|
|
1388
|
+
/**
|
|
1389
|
+
* Helper function to create a {@link Converters.ObjectConverter | ObjectConverter<T>} which converts an object
|
|
1390
|
+
* without changing shape, given a {@link Converters.FieldConverters | FieldConverters<T>} and a set of
|
|
1391
|
+
* optional properties.
|
|
1392
|
+
* @remarks
|
|
1393
|
+
* By default, if all of the requested fields exist and can be converted, returns {@link Success}
|
|
1394
|
+
* with a new object that contains the converted values under the original key names. If any requried properties
|
|
1395
|
+
* do not exist or cannot be converted, the entire conversion fails, returning {@link Failure} with additional
|
|
1396
|
+
* error information.
|
|
1397
|
+
*
|
|
1398
|
+
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
1399
|
+
* fail the conversion.
|
|
1400
|
+
* @param properties - An {@link Converters.FieldConverters | FieldConverters<T>} defining the shape of the
|
|
1401
|
+
* source object and {@link Converter | converters} to be applied to each properties.
|
|
1402
|
+
* @param optional - An array of `(keyof T)` listing the keys to be considered optional.
|
|
1403
|
+
* {@label withKeys}
|
|
1404
|
+
* @returns A new {@link Converters.ObjectConverter | ObjectConverter} which applies the specified conversions.
|
|
1405
|
+
* @public
|
|
1406
|
+
* @deprecated Use {@link Converters.(object:withOptions) | Converters.object(fields, options)} instead.
|
|
1407
|
+
*/
|
|
1408
|
+
declare function object<T>(properties: FieldConverters<T>, optional: (keyof T)[]): ObjectConverter<T>;
|
|
1409
|
+
|
|
1410
|
+
/**
|
|
1411
|
+
* Helper function to create a {@link Validation.Classes.ObjectValidator | ObjectValidator} which validates
|
|
1412
|
+
* an object in place.
|
|
1413
|
+
* @param fields - A {@link Validation.Classes.FieldValidators | field validator definition}
|
|
1414
|
+
* describing the validations to be applied.
|
|
1415
|
+
* @param params - Optional {@link Validation.Classes.ObjectValidatorConstructorParams | parameters}
|
|
1416
|
+
* to refine the behavior of the resulting {@link Validation.Validator | validator}.
|
|
1417
|
+
* @returns A new {@link Validation.Validator | Validator} which validates the desired
|
|
1418
|
+
* object in place.
|
|
1419
|
+
* @public
|
|
1420
|
+
*/
|
|
1421
|
+
declare function object_2<T, TC>(fields: FieldValidators<T, TC>, params?: Omit<ObjectValidatorConstructorParams<T, TC>, 'fields'>): ObjectValidator<T, TC>;
|
|
1422
|
+
|
|
1423
|
+
/**
|
|
1424
|
+
* A {@link Converter} which converts an object of type `<T>` without changing shape, given
|
|
1425
|
+
* a {@link Converters.FieldConverters | FieldConverters<T>} for the fields in the object.
|
|
1426
|
+
* @remarks
|
|
1427
|
+
* By default, if all of the required fields exist and can be converted, returns a new object with
|
|
1428
|
+
* the converted values under the original key names. If any required fields do not exist or cannot
|
|
1429
|
+
* be converted, the entire conversion fails. See {@link Converters.ObjectConverterOptions | ObjectConverterOptions}
|
|
1430
|
+
* for other conversion options.
|
|
1431
|
+
* @public
|
|
1432
|
+
*/
|
|
1433
|
+
declare class ObjectConverter<T, TC = unknown> extends BaseConverter<T, TC> {
|
|
1434
|
+
/**
|
|
1435
|
+
* Fields converted by this {@link Converters.ObjectConverter | ObjectConverter}.
|
|
1436
|
+
*/
|
|
1437
|
+
readonly fields: FieldConverters<T>;
|
|
1438
|
+
/**
|
|
1439
|
+
* Options used to initialize this {@link Converters.ObjectConverter | ObjectConverter}.
|
|
1440
|
+
*/
|
|
1441
|
+
readonly options: ObjectConverterOptions<T>;
|
|
1442
|
+
/**
|
|
1443
|
+
* Constructs a new {@link Converters.ObjectConverter | ObjectConverter<T>} using options
|
|
1444
|
+
* supplied in a {@link Converters.ObjectConverterOptions | ObjectConverterOptions<T>}.
|
|
1445
|
+
* @param fields - A {@link Converters.FieldConverters | FieldConverters<T>} containing
|
|
1446
|
+
* a {@link Converter} for each field
|
|
1447
|
+
* @param options - An optional @see ObjectConverterOptions to configure the conversion
|
|
1448
|
+
* {@label withOptions}
|
|
1449
|
+
*/
|
|
1450
|
+
constructor(fields: FieldConverters<T, TC>, options?: ObjectConverterOptions<T>);
|
|
1451
|
+
/**
|
|
1452
|
+
* Constructs a new {@link Converters.ObjectConverter | ObjectConverter<T>} with optional
|
|
1453
|
+
* properties specified as an array of `keyof T`.
|
|
1454
|
+
* @param fields - A {@link Converters.FieldConverters | FieldConverters<T>} containing
|
|
1455
|
+
* a {@link Converter} for each field.
|
|
1456
|
+
* @param optional - An array of `keyof T` listing fields that are not required.
|
|
1457
|
+
* {@label withKeys}
|
|
1458
|
+
*/
|
|
1459
|
+
constructor(fields: FieldConverters<T, TC>, optional?: (keyof T)[]);
|
|
1460
|
+
/**
|
|
1461
|
+
* Creates a new {@link Converters.ObjectConverter | ObjectConverter} derived from this one but with
|
|
1462
|
+
* new optional properties as specified by a supplied {@link Converters.ObjectConverterOptions | ObjectConverterOptions<T>}.
|
|
1463
|
+
* @param options - The {@link Converters.ObjectConverterOptions | options} to be applied to the new
|
|
1464
|
+
* converter.
|
|
1465
|
+
* @returns A new {@link Converters.ObjectConverter | ObjectConverter} with the additional optional source properties.
|
|
1466
|
+
* {@label withOptions}
|
|
1467
|
+
*/
|
|
1468
|
+
partial(options: ObjectConverterOptions<T>): ObjectConverter<Partial<T>, TC>;
|
|
1469
|
+
/**
|
|
1470
|
+
* Creates a new {@link Converters.ObjectConverter | ObjectConverter} derived from this one but with
|
|
1471
|
+
* new optional properties as specified by a supplied array of `keyof T`.
|
|
1472
|
+
* @param optional - The keys of the source object properties to be made optional.
|
|
1473
|
+
* @returns A new {@link Converters.ObjectConverter | ObjectConverter} with the additional optional source
|
|
1474
|
+
* properties.
|
|
1475
|
+
* {@label withKeys}
|
|
1476
|
+
*/
|
|
1477
|
+
partial(optional?: (keyof T)[]): ObjectConverter<Partial<T>, TC>;
|
|
1478
|
+
/**
|
|
1479
|
+
* Creates a new {@link Converters.ObjectConverter | ObjectConverter} derived from this one but with
|
|
1480
|
+
* new optional properties as specified by a supplied array of `keyof T`.
|
|
1481
|
+
* @param addOptionalProperties - The keys to be made optional.
|
|
1482
|
+
* @returns A new {@link Converters.ObjectConverter | ObjectConverter} with the additional optional source
|
|
1483
|
+
* properties.
|
|
1484
|
+
*/
|
|
1485
|
+
addPartial(addOptionalProperties: (keyof T)[]): ObjectConverter<Partial<T>, TC>;
|
|
1486
|
+
}
|
|
1487
|
+
|
|
1488
|
+
/**
|
|
1489
|
+
* Options for an {@link Converters.ObjectConverter | ObjectConverter}.
|
|
1490
|
+
* @public
|
|
1491
|
+
*/
|
|
1492
|
+
declare interface ObjectConverterOptions<T> {
|
|
1493
|
+
/**
|
|
1494
|
+
* If present, lists optional fields. Missing non-optional fields cause an error.
|
|
1495
|
+
*/
|
|
1496
|
+
optionalFields?: (keyof T)[];
|
|
1497
|
+
/**
|
|
1498
|
+
* If true, unrecognized fields yield an error. If false or undefined (default),
|
|
1499
|
+
* unrecognized fields are ignored.
|
|
1500
|
+
*/
|
|
1501
|
+
strict?: boolean;
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
/**
|
|
1505
|
+
* In-place {@link Validation.Validator | Validator} for an object of type `<T>`.
|
|
1506
|
+
* @remarks
|
|
1507
|
+
* By default, succeeds if all of the required fields exist and are validate, and fails if
|
|
1508
|
+
* any required fields do not exist or are invalid. See {@link Validation.Classes.ObjectValidatorOptions}
|
|
1509
|
+
* for other validation options.
|
|
1510
|
+
* @public
|
|
1511
|
+
*/
|
|
1512
|
+
declare class ObjectValidator<T, TC = unknown> extends ValidatorBase<T, TC> {
|
|
1513
|
+
/**
|
|
1514
|
+
* A {@link Validation.Classes.FieldValidators | FieldValidators} object specifying a
|
|
1515
|
+
* {@link Validation.Validator | Validator} for each of the expected properties
|
|
1516
|
+
*/
|
|
1517
|
+
readonly fields: FieldValidators<T>;
|
|
1518
|
+
/**
|
|
1519
|
+
* {@link Validation.Classes.ObjectValidatorOptions | Options} which apply to this
|
|
1520
|
+
* validator.
|
|
1521
|
+
*/
|
|
1522
|
+
readonly options: ObjectValidatorOptions<T, TC>;
|
|
1523
|
+
/**
|
|
1524
|
+
* @internal
|
|
1525
|
+
*/
|
|
1526
|
+
protected readonly _innerValidators: FieldValidators<T>;
|
|
1527
|
+
/**
|
|
1528
|
+
* @internal
|
|
1529
|
+
*/
|
|
1530
|
+
protected readonly _allowedFields?: Set<keyof T>;
|
|
1531
|
+
/**
|
|
1532
|
+
* Constructs a new {@link Validation.Classes.ObjectValidator | ObjectValidator<T>}.
|
|
1533
|
+
* @param fields - A {@link Validation.Classes.FieldValidators | FieldValidators<T>} containing
|
|
1534
|
+
* a {@link Validation.Validator | Validator} for each field.
|
|
1535
|
+
* @param options - An optional {@link Validation.Classes.ObjectValidatorOptions} to configure
|
|
1536
|
+
* validation.
|
|
1537
|
+
*/
|
|
1538
|
+
constructor(params: ObjectValidatorConstructorParams<T, TC>);
|
|
1539
|
+
/**
|
|
1540
|
+
* Creates the actual {@link Validation.Classes.FieldValidators | FieldValidators<T>} to be
|
|
1541
|
+
* used by this converter by applying any options or traits defined in the options
|
|
1542
|
+
* to the field converters passed to the constructor.
|
|
1543
|
+
* @param fields - The base {@link Validation.Classes.FieldValidators | FieldValidators<T>} passed
|
|
1544
|
+
* in to the constructor.
|
|
1545
|
+
* @param options - The {@link Validation.Classes.ObjectValidatorOptions | object validator options}
|
|
1546
|
+
* passed in to the constructor.
|
|
1547
|
+
* @returns A new {@link Validation.Classes.FieldValidators | FieldValidators} with the fully-configured
|
|
1548
|
+
* individual {@link Validation.Validator | field validators} to be applied.
|
|
1549
|
+
* @internal
|
|
1550
|
+
*/
|
|
1551
|
+
protected static _resolveValidators<T, TC>(fields: FieldValidators<T, TC>, options?: ObjectValidatorOptions<T, TC>): FieldValidators<T, TC>;
|
|
1552
|
+
/**
|
|
1553
|
+
* Creates a new {@link Validation.Classes.ObjectValidator | ObjectValidator} derived from this one but with
|
|
1554
|
+
* new optional properties as specified by a supplied
|
|
1555
|
+
* {@link Validation.Classes.ObjectValidatorOptions | ObjectValidatorOptions<T>}.
|
|
1556
|
+
* @param options - The {@link Validation.Classes.ObjectValidatorOptions | options} to be applied to the new
|
|
1557
|
+
* {@link Validation.Classes.ObjectValidator | validator}.
|
|
1558
|
+
* @returns A new {@link Validation.Classes.ObjectValidator | ObjectValidator} with the additional optional
|
|
1559
|
+
* source properties.
|
|
1560
|
+
*/
|
|
1561
|
+
partial(options?: ObjectValidatorOptions<T, TC>): ObjectValidator<Partial<T>, TC>;
|
|
1562
|
+
/**
|
|
1563
|
+
* Creates a new {@link Validation.Classes.ObjectValidator | ObjectValidator} derived from this one but with
|
|
1564
|
+
* new optional properties as specified by a supplied array of `keyof T`.
|
|
1565
|
+
* @param addOptionalProperties - The keys to be made optional.
|
|
1566
|
+
* @returns A new {@link Validation.Classes.ObjectValidator | ObjectValidator} with the additional optional
|
|
1567
|
+
* source properties.
|
|
1568
|
+
*/
|
|
1569
|
+
addPartial(addOptionalFields: (keyof T)[]): ObjectValidator<Partial<T>, TC>;
|
|
1570
|
+
/**
|
|
1571
|
+
* {@inheritdoc Validation.ValidatorBase._validate}
|
|
1572
|
+
* @internal
|
|
1573
|
+
*/
|
|
1574
|
+
protected _validate(from: unknown, context?: TC): boolean | Failure<T>;
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
/**
|
|
1578
|
+
* Options for the {@link Validation.Classes.ObjectValidator | ObjectValidator} constructor.
|
|
1579
|
+
* @public
|
|
1580
|
+
*/
|
|
1581
|
+
declare interface ObjectValidatorConstructorParams<T, TC> extends ValidatorBaseConstructorParams<T, TC> {
|
|
1582
|
+
/**
|
|
1583
|
+
* A {@link Validation.Classes.FieldValidators | FieldValidators} object specifying a
|
|
1584
|
+
* {@link Validation.Validator | Validator} for each of the expected properties
|
|
1585
|
+
* of a result object.
|
|
1586
|
+
*/
|
|
1587
|
+
fields: FieldValidators<T>;
|
|
1588
|
+
/**
|
|
1589
|
+
* Optional additional {@link Validation.Classes.ObjectValidatorOptions | ValidatorOptions} to
|
|
1590
|
+
* configure validation.
|
|
1591
|
+
*/
|
|
1592
|
+
options?: ObjectValidatorOptions<T, TC>;
|
|
1593
|
+
}
|
|
1594
|
+
|
|
1595
|
+
/**
|
|
1596
|
+
* Options for an {@link Validation.Classes.ObjectValidator | ObjectValidator}.
|
|
1597
|
+
* @public
|
|
1598
|
+
*/
|
|
1599
|
+
declare interface ObjectValidatorOptions<T, TC> extends ValidatorOptions<TC> {
|
|
1600
|
+
/**
|
|
1601
|
+
* If present, lists optional fields. Missing non-optional fields cause an error.
|
|
1602
|
+
*/
|
|
1603
|
+
optionalFields?: (keyof T)[];
|
|
1604
|
+
/**
|
|
1605
|
+
* If true, unrecognized fields yield an error. If false or undefined (default),
|
|
1606
|
+
* unrecognized fields are ignored.
|
|
1607
|
+
*/
|
|
1608
|
+
strict?: boolean;
|
|
1609
|
+
}
|
|
1610
|
+
|
|
1611
|
+
/**
|
|
1612
|
+
* A helper function to create a {@link Converter} for polymorphic values. Returns a
|
|
1613
|
+
* converter which Invokes the wrapped converters in sequence, returning the first successful
|
|
1614
|
+
* result. Returns an error if none of the supplied converters can convert the value.
|
|
1615
|
+
* @remarks
|
|
1616
|
+
* If `onError` is `ignoreErrors` (default), then errors from any of the
|
|
1617
|
+
* converters are ignored provided that some converter succeeds. If
|
|
1618
|
+
* onError is `failOnError`, then an error from any converter fails the entire
|
|
1619
|
+
* conversion.
|
|
1620
|
+
*
|
|
1621
|
+
* @param converters - An ordered list of {@link Converter | converters} to be considered.
|
|
1622
|
+
* @param onError - Specifies treatment of unconvertable elements.
|
|
1623
|
+
* @returns A new {@link Converter} which yields a value from the union of the types returned
|
|
1624
|
+
* by the wrapped converters.
|
|
1625
|
+
* @public
|
|
1626
|
+
*/
|
|
1627
|
+
declare function oneOf<T, TC = unknown>(converters: Array<Converter<T, TC>>, onError?: OnError_2): Converter<T, TC>;
|
|
1628
|
+
|
|
1629
|
+
declare type OnError = 'failOnError' | 'ignoreErrors';
|
|
1630
|
+
|
|
1631
|
+
declare type OnError_2 = 'failOnError' | 'ignoreErrors';
|
|
1632
|
+
|
|
1633
|
+
/**
|
|
1634
|
+
* A {@link Converter} to convert an optional `boolean` value.
|
|
1635
|
+
* @remarks
|
|
1636
|
+
* Values of type `boolean` or strings that match (case-insensitive) `'true'`
|
|
1637
|
+
* or `'false'` are converted and returned. Anything else returns {@link Success}
|
|
1638
|
+
* with value `undefined`.
|
|
1639
|
+
* @public
|
|
1640
|
+
*/
|
|
1641
|
+
declare const optionalBoolean: Converter<boolean | undefined, undefined>;
|
|
1642
|
+
|
|
1643
|
+
/**
|
|
1644
|
+
* A helper function to create a {@link Converter} which extracts and converts an optional element from an array.
|
|
1645
|
+
* @remarks
|
|
1646
|
+
* The resulting {@link Converter} returns {@link Success} with the converted value if the element exsist
|
|
1647
|
+
* in the supplied array and can be converted. Returns {@link Success} with value `undefined` if the parameter
|
|
1648
|
+
* is an array but the index is out of range. Returns {@link Failure} with a message if the supplied parameter
|
|
1649
|
+
* is not an array, if the requested index is negative, or if the element cannot be converted.
|
|
1650
|
+
* @param index - The index of the element to be extracted.
|
|
1651
|
+
* @param converter - A {@link Converter} used to convert the extracted element.
|
|
1652
|
+
* @returns A {@link Converter | Converter<T>} which extracts the specified element from an array.
|
|
1653
|
+
* @public
|
|
1654
|
+
*/
|
|
1655
|
+
declare function optionalElement<T, TC = undefined>(index: number, converter: Converter<T, TC>): Converter<T | undefined, TC>;
|
|
1656
|
+
|
|
1657
|
+
/**
|
|
1658
|
+
* A helper function to create a {@link Converter} which extracts and convert a property specified
|
|
1659
|
+
* by name from an object.
|
|
1660
|
+
* @remarks
|
|
1661
|
+
* The resulting {@link Converter} returns {@link Success} with the converted value of the correpsonding
|
|
1662
|
+
* object property if the field exists and can be converted. Returns {@link Success} with value `undefined`
|
|
1663
|
+
* if the supplied parametr is an object but the named field is not present. Returns {@link Failure} with
|
|
1664
|
+
* an error message otherwise.
|
|
1665
|
+
* @param name - The name of the field to be extracted.
|
|
1666
|
+
* @param converter - {@link Converter} used to convert the extracted field.
|
|
1667
|
+
* @public
|
|
1668
|
+
*/
|
|
1669
|
+
declare function optionalField<T, TC = undefined>(name: string, converter: Converter<T, TC>): Converter<T | undefined, TC>;
|
|
1670
|
+
|
|
1671
|
+
/**
|
|
1672
|
+
* Applies a factory method to convert an optional `Map<string, TS>` into a `Record<string, TD>`
|
|
1673
|
+
* @param src - The `Map` object to be converted, or `undefined`.
|
|
1674
|
+
* @param factory - The factory method used to convert elements.
|
|
1675
|
+
* @returns {@link Success} with the resulting record (empty if `src` is `undefined`) if conversion succeeds.
|
|
1676
|
+
* Returns {@link Failure} with a message if an error occurs.
|
|
1677
|
+
* @public
|
|
1678
|
+
*/
|
|
1679
|
+
export declare function optionalMapToPossiblyEmptyRecord<TS, TD, TK extends string = string>(src: Map<TK, TS> | undefined, factory: KeyedThingFactory<TS, TD, TK>): Result<Record<TK, TD>>;
|
|
1680
|
+
|
|
1681
|
+
/**
|
|
1682
|
+
* Applies a factory method to convert an optional `Map<string, TS>` into a `Record<string, TD>` or `undefined`.
|
|
1683
|
+
* @param src - The `Map` object to be converted, or `undefined`.
|
|
1684
|
+
* @param factory - The factory method used to convert elements.
|
|
1685
|
+
* @returns {@link Success} with the resulting record if conversion succeeds, or {@link Success} with `undefined` if
|
|
1686
|
+
* `src` is `undefined`. Returns {@link Failure} with a message if an error occurs.
|
|
1687
|
+
* @public
|
|
1688
|
+
*/
|
|
1689
|
+
export declare function optionalMapToRecord<TS, TD, TK extends string = string>(src: Map<TK, TS> | undefined, factory: KeyedThingFactory<TS, TD, TK>): Result<Record<TK, TD> | undefined>;
|
|
1690
|
+
|
|
1691
|
+
/**
|
|
1692
|
+
* A {@link Converter} which converts an optional `number` value.
|
|
1693
|
+
* @remarks
|
|
1694
|
+
* Values of type `number` or numeric strings are converted and returned.
|
|
1695
|
+
* Anything else returns {@link Success} with value `undefined`.
|
|
1696
|
+
* @public
|
|
1697
|
+
*/
|
|
1698
|
+
declare const optionalNumber: Converter<number | undefined, undefined>;
|
|
1699
|
+
|
|
1700
|
+
/**
|
|
1701
|
+
* Applies a factory method to convert an optional `Record<TK, TS>` into a `Map<TK, TD>`, or `undefined`.
|
|
1702
|
+
* @param src - The `Record` to be converted, or undefined.
|
|
1703
|
+
* @param factory - The factory method used to convert elements.
|
|
1704
|
+
* @returns {@link Success} with the resulting map if conversion succeeds, or {@link Success} with `undefined`
|
|
1705
|
+
* if `src` is `undefined`. Returns {@link Failure} with a message if an error occurs.
|
|
1706
|
+
* @public
|
|
1707
|
+
*/
|
|
1708
|
+
export declare function optionalRecordToMap<TS, TD, TK extends string = string>(src: Record<TK, TS> | undefined, factory: KeyedThingFactory<TS, TD, TK>): Result<Map<TK, TD> | undefined>;
|
|
1709
|
+
|
|
1710
|
+
/**
|
|
1711
|
+
* Applies a factory method to convert an optional `Record<TK, TS>` into a `Map<TK, TD>`
|
|
1712
|
+
* @param src - The `Record` to be converted, or `undefined`.
|
|
1713
|
+
* @param factory - The factory method used to convert elements.
|
|
1714
|
+
* @returns {@link Success} with the resulting map (empty if `src` is `undefined`) if conversion succeeds.
|
|
1715
|
+
* Returns {@link Failure} with a message if an error occurs.
|
|
1716
|
+
* @public
|
|
1717
|
+
*/
|
|
1718
|
+
export declare function optionalRecordToPossiblyEmptyMap<TS, TD, TK extends string = string>(src: Record<TK, TS> | undefined, factory: KeyedThingFactory<TS, TD, TK>): Result<Map<TK, TD>>;
|
|
1719
|
+
|
|
1720
|
+
/**
|
|
1721
|
+
* A {@link Converter} which converts an optional `string` value. Values of type
|
|
1722
|
+
* `string` are returned. Anything else returns {@link Success} with value `undefined`.
|
|
1723
|
+
* @public
|
|
1724
|
+
*/
|
|
1725
|
+
declare const optionalString: Converter<string | undefined, unknown>;
|
|
1726
|
+
|
|
1727
|
+
/**
|
|
1728
|
+
* Populates an an object based on a prototype full of field initializers that return {@link Result | Result<T[key]>}.
|
|
1729
|
+
* Returns {@link Success} with the populated object if all initializers succeed, or {@link Failure} with a
|
|
1730
|
+
* concatenated list of all error messages.
|
|
1731
|
+
* @param initializers - An object with the shape of the target but with initializer functions for
|
|
1732
|
+
* each property.
|
|
1733
|
+
* @public
|
|
1734
|
+
*/
|
|
1735
|
+
export declare function populateObject<T>(initializers: FieldInitializers<T>, order?: (keyof T)[]): Result<T>;
|
|
1736
|
+
|
|
1737
|
+
/**
|
|
1738
|
+
* Propagates a {@link Success} or {@link Failure} {@link Result}, adding supplied
|
|
1739
|
+
* event details as appropriate.
|
|
1740
|
+
* @param result - The {@link Result} to be propagated.
|
|
1741
|
+
* @param detail - The event detail (type `<TD>`) to be added to the {@link Result | result}.
|
|
1742
|
+
* @param successDetail - An optional distinct event detail to be added to {@link Success} results. If `successDetail`
|
|
1743
|
+
* is omitted or `undefined`, then `detail` will be applied to {@link Success} results.
|
|
1744
|
+
* @returns A new {@link DetailedResult | DetailedResult<T, TD>} with the success value or error
|
|
1745
|
+
* message from the original `result` but with the specified detail added.
|
|
1746
|
+
* @public
|
|
1747
|
+
*/
|
|
1748
|
+
export declare function propagateWithDetail<T, TD>(result: Result<T>, detail: TD, successDetail?: TD): DetailedResult<T, TD>;
|
|
1749
|
+
|
|
1750
|
+
/**
|
|
1751
|
+
* Simple implementation of a possibly open-ended range of some comparable
|
|
1752
|
+
* type `<T>` with test and formatting.
|
|
1753
|
+
* @public
|
|
1754
|
+
*/
|
|
1755
|
+
export declare class RangeOf<T> implements RangeOfProperties<T> {
|
|
1756
|
+
/**
|
|
1757
|
+
* Minimum extent of the range.
|
|
1758
|
+
*/
|
|
1759
|
+
readonly min?: T;
|
|
1760
|
+
/**
|
|
1761
|
+
* Maximum extent of the range.
|
|
1762
|
+
*/
|
|
1763
|
+
readonly max?: T;
|
|
1764
|
+
/**
|
|
1765
|
+
* Creates a new {@link RangeOf | RangeOf<T>}.
|
|
1766
|
+
* @param min - Optional mininum extent of the range.
|
|
1767
|
+
* @param max - Optional maximum extent of the range.
|
|
1768
|
+
*/
|
|
1769
|
+
constructor(min?: T, max?: T);
|
|
1770
|
+
/**
|
|
1771
|
+
* Static constructor for a {@link RangeOf | RangeOf<T>}.
|
|
1772
|
+
* @param init - {@link RangeOfProperties | Range initializer}.
|
|
1773
|
+
* @returns A new {@link RangeOf | RangeOf<T>}.
|
|
1774
|
+
*/
|
|
1775
|
+
static createRange<T>(init?: RangeOfProperties<T>): Result<RangeOf<T>>;
|
|
1776
|
+
/**
|
|
1777
|
+
* Gets a formatted description of a {@link RangeOfProperties | RangeOfProperties<T>} given an
|
|
1778
|
+
* optional set of formats and 'empty' value to use.
|
|
1779
|
+
* @param range - The {@link RangeOfProperties | RangeOfProperties<T>} to be formatted.
|
|
1780
|
+
* @param formats - Optionas {@link RangeOfFormats | formats} to use. Default is
|
|
1781
|
+
* {@link DEFAULT_RANGEOF_FORMATS | DEFAULT_RANGEOF_FORMATS}.
|
|
1782
|
+
* @param emptyValue - Value which represents unbounded minimum or maximum for this range. Default is `undefined`.
|
|
1783
|
+
* @returns A string representation of the range.
|
|
1784
|
+
*/
|
|
1785
|
+
static propertiesToString<T>(range: RangeOfProperties<T>, formats?: RangeOfFormats, emptyValue?: T): string | undefined;
|
|
1786
|
+
/**
|
|
1787
|
+
* Default comparison uses javascript built-in comparison.
|
|
1788
|
+
* @param t1 - First value to be compared.
|
|
1789
|
+
* @param t2 - Second value to be compared.
|
|
1790
|
+
* @returns `'less'` if `t1` is less than `t2`, `'greater'` if `t1` is larger
|
|
1791
|
+
* and `'equal'` if `t1` and `t2` are equal.
|
|
1792
|
+
* @internal
|
|
1793
|
+
*/
|
|
1794
|
+
protected static _defaultCompare<T>(t1: T, t2: T): 'less' | 'equal' | 'greater';
|
|
1795
|
+
/**
|
|
1796
|
+
* Checks if a supplied value is within this range.
|
|
1797
|
+
* @param t - The value to be tested.
|
|
1798
|
+
* @returns `'included'` if `t` falls within the range, `'less'` if `t` falls
|
|
1799
|
+
* below the minimum extent of the range and `'greater'` if `t` is above the
|
|
1800
|
+
* maximum extent.
|
|
1801
|
+
*/
|
|
1802
|
+
check(t: T): 'less' | 'included' | 'greater';
|
|
1803
|
+
/**
|
|
1804
|
+
* Determines if a supplied value is within this range.
|
|
1805
|
+
* @param t - The value to be tested.
|
|
1806
|
+
* @returns Returns `true` if `t` falls within the range, `false` otherwise.
|
|
1807
|
+
*/
|
|
1808
|
+
includes(t: T): boolean;
|
|
1809
|
+
/**
|
|
1810
|
+
* Finds the transition value that would bring a supplied value `t` into
|
|
1811
|
+
* range.
|
|
1812
|
+
* @param t - The value to be tested.
|
|
1813
|
+
* @returns The minimum extent of the range if `t` is below the range or
|
|
1814
|
+
* the maximum extent of the range if `t` is above the range. Returns
|
|
1815
|
+
* `undefined` if `t` already falls within the range.
|
|
1816
|
+
*/
|
|
1817
|
+
findTransition(t: T): T | undefined;
|
|
1818
|
+
/**
|
|
1819
|
+
* Formats the minimum and maximum values of this range.
|
|
1820
|
+
* @param format - A format function used to format the values.
|
|
1821
|
+
* @returns A {@link RangeOfProperties | RangeOfProperties<string>} contaning the
|
|
1822
|
+
* formatted representation of the {@link RangeOf.min | minimum} and {@link RangeOf.max | maximum}
|
|
1823
|
+
* extent of the range, or `undefined` for an extent that is not present.
|
|
1824
|
+
*/
|
|
1825
|
+
toFormattedProperties(format: (value: T) => string | undefined): RangeOfProperties<string>;
|
|
1826
|
+
/**
|
|
1827
|
+
* Formats this range using the supplied format function.
|
|
1828
|
+
* @param format - Format function used to format minimum and maxiumum extent values.
|
|
1829
|
+
* @param formats - The {@link RangeOfFormats | format strings} used to format the range
|
|
1830
|
+
* (default {@link DEFAULT_RANGEOF_FORMATS}).
|
|
1831
|
+
* @returns Returns a formatted representation of this range.
|
|
1832
|
+
*/
|
|
1833
|
+
format(format: (value: T) => string | undefined, formats?: RangeOfFormats): string | undefined;
|
|
1834
|
+
/**
|
|
1835
|
+
* Inner compare method can be overriden by a derived class.
|
|
1836
|
+
* @param t1 - First value to compare.
|
|
1837
|
+
* @param t2 - Second value to compare.
|
|
1838
|
+
* @returns `'less'` if `t1` is less than `t2`, `'greater'` if `t1` is larger
|
|
1839
|
+
* and `'equal'` if `t1` and `t2` are equal.
|
|
1840
|
+
* @internal
|
|
1841
|
+
*/
|
|
1842
|
+
protected _compare(t1: T, t2: T): 'less' | 'equal' | 'greater';
|
|
1843
|
+
}
|
|
1844
|
+
|
|
1845
|
+
/**
|
|
1846
|
+
* A helper wrapper to construct a {@link Converter} which converts to {@link RangeOf | RangeOf<T>}
|
|
1847
|
+
* where `<T>` is some comparable type.
|
|
1848
|
+
* @param converter - {@link Converter} used to convert `min` and `max` extent of the range.
|
|
1849
|
+
* @public
|
|
1850
|
+
*/
|
|
1851
|
+
declare function rangeOf<T, TC = unknown>(converter: Converter<T, TC>): Converter<RangeOf<T>, TC>;
|
|
1852
|
+
|
|
1853
|
+
/**
|
|
1854
|
+
* Format strings (in mustache format) to
|
|
1855
|
+
* use for both open-ended and complete
|
|
1856
|
+
* {@link RangeOf | RangeOf<T>}.
|
|
1857
|
+
* @public
|
|
1858
|
+
*/
|
|
1859
|
+
export declare interface RangeOfFormats {
|
|
1860
|
+
minOnly: string;
|
|
1861
|
+
maxOnly: string;
|
|
1862
|
+
minMax: string;
|
|
1863
|
+
}
|
|
1864
|
+
|
|
1865
|
+
/**
|
|
1866
|
+
* Represents a generic range of some comparable type `<T>`.
|
|
1867
|
+
* @public
|
|
1868
|
+
*/
|
|
1869
|
+
export declare interface RangeOfProperties<T> {
|
|
1870
|
+
readonly min?: T;
|
|
1871
|
+
readonly max?: T;
|
|
1872
|
+
}
|
|
1873
|
+
|
|
1874
|
+
/**
|
|
1875
|
+
* A helper wrapper to construct a {@link Converter} which converts to an arbitrary strongly-typed
|
|
1876
|
+
* range of some comparable type.
|
|
1877
|
+
* @param converter - {@link Converter} used to convert `min` and `max` extent of the range.
|
|
1878
|
+
* @param constructor - Static constructor to instantiate the object.
|
|
1879
|
+
* @public
|
|
1880
|
+
*/
|
|
1881
|
+
declare function rangeTypeOf<T, RT extends RangeOf<T>, TC = unknown>(converter: Converter<T, TC>, constructor: (init: RangeOfProperties<T>) => Result<RT>): Converter<RT, TC>;
|
|
1882
|
+
|
|
1883
|
+
/**
|
|
1884
|
+
* Reads a CSV file from a supplied path.
|
|
1885
|
+
* @param srcPath - Source path from which the file is read.
|
|
1886
|
+
* @returns The contents of the file.
|
|
1887
|
+
* @beta
|
|
1888
|
+
*/
|
|
1889
|
+
declare function readCsvFileSync(srcPath: string): Result<unknown>;
|
|
1890
|
+
|
|
1891
|
+
/**
|
|
1892
|
+
* A helper function to create a {@link Converter} which converts the `string`-keyed properties
|
|
1893
|
+
* using a supplied {@link Converter | Converter<T>} to produce a `Record<string, T>`.
|
|
1894
|
+
* @remarks
|
|
1895
|
+
* The resulting converter fails conversion if any element cannot be converted.
|
|
1896
|
+
* @param converter - {@link Converter} used to convert each item in the source object.
|
|
1897
|
+
* @returns A {@link Converter} which returns `Record<string, T>`.
|
|
1898
|
+
* {@label default}
|
|
1899
|
+
* @public
|
|
1900
|
+
*/
|
|
1901
|
+
declare function recordOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>): Converter<Record<TK, T>, TC>;
|
|
1902
|
+
|
|
1903
|
+
/**
|
|
1904
|
+
* A helper function to create a {@link Converter} which converts the `string`-keyed properties
|
|
1905
|
+
* using a supplied {@link Converter | Converter<T>} to produce a `Record<string, T>` and optionally
|
|
1906
|
+
* specified handling of elements that cannot be converted.
|
|
1907
|
+
* @remarks
|
|
1908
|
+
* if `onError` is `'fail'` (default), then the entire conversion fails if any key or element
|
|
1909
|
+
* cannot be converted. If `onError` is `'ignore'`, failing elements are silently ignored.
|
|
1910
|
+
* @param converter - {@link Converter} used to convert each item in the source object.
|
|
1911
|
+
* @returns A {@link Converter} which returns `Record<string, T>`.
|
|
1912
|
+
* {@label withOnError}
|
|
1913
|
+
* @public
|
|
1914
|
+
*/
|
|
1915
|
+
declare function recordOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>, onError: 'fail' | 'ignore'): Converter<Record<TK, T>, TC>;
|
|
1916
|
+
|
|
1917
|
+
/**
|
|
1918
|
+
* A helper function to create a {@link Converter} which converts the `string`-keyed properties
|
|
1919
|
+
* using a supplied {@link Converter | Converter<T>} to produce a `Record<TK, T>`.
|
|
1920
|
+
* @remarks
|
|
1921
|
+
* If present, the supplied {@link Converters.KeyedConverterOptions | options} can provide a strongly-typed
|
|
1922
|
+
* converter for keys and/or control the handling of elements that fail conversion.
|
|
1923
|
+
* @param converter - {@link Converter} used to convert each item in the source object.
|
|
1924
|
+
* @param options - Optional {@link Converters.KeyedConverterOptions | KeyedConverterOptions<TK, TC>} which
|
|
1925
|
+
* supplies a key converter and/or error-handling options.
|
|
1926
|
+
* @returns A {@link Converter} which returns `Record<TK, T>`.
|
|
1927
|
+
* {@label withOptions}
|
|
1928
|
+
* @public
|
|
1929
|
+
*/
|
|
1930
|
+
declare function recordOf<T, TC = undefined, TK extends string = string>(converter: Converter<T, TC>, options: KeyedConverterOptions<TK, TC>): Converter<Record<TK, T>, TC>;
|
|
1931
|
+
|
|
1932
|
+
/**
|
|
1933
|
+
* Applies a factory method to convert a `Record<TK, TS>` into a `Map<TK, TD>`.
|
|
1934
|
+
* @param src - The `Record` to be converted.
|
|
1935
|
+
* @param factory - The factory method used to convert elements.
|
|
1936
|
+
* @returns {@link Success} with the resulting map on success, or {@link Failure} with a
|
|
1937
|
+
* message if an error occurs.
|
|
1938
|
+
* @public
|
|
1939
|
+
*/
|
|
1940
|
+
export declare function recordToMap<TS, TD, TK extends string = string>(src: Record<TK, TS>, factory: KeyedThingFactory<TS, TD, TK>): Result<Map<TK, TD>>;
|
|
1941
|
+
|
|
1942
|
+
/**
|
|
1943
|
+
* Represents the {@link IResult | result} of some operation or sequence of operations.
|
|
1944
|
+
* @remarks
|
|
1945
|
+
* {@link Success | Success<T>} and {@link Failure | Failure<T>} share the common
|
|
1946
|
+
* contract {@link IResult}, enabling comingled discriminated usage.
|
|
1947
|
+
* @public
|
|
1948
|
+
*/
|
|
1949
|
+
export declare type Result<T> = Success<T> | Failure<T>;
|
|
1950
|
+
|
|
1951
|
+
/**
|
|
1952
|
+
* Type inference to determine the detail type `TD` of a {@link DetailedResult | DetailedResult<T, TD>}.
|
|
1953
|
+
* @beta
|
|
1954
|
+
*/
|
|
1955
|
+
export declare type ResultDetailType<T> = T extends DetailedResult<unknown, infer TD> ? TD : never;
|
|
1956
|
+
|
|
1957
|
+
/**
|
|
1958
|
+
* Type inference to determine the result type of an {@link Result}.
|
|
1959
|
+
* @beta
|
|
1960
|
+
*/
|
|
1961
|
+
export declare type ResultValueType<T> = T extends Result<infer TV> ? TV : never;
|
|
1962
|
+
|
|
1963
|
+
/**
|
|
1964
|
+
* Helper function to create a {@link Converters.ObjectConverter | ObjectConverter} which converts an object
|
|
1965
|
+
* without changing shape, a {@link Converters.FieldConverters | FieldConverters<T>} and an optional
|
|
1966
|
+
* {@link Converters.StrictObjectConverterOptions | StrictObjectConverterOptions<T>} to further refine
|
|
1967
|
+
* conversion behavior.
|
|
1968
|
+
*
|
|
1969
|
+
* @remarks
|
|
1970
|
+
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
1971
|
+
* fail the conversion.
|
|
1972
|
+
*
|
|
1973
|
+
* The conversion fails if any unexpected fields are encountered.
|
|
1974
|
+
*
|
|
1975
|
+
* @param properties - An object containing defining the shape and converters to be applied.
|
|
1976
|
+
* @param options - An optional @see StrictObjectConverterOptions<T> containing options for the object converter.
|
|
1977
|
+
* @returns A new {@link Converters.ObjectConverter | ObjectConverter} which applies the specified conversions.
|
|
1978
|
+
* {@label withOptions}
|
|
1979
|
+
* @public
|
|
1980
|
+
*/
|
|
1981
|
+
declare function strictObject<T>(properties: FieldConverters<T>, options?: StrictObjectConverterOptions<T>): ObjectConverter<T>;
|
|
1982
|
+
|
|
1983
|
+
/**
|
|
1984
|
+
* Helper function to create a {@link Converters.ObjectConverter | ObjectConverter} which converts an object
|
|
1985
|
+
* without changing shape, a {@link Converters.FieldConverters | FieldConverters<T>} and an optional
|
|
1986
|
+
* {@link Converters.StrictObjectConverterOptions | StrictObjectConverterOptions<T>} to further refine
|
|
1987
|
+
* conversion behavior.
|
|
1988
|
+
*
|
|
1989
|
+
* @remarks
|
|
1990
|
+
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
1991
|
+
* fail the conversion.
|
|
1992
|
+
*
|
|
1993
|
+
* The conversion fails if any unexpected fields are encountered.
|
|
1994
|
+
*
|
|
1995
|
+
* @param properties - An object containing defining the shape and converters to be applied.
|
|
1996
|
+
* @param optional - An array of `keyof T` containing keys to be considered optional.
|
|
1997
|
+
* @returns A new {@link Converters.ObjectConverter | ObjectConverter} which applies the specified conversions.
|
|
1998
|
+
* {@label withKeys}
|
|
1999
|
+
* @deprecated Use {@link Converters.(strictObject:withOptions) | Converters.strictObject(options)} instead.
|
|
2000
|
+
* @public
|
|
2001
|
+
*/
|
|
2002
|
+
declare function strictObject<T>(properties: FieldConverters<T>, optional: (keyof T)[]): ObjectConverter<T>;
|
|
2003
|
+
|
|
2004
|
+
/**
|
|
2005
|
+
* Options for the {@link Converters.(strictObject:withOptions)} helper function.
|
|
2006
|
+
* @public
|
|
2007
|
+
*/
|
|
2008
|
+
declare type StrictObjectConverterOptions<T> = Omit<ObjectConverterOptions<T>, 'strict'>;
|
|
2009
|
+
|
|
2010
|
+
/**
|
|
2011
|
+
* A converter to convert unknown to string. Values of type
|
|
2012
|
+
* string succeed. Anything else fails.
|
|
2013
|
+
* @public
|
|
2014
|
+
*/
|
|
2015
|
+
declare const string: StringConverter<string, unknown>;
|
|
2016
|
+
|
|
2017
|
+
/**
|
|
2018
|
+
* A {@link Validation.Classes.StringValidator | StringValidator} which validates a string in place.
|
|
2019
|
+
* @public
|
|
2020
|
+
*/
|
|
2021
|
+
declare const string_2: StringValidator<string, unknown>;
|
|
2022
|
+
|
|
2023
|
+
/**
|
|
2024
|
+
* {@link Converter} to convert an `unknown` to an array of `string`.
|
|
2025
|
+
* @remarks
|
|
2026
|
+
* Returns {@link Success} with the the supplied value if it as an array
|
|
2027
|
+
* of strings, returns {@link Failure} with an error message otherwise.
|
|
2028
|
+
* @public
|
|
2029
|
+
*/
|
|
2030
|
+
declare const stringArray: Converter<string[], unknown>;
|
|
2031
|
+
|
|
2032
|
+
/**
|
|
2033
|
+
* The {@link Converters.StringConverter | StringConverter} class extends {@link BaseConverter}
|
|
2034
|
+
* to provide string-specific helper methods.
|
|
2035
|
+
* @public
|
|
2036
|
+
*/
|
|
2037
|
+
declare class StringConverter<T extends string = string, TC = unknown> extends BaseConverter<T, TC> {
|
|
2038
|
+
/**
|
|
2039
|
+
* Construct a new {@link Converters.StringConverter | StringConverter}.
|
|
2040
|
+
* @param defaultContext - Optional context used by the conversion.
|
|
2041
|
+
* @param traits - Optional traits to be applied to the conversion.
|
|
2042
|
+
* @param converter - Optional conversion function to be used for the conversion.
|
|
2043
|
+
*/
|
|
2044
|
+
constructor(defaultContext?: TC, traits?: ConverterTraits, converter?: (from: unknown, self: Converter<T, TC>, context?: TC) => Result<T>);
|
|
2045
|
+
/**
|
|
2046
|
+
* @internal
|
|
2047
|
+
*/
|
|
2048
|
+
protected static _convert<T extends string>(from: unknown): Result<T>;
|
|
2049
|
+
/**
|
|
2050
|
+
* @internal
|
|
2051
|
+
*/
|
|
2052
|
+
protected static _wrap<T extends string, TC>(wrapped: StringConverter<T, TC>, converter: (from: T) => Result<T>, traits?: ConverterTraits): StringConverter<T, TC>;
|
|
2053
|
+
/**
|
|
2054
|
+
* Returns a {@link Converters.StringConverter | StringConverter} which constrains the result to match
|
|
2055
|
+
* a supplied string.
|
|
2056
|
+
* @param match - The string to be matched
|
|
2057
|
+
* @param options - Optional {@link Converters.StringMatchOptions} for this conversion.
|
|
2058
|
+
* @returns {@link Success} with a matching string or {@link Failure} with an informative
|
|
2059
|
+
* error if the string does not match.
|
|
2060
|
+
* {@label string}
|
|
2061
|
+
*/
|
|
2062
|
+
matching(match: string, options?: Partial<StringMatchOptions>): StringConverter<T, TC>;
|
|
2063
|
+
/**
|
|
2064
|
+
* Returns a {@link Converters.StringConverter | StringConverter} which constrains the result to match
|
|
2065
|
+
* one of a supplied array of strings.
|
|
2066
|
+
* @param match - The array of allowed strings.
|
|
2067
|
+
* @param options - Optional {@link Converters.StringMatchOptions} for this conversion.
|
|
2068
|
+
* @returns {@link Success} with a matching string or {@link Failure} with an informative
|
|
2069
|
+
* error if the string does not match.
|
|
2070
|
+
* {@label array}
|
|
2071
|
+
*/
|
|
2072
|
+
matching(match: string[], options?: Partial<StringMatchOptions>): StringConverter<T, TC>;
|
|
2073
|
+
/**
|
|
2074
|
+
* Returns a {@link Converters.StringConverter | StringConverter} which constrains the result to match
|
|
2075
|
+
* one of a supplied `Set` of strings.
|
|
2076
|
+
* @param match - The `Set` of allowed strings.
|
|
2077
|
+
* @param options - Optional {@link Converters.StringMatchOptions} for this conversion.
|
|
2078
|
+
* @returns {@link Success} with a matching string or {@link Failure} with an informative
|
|
2079
|
+
* error if the string does not match.
|
|
2080
|
+
* {@label set}
|
|
2081
|
+
*/
|
|
2082
|
+
matching(match: Set<T>, options?: Partial<StringMatchOptions>): StringConverter<T, TC>;
|
|
2083
|
+
/**
|
|
2084
|
+
* Returns a {@link Converters.StringConverter | StringConverter} which constrains the result to match
|
|
2085
|
+
* a supplied regular expression.
|
|
2086
|
+
* @param match - The regular expression to be used as a constraint.
|
|
2087
|
+
* @param options - Optional {@link Converters.StringMatchOptions} for this conversion
|
|
2088
|
+
* @returns {@link Success} with a matching string or {@link Failure} with an informative
|
|
2089
|
+
* error if the string does not match.
|
|
2090
|
+
* {@label regexp}
|
|
2091
|
+
*/
|
|
2092
|
+
matching(match: RegExp, options?: Partial<StringMatchOptions>): StringConverter<T, TC>;
|
|
2093
|
+
}
|
|
2094
|
+
|
|
2095
|
+
/**
|
|
2096
|
+
* Options for {@link Converters.StringConverter | StringConverter}
|
|
2097
|
+
* matching method
|
|
2098
|
+
* @public
|
|
2099
|
+
*/
|
|
2100
|
+
declare interface StringMatchOptions {
|
|
2101
|
+
/**
|
|
2102
|
+
* An optional message to be displayed if a non-matching string
|
|
2103
|
+
* is encountered.
|
|
2104
|
+
*/
|
|
2105
|
+
message?: string;
|
|
2106
|
+
}
|
|
2107
|
+
|
|
2108
|
+
/**
|
|
2109
|
+
* An in-place {@link Validation.Validator | Validator} for `string` values.
|
|
2110
|
+
* @public
|
|
2111
|
+
*/
|
|
2112
|
+
declare class StringValidator<T extends string = string, TC = unknown> extends GenericValidator<T, TC> {
|
|
2113
|
+
/**
|
|
2114
|
+
* Constructs a new {@link Validation.Classes.StringValidator | StringValidator}.
|
|
2115
|
+
* @param params - Optional {@link Validation.Classes.StringValidatorConstructorParams | init params}
|
|
2116
|
+
* for the new {@link Validation.Classes.StringValidator | StringValidator}.
|
|
2117
|
+
*/
|
|
2118
|
+
constructor(params?: StringValidatorConstructorParams<T, TC>);
|
|
2119
|
+
/**
|
|
2120
|
+
* Static method which validates that a supplied `unknown` value is a `string`.
|
|
2121
|
+
* @param from - The `unknown` value to be tested.
|
|
2122
|
+
* @returns Returns `true` if `from` is a `string`, or {@link Failure} with an error
|
|
2123
|
+
* message if not.
|
|
2124
|
+
*/
|
|
2125
|
+
static validateString<T extends string>(from: unknown): boolean | Failure<T>;
|
|
2126
|
+
}
|
|
2127
|
+
|
|
2128
|
+
/**
|
|
2129
|
+
* Parameters used to construct a {@link Validation.Classes.StringValidator | StringValidator}.
|
|
2130
|
+
* @public
|
|
2131
|
+
*/
|
|
2132
|
+
declare type StringValidatorConstructorParams<T extends string = string, TC = unknown> = GenericValidatorConstructorParams<T, TC>;
|
|
2133
|
+
|
|
2134
|
+
/**
|
|
2135
|
+
* Returns {@link Success | Success<T>} with the supplied result value.
|
|
2136
|
+
* @param value - The successful result value to be returned
|
|
2137
|
+
* @public
|
|
2138
|
+
*/
|
|
2139
|
+
export declare function succeed<T>(value: T): Success<T>;
|
|
2140
|
+
|
|
2141
|
+
/**
|
|
2142
|
+
* Returns {@link DetailedSuccess | DetailedSuccess<T, TD>} with a supplied value and optional
|
|
2143
|
+
* detail.
|
|
2144
|
+
* @param value - The value of type `<T>` to be returned.
|
|
2145
|
+
* @param detail - An optional detail of type `<TD>` to be returned.
|
|
2146
|
+
* @returns A {@link DetailedSuccess | DetailedSuccess<T, TD>} with the supplied value
|
|
2147
|
+
* and detail, if supplied.
|
|
2148
|
+
* @public
|
|
2149
|
+
*/
|
|
2150
|
+
export declare function succeedWithDetail<T, TD>(value: T, detail?: TD): DetailedSuccess<T, TD>;
|
|
2151
|
+
|
|
2152
|
+
/**
|
|
2153
|
+
* Reports a successful {@link IResult | result} from some operation and the
|
|
2154
|
+
* corresponding value.
|
|
2155
|
+
* @public
|
|
2156
|
+
*/
|
|
2157
|
+
export declare class Success<T> implements IResult<T> {
|
|
2158
|
+
/**
|
|
2159
|
+
* {@inheritdoc IResult.success}
|
|
2160
|
+
*/
|
|
2161
|
+
readonly success = true;
|
|
2162
|
+
/**
|
|
2163
|
+
* @internal
|
|
2164
|
+
*/
|
|
2165
|
+
private readonly _value;
|
|
2166
|
+
/**
|
|
2167
|
+
* Constructs a {@link Success} with the supplied value.
|
|
2168
|
+
* @param value - The value to be returned.
|
|
2169
|
+
*/
|
|
2170
|
+
constructor(value: T);
|
|
2171
|
+
/**
|
|
2172
|
+
* The result value returned by the successful operation.
|
|
2173
|
+
*/
|
|
2174
|
+
get value(): T;
|
|
2175
|
+
/**
|
|
2176
|
+
* {@inheritdoc IResult.isSuccess}
|
|
2177
|
+
*/
|
|
2178
|
+
isSuccess(): this is Success<T>;
|
|
2179
|
+
/**
|
|
2180
|
+
* {@inheritdoc IResult.isFailure}
|
|
2181
|
+
*/
|
|
2182
|
+
isFailure(): this is Failure<T>;
|
|
2183
|
+
/**
|
|
2184
|
+
* {@inheritdoc IResult.getValueOrThrow}
|
|
2185
|
+
*/
|
|
2186
|
+
getValueOrThrow(_logger?: IResultLogger): T;
|
|
2187
|
+
/**
|
|
2188
|
+
* {@inheritdoc IResult.getValueOrDefault}
|
|
2189
|
+
*/
|
|
2190
|
+
getValueOrDefault(dflt?: T): T | undefined;
|
|
2191
|
+
/**
|
|
2192
|
+
* {@inheritdoc IResult.onSuccess}
|
|
2193
|
+
*/
|
|
2194
|
+
onSuccess<TN>(cb: SuccessContinuation<T, TN>): Result<TN>;
|
|
2195
|
+
/**
|
|
2196
|
+
* {@inheritdoc IResult.onFailure}
|
|
2197
|
+
*/
|
|
2198
|
+
onFailure(_: FailureContinuation<T>): Result<T>;
|
|
2199
|
+
/**
|
|
2200
|
+
* {@inheritdoc IResult.withFailureDetail}
|
|
2201
|
+
*/
|
|
2202
|
+
withFailureDetail<TD>(_detail: TD): DetailedResult<T, TD>;
|
|
2203
|
+
/**
|
|
2204
|
+
* {@inheritdoc IResult.withDetail}
|
|
2205
|
+
*/
|
|
2206
|
+
withDetail<TD>(detail: TD, successDetail?: TD): DetailedResult<T, TD>;
|
|
2207
|
+
}
|
|
2208
|
+
|
|
2209
|
+
/**
|
|
2210
|
+
* Continuation callback to be called in the event that an
|
|
2211
|
+
* {@link Result} is successful.
|
|
2212
|
+
* @public
|
|
2213
|
+
*/
|
|
2214
|
+
export declare type SuccessContinuation<T, TN> = (value: T) => Result<TN>;
|
|
2215
|
+
|
|
2216
|
+
/**
|
|
2217
|
+
* Helper function to create a {@link Converters.StringConverter | StringConverter} which converts
|
|
2218
|
+
* `unknown` to `string`, applying template conversions supplied at construction time or at
|
|
2219
|
+
* runtime as context.
|
|
2220
|
+
* @remarks
|
|
2221
|
+
* Template conversions are applied using `mustache` syntax.
|
|
2222
|
+
* @param defaultContext - Optional default context to use for template values.
|
|
2223
|
+
* @returns A new {@link Converter} returning `string`.
|
|
2224
|
+
* @public
|
|
2225
|
+
*/
|
|
2226
|
+
declare function templateString(defaultContext?: unknown): StringConverter<string, unknown>;
|
|
2227
|
+
|
|
2228
|
+
/**
|
|
2229
|
+
* Helper to create a {@link Converter} which converts a source object to a new object with a
|
|
2230
|
+
* different shape.
|
|
2231
|
+
*
|
|
2232
|
+
* @remarks
|
|
2233
|
+
* On successful conversion, the resulting {@link Converter} returns {@link Success} with a new
|
|
2234
|
+
* object, which contains the converted values under the key names specified at initialization time.
|
|
2235
|
+
* It returns {@link Failure} with an error message if any fields to be extracted do not exist
|
|
2236
|
+
* or cannot be converted.
|
|
2237
|
+
*
|
|
2238
|
+
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
2239
|
+
* fail the conversion.
|
|
2240
|
+
*
|
|
2241
|
+
* @param properties - An object with key names that correspond to the target object and an
|
|
2242
|
+
* appropriate {@link Converters.FieldConverters | FieldConverter} which extracts and converts
|
|
2243
|
+
* a single filed from the source object.
|
|
2244
|
+
* @returns A {@link Converter} with the specified conversion behavior.
|
|
2245
|
+
* @public
|
|
2246
|
+
*/
|
|
2247
|
+
declare function transform<T, TC = unknown>(properties: FieldConverters<T, TC>): Converter<T, TC>;
|
|
2248
|
+
|
|
2249
|
+
/**
|
|
2250
|
+
* Helper function to create a {@link Converter} which validates that a supplied value is
|
|
2251
|
+
* of a type validated by a supplied validator function and returns it.
|
|
2252
|
+
* @remarks
|
|
2253
|
+
* If `validator` succeeds, this {@link Converter} returns {@link Success} with the supplied
|
|
2254
|
+
* value of `from` coerced to type `<T>`. Returns a {@link Failure} with additional
|
|
2255
|
+
* information otherwise.
|
|
2256
|
+
* @param validator - A validator function to determine if the converted value is valid.
|
|
2257
|
+
* @param description - A description of the validated type for use in error messages.
|
|
2258
|
+
* @returns A new {@link Converter | Converter<T>} which applies the supplied validation.
|
|
2259
|
+
* @public
|
|
2260
|
+
*/
|
|
2261
|
+
declare function validateWith<T, TC = undefined>(validator: (from: unknown) => from is T, description?: string): Converter<T, TC>;
|
|
2262
|
+
|
|
2263
|
+
declare namespace Validation {
|
|
2264
|
+
export {
|
|
2265
|
+
Base,
|
|
2266
|
+
Classes,
|
|
2267
|
+
Validators,
|
|
2268
|
+
FunctionConstraintTrait,
|
|
2269
|
+
ConstraintTrait,
|
|
2270
|
+
ValidatorTraitValues,
|
|
2271
|
+
defaultValidatorTraits,
|
|
2272
|
+
ValidatorTraits,
|
|
2273
|
+
ValidatorOptions,
|
|
2274
|
+
Constraint,
|
|
2275
|
+
Validator
|
|
2276
|
+
}
|
|
2277
|
+
}
|
|
2278
|
+
export { Validation }
|
|
2279
|
+
|
|
2280
|
+
/**
|
|
2281
|
+
* In-place validation that a supplied unknown matches some
|
|
2282
|
+
* required characteristics (type, values, etc).
|
|
2283
|
+
* @public
|
|
2284
|
+
*/
|
|
2285
|
+
declare interface Validator<T, TC = undefined> {
|
|
2286
|
+
/**
|
|
2287
|
+
* {@link Validation.ValidatorTraits | Traits} describing this validation.
|
|
2288
|
+
*/
|
|
2289
|
+
readonly traits: ValidatorTraits;
|
|
2290
|
+
/**
|
|
2291
|
+
* Indicates whether this element is explicitly optional.
|
|
2292
|
+
*/
|
|
2293
|
+
readonly isOptional: boolean;
|
|
2294
|
+
/**
|
|
2295
|
+
* The brand for a branded type.
|
|
2296
|
+
*/
|
|
2297
|
+
readonly brand: string | undefined;
|
|
2298
|
+
/**
|
|
2299
|
+
* Tests to see if a supplied `unknown` value matches this
|
|
2300
|
+
* validation.
|
|
2301
|
+
* @param from - The `unknown` value to be tested.
|
|
2302
|
+
* @param context - Optional validation context.
|
|
2303
|
+
* @returns {@link Success} with the typed, validated value,
|
|
2304
|
+
* or {@link Failure} with an error message if validation fails.
|
|
2305
|
+
*/
|
|
2306
|
+
validate(from: unknown, context?: TC): Result<T>;
|
|
2307
|
+
/**
|
|
2308
|
+
* Tests to see if a supplied `unknown` value matches this
|
|
2309
|
+
* validation. Accepts `undefined`.
|
|
2310
|
+
* @param from - The `unknown` value to be tested.
|
|
2311
|
+
* @param context - Optional validation context.
|
|
2312
|
+
* @returns {@link Success} with the typed, validated value,
|
|
2313
|
+
* or {@link Failure} with an error message if validation fails.
|
|
2314
|
+
*/
|
|
2315
|
+
validateOptional(from: unknown, context?: TC): Result<T | undefined>;
|
|
2316
|
+
/**
|
|
2317
|
+
* Non-throwing type guard
|
|
2318
|
+
* @param from - The value to be tested.
|
|
2319
|
+
* @param context - Optional validation context.
|
|
2320
|
+
*/
|
|
2321
|
+
guard(from: unknown, context?: TC): from is T;
|
|
2322
|
+
/**
|
|
2323
|
+
* Creates an {@link Validation.Validator | in-place validator}
|
|
2324
|
+
* which is derived from this one but which also matches `undefined`.
|
|
2325
|
+
*/
|
|
2326
|
+
optional(): Validator<T | undefined, TC>;
|
|
2327
|
+
/**
|
|
2328
|
+
* Creates an {@link Validation.Validator | in-place validator}
|
|
2329
|
+
* which is derived from this one but which applies additional constraints.
|
|
2330
|
+
* @param constraint - the constraint to be applied
|
|
2331
|
+
* @param trait - As optional {@link Validation.ConstraintTrait | ConstraintTrait}
|
|
2332
|
+
* to be applied to the resulting {@link Validation.Validator | Validator}.
|
|
2333
|
+
* @returns A new {@link Validation.Validator | Validator}.
|
|
2334
|
+
*/
|
|
2335
|
+
withConstraint(constraint: Constraint<T>, trait?: ConstraintTrait): Validator<T, TC>;
|
|
2336
|
+
/**
|
|
2337
|
+
* Creates a new {@link Validation.Validator | in-place validator} which
|
|
2338
|
+
* is derived from this one but which matches a branded result.
|
|
2339
|
+
* @param brand - The brand to be applied.
|
|
2340
|
+
*/
|
|
2341
|
+
withBrand<B extends string>(brand: B): Validator<Brand<T, B>, TC>;
|
|
2342
|
+
}
|
|
2343
|
+
|
|
2344
|
+
/**
|
|
2345
|
+
* Abstract base helper class for specific validator implementations
|
|
2346
|
+
* @internal
|
|
2347
|
+
*/
|
|
2348
|
+
declare abstract class ValidatorBase<T, TC = undefined> extends GenericValidator<T, TC> {
|
|
2349
|
+
/**
|
|
2350
|
+
* Inner constructor
|
|
2351
|
+
* @param params - Initialization params.
|
|
2352
|
+
* @internal
|
|
2353
|
+
*/
|
|
2354
|
+
protected constructor(params: Partial<ValidatorBaseConstructorParams<T, TC>>);
|
|
2355
|
+
/**
|
|
2356
|
+
* Abstract validation method to me implemented by derived classes.
|
|
2357
|
+
* @param from - Value to be converted.
|
|
2358
|
+
* @param context - Optional validation context.
|
|
2359
|
+
* @returns `true` if `from` is valid, {@link Failure | Failure<T>}
|
|
2360
|
+
* with an error message if `from` is invalid.
|
|
2361
|
+
* @internal
|
|
2362
|
+
*/
|
|
2363
|
+
protected abstract _validate(from: unknown, context?: TC): boolean | Failure<T>;
|
|
2364
|
+
}
|
|
2365
|
+
|
|
2366
|
+
declare type ValidatorBaseConstructorParams<T, TC> = Omit<GenericValidatorConstructorParams<T, TC>, 'validator'>;
|
|
2367
|
+
|
|
2368
|
+
/**
|
|
2369
|
+
* Type for a validation function, which validates that a supplied `unknown`
|
|
2370
|
+
* value is a valid value of type `<T>`, possibly as influenced by
|
|
2371
|
+
* an optionally-supplied validation context of type `<TC>`.
|
|
2372
|
+
* @public
|
|
2373
|
+
*/
|
|
2374
|
+
declare type ValidatorFunc<T, TC> = (from: unknown, context?: TC) => boolean | Failure<T>;
|
|
2375
|
+
|
|
2376
|
+
/**
|
|
2377
|
+
* Options that apply to any {@link Validation.Validator | Validator}.
|
|
2378
|
+
* @public
|
|
2379
|
+
*/
|
|
2380
|
+
declare interface ValidatorOptions<TC> {
|
|
2381
|
+
defaultContext?: TC;
|
|
2382
|
+
}
|
|
2383
|
+
|
|
2384
|
+
declare namespace Validators {
|
|
2385
|
+
export {
|
|
2386
|
+
object_2 as object,
|
|
2387
|
+
string_2 as string,
|
|
2388
|
+
number_2 as number,
|
|
2389
|
+
boolean_2 as boolean
|
|
2390
|
+
}
|
|
2391
|
+
}
|
|
2392
|
+
|
|
2393
|
+
/**
|
|
2394
|
+
* Generic implementation of {@link Validation.ValidatorTraitValues | ValidatorTraitValues}.
|
|
2395
|
+
* @public
|
|
2396
|
+
*/
|
|
2397
|
+
declare class ValidatorTraits implements ValidatorTraitValues {
|
|
2398
|
+
/**
|
|
2399
|
+
* {@inheritdoc Validation.ValidatorTraitValues.isOptional}
|
|
2400
|
+
*/
|
|
2401
|
+
readonly isOptional: boolean;
|
|
2402
|
+
/**
|
|
2403
|
+
* {@inheritdoc Validation.ValidatorTraitValues.brand}
|
|
2404
|
+
*/
|
|
2405
|
+
readonly brand?: string;
|
|
2406
|
+
/**
|
|
2407
|
+
* {@inheritdoc Validation.ValidatorTraitValues.constraints}
|
|
2408
|
+
*/
|
|
2409
|
+
readonly constraints: ConstraintTrait[];
|
|
2410
|
+
/**
|
|
2411
|
+
* Constructs a new {@link Validation.ValidatorTraits | ValidatorTraits} optionally
|
|
2412
|
+
* initialized with the supplied base and initial values.
|
|
2413
|
+
* @remarks
|
|
2414
|
+
* Initial values take priority over base values, which fall back to the global default values.
|
|
2415
|
+
* @param init - Partial initial values to be set in the resulting {@link Validation.Validator | Validator}.
|
|
2416
|
+
* @param base - Base values to be used when no initial values are present.
|
|
2417
|
+
*/
|
|
2418
|
+
constructor(init?: Partial<ValidatorTraitValues>, base?: ValidatorTraitValues);
|
|
2419
|
+
}
|
|
2420
|
+
|
|
2421
|
+
/**
|
|
2422
|
+
* Interface describing the supported validator traits.
|
|
2423
|
+
* @public
|
|
2424
|
+
*/
|
|
2425
|
+
declare interface ValidatorTraitValues {
|
|
2426
|
+
/**
|
|
2427
|
+
* Indicates whether the validator accepts `undefined` as
|
|
2428
|
+
* a valid value.
|
|
2429
|
+
*/
|
|
2430
|
+
readonly isOptional: boolean;
|
|
2431
|
+
/**
|
|
2432
|
+
* If present, indicates that the result will be branded
|
|
2433
|
+
* with the corresponding brand.
|
|
2434
|
+
*/
|
|
2435
|
+
readonly brand?: string;
|
|
2436
|
+
/**
|
|
2437
|
+
* Zero or more additional {@link Validation.ConstraintTrait | ConstraintTrait}s
|
|
2438
|
+
* describing additional constraints applied by this {@link Validation.Validator | Validator}.
|
|
2439
|
+
*/
|
|
2440
|
+
readonly constraints: ConstraintTrait[];
|
|
2441
|
+
}
|
|
2442
|
+
|
|
2443
|
+
/**
|
|
2444
|
+
* Deprecated alias for @see literal
|
|
2445
|
+
* @param value - The value to be compared.
|
|
2446
|
+
* @deprecated Use {@link Converters.literal} instead.
|
|
2447
|
+
* @internal
|
|
2448
|
+
*/
|
|
2449
|
+
declare const value: typeof literal;
|
|
2450
|
+
|
|
2451
|
+
export { }
|