@fgv/ts-json-base 5.0.1-7 → 5.0.1-9
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-json-base.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { Converter } from '@fgv/ts-utils';
|
|
|
3
3
|
import { Converters as Converters_3 } from '@fgv/ts-utils';
|
|
4
4
|
import { ObjectConverter as ObjectConverter_2 } from '@fgv/ts-utils';
|
|
5
5
|
import { Result } from '@fgv/ts-utils';
|
|
6
|
+
import { StringConverter } from '@fgv/ts-utils';
|
|
6
7
|
import { Validation } from '@fgv/ts-utils';
|
|
7
8
|
import { Validator } from '@fgv/ts-utils';
|
|
8
9
|
import { Validators as Validators_3 } from '@fgv/ts-utils';
|
|
@@ -38,6 +39,21 @@ declare function arrayOf_2<T, TC = unknown>(validateElement: JsonCompatible_2.Va
|
|
|
38
39
|
*/
|
|
39
40
|
declare type ArrayValidator<T, TC = unknown> = Validation.Classes.ArrayValidator<JsonCompatibleType<T>, TC>;
|
|
40
41
|
|
|
42
|
+
/**
|
|
43
|
+
* A {@link Converter | Converter} which converts `unknown` to a `boolean`.
|
|
44
|
+
* Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
|
|
45
|
+
* Mirrors the behavior of `@fgv/ts-utils`.
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
declare const boolean: Converter<boolean, IJsonConverterContext>;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A {@link Validation.Classes.BooleanValidator | BooleanValidator} which validates a boolean in place.
|
|
52
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
declare const boolean_2: Validator<boolean, IJsonValidatorContext>;
|
|
56
|
+
|
|
41
57
|
/**
|
|
42
58
|
* Identifies whether some `unknown` value is a {@link JsonPrimitive | primitive},
|
|
43
59
|
* {@link JsonObject | object} or {@link JsonArray | array}. Fails for any value
|
|
@@ -62,11 +78,16 @@ declare type Converter_2<T, TC = unknown> = Converter<JsonCompatibleType<T>, TC>
|
|
|
62
78
|
|
|
63
79
|
declare namespace Converters {
|
|
64
80
|
export {
|
|
81
|
+
literal,
|
|
82
|
+
enumeratedValue,
|
|
65
83
|
IJsonConverterContext,
|
|
66
84
|
jsonPrimitive,
|
|
67
85
|
jsonObject,
|
|
68
86
|
jsonArray,
|
|
69
|
-
jsonValue
|
|
87
|
+
jsonValue,
|
|
88
|
+
string,
|
|
89
|
+
number,
|
|
90
|
+
boolean
|
|
70
91
|
}
|
|
71
92
|
}
|
|
72
93
|
export { Converters }
|
|
@@ -187,6 +208,38 @@ declare class DirectoryItem<TCT extends string = string> implements IFileTreeDir
|
|
|
187
208
|
*/
|
|
188
209
|
declare function discriminatedObject<T, TD extends string = string, TC = unknown>(discriminatorProp: string, converters: Converters_3.DiscriminatedObjectConverters<JsonCompatibleType<T>, TD, TC>): JsonCompatible_2.Converter<T, TC>;
|
|
189
210
|
|
|
211
|
+
/**
|
|
212
|
+
* Helper function to create a {@link Converter | Converter} which converts `unknown` to one of a set of
|
|
213
|
+
* supplied enumerated values. Anything else fails.
|
|
214
|
+
*
|
|
215
|
+
* @remarks
|
|
216
|
+
* This JSON variant accepts an {@link Converters.IJsonConverterContext | IJsonConverterContext} OR
|
|
217
|
+
* a `ReadonlyArray<T>` as its conversion context. If the context is an array, it is used to override the
|
|
218
|
+
* allowed values for that conversion; otherwise, the original `values` supplied at creation time are used.
|
|
219
|
+
*
|
|
220
|
+
* @param values - Array of allowed values.
|
|
221
|
+
* @param message - Optional custom failure message.
|
|
222
|
+
* @returns A new {@link Converter | Converter} returning `<T>`.
|
|
223
|
+
* @public
|
|
224
|
+
*/
|
|
225
|
+
declare function enumeratedValue<T>(values: ReadonlyArray<T>, message?: string): Converter<T, IJsonConverterContext | ReadonlyArray<T>>;
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Helper function to create a {@link Validator | Validator} which validates `unknown` to one of a set of
|
|
229
|
+
* supplied enumerated values. Anything else fails.
|
|
230
|
+
*
|
|
231
|
+
* @remarks
|
|
232
|
+
* This JSON variant accepts an {@link Validators.IJsonValidatorContext | IJsonValidatorContext} OR
|
|
233
|
+
* a `ReadonlyArray<T>` as its validation context. If the context is an array, it is used to override the
|
|
234
|
+
* allowed values for that validation; otherwise, the original `values` supplied at creation time are used.
|
|
235
|
+
*
|
|
236
|
+
* @param values - Array of allowed values.
|
|
237
|
+
* @param message - Optional custom failure message.
|
|
238
|
+
* @returns A new {@link Validator | Validator} returning `<T>`.
|
|
239
|
+
* @public
|
|
240
|
+
*/
|
|
241
|
+
declare function enumeratedValue_2<T>(values: ReadonlyArray<T>, message?: string): Validator<T, IJsonValidatorContext | ReadonlyArray<T>>;
|
|
242
|
+
|
|
190
243
|
/**
|
|
191
244
|
* Class representing a file in a file tree.
|
|
192
245
|
* @public
|
|
@@ -1146,6 +1199,37 @@ declare const jsonValue_2: Validator<JsonValue, IJsonValidatorContext>;
|
|
|
1146
1199
|
*/
|
|
1147
1200
|
export declare type JsonValueType = 'primitive' | 'object' | 'array';
|
|
1148
1201
|
|
|
1202
|
+
/**
|
|
1203
|
+
* Helper to create a converter for a literal value.
|
|
1204
|
+
* Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
|
|
1205
|
+
* Mirrors the behavior of `@fgv/ts-utils`.
|
|
1206
|
+
* @public
|
|
1207
|
+
*/
|
|
1208
|
+
declare function literal<T>(value: T): Converter<T, IJsonConverterContext>;
|
|
1209
|
+
|
|
1210
|
+
/**
|
|
1211
|
+
* Helper to create a validator for a literal value.
|
|
1212
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
1213
|
+
* Mirrors the behavior of `@fgv/ts-utils`.
|
|
1214
|
+
* @public
|
|
1215
|
+
*/
|
|
1216
|
+
declare function literal_2<T>(value: T): Validator<T, IJsonValidatorContext>;
|
|
1217
|
+
|
|
1218
|
+
/**
|
|
1219
|
+
* A {@link Converter | Converter} which converts `unknown` to a `number`.
|
|
1220
|
+
* Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
|
|
1221
|
+
* Mirrors the behavior of `@fgv/ts-utils`.
|
|
1222
|
+
* @public
|
|
1223
|
+
*/
|
|
1224
|
+
declare const number: Converter<number, IJsonConverterContext>;
|
|
1225
|
+
|
|
1226
|
+
/**
|
|
1227
|
+
* A {@link Validation.Classes.NumberValidator | NumberValidator} which validates a number in place.
|
|
1228
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
1229
|
+
* @public
|
|
1230
|
+
*/
|
|
1231
|
+
declare const number_2: Validator<number, IJsonValidatorContext>;
|
|
1232
|
+
|
|
1149
1233
|
/**
|
|
1150
1234
|
* A helper function to create a {@link JsonCompatible.ObjectConverter | JSON-compatible ObjectConverter<T, TC>} which converts a
|
|
1151
1235
|
* supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
@@ -1278,6 +1362,20 @@ export declare function sanitizeJsonObject<T>(from: T): Result<T>;
|
|
|
1278
1362
|
*/
|
|
1279
1363
|
declare function strictObject<T, TC = unknown>(properties: Conversion.FieldConverters<JsonCompatibleType<T>, TC>, options?: Converters_3.StrictObjectConverterOptions<JsonCompatibleType<T>>): JsonCompatible_2.ObjectConverter<T, TC>;
|
|
1280
1364
|
|
|
1365
|
+
/**
|
|
1366
|
+
* A {@link Converter | Converter} which converts `unknown` to a `string`.
|
|
1367
|
+
* Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
|
|
1368
|
+
* @public
|
|
1369
|
+
*/
|
|
1370
|
+
declare const string: StringConverter<string, IJsonConverterContext>;
|
|
1371
|
+
|
|
1372
|
+
/**
|
|
1373
|
+
* A {@link Validation.Classes.StringValidator | StringValidator} which validates a string in place.
|
|
1374
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
1375
|
+
* @public
|
|
1376
|
+
*/
|
|
1377
|
+
declare const string_2: Validation.Classes.StringValidator<string, IJsonValidatorContext>;
|
|
1378
|
+
|
|
1281
1379
|
/**
|
|
1282
1380
|
* A validator which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
1283
1381
|
* @public
|
|
@@ -1286,11 +1384,16 @@ declare type Validator_2<T, TC = unknown> = Validator<JsonCompatibleType<T>, TC>
|
|
|
1286
1384
|
|
|
1287
1385
|
declare namespace Validators {
|
|
1288
1386
|
export {
|
|
1387
|
+
literal_2 as literal,
|
|
1388
|
+
enumeratedValue_2 as enumeratedValue,
|
|
1289
1389
|
IJsonValidatorContext,
|
|
1290
1390
|
jsonPrimitive_2 as jsonPrimitive,
|
|
1291
1391
|
jsonObject_2 as jsonObject,
|
|
1292
1392
|
jsonArray_2 as jsonArray,
|
|
1293
|
-
jsonValue_2 as jsonValue
|
|
1393
|
+
jsonValue_2 as jsonValue,
|
|
1394
|
+
string_2 as string,
|
|
1395
|
+
number_2 as number,
|
|
1396
|
+
boolean_2 as boolean
|
|
1294
1397
|
}
|
|
1295
1398
|
}
|
|
1296
1399
|
export { Validators }
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Converter } from '@fgv/ts-utils';
|
|
1
|
+
import { Converter, StringConverter } from '@fgv/ts-utils';
|
|
2
2
|
import { JsonArray, JsonObject, JsonPrimitive, JsonValue } from '../json';
|
|
3
3
|
/**
|
|
4
4
|
* Conversion context for JSON converters.
|
|
@@ -40,4 +40,46 @@ export declare const jsonArray: Converter<JsonArray, IJsonConverterContext>;
|
|
|
40
40
|
* @public
|
|
41
41
|
*/
|
|
42
42
|
export declare const jsonValue: Converter<JsonValue, IJsonConverterContext>;
|
|
43
|
+
/**
|
|
44
|
+
* A {@link Converter | Converter} which converts `unknown` to a `string`.
|
|
45
|
+
* Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
export declare const string: StringConverter<string, IJsonConverterContext>;
|
|
49
|
+
/**
|
|
50
|
+
* A {@link Converter | Converter} which converts `unknown` to a `number`.
|
|
51
|
+
* Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
|
|
52
|
+
* Mirrors the behavior of `@fgv/ts-utils`.
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
export declare const number: Converter<number, IJsonConverterContext>;
|
|
56
|
+
/**
|
|
57
|
+
* A {@link Converter | Converter} which converts `unknown` to a `boolean`.
|
|
58
|
+
* Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
|
|
59
|
+
* Mirrors the behavior of `@fgv/ts-utils`.
|
|
60
|
+
* @public
|
|
61
|
+
*/
|
|
62
|
+
export declare const boolean: Converter<boolean, IJsonConverterContext>;
|
|
63
|
+
/**
|
|
64
|
+
* Helper to create a converter for a literal value.
|
|
65
|
+
* Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
|
|
66
|
+
* Mirrors the behavior of `@fgv/ts-utils`.
|
|
67
|
+
* @public
|
|
68
|
+
*/
|
|
69
|
+
export declare function literal<T>(value: T): Converter<T, IJsonConverterContext>;
|
|
70
|
+
/**
|
|
71
|
+
* Helper function to create a {@link Converter | Converter} which converts `unknown` to one of a set of
|
|
72
|
+
* supplied enumerated values. Anything else fails.
|
|
73
|
+
*
|
|
74
|
+
* @remarks
|
|
75
|
+
* This JSON variant accepts an {@link Converters.IJsonConverterContext | IJsonConverterContext} OR
|
|
76
|
+
* a `ReadonlyArray<T>` as its conversion context. If the context is an array, it is used to override the
|
|
77
|
+
* allowed values for that conversion; otherwise, the original `values` supplied at creation time are used.
|
|
78
|
+
*
|
|
79
|
+
* @param values - Array of allowed values.
|
|
80
|
+
* @param message - Optional custom failure message.
|
|
81
|
+
* @returns A new {@link Converter | Converter} returning `<T>`.
|
|
82
|
+
* @public
|
|
83
|
+
*/
|
|
84
|
+
export declare function enumeratedValue<T>(values: ReadonlyArray<T>, message?: string): Converter<T, IJsonConverterContext | ReadonlyArray<T>>;
|
|
43
85
|
//# sourceMappingURL=converters.d.ts.map
|
|
@@ -21,7 +21,9 @@
|
|
|
21
21
|
* SOFTWARE.
|
|
22
22
|
*/
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
-
exports.jsonValue = exports.jsonArray = exports.jsonObject = exports.jsonPrimitive = void 0;
|
|
24
|
+
exports.boolean = exports.number = exports.string = exports.jsonValue = exports.jsonArray = exports.jsonObject = exports.jsonPrimitive = void 0;
|
|
25
|
+
exports.literal = literal;
|
|
26
|
+
exports.enumeratedValue = enumeratedValue;
|
|
25
27
|
const ts_utils_1 = require("@fgv/ts-utils");
|
|
26
28
|
const json_1 = require("../json");
|
|
27
29
|
/**
|
|
@@ -134,4 +136,57 @@ exports.jsonValue = new ts_utils_1.Conversion.BaseConverter((from, __self, ctx)
|
|
|
134
136
|
}
|
|
135
137
|
return exports.jsonPrimitive.convert(from, ctx);
|
|
136
138
|
});
|
|
139
|
+
/**
|
|
140
|
+
* A {@link Converter | Converter} which converts `unknown` to a `string`.
|
|
141
|
+
* Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
|
|
142
|
+
* @public
|
|
143
|
+
*/
|
|
144
|
+
exports.string = new ts_utils_1.StringConverter();
|
|
145
|
+
/**
|
|
146
|
+
* A {@link Converter | Converter} which converts `unknown` to a `number`.
|
|
147
|
+
* Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
|
|
148
|
+
* Mirrors the behavior of `@fgv/ts-utils`.
|
|
149
|
+
* @public
|
|
150
|
+
*/
|
|
151
|
+
exports.number = new ts_utils_1.Conversion.BaseConverter((from) => ts_utils_1.Converters.number.convert(from));
|
|
152
|
+
/**
|
|
153
|
+
* A {@link Converter | Converter} which converts `unknown` to a `boolean`.
|
|
154
|
+
* Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
|
|
155
|
+
* Mirrors the behavior of `@fgv/ts-utils`.
|
|
156
|
+
* @public
|
|
157
|
+
*/
|
|
158
|
+
exports.boolean = new ts_utils_1.Conversion.BaseConverter((from) => ts_utils_1.Converters.boolean.convert(from));
|
|
159
|
+
/**
|
|
160
|
+
* Helper to create a converter for a literal value.
|
|
161
|
+
* Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
|
|
162
|
+
* Mirrors the behavior of `@fgv/ts-utils`.
|
|
163
|
+
* @public
|
|
164
|
+
*/
|
|
165
|
+
function literal(value) {
|
|
166
|
+
return ts_utils_1.Converters.literal(value);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Helper function to create a {@link Converter | Converter} which converts `unknown` to one of a set of
|
|
170
|
+
* supplied enumerated values. Anything else fails.
|
|
171
|
+
*
|
|
172
|
+
* @remarks
|
|
173
|
+
* This JSON variant accepts an {@link Converters.IJsonConverterContext | IJsonConverterContext} OR
|
|
174
|
+
* a `ReadonlyArray<T>` as its conversion context. If the context is an array, it is used to override the
|
|
175
|
+
* allowed values for that conversion; otherwise, the original `values` supplied at creation time are used.
|
|
176
|
+
*
|
|
177
|
+
* @param values - Array of allowed values.
|
|
178
|
+
* @param message - Optional custom failure message.
|
|
179
|
+
* @returns A new {@link Converter | Converter} returning `<T>`.
|
|
180
|
+
* @public
|
|
181
|
+
*/
|
|
182
|
+
function enumeratedValue(values, message) {
|
|
183
|
+
return new ts_utils_1.Conversion.BaseConverter((from, __self, context) => {
|
|
184
|
+
const effectiveValues = Array.isArray(context) ? context : values;
|
|
185
|
+
const index = effectiveValues.indexOf(from);
|
|
186
|
+
if (index >= 0) {
|
|
187
|
+
return (0, ts_utils_1.succeed)(effectiveValues[index]);
|
|
188
|
+
}
|
|
189
|
+
return (0, ts_utils_1.fail)(message !== null && message !== void 0 ? message : `Invalid enumerated value ${JSON.stringify(from)}`);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
137
192
|
//# sourceMappingURL=converters.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Validator } from '@fgv/ts-utils';
|
|
1
|
+
import { Validation, Validator } from '@fgv/ts-utils';
|
|
2
2
|
import { JsonArray, JsonObject, JsonPrimitive, JsonValue } from '../json';
|
|
3
3
|
/**
|
|
4
4
|
* Validation context for in-place JSON validators.
|
|
@@ -37,4 +37,44 @@ export declare const jsonArray: Validator<JsonArray, IJsonValidatorContext>;
|
|
|
37
37
|
* @public
|
|
38
38
|
*/
|
|
39
39
|
export declare const jsonValue: Validator<JsonValue, IJsonValidatorContext>;
|
|
40
|
+
/**
|
|
41
|
+
* A {@link Validation.Classes.StringValidator | StringValidator} which validates a string in place.
|
|
42
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export declare const string: Validation.Classes.StringValidator<string, IJsonValidatorContext>;
|
|
46
|
+
/**
|
|
47
|
+
* A {@link Validation.Classes.NumberValidator | NumberValidator} which validates a number in place.
|
|
48
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
49
|
+
* @public
|
|
50
|
+
*/
|
|
51
|
+
export declare const number: Validator<number, IJsonValidatorContext>;
|
|
52
|
+
/**
|
|
53
|
+
* A {@link Validation.Classes.BooleanValidator | BooleanValidator} which validates a boolean in place.
|
|
54
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
55
|
+
* @public
|
|
56
|
+
*/
|
|
57
|
+
export declare const boolean: Validator<boolean, IJsonValidatorContext>;
|
|
58
|
+
/**
|
|
59
|
+
* Helper to create a validator for a literal value.
|
|
60
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
61
|
+
* Mirrors the behavior of `@fgv/ts-utils`.
|
|
62
|
+
* @public
|
|
63
|
+
*/
|
|
64
|
+
export declare function literal<T>(value: T): Validator<T, IJsonValidatorContext>;
|
|
65
|
+
/**
|
|
66
|
+
* Helper function to create a {@link Validator | Validator} which validates `unknown` to one of a set of
|
|
67
|
+
* supplied enumerated values. Anything else fails.
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* This JSON variant accepts an {@link Validators.IJsonValidatorContext | IJsonValidatorContext} OR
|
|
71
|
+
* a `ReadonlyArray<T>` as its validation context. If the context is an array, it is used to override the
|
|
72
|
+
* allowed values for that validation; otherwise, the original `values` supplied at creation time are used.
|
|
73
|
+
*
|
|
74
|
+
* @param values - Array of allowed values.
|
|
75
|
+
* @param message - Optional custom failure message.
|
|
76
|
+
* @returns A new {@link Validator | Validator} returning `<T>`.
|
|
77
|
+
* @public
|
|
78
|
+
*/
|
|
79
|
+
export declare function enumeratedValue<T>(values: ReadonlyArray<T>, message?: string): Validator<T, IJsonValidatorContext | ReadonlyArray<T>>;
|
|
40
80
|
//# sourceMappingURL=validators.d.ts.map
|
|
@@ -21,7 +21,9 @@
|
|
|
21
21
|
* SOFTWARE.
|
|
22
22
|
*/
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
-
exports.jsonValue = exports.jsonArray = exports.jsonObject = exports.jsonPrimitive = void 0;
|
|
24
|
+
exports.boolean = exports.number = exports.string = exports.jsonValue = exports.jsonArray = exports.jsonObject = exports.jsonPrimitive = void 0;
|
|
25
|
+
exports.literal = literal;
|
|
26
|
+
exports.enumeratedValue = enumeratedValue;
|
|
25
27
|
const ts_utils_1 = require("@fgv/ts-utils");
|
|
26
28
|
const json_1 = require("../json");
|
|
27
29
|
/**
|
|
@@ -122,4 +124,61 @@ exports.jsonValue = new ts_utils_1.Validation.Base.GenericValidator({
|
|
|
122
124
|
return result.success === true ? true : result;
|
|
123
125
|
}
|
|
124
126
|
});
|
|
127
|
+
/**
|
|
128
|
+
* A {@link Validation.Classes.StringValidator | StringValidator} which validates a string in place.
|
|
129
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
130
|
+
* @public
|
|
131
|
+
*/
|
|
132
|
+
exports.string = new ts_utils_1.Validation.Classes.StringValidator();
|
|
133
|
+
/**
|
|
134
|
+
* A {@link Validation.Classes.NumberValidator | NumberValidator} which validates a number in place.
|
|
135
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
136
|
+
* @public
|
|
137
|
+
*/
|
|
138
|
+
exports.number = new ts_utils_1.Validation.Classes.NumberValidator();
|
|
139
|
+
/**
|
|
140
|
+
* A {@link Validation.Classes.BooleanValidator | BooleanValidator} which validates a boolean in place.
|
|
141
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
142
|
+
* @public
|
|
143
|
+
*/
|
|
144
|
+
exports.boolean = new ts_utils_1.Validation.Classes.BooleanValidator();
|
|
145
|
+
/**
|
|
146
|
+
* Helper to create a validator for a literal value.
|
|
147
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
148
|
+
* Mirrors the behavior of `@fgv/ts-utils`.
|
|
149
|
+
* @public
|
|
150
|
+
*/
|
|
151
|
+
function literal(value) {
|
|
152
|
+
return new ts_utils_1.Validation.Base.GenericValidator({
|
|
153
|
+
validator: (from) => {
|
|
154
|
+
return from === value ? true : (0, ts_utils_1.fail)(`Expected literal ${String(value)}, found ${JSON.stringify(from)}`);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Helper function to create a {@link Validator | Validator} which validates `unknown` to one of a set of
|
|
160
|
+
* supplied enumerated values. Anything else fails.
|
|
161
|
+
*
|
|
162
|
+
* @remarks
|
|
163
|
+
* This JSON variant accepts an {@link Validators.IJsonValidatorContext | IJsonValidatorContext} OR
|
|
164
|
+
* a `ReadonlyArray<T>` as its validation context. If the context is an array, it is used to override the
|
|
165
|
+
* allowed values for that validation; otherwise, the original `values` supplied at creation time are used.
|
|
166
|
+
*
|
|
167
|
+
* @param values - Array of allowed values.
|
|
168
|
+
* @param message - Optional custom failure message.
|
|
169
|
+
* @returns A new {@link Validator | Validator} returning `<T>`.
|
|
170
|
+
* @public
|
|
171
|
+
*/
|
|
172
|
+
function enumeratedValue(values, message) {
|
|
173
|
+
return new ts_utils_1.Validation.Base.GenericValidator({
|
|
174
|
+
validator: (from, context) => {
|
|
175
|
+
const effectiveValues = Array.isArray(context) ? context : values;
|
|
176
|
+
const index = effectiveValues.indexOf(from);
|
|
177
|
+
if (index >= 0) {
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
return (0, ts_utils_1.fail)(message !== null && message !== void 0 ? message : `Invalid enumerated value ${JSON.stringify(from)}`);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
125
184
|
//# sourceMappingURL=validators.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fgv/ts-json-base",
|
|
3
|
-
"version": "5.0.1-
|
|
3
|
+
"version": "5.0.1-9",
|
|
4
4
|
"description": "Typescript types and basic functions for working with json",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "dist/ts-json-base.d.ts",
|
|
@@ -53,11 +53,11 @@
|
|
|
53
53
|
"@rushstack/eslint-config": "4.5.3",
|
|
54
54
|
"eslint-plugin-tsdoc": "~0.4.0",
|
|
55
55
|
"@types/luxon": "^3.7.1",
|
|
56
|
-
"@fgv/ts-utils": "5.0.1-
|
|
57
|
-
"@fgv/ts-utils-jest": "5.0.1-
|
|
56
|
+
"@fgv/ts-utils": "5.0.1-9",
|
|
57
|
+
"@fgv/ts-utils-jest": "5.0.1-9"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
|
-
"@fgv/ts-utils": "5.0.1-
|
|
60
|
+
"@fgv/ts-utils": "5.0.1-9"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
63
|
"luxon": "^3.7.2"
|