@fgv/ts-utils 3.0.0 → 3.0.1-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ts-utils.d.ts +419 -270
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/packlets/conversion/baseConverter.d.ts +120 -0
- package/lib/packlets/conversion/baseConverter.d.ts.map +1 -0
- package/lib/packlets/conversion/baseConverter.js +233 -0
- package/lib/packlets/conversion/baseConverter.js.map +1 -0
- package/lib/packlets/conversion/converter.d.ts +27 -104
- package/lib/packlets/conversion/converter.d.ts.map +1 -1
- package/lib/packlets/conversion/converter.js +0 -215
- package/lib/packlets/conversion/converter.js.map +1 -1
- package/lib/packlets/conversion/converters.d.ts +138 -124
- package/lib/packlets/conversion/converters.d.ts.map +1 -1
- package/lib/packlets/conversion/converters.js +98 -94
- package/lib/packlets/conversion/converters.js.map +1 -1
- package/lib/packlets/conversion/defaultingConverter.d.ts +86 -0
- package/lib/packlets/conversion/defaultingConverter.d.ts.map +1 -0
- package/lib/packlets/conversion/defaultingConverter.js +146 -0
- package/lib/packlets/conversion/defaultingConverter.js.map +1 -0
- package/lib/packlets/conversion/index.d.ts +2 -0
- package/lib/packlets/conversion/index.d.ts.map +1 -1
- package/lib/packlets/conversion/index.js +2 -0
- package/lib/packlets/conversion/index.js.map +1 -1
- package/lib/packlets/conversion/objectConverter.d.ts +5 -3
- package/lib/packlets/conversion/objectConverter.d.ts.map +1 -1
- package/lib/packlets/conversion/objectConverter.js +2 -2
- package/lib/packlets/conversion/objectConverter.js.map +1 -1
- package/lib/packlets/conversion/stringConverter.d.ts +2 -1
- package/lib/packlets/conversion/stringConverter.d.ts.map +1 -1
- package/lib/packlets/conversion/stringConverter.js +2 -2
- package/lib/packlets/conversion/stringConverter.js.map +1 -1
- package/lib/packlets/validation/genericValidator.d.ts +4 -0
- package/lib/packlets/validation/genericValidator.d.ts.map +1 -1
- package/lib/packlets/validation/genericValidator.js +10 -0
- package/lib/packlets/validation/genericValidator.js.map +1 -1
- package/lib/packlets/validation/index.d.ts +1 -1
- package/lib/packlets/validation/index.d.ts.map +1 -1
- package/lib/packlets/validation/index.js +2 -2
- package/lib/packlets/validation/index.js.map +1 -1
- package/lib/packlets/validation/validator.d.ts +12 -2
- package/lib/packlets/validation/validator.d.ts.map +1 -1
- package/lib/packlets/validation/validator.js.map +1 -1
- package/package.json +13 -13
|
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
27
|
exports.transformObject = exports.transform = exports.discriminatedObject = exports.strictObject = exports.object = exports.optionalField = exports.field = exports.optionalElement = exports.element = exports.validateWith = exports.mapOf = exports.recordOf = exports.numberArray = exports.stringArray = exports.arrayOf = exports.oneOf = exports.optionalBoolean = exports.optionalNumber = exports.isA = exports.validated = exports.isoDate = exports.delimitedString = exports.optionalString = exports.boolean = exports.number = exports.value = exports.literal = exports.mappedEnumeratedValue = exports.enumeratedValue = exports.templateString = exports.string = void 0;
|
|
28
28
|
const base_1 = require("../base");
|
|
29
|
-
const
|
|
29
|
+
const baseConverter_1 = require("./baseConverter");
|
|
30
30
|
const objectConverter_1 = require("./objectConverter");
|
|
31
31
|
const stringConverter_1 = require("./stringConverter");
|
|
32
32
|
const luxon_1 = require("luxon");
|
|
@@ -44,7 +44,7 @@ exports.string = new stringConverter_1.StringConverter();
|
|
|
44
44
|
* @remarks
|
|
45
45
|
* Template conversions are applied using `mustache` syntax.
|
|
46
46
|
* @param defaultContext - Optional default context to use for template values.
|
|
47
|
-
* @returns A new {@link Converter} returning `string`.
|
|
47
|
+
* @returns A new {@link Converter | Converter} returning `string`.
|
|
48
48
|
* @public
|
|
49
49
|
*/
|
|
50
50
|
function templateString(defaultContext) {
|
|
@@ -57,17 +57,17 @@ function templateString(defaultContext) {
|
|
|
57
57
|
}
|
|
58
58
|
exports.templateString = templateString;
|
|
59
59
|
/**
|
|
60
|
-
* Helper function to create a {@link Converter} which converts `unknown` to one of a set of supplied
|
|
60
|
+
* Helper function to create a {@link Converter | Converter} which converts `unknown` to one of a set of supplied
|
|
61
61
|
* enumerated values. Anything else fails.
|
|
62
62
|
*
|
|
63
63
|
* @remarks
|
|
64
64
|
* Allowed enumerated values can also be supplied as context at conversion time.
|
|
65
65
|
* @param values - Array of allowed values.
|
|
66
|
-
* @returns A new {@link Converter} returning `<T>`.
|
|
66
|
+
* @returns A new {@link Converter | Converter} returning `<T>`.
|
|
67
67
|
* @public
|
|
68
68
|
*/
|
|
69
69
|
function enumeratedValue(values) {
|
|
70
|
-
return new
|
|
70
|
+
return new baseConverter_1.BaseConverter((from, __self, context) => {
|
|
71
71
|
const v = context !== null && context !== void 0 ? context : values;
|
|
72
72
|
const index = v.indexOf(from);
|
|
73
73
|
return index >= 0 ? (0, base_1.succeed)(v[index]) : (0, base_1.fail)(`Invalid enumerated value ${JSON.stringify(from)}`);
|
|
@@ -75,7 +75,7 @@ function enumeratedValue(values) {
|
|
|
75
75
|
}
|
|
76
76
|
exports.enumeratedValue = enumeratedValue;
|
|
77
77
|
/**
|
|
78
|
-
* Helper function to create a {@link Converter} which converts `unknown` to one of a set of supplied enumerated
|
|
78
|
+
* Helper function to create a {@link Converter | Converter} which converts `unknown` to one of a set of supplied enumerated
|
|
79
79
|
* values, mapping any of multiple supplied values to the enumeration.
|
|
80
80
|
* @remarks
|
|
81
81
|
* Enables mapping of multiple input values to a consistent internal representation (so e.g. `'y'`, `'yes'`,
|
|
@@ -84,11 +84,11 @@ exports.enumeratedValue = enumeratedValue;
|
|
|
84
84
|
* value, the second is the set of values that map to the result. Tuples are evaluated in the order
|
|
85
85
|
* supplied and are not checked for duplicates.
|
|
86
86
|
* @param message - An optional error message.
|
|
87
|
-
* @returns A {@link Converter} which applies the mapping and yields `<T>` on success.
|
|
87
|
+
* @returns A {@link Converter | Converter} which applies the mapping and yields `<T>` on success.
|
|
88
88
|
* @public
|
|
89
89
|
*/
|
|
90
90
|
function mappedEnumeratedValue(map, message) {
|
|
91
|
-
return new
|
|
91
|
+
return new baseConverter_1.BaseConverter((from, __self, __context) => {
|
|
92
92
|
for (const item of map) {
|
|
93
93
|
if (item[1].includes(from)) {
|
|
94
94
|
return (0, base_1.succeed)(item[0]);
|
|
@@ -101,14 +101,14 @@ function mappedEnumeratedValue(map, message) {
|
|
|
101
101
|
}
|
|
102
102
|
exports.mappedEnumeratedValue = mappedEnumeratedValue;
|
|
103
103
|
/**
|
|
104
|
-
* Helper function to create a {@link Converter} which converts `unknown` to some supplied literal value. Succeeds with
|
|
104
|
+
* Helper function to create a {@link Converter | Converter} which converts `unknown` to some supplied literal value. Succeeds with
|
|
105
105
|
* the supplied value if an identity comparison succeeds, fails otherwise.
|
|
106
106
|
* @param value - The value to be compared.
|
|
107
|
-
* @returns A {@link Converter} which returns the supplied value on success.
|
|
107
|
+
* @returns A {@link Converter | Converter} which returns the supplied value on success.
|
|
108
108
|
* @public
|
|
109
109
|
*/
|
|
110
110
|
function literal(value) {
|
|
111
|
-
return new
|
|
111
|
+
return new baseConverter_1.BaseConverter((from, __self, __context) => {
|
|
112
112
|
return from === value
|
|
113
113
|
? (0, base_1.succeed)(value)
|
|
114
114
|
: (0, base_1.fail)(`${JSON.stringify(from)}: does not match ${JSON.stringify(value)}`);
|
|
@@ -124,12 +124,12 @@ exports.literal = literal;
|
|
|
124
124
|
// eslint-disable-next-line @rushstack/typedef-var
|
|
125
125
|
exports.value = literal;
|
|
126
126
|
/**
|
|
127
|
-
* A {@link Converter} which converts `unknown` to a `number`.
|
|
127
|
+
* A {@link Converter | Converter} which converts `unknown` to a `number`.
|
|
128
128
|
* @remarks
|
|
129
129
|
* Numbers and strings with a numeric format succeed. Anything else fails.
|
|
130
130
|
* @public
|
|
131
131
|
*/
|
|
132
|
-
exports.number = new
|
|
132
|
+
exports.number = new baseConverter_1.BaseConverter((from) => {
|
|
133
133
|
if (typeof from !== 'number') {
|
|
134
134
|
const num = typeof from === 'string' ? Number(from) : NaN;
|
|
135
135
|
return isNaN(num) ? (0, base_1.fail)(`Not a number: ${JSON.stringify(from)}`) : (0, base_1.succeed)(num);
|
|
@@ -137,13 +137,13 @@ exports.number = new converter_1.BaseConverter((from) => {
|
|
|
137
137
|
return (0, base_1.succeed)(from);
|
|
138
138
|
});
|
|
139
139
|
/**
|
|
140
|
-
* A {@link Converter} which converts `unknown` to `boolean`.
|
|
140
|
+
* A {@link Converter | Converter} which converts `unknown` to `boolean`.
|
|
141
141
|
* @remarks
|
|
142
142
|
* Boolean values or the case-insensitive strings `'true'` and `'false'` succeed.
|
|
143
143
|
* Anything else fails.
|
|
144
144
|
* @public
|
|
145
145
|
*/
|
|
146
|
-
exports.boolean = new
|
|
146
|
+
exports.boolean = new baseConverter_1.BaseConverter((from) => {
|
|
147
147
|
if (typeof from === 'boolean') {
|
|
148
148
|
return (0, base_1.succeed)(from);
|
|
149
149
|
}
|
|
@@ -158,22 +158,22 @@ exports.boolean = new converter_1.BaseConverter((from) => {
|
|
|
158
158
|
return (0, base_1.fail)(`Not a boolean: ${JSON.stringify(from)}`);
|
|
159
159
|
});
|
|
160
160
|
/**
|
|
161
|
-
* A {@link Converter} which converts an optional `string` value. Values of type
|
|
162
|
-
* `string` are returned. Anything else returns {@link Success} with value `undefined`.
|
|
161
|
+
* A {@link Converter | Converter} which converts an optional `string` value. Values of type
|
|
162
|
+
* `string` are returned. Anything else returns {@link Success | Success} with value `undefined`.
|
|
163
163
|
* @public
|
|
164
164
|
*/
|
|
165
165
|
exports.optionalString = exports.string.optional();
|
|
166
166
|
/**
|
|
167
|
-
* Helper function to create a {@link Converter} which converts any `string` into an
|
|
167
|
+
* Helper function to create a {@link Converter | Converter} which converts any `string` into an
|
|
168
168
|
* array of `string`, by separating at a supplied delimiter.
|
|
169
169
|
* @remarks
|
|
170
170
|
* Delimiter may also be supplied as context at conversion time.
|
|
171
171
|
* @param delimiter - The delimiter at which to split.
|
|
172
|
-
* @returns A new {@link Converter} returning `string[]`.
|
|
172
|
+
* @returns A new {@link Converter | Converter} returning `string[]`.
|
|
173
173
|
* @public
|
|
174
174
|
*/
|
|
175
175
|
function delimitedString(delimiter, options = 'filtered') {
|
|
176
|
-
return new
|
|
176
|
+
return new baseConverter_1.BaseConverter((from, __self, context) => {
|
|
177
177
|
const result = exports.string.convert(from);
|
|
178
178
|
if (result.isSuccess()) {
|
|
179
179
|
let strings = result.value.split(context !== null && context !== void 0 ? context : delimiter);
|
|
@@ -187,11 +187,11 @@ function delimitedString(delimiter, options = 'filtered') {
|
|
|
187
187
|
}
|
|
188
188
|
exports.delimitedString = delimitedString;
|
|
189
189
|
/**
|
|
190
|
-
* A {@link Converter} which converts an iso formatted string, a number or a `Date` object to
|
|
190
|
+
* A {@link Converter | Converter} which converts an iso formatted string, a number or a `Date` object to
|
|
191
191
|
* a `Date` object.
|
|
192
192
|
* @public
|
|
193
193
|
*/
|
|
194
|
-
exports.isoDate = new
|
|
194
|
+
exports.isoDate = new baseConverter_1.BaseConverter((from) => {
|
|
195
195
|
if (typeof from === 'string') {
|
|
196
196
|
const dt = luxon_1.DateTime.fromISO(from);
|
|
197
197
|
if (dt.isValid) {
|
|
@@ -208,27 +208,27 @@ exports.isoDate = new converter_1.BaseConverter((from) => {
|
|
|
208
208
|
return (0, base_1.fail)(`Cannot convert ${JSON.stringify(from)} to Date`);
|
|
209
209
|
});
|
|
210
210
|
/**
|
|
211
|
-
* Helper function to create a {@link Converter} from any {@link Validation.Validator}
|
|
211
|
+
* Helper function to create a {@link Converter | Converter} from any {@link Validation.Validator}
|
|
212
212
|
* @param validator - the validator to be wrapped
|
|
213
|
-
* @returns A {@link Converter} which uses the supplied validator.
|
|
213
|
+
* @returns A {@link Converter | Converter} which uses the supplied validator.
|
|
214
214
|
* @public
|
|
215
215
|
*/
|
|
216
216
|
function validated(validator) {
|
|
217
|
-
return new
|
|
217
|
+
return new baseConverter_1.BaseConverter((from, __self, context) => {
|
|
218
218
|
return validator.validate(from, context);
|
|
219
219
|
});
|
|
220
220
|
}
|
|
221
221
|
exports.validated = validated;
|
|
222
222
|
/**
|
|
223
|
-
* Helper function to create a {@link Converter} from a supplied type guard function.
|
|
223
|
+
* Helper function to create a {@link Converter | Converter} from a supplied type guard function.
|
|
224
224
|
* @param description - a description of the thing to be validated for use in error messages
|
|
225
225
|
* @param guard - a {@link Validation.TypeGuardWithContext} which performs the validation.
|
|
226
|
-
* @returns A new {@link Converter} which validates the values using the supplied type guard
|
|
226
|
+
* @returns A new {@link Converter | Converter} which validates the values using the supplied type guard
|
|
227
227
|
* and returns them in place.
|
|
228
228
|
* @public
|
|
229
229
|
*/
|
|
230
230
|
function isA(description, guard) {
|
|
231
|
-
return new
|
|
231
|
+
return new baseConverter_1.BaseConverter((from, __self, context) => {
|
|
232
232
|
if (guard(from, context)) {
|
|
233
233
|
return (0, base_1.succeed)(from);
|
|
234
234
|
}
|
|
@@ -237,40 +237,42 @@ function isA(description, guard) {
|
|
|
237
237
|
}
|
|
238
238
|
exports.isA = isA;
|
|
239
239
|
/**
|
|
240
|
-
* A {@link Converter} which converts an optional `number` value.
|
|
240
|
+
* A {@link Converter | Converter} which converts an optional `number` value.
|
|
241
241
|
* @remarks
|
|
242
242
|
* Values of type `number` or numeric strings are converted and returned.
|
|
243
|
-
* Anything else returns {@link Success} with value `undefined`.
|
|
243
|
+
* Anything else returns {@link Success | Success} with value `undefined`.
|
|
244
244
|
* @public
|
|
245
245
|
*/
|
|
246
246
|
exports.optionalNumber = exports.number.optional();
|
|
247
247
|
/**
|
|
248
|
-
* A {@link Converter} to convert an optional `boolean` value.
|
|
248
|
+
* A {@link Converter | Converter} to convert an optional `boolean` value.
|
|
249
249
|
* @remarks
|
|
250
250
|
* Values of type `boolean` or strings that match (case-insensitive) `'true'`
|
|
251
|
-
* or `'false'` are converted and returned. Anything else returns {@link Success}
|
|
251
|
+
* or `'false'` are converted and returned. Anything else returns {@link Success | Success}
|
|
252
252
|
* with value `undefined`.
|
|
253
253
|
* @public
|
|
254
254
|
*/
|
|
255
255
|
exports.optionalBoolean = exports.boolean.optional();
|
|
256
256
|
/**
|
|
257
|
-
* A helper function to create a {@link Converter} for polymorphic values.
|
|
258
|
-
* converter which
|
|
259
|
-
* result. Returns an error if none of the supplied converters can
|
|
257
|
+
* A helper function to create a {@link Converter | Converter} for polymorphic values.
|
|
258
|
+
* Returns a converter which invokes the wrapped converters in sequence, returning the
|
|
259
|
+
* first successful result. Returns an error if none of the supplied converters can
|
|
260
|
+
* convert the value.
|
|
260
261
|
* @remarks
|
|
261
262
|
* If `onError` is `ignoreErrors` (default), then errors from any of the
|
|
262
263
|
* converters are ignored provided that some converter succeeds. If
|
|
263
264
|
* onError is `failOnError`, then an error from any converter fails the entire
|
|
264
265
|
* conversion.
|
|
265
266
|
*
|
|
266
|
-
* @param converters - An ordered list of {@link Converter | converters}
|
|
267
|
+
* @param converters - An ordered list of {@link Converter | converters} or {@link Validator | validators}
|
|
268
|
+
* to be considered.
|
|
267
269
|
* @param onError - Specifies treatment of unconvertible elements.
|
|
268
|
-
* @returns A new {@link Converter} which yields a value from the union of the types returned
|
|
270
|
+
* @returns A new {@link Converter | Converter} which yields a value from the union of the types returned
|
|
269
271
|
* by the wrapped converters.
|
|
270
272
|
* @public
|
|
271
273
|
*/
|
|
272
274
|
function oneOf(converters, onError = 'ignoreErrors') {
|
|
273
|
-
return new
|
|
275
|
+
return new baseConverter_1.BaseConverter((from, __self, context) => {
|
|
274
276
|
const errors = [];
|
|
275
277
|
for (const converter of converters) {
|
|
276
278
|
const result = converter.convert(from, context);
|
|
@@ -289,17 +291,18 @@ function oneOf(converters, onError = 'ignoreErrors') {
|
|
|
289
291
|
}
|
|
290
292
|
exports.oneOf = oneOf;
|
|
291
293
|
/**
|
|
292
|
-
* A helper function to create a {@link Converter} which converts `unknown` to an array of `<T>`.
|
|
294
|
+
* A helper function to create a {@link Converter | Converter} which converts `unknown` to an array of `<T>`.
|
|
293
295
|
* @remarks
|
|
294
296
|
* If `onError` is `'failOnError'` (default), then the entire conversion fails if any element cannot
|
|
295
297
|
* be converted. If `onError` is `'ignoreErrors'`, then failing elements are silently ignored.
|
|
296
|
-
* @param converter - {@link Converter} used to convert each
|
|
298
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} used to convert each
|
|
299
|
+
* item in the array.
|
|
297
300
|
* @param ignoreErrors - Specifies treatment of unconvertible elements.
|
|
298
|
-
* @returns A {@link Converter} which returns an array of `<T>`.
|
|
301
|
+
* @returns A {@link Converter | Converter} which returns an array of `<T>`.
|
|
299
302
|
* @public
|
|
300
303
|
*/
|
|
301
304
|
function arrayOf(converter, onError = 'failOnError') {
|
|
302
|
-
return new
|
|
305
|
+
return new baseConverter_1.BaseConverter((from, __self, context) => {
|
|
303
306
|
if (!Array.isArray(from)) {
|
|
304
307
|
return (0, base_1.fail)(`Not an array: ${JSON.stringify(from)}`);
|
|
305
308
|
}
|
|
@@ -319,10 +322,10 @@ function arrayOf(converter, onError = 'failOnError') {
|
|
|
319
322
|
}
|
|
320
323
|
exports.arrayOf = arrayOf;
|
|
321
324
|
/**
|
|
322
|
-
* {@link Converter} to convert an `unknown` to an array of `string`.
|
|
325
|
+
* {@link Converter | Converter} to convert an `unknown` to an array of `string`.
|
|
323
326
|
* @remarks
|
|
324
|
-
* Returns {@link Success} with the the supplied value if it as an array
|
|
325
|
-
* of strings, returns {@link Failure} with an error message otherwise.
|
|
327
|
+
* Returns {@link Success | Success} with the the supplied value if it as an array
|
|
328
|
+
* of strings, returns {@link Failure | Failure} with an error message otherwise.
|
|
326
329
|
* @public
|
|
327
330
|
*/
|
|
328
331
|
exports.stringArray = arrayOf(exports.string);
|
|
@@ -342,7 +345,7 @@ exports.numberArray = arrayOf(exports.number);
|
|
|
342
345
|
*/
|
|
343
346
|
function recordOf(converter, option = 'fail') {
|
|
344
347
|
const options = typeof option === 'string' ? { onError: option } : Object.assign({ onError: 'fail' }, option);
|
|
345
|
-
return new
|
|
348
|
+
return new baseConverter_1.BaseConverter((from, __self, context) => {
|
|
346
349
|
var _a, _b;
|
|
347
350
|
if (typeof from !== 'object' || from === null || Array.isArray(from)) {
|
|
348
351
|
return (0, base_1.fail)(`Not a string-keyed object: ${JSON.stringify(from)}`);
|
|
@@ -377,7 +380,7 @@ exports.recordOf = recordOf;
|
|
|
377
380
|
*/
|
|
378
381
|
function mapOf(converter, option = 'fail') {
|
|
379
382
|
const options = typeof option === 'string' ? { onError: option } : Object.assign({ onError: 'fail' }, option);
|
|
380
|
-
return new
|
|
383
|
+
return new baseConverter_1.BaseConverter((from, __self, context) => {
|
|
381
384
|
var _a, _b;
|
|
382
385
|
if (typeof from !== 'object' || from === null || Array.isArray(from)) {
|
|
383
386
|
return (0, base_1.fail)(`Not a string-keyed object: ${JSON.stringify(from)}`);
|
|
@@ -405,11 +408,11 @@ function mapOf(converter, option = 'fail') {
|
|
|
405
408
|
}
|
|
406
409
|
exports.mapOf = mapOf;
|
|
407
410
|
/**
|
|
408
|
-
* Helper function to create a {@link Converter} which validates that a supplied value is
|
|
411
|
+
* Helper function to create a {@link Converter | Converter} which validates that a supplied value is
|
|
409
412
|
* of a type validated by a supplied validator function and returns it.
|
|
410
413
|
* @remarks
|
|
411
|
-
* If `validator` succeeds, this {@link Converter} returns {@link Success} with the supplied
|
|
412
|
-
* value of `from` coerced to type `<T>`. Returns a {@link Failure} with additional
|
|
414
|
+
* If `validator` succeeds, this {@link Converter | Converter} returns {@link Success | Success} with the supplied
|
|
415
|
+
* value of `from` coerced to type `<T>`. Returns a {@link Failure | Failure} with additional
|
|
413
416
|
* information otherwise.
|
|
414
417
|
* @param validator - A validator function to determine if the converted value is valid.
|
|
415
418
|
* @param description - A description of the validated type for use in error messages.
|
|
@@ -417,7 +420,7 @@ exports.mapOf = mapOf;
|
|
|
417
420
|
* @public
|
|
418
421
|
*/
|
|
419
422
|
function validateWith(validator, description) {
|
|
420
|
-
return new
|
|
423
|
+
return new baseConverter_1.BaseConverter((from, __self, __context) => {
|
|
421
424
|
if (validator(from)) {
|
|
422
425
|
return (0, base_1.succeed)(from);
|
|
423
426
|
}
|
|
@@ -426,17 +429,17 @@ function validateWith(validator, description) {
|
|
|
426
429
|
}
|
|
427
430
|
exports.validateWith = validateWith;
|
|
428
431
|
/**
|
|
429
|
-
* A helper function to create a {@link Converter} which extracts and converts an element from an array.
|
|
432
|
+
* A helper function to create a {@link Converter | Converter} which extracts and converts an element from an array.
|
|
430
433
|
* @remarks
|
|
431
|
-
* The returned {@link Converter} returns {@link Success} with the converted value if the element exists
|
|
432
|
-
* in the supplied array and can be converted. Returns {@link Failure} with an error message otherwise.
|
|
434
|
+
* The returned {@link Converter | Converter} returns {@link Success | Success} with the converted value if the element exists
|
|
435
|
+
* in the supplied array and can be converted. Returns {@link Failure | Failure} with an error message otherwise.
|
|
433
436
|
* @param index - The index of the element to be extracted.
|
|
434
|
-
* @param converter - A {@link Converter}
|
|
437
|
+
* @param converter - A {@link Converter | Converter} or {@link Validator | Validator} for the extracted element.
|
|
435
438
|
* @returns A {@link Converter | Converter<T>} which extracts the specified element from an array.
|
|
436
439
|
* @public
|
|
437
440
|
*/
|
|
438
441
|
function element(index, converter) {
|
|
439
|
-
return new
|
|
442
|
+
return new baseConverter_1.BaseConverter((from, __self, context) => {
|
|
440
443
|
if (index < 0) {
|
|
441
444
|
return (0, base_1.fail)(`${index}: cannot convert for a negative element index`);
|
|
442
445
|
}
|
|
@@ -451,19 +454,19 @@ function element(index, converter) {
|
|
|
451
454
|
}
|
|
452
455
|
exports.element = element;
|
|
453
456
|
/**
|
|
454
|
-
* A helper function to create a {@link Converter} which extracts and converts an optional element from an array.
|
|
457
|
+
* A helper function to create a {@link Converter | Converter} which extracts and converts an optional element from an array.
|
|
455
458
|
* @remarks
|
|
456
|
-
* The resulting {@link Converter} returns {@link Success} with the converted value if the element exists
|
|
457
|
-
* in the supplied array and can be converted. Returns {@link Success} with value `undefined` if the parameter
|
|
458
|
-
* is an array but the index is out of range. Returns {@link Failure} with a message if the supplied parameter
|
|
459
|
+
* The resulting {@link Converter | Converter} returns {@link Success | Success} with the converted value if the element exists
|
|
460
|
+
* in the supplied array and can be converted. Returns {@link Success | Success} with value `undefined` if the parameter
|
|
461
|
+
* is an array but the index is out of range. Returns {@link Failure | Failure} with a message if the supplied parameter
|
|
459
462
|
* is not an array, if the requested index is negative, or if the element cannot be converted.
|
|
460
463
|
* @param index - The index of the element to be extracted.
|
|
461
|
-
* @param converter - A {@link Converter} used
|
|
464
|
+
* @param converter - A {@link Converter | Converter} or {@link Validator | Validator} used for the extracted element.
|
|
462
465
|
* @returns A {@link Converter | Converter<T>} which extracts the specified element from an array.
|
|
463
466
|
* @public
|
|
464
467
|
*/
|
|
465
468
|
function optionalElement(index, converter) {
|
|
466
|
-
return new
|
|
469
|
+
return new baseConverter_1.BaseConverter((from, __self, context) => {
|
|
467
470
|
if (index < 0) {
|
|
468
471
|
return (0, base_1.fail)(`${index}: cannot convert for a negative element index`);
|
|
469
472
|
}
|
|
@@ -478,18 +481,19 @@ function optionalElement(index, converter) {
|
|
|
478
481
|
}
|
|
479
482
|
exports.optionalElement = optionalElement;
|
|
480
483
|
/**
|
|
481
|
-
* A helper function to create a {@link Converter} which extracts and convert a property specified
|
|
484
|
+
* A helper function to create a {@link Converter | Converter} which extracts and convert a property specified
|
|
482
485
|
* by name from an object.
|
|
483
486
|
* @remarks
|
|
484
|
-
* The resulting {@link Converter} returns {@link Success} with the converted value of the corresponding
|
|
485
|
-
* object property if the field exists and can be converted. Returns {@link Failure} with an error message
|
|
487
|
+
* The resulting {@link Converter | Converter} returns {@link Success | Success} with the converted value of the corresponding
|
|
488
|
+
* object property if the field exists and can be converted. Returns {@link Failure | Failure} with an error message
|
|
486
489
|
* otherwise.
|
|
487
490
|
* @param name - The name of the field to be extracted.
|
|
488
|
-
* @param converter - {@link Converter}
|
|
491
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} to use for the extracted
|
|
492
|
+
* field.
|
|
489
493
|
* @public
|
|
490
494
|
*/
|
|
491
495
|
function field(name, converter) {
|
|
492
|
-
return new
|
|
496
|
+
return new baseConverter_1.BaseConverter((from, __self, context) => {
|
|
493
497
|
if (typeof from === 'object' && !Array.isArray(from) && from !== null) {
|
|
494
498
|
if ((0, base_1.isKeyOf)(name, from)) {
|
|
495
499
|
return converter.convert(from[name], context).onFailure((message) => {
|
|
@@ -503,19 +507,19 @@ function field(name, converter) {
|
|
|
503
507
|
}
|
|
504
508
|
exports.field = field;
|
|
505
509
|
/**
|
|
506
|
-
* A helper function to create a {@link Converter} which extracts and convert a property specified
|
|
510
|
+
* A helper function to create a {@link Converter | Converter} which extracts and convert a property specified
|
|
507
511
|
* by name from an object.
|
|
508
512
|
* @remarks
|
|
509
|
-
* The resulting {@link Converter} returns {@link Success} with the converted value of
|
|
510
|
-
* object property if the field exists and can be converted. Returns {@link Success
|
|
511
|
-
* if the supplied parameter is an object but the named field is not present.
|
|
512
|
-
* an error message otherwise.
|
|
513
|
+
* The resulting {@link Converter | Converter} returns {@link Success | Success} with the converted value of
|
|
514
|
+
* the corresponding object property if the field exists and can be converted. Returns {@link Success | Success}
|
|
515
|
+
* with `undefined` if the supplied parameter is an object but the named field is not present.
|
|
516
|
+
* Returns {@link Failure | Failure} with an error message otherwise.
|
|
513
517
|
* @param name - The name of the field to be extracted.
|
|
514
|
-
* @param converter - {@link Converter}
|
|
518
|
+
* @param converter - {@link Converter | Converter} or {@link Validator | Validator} to use for the extracted field.
|
|
515
519
|
* @public
|
|
516
520
|
*/
|
|
517
521
|
function optionalField(name, converter) {
|
|
518
|
-
return new
|
|
522
|
+
return new baseConverter_1.BaseConverter((from, __self, context) => {
|
|
519
523
|
if (typeof from === 'object' && !Array.isArray(from) && from !== null) {
|
|
520
524
|
if ((0, base_1.isKeyOf)(name, from)) {
|
|
521
525
|
const result = converter.convert(from[name], context).onFailure((message) => {
|
|
@@ -554,23 +558,23 @@ function strictObject(properties, opt) {
|
|
|
554
558
|
}
|
|
555
559
|
exports.strictObject = strictObject;
|
|
556
560
|
/**
|
|
557
|
-
* Helper to create a {@link Converter} which converts a discriminated object without changing shape.
|
|
561
|
+
* Helper to create a {@link Converter | Converter} which converts a discriminated object without changing shape.
|
|
558
562
|
* @remarks
|
|
559
563
|
* Takes the name of the discriminator property and a
|
|
560
|
-
* {@link Converters.DiscriminatedObjectConverters | string-keyed Record of converters}. During conversion,
|
|
561
|
-
* the resulting {@link Converter} invokes the converter from `converters` that corresponds to the value of
|
|
564
|
+
* {@link Converters.DiscriminatedObjectConverters | string-keyed Record of converters and validators}. During conversion,
|
|
565
|
+
* the resulting {@link Converter | Converter} invokes the converter from `converters` that corresponds to the value of
|
|
562
566
|
* the discriminator property in the source object.
|
|
563
567
|
*
|
|
564
568
|
* If the source is not an object, the discriminator property is missing, or the discriminator has
|
|
565
|
-
* a value not present in the converters, conversion fails and returns {@link Failure} with more information.
|
|
569
|
+
* a value not present in the converters, conversion fails and returns {@link Failure | Failure} with more information.
|
|
566
570
|
* @param discriminatorProp - Name of the property used to discriminate types.
|
|
567
|
-
* @param converters - {@link Converters.DiscriminatedObjectConverters | String-keyed record of converters}
|
|
568
|
-
* invoke, where each key corresponds to a value of the discriminator property.
|
|
569
|
-
* @returns A {@link Converter} which converts the corresponding discriminated object.
|
|
571
|
+
* @param converters - {@link Converters.DiscriminatedObjectConverters | String-keyed record of converters and validators}
|
|
572
|
+
* to invoke, where each key corresponds to a value of the discriminator property.
|
|
573
|
+
* @returns A {@link Converter | Converter} which converts the corresponding discriminated object.
|
|
570
574
|
* @public
|
|
571
575
|
*/
|
|
572
576
|
function discriminatedObject(discriminatorProp, converters) {
|
|
573
|
-
return new
|
|
577
|
+
return new baseConverter_1.BaseConverter((from) => {
|
|
574
578
|
if (typeof from !== 'object' || Array.isArray(from) || from === null) {
|
|
575
579
|
return (0, base_1.fail)(`Not a discriminated object: "${JSON.stringify(from)}"`);
|
|
576
580
|
}
|
|
@@ -587,13 +591,13 @@ function discriminatedObject(discriminatorProp, converters) {
|
|
|
587
591
|
}
|
|
588
592
|
exports.discriminatedObject = discriminatedObject;
|
|
589
593
|
/**
|
|
590
|
-
* Helper to create a {@link Converter} which converts a source object to a new object with a
|
|
594
|
+
* Helper to create a {@link Converter | Converter} which converts a source object to a new object with a
|
|
591
595
|
* different shape.
|
|
592
596
|
*
|
|
593
597
|
* @remarks
|
|
594
|
-
* On successful conversion, the resulting {@link Converter} returns {@link Success} with a new
|
|
598
|
+
* On successful conversion, the resulting {@link Converter | Converter} returns {@link Success | Success} with a new
|
|
595
599
|
* object, which contains the converted values under the key names specified at initialization time.
|
|
596
|
-
* It returns {@link Failure} with an error message if any fields to be extracted do not exist
|
|
600
|
+
* It returns {@link Failure | Failure} with an error message if any fields to be extracted do not exist
|
|
597
601
|
* or cannot be converted.
|
|
598
602
|
*
|
|
599
603
|
* Fields that succeed but convert to undefined are omitted from the result object but do not
|
|
@@ -602,11 +606,11 @@ exports.discriminatedObject = discriminatedObject;
|
|
|
602
606
|
* @param properties - An object with key names that correspond to the target object and an
|
|
603
607
|
* appropriate {@link Conversion.FieldConverters | FieldConverter} which extracts and converts
|
|
604
608
|
* a single filed from the source object.
|
|
605
|
-
* @returns A {@link Converter} with the specified conversion behavior.
|
|
609
|
+
* @returns A {@link Converter | Converter} with the specified conversion behavior.
|
|
606
610
|
* @public
|
|
607
611
|
*/
|
|
608
612
|
function transform(properties) {
|
|
609
|
-
return new
|
|
613
|
+
return new baseConverter_1.BaseConverter((from, __self, context) => {
|
|
610
614
|
// eslint bug thinks key is used before defined
|
|
611
615
|
// eslint-disable-next-line no-use-before-define
|
|
612
616
|
const converted = {};
|
|
@@ -627,27 +631,27 @@ function transform(properties) {
|
|
|
627
631
|
}
|
|
628
632
|
exports.transform = transform;
|
|
629
633
|
/**
|
|
630
|
-
* Helper to create a strongly-typed {@link Converter} which converts a source object to a
|
|
634
|
+
* Helper to create a strongly-typed {@link Converter | Converter} which converts a source object to a
|
|
631
635
|
* new object with a different shape.
|
|
632
636
|
*
|
|
633
637
|
* @remarks
|
|
634
|
-
* On successful conversion, the resulting {@link Converter} returns {@link Success} with a new
|
|
638
|
+
* On successful conversion, the resulting {@link Converter | Converter} returns {@link Success | Success} with a new
|
|
635
639
|
* object, which contains the converted values under the key names specified at initialization time.
|
|
636
640
|
*
|
|
637
|
-
* It returns {@link Failure} with an error message if any fields to be extracted do not exist
|
|
641
|
+
* It returns {@link Failure | Failure} with an error message if any fields to be extracted do not exist
|
|
638
642
|
* or cannot be converted.
|
|
639
643
|
*
|
|
640
644
|
* @param destinationFields - An object with key names that correspond to the target object and an
|
|
641
645
|
* appropriate {@link Converters.FieldTransformers | FieldTransformers} which specifies the name
|
|
642
|
-
* of the corresponding property in the source object, the converter used
|
|
643
|
-
* property and any configuration to guide the conversion.
|
|
646
|
+
* of the corresponding property in the source object, the converter or validator used for each source
|
|
647
|
+
* property and any other configuration to guide the conversion.
|
|
644
648
|
* @param options - Options which affect the transformation.
|
|
645
649
|
*
|
|
646
|
-
* @returns A {@link Converter} with the specified conversion behavior.
|
|
650
|
+
* @returns A {@link Converter | Converter} with the specified conversion behavior.
|
|
647
651
|
* @public
|
|
648
652
|
*/
|
|
649
653
|
function transformObject(destinationFields, options) {
|
|
650
|
-
return new
|
|
654
|
+
return new baseConverter_1.BaseConverter((from, __self, context) => {
|
|
651
655
|
// eslint bug thinks key is used before defined
|
|
652
656
|
// eslint-disable-next-line no-use-before-define
|
|
653
657
|
const converted = {};
|