@fgv/ts-json-base 5.0.1-8 → 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.
@@ -47,6 +47,13 @@ declare type ArrayValidator<T, TC = unknown> = Validation.Classes.ArrayValidator
47
47
  */
48
48
  declare const boolean: Converter<boolean, IJsonConverterContext>;
49
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
+
50
57
  /**
51
58
  * Identifies whether some `unknown` value is a {@link JsonPrimitive | primitive},
52
59
  * {@link JsonObject | object} or {@link JsonArray | array}. Fails for any value
@@ -217,6 +224,22 @@ declare function discriminatedObject<T, TD extends string = string, TC = unknown
217
224
  */
218
225
  declare function enumeratedValue<T>(values: ReadonlyArray<T>, message?: string): Converter<T, IJsonConverterContext | ReadonlyArray<T>>;
219
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
+
220
243
  /**
221
244
  * Class representing a file in a file tree.
222
245
  * @public
@@ -1184,6 +1207,14 @@ export declare type JsonValueType = 'primitive' | 'object' | 'array';
1184
1207
  */
1185
1208
  declare function literal<T>(value: T): Converter<T, IJsonConverterContext>;
1186
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
+
1187
1218
  /**
1188
1219
  * A {@link Converter | Converter} which converts `unknown` to a `number`.
1189
1220
  * Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
@@ -1192,6 +1223,13 @@ declare function literal<T>(value: T): Converter<T, IJsonConverterContext>;
1192
1223
  */
1193
1224
  declare const number: Converter<number, IJsonConverterContext>;
1194
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
+
1195
1233
  /**
1196
1234
  * A helper function to create a {@link JsonCompatible.ObjectConverter | JSON-compatible ObjectConverter<T, TC>} which converts a
1197
1235
  * supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
@@ -1331,6 +1369,13 @@ declare function strictObject<T, TC = unknown>(properties: Conversion.FieldConve
1331
1369
  */
1332
1370
  declare const string: StringConverter<string, IJsonConverterContext>;
1333
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
+
1334
1379
  /**
1335
1380
  * A validator which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
1336
1381
  * @public
@@ -1339,11 +1384,16 @@ declare type Validator_2<T, TC = unknown> = Validator<JsonCompatibleType<T>, TC>
1339
1384
 
1340
1385
  declare namespace Validators {
1341
1386
  export {
1387
+ literal_2 as literal,
1388
+ enumeratedValue_2 as enumeratedValue,
1342
1389
  IJsonValidatorContext,
1343
1390
  jsonPrimitive_2 as jsonPrimitive,
1344
1391
  jsonObject_2 as jsonObject,
1345
1392
  jsonArray_2 as jsonArray,
1346
- jsonValue_2 as jsonValue
1393
+ jsonValue_2 as jsonValue,
1394
+ string_2 as string,
1395
+ number_2 as number,
1396
+ boolean_2 as boolean
1347
1397
  }
1348
1398
  }
1349
1399
  export { Validators }
@@ -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-8",
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-8",
57
- "@fgv/ts-utils-jest": "5.0.1-8"
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-8"
60
+ "@fgv/ts-utils": "5.0.1-9"
61
61
  },
62
62
  "dependencies": {
63
63
  "luxon": "^3.7.2"