@fgv/ts-json-base 5.0.1-2 → 5.0.1-3
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/README.md +5 -5
- package/dist/ts-json-base.d.ts +161 -11
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/index.d.ts +2 -1
- package/lib/index.js +3 -1
- package/lib/packlets/json/common.d.ts +4 -4
- package/lib/packlets/json-compatible/common.d.ts +23 -0
- package/lib/packlets/json-compatible/common.js +24 -0
- package/lib/packlets/json-compatible/converters.d.ts +54 -0
- package/lib/packlets/json-compatible/converters.js +91 -0
- package/lib/packlets/json-compatible/index.d.ts +5 -0
- package/lib/packlets/json-compatible/index.js +66 -0
- package/lib/packlets/json-compatible/validators.d.ts +28 -0
- package/lib/packlets/json-compatible/validators.js +58 -0
- package/lib/packlets/json-file/jsonTreeHelper.js +1 -1
- package/package.json +17 -17
package/README.md
CHANGED
|
@@ -34,7 +34,7 @@ interface JsonArray extends Array<JsonValue> { }
|
|
|
34
34
|
|
|
35
35
|
### JsonCompatible Type
|
|
36
36
|
|
|
37
|
-
The `
|
|
37
|
+
The `JsonCompatibleType<T>` type provides compile-time validation that a type is JSON-serializable, unlike `JsonValue` which requires an index signature. This is particularly useful for strongly-typed interfaces.
|
|
38
38
|
|
|
39
39
|
#### Basic Usage
|
|
40
40
|
|
|
@@ -52,7 +52,7 @@ interface UserData {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
// ✅ This type remains unchanged because it's fully JSON-compatible
|
|
55
|
-
type ValidatedUserData =
|
|
55
|
+
type ValidatedUserData = JsonCompatibleType<UserData>;
|
|
56
56
|
|
|
57
57
|
// ❌ Interface with non-JSON properties
|
|
58
58
|
interface UserWithMethods {
|
|
@@ -62,16 +62,16 @@ interface UserWithMethods {
|
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
// ❌ This type transforms 'save' to an error type
|
|
65
|
-
type InvalidUserData =
|
|
65
|
+
type InvalidUserData = JsonCompatibleType<UserWithMethods>;
|
|
66
66
|
// Result: { id: string; name: string; save: ['Error: Function is not JSON-compatible'] }
|
|
67
67
|
```
|
|
68
68
|
|
|
69
69
|
#### The MapOf Pattern
|
|
70
70
|
|
|
71
|
-
The most powerful application is using `
|
|
71
|
+
The most powerful application is using `JsonCompatibleType<T>` as a constraint with default type parameters:
|
|
72
72
|
|
|
73
73
|
```typescript
|
|
74
|
-
class MapOf<T, TV extends
|
|
74
|
+
class MapOf<T, TV extends JsonCompatibleType<T> = JsonCompatibleType<T>> extends Map<string, TV> {
|
|
75
75
|
public override set(key: string, value: TV): this {
|
|
76
76
|
return super.set(key, value);
|
|
77
77
|
}
|
package/dist/ts-json-base.d.ts
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
|
+
import { Conversion } from '@fgv/ts-utils';
|
|
1
2
|
import { Converter } from '@fgv/ts-utils';
|
|
3
|
+
import { Converters as Converters_3 } from '@fgv/ts-utils';
|
|
4
|
+
import { ObjectConverter as ObjectConverter_2 } from '@fgv/ts-utils';
|
|
2
5
|
import { Result } from '@fgv/ts-utils';
|
|
6
|
+
import { Validation } from '@fgv/ts-utils';
|
|
3
7
|
import { Validator } from '@fgv/ts-utils';
|
|
8
|
+
import { Validators as Validators_3 } from '@fgv/ts-utils';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A helper function to create a {@link Converter | Converter} which converts a supplied `unknown` value to a valid
|
|
12
|
+
* array of {@link JsonCompatibleType | JsonCompatibleType<T>}.
|
|
13
|
+
* @param converter - {@link JsonCompatible.Converter | JSON-compatible Converter<T, TC>}
|
|
14
|
+
* or {@link JsonCompatible.Validator | JSON-compatible Validator<T>} used for each item in the source array.
|
|
15
|
+
* @param onError - The error handling option to use for the conversion.
|
|
16
|
+
* @returns A {@link Converter | Converter} which returns `JsonCompatibleType<T>[]`.
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
19
|
+
declare function arrayOf<T, TC = unknown>(converter: JsonCompatible_2.Converter<T, TC> | JsonCompatible_2.Validator<T, TC>, onError?: Conversion.OnError): Converter<JsonCompatibleType<T>[], TC>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A helper function to create a {@link JsonCompatible.ArrayValidator | JSON-compatible ArrayValidator<T, TC>} which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
23
|
+
* @param validateElement - The element to validate.
|
|
24
|
+
* @param params - The parameters to use for the validation.
|
|
25
|
+
* @returns A {@link JsonCompatible.ArrayValidator | JSON-compatible ArrayValidator<T, TC>} which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
declare function arrayOf_2<T, TC = unknown>(validateElement: JsonCompatible_2.Validator<T, TC>, params?: Omit<Validation.Classes.ArrayValidatorConstructorParams<JsonCompatibleType<T>, TC>, 'validateElement'>): Validation.Classes.ArrayValidator<JsonCompatibleType<T>, TC>;
|
|
4
29
|
|
|
5
30
|
/**
|
|
6
31
|
* Identifies whether some `unknown` value is a {@link JsonPrimitive | primitive},
|
|
@@ -18,6 +43,12 @@ export declare function classifyJsonValue(from: unknown): Result<JsonValueType>;
|
|
|
18
43
|
*/
|
|
19
44
|
declare type ContentTypeFactory<TCT extends string = string> = (filePath: string, provided?: string) => Result<TCT | undefined>;
|
|
20
45
|
|
|
46
|
+
/**
|
|
47
|
+
* A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
48
|
+
* @public
|
|
49
|
+
*/
|
|
50
|
+
declare type Converter_2<T, TC = unknown> = Converter<JsonCompatibleType<T>, TC>;
|
|
51
|
+
|
|
21
52
|
declare namespace Converters {
|
|
22
53
|
export {
|
|
23
54
|
IJsonConverterContext,
|
|
@@ -29,6 +60,16 @@ declare namespace Converters {
|
|
|
29
60
|
}
|
|
30
61
|
export { Converters }
|
|
31
62
|
|
|
63
|
+
declare namespace Converters_2 {
|
|
64
|
+
export {
|
|
65
|
+
arrayOf,
|
|
66
|
+
recordOf,
|
|
67
|
+
object,
|
|
68
|
+
strictObject,
|
|
69
|
+
discriminatedObject
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
32
73
|
/**
|
|
33
74
|
* Reads all JSON files from a directory and apply a supplied converter.
|
|
34
75
|
* @param srcPath - The path of the folder to be read.
|
|
@@ -124,6 +165,15 @@ declare class DirectoryItem<TCT extends string = string> implements IFileTreeDir
|
|
|
124
165
|
getChildren(): Result<ReadonlyArray<FileTreeItem<TCT>>>;
|
|
125
166
|
}
|
|
126
167
|
|
|
168
|
+
/**
|
|
169
|
+
* A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
170
|
+
* @param discriminatorProp - The name of the property used to discriminate types.
|
|
171
|
+
* @param converters - The converters to use for the conversion.
|
|
172
|
+
* @returns A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
173
|
+
* @public
|
|
174
|
+
*/
|
|
175
|
+
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>;
|
|
176
|
+
|
|
127
177
|
/**
|
|
128
178
|
* Class representing a file in a file tree.
|
|
129
179
|
* @public
|
|
@@ -758,6 +808,35 @@ declare const jsonArray: Converter<JsonArray, IJsonConverterContext>;
|
|
|
758
808
|
*/
|
|
759
809
|
declare const jsonArray_2: Validator<JsonArray, IJsonValidatorContext>;
|
|
760
810
|
|
|
811
|
+
declare namespace JsonCompatible {
|
|
812
|
+
export {
|
|
813
|
+
Converters_2 as Converters,
|
|
814
|
+
Validators_2 as Validators,
|
|
815
|
+
Converter_2 as Converter,
|
|
816
|
+
Validator_2 as Validator,
|
|
817
|
+
ObjectConverter,
|
|
818
|
+
ObjectValidator
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
export { JsonCompatible }
|
|
822
|
+
|
|
823
|
+
declare namespace JsonCompatible_2 {
|
|
824
|
+
export {
|
|
825
|
+
Converter_2 as Converter,
|
|
826
|
+
Validator_2 as Validator,
|
|
827
|
+
ObjectConverter,
|
|
828
|
+
ObjectValidator
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
/**
|
|
833
|
+
* A type that represents an array of JSON-compatible values.
|
|
834
|
+
* @param T - The type to be constrained
|
|
835
|
+
* @returns A constrained type that is compatible with JSON serialization.
|
|
836
|
+
* @public
|
|
837
|
+
*/
|
|
838
|
+
export declare type JsonCompatibleArray<T> = Array<JsonCompatibleType<T>>;
|
|
839
|
+
|
|
761
840
|
/**
|
|
762
841
|
* A constrained type that is compatible with JSON serialization.
|
|
763
842
|
*
|
|
@@ -783,7 +862,7 @@ declare const jsonArray_2: Validator<JsonArray, IJsonValidatorContext>;
|
|
|
783
862
|
* email?: string; // Optional property can be undefined
|
|
784
863
|
* }
|
|
785
864
|
*
|
|
786
|
-
* type UserCompatible =
|
|
865
|
+
* type UserCompatible = JsonCompatibleType<IUser>; // Allows undefined for email
|
|
787
866
|
*
|
|
788
867
|
* const user: UserCompatible = {
|
|
789
868
|
* name: "John",
|
|
@@ -797,18 +876,10 @@ declare const jsonArray_2: Validator<JsonArray, IJsonValidatorContext>;
|
|
|
797
876
|
*
|
|
798
877
|
* @public
|
|
799
878
|
*/
|
|
800
|
-
export declare type
|
|
801
|
-
[K in keyof T]:
|
|
879
|
+
export declare type JsonCompatibleType<T> = IsUnknown<T> extends true ? JsonValue : T extends JsonPrimitive | undefined ? T : T extends Array<unknown> ? JsonCompatibleArray<T[number]> : T extends Function ? ['Error: Function is not JSON-compatible'] : T extends object ? {
|
|
880
|
+
[K in keyof T]: JsonCompatibleType<T[K]>;
|
|
802
881
|
} : ['Error: Non-JSON type'];
|
|
803
882
|
|
|
804
|
-
/**
|
|
805
|
-
* A type that represents an array of JSON-compatible values.
|
|
806
|
-
* @param T - The type to be constrained
|
|
807
|
-
* @returns A constrained type that is compatible with JSON serialization.
|
|
808
|
-
* @public
|
|
809
|
-
*/
|
|
810
|
-
export declare type JsonCompatibleArray<T> = Array<JsonCompatible<T>>;
|
|
811
|
-
|
|
812
883
|
declare namespace JsonFile {
|
|
813
884
|
export {
|
|
814
885
|
readJsonFileSync,
|
|
@@ -1054,6 +1125,36 @@ declare const jsonValue_2: Validator<JsonValue, IJsonValidatorContext>;
|
|
|
1054
1125
|
*/
|
|
1055
1126
|
export declare type JsonValueType = 'primitive' | 'object' | 'array';
|
|
1056
1127
|
|
|
1128
|
+
/**
|
|
1129
|
+
* A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
1130
|
+
* @param properties - The properties to convert.
|
|
1131
|
+
* @param options - The options to use for the conversion.
|
|
1132
|
+
* @returns A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
1133
|
+
* @public
|
|
1134
|
+
*/
|
|
1135
|
+
declare function object<T, TC = unknown>(properties: Conversion.FieldConverters<JsonCompatibleType<T>, TC>, options?: Conversion.ObjectConverterOptions<JsonCompatibleType<T>>): JsonCompatible_2.ObjectConverter<T, TC>;
|
|
1136
|
+
|
|
1137
|
+
/**
|
|
1138
|
+
* A helper function to create a {@link JsonCompatible.ObjectValidator | JSON-compatible ObjectValidator<T, TC>} which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
1139
|
+
* @param properties - The properties to validate.
|
|
1140
|
+
* @param params - The parameters to use for the validation.
|
|
1141
|
+
* @returns A {@link JsonCompatible.ObjectValidator | JSON-compatible ObjectValidator<T, TC>} which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
1142
|
+
* @public
|
|
1143
|
+
*/
|
|
1144
|
+
declare function object_2<T, TC = unknown>(properties: Validation.Classes.FieldValidators<JsonCompatibleType<T>, TC>, params?: Omit<Validation.Classes.ObjectValidatorConstructorParams<JsonCompatibleType<T>, TC>, 'fields'>): JsonCompatible_2.ObjectValidator<T, TC>;
|
|
1145
|
+
|
|
1146
|
+
/**
|
|
1147
|
+
* A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
1148
|
+
* @public
|
|
1149
|
+
*/
|
|
1150
|
+
declare type ObjectConverter<T, TC = unknown> = ObjectConverter_2<JsonCompatibleType<T>, TC>;
|
|
1151
|
+
|
|
1152
|
+
/**
|
|
1153
|
+
* A validator which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
1154
|
+
* @public
|
|
1155
|
+
*/
|
|
1156
|
+
declare type ObjectValidator<T, TC = unknown> = Validation.Classes.ObjectValidator<JsonCompatibleType<T>, TC>;
|
|
1157
|
+
|
|
1057
1158
|
/**
|
|
1058
1159
|
* Picks a nested {@link JsonObject | JsonObject} from a supplied
|
|
1059
1160
|
* {@link JsonObject | JsonObject}.
|
|
@@ -1082,6 +1183,32 @@ export declare function pickJsonValue(src: JsonObject, path: string): Result<Jso
|
|
|
1082
1183
|
*/
|
|
1083
1184
|
declare function readJsonFileSync(srcPath: string): Result<JsonValue>;
|
|
1084
1185
|
|
|
1186
|
+
/**
|
|
1187
|
+
* A helper function to create a {@link Converter | Converter} or which converts the `string`-keyed properties
|
|
1188
|
+
* using a supplied {@link JsonCompatible.Converter | JSON-compatible Converter<T, TC>} or
|
|
1189
|
+
* {@link JsonCompatible.Validator | JSON-compatible Validator<T>} to produce a
|
|
1190
|
+
* `Record<TK, JsonCompatibleType<T>>`.
|
|
1191
|
+
* @remarks
|
|
1192
|
+
* If present, the supplied {@link Converters.KeyedConverterOptions | options} can provide a strongly-typed
|
|
1193
|
+
* converter for keys and/or control the handling of elements that fail conversion.
|
|
1194
|
+
* @param converter - {@link JsonCompatible.Converter | JSON-compatible Converter<T, TC>}
|
|
1195
|
+
* or {@link JsonCompatible.Validator | JSON-compatible Validator<T>} used for each item in the source object.
|
|
1196
|
+
* @param options - Optional {@link Converters.KeyedConverterOptions | KeyedConverterOptions<TK, TC>} which
|
|
1197
|
+
* supplies a key converter and/or error-handling options.
|
|
1198
|
+
* @returns A {@link Converter | Converter} which returns `Record<TK, JsonCompatibleType<T>>`.
|
|
1199
|
+
* @public
|
|
1200
|
+
*/
|
|
1201
|
+
declare function recordOf<T, TC = unknown, TK extends string = string>(converter: JsonCompatible_2.Converter<T, TC> | JsonCompatible_2.Validator<T, TC>, options?: Converters_3.KeyedConverterOptions<TK, TC>): Converter<Record<TK, JsonCompatibleType<T>>, TC>;
|
|
1202
|
+
|
|
1203
|
+
/**
|
|
1204
|
+
* A helper function to create a {@link JsonCompatible.RecordValidator | JSON-compatible RecordValidator<T, TC>} which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
1205
|
+
* @param validateElement - The element to validate.
|
|
1206
|
+
* @param options - The options to use for the validation.
|
|
1207
|
+
* @returns A {@link JsonCompatible.RecordValidator | JSON-compatible RecordValidator<T, TC>} which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
1208
|
+
* @public
|
|
1209
|
+
*/
|
|
1210
|
+
declare function recordOf_2<T, TC = unknown, TK extends string = string>(validateElement: JsonCompatible_2.Validator<T, TC>, options?: Validators_3.IRecordOfValidatorOptions<TK, TC>): Validation.Validator<Record<TK, JsonCompatibleType<T>>, TC>;
|
|
1211
|
+
|
|
1085
1212
|
/**
|
|
1086
1213
|
* "Sanitizes" an `unknown` by stringifying and then parsing it. Guarantees a
|
|
1087
1214
|
* valid {@link JsonValue | JsonValue} but is not idempotent and gives no guarantees
|
|
@@ -1103,6 +1230,21 @@ export declare function sanitizeJson(from: unknown): Result<JsonValue>;
|
|
|
1103
1230
|
*/
|
|
1104
1231
|
export declare function sanitizeJsonObject<T>(from: T): Result<T>;
|
|
1105
1232
|
|
|
1233
|
+
/**
|
|
1234
|
+
* A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
1235
|
+
* @param properties - The properties to convert.
|
|
1236
|
+
* @param options - The options to use for the conversion.
|
|
1237
|
+
* @returns A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
1238
|
+
* @public
|
|
1239
|
+
*/
|
|
1240
|
+
declare function strictObject<T, TC = unknown>(properties: Conversion.FieldConverters<JsonCompatibleType<T>, TC>, options?: Converters_3.StrictObjectConverterOptions<JsonCompatibleType<T>>): JsonCompatible_2.ObjectConverter<T, TC>;
|
|
1241
|
+
|
|
1242
|
+
/**
|
|
1243
|
+
* A validator which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
1244
|
+
* @public
|
|
1245
|
+
*/
|
|
1246
|
+
declare type Validator_2<T, TC = unknown> = Validator<JsonCompatibleType<T>, TC>;
|
|
1247
|
+
|
|
1106
1248
|
declare namespace Validators {
|
|
1107
1249
|
export {
|
|
1108
1250
|
IJsonValidatorContext,
|
|
@@ -1114,6 +1256,14 @@ declare namespace Validators {
|
|
|
1114
1256
|
}
|
|
1115
1257
|
export { Validators }
|
|
1116
1258
|
|
|
1259
|
+
declare namespace Validators_2 {
|
|
1260
|
+
export {
|
|
1261
|
+
arrayOf_2 as arrayOf,
|
|
1262
|
+
recordOf_2 as recordOf,
|
|
1263
|
+
object_2 as object
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1117
1267
|
/**
|
|
1118
1268
|
* {@inheritDoc JsonFile.JsonFsHelper.writeJsonFileSync}
|
|
1119
1269
|
* @public
|
package/dist/tsdoc-metadata.json
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import * as Converters from './packlets/converters';
|
|
2
2
|
import * as FileTree from './packlets/file-tree';
|
|
3
|
+
import * as JsonCompatible from './packlets/json-compatible';
|
|
3
4
|
import * as JsonFile from './packlets/json-file';
|
|
4
5
|
import * as Validators from './packlets/validators';
|
|
5
6
|
export * from './packlets/json';
|
|
6
|
-
export { Converters, FileTree, JsonFile, Validators };
|
|
7
|
+
export { Converters, FileTree, JsonCompatible, JsonFile, Validators };
|
|
7
8
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.js
CHANGED
|
@@ -57,11 +57,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
57
57
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
58
58
|
};
|
|
59
59
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
60
|
-
exports.Validators = exports.JsonFile = exports.FileTree = exports.Converters = void 0;
|
|
60
|
+
exports.Validators = exports.JsonFile = exports.JsonCompatible = exports.FileTree = exports.Converters = void 0;
|
|
61
61
|
const Converters = __importStar(require("./packlets/converters"));
|
|
62
62
|
exports.Converters = Converters;
|
|
63
63
|
const FileTree = __importStar(require("./packlets/file-tree"));
|
|
64
64
|
exports.FileTree = FileTree;
|
|
65
|
+
const JsonCompatible = __importStar(require("./packlets/json-compatible"));
|
|
66
|
+
exports.JsonCompatible = JsonCompatible;
|
|
65
67
|
const JsonFile = __importStar(require("./packlets/json-file"));
|
|
66
68
|
exports.JsonFile = JsonFile;
|
|
67
69
|
const Validators = __importStar(require("./packlets/validators"));
|
|
@@ -58,7 +58,7 @@ type IsUnknown<T> = unknown extends T ? ([T] extends [unknown] ? true : false) :
|
|
|
58
58
|
* email?: string; // Optional property can be undefined
|
|
59
59
|
* }
|
|
60
60
|
*
|
|
61
|
-
* type UserCompatible =
|
|
61
|
+
* type UserCompatible = JsonCompatibleType<IUser>; // Allows undefined for email
|
|
62
62
|
*
|
|
63
63
|
* const user: UserCompatible = {
|
|
64
64
|
* name: "John",
|
|
@@ -72,8 +72,8 @@ type IsUnknown<T> = unknown extends T ? ([T] extends [unknown] ? true : false) :
|
|
|
72
72
|
*
|
|
73
73
|
* @public
|
|
74
74
|
*/
|
|
75
|
-
export type
|
|
76
|
-
[K in keyof T]:
|
|
75
|
+
export type JsonCompatibleType<T> = IsUnknown<T> extends true ? JsonValue : T extends JsonPrimitive | undefined ? T : T extends Array<unknown> ? JsonCompatibleArray<T[number]> : T extends Function ? ['Error: Function is not JSON-compatible'] : T extends object ? {
|
|
76
|
+
[K in keyof T]: JsonCompatibleType<T[K]>;
|
|
77
77
|
} : ['Error: Non-JSON type'];
|
|
78
78
|
/**
|
|
79
79
|
* A type that represents an array of JSON-compatible values.
|
|
@@ -81,7 +81,7 @@ export type JsonCompatible<T> = IsUnknown<T> extends true ? JsonValue : T extend
|
|
|
81
81
|
* @returns A constrained type that is compatible with JSON serialization.
|
|
82
82
|
* @public
|
|
83
83
|
*/
|
|
84
|
-
export type JsonCompatibleArray<T> = Array<
|
|
84
|
+
export type JsonCompatibleArray<T> = Array<JsonCompatibleType<T>>;
|
|
85
85
|
/**
|
|
86
86
|
* Test if an `unknown` is a {@link JsonValue | JsonValue}.
|
|
87
87
|
* @param from - The `unknown` to be tested
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Converter as BaseConverter, ObjectConverter as BaseObjectConverter, Validation, Validator as BaseValidator } from '@fgv/ts-utils';
|
|
2
|
+
import { JsonCompatibleType } from '../json';
|
|
3
|
+
/**
|
|
4
|
+
* A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export type Converter<T, TC = unknown> = BaseConverter<JsonCompatibleType<T>, TC>;
|
|
8
|
+
/**
|
|
9
|
+
* A validator which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
export type Validator<T, TC = unknown> = BaseValidator<JsonCompatibleType<T>, TC>;
|
|
13
|
+
/**
|
|
14
|
+
* A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
15
|
+
* @public
|
|
16
|
+
*/
|
|
17
|
+
export type ObjectConverter<T, TC = unknown> = BaseObjectConverter<JsonCompatibleType<T>, TC>;
|
|
18
|
+
/**
|
|
19
|
+
* A validator which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export type ObjectValidator<T, TC = unknown> = Validation.Classes.ObjectValidator<JsonCompatibleType<T>, TC>;
|
|
23
|
+
//# sourceMappingURL=common.d.ts.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2025 Erik Fortune
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
* copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
* SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
//# sourceMappingURL=common.js.map
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Conversion, Converter, Converters } from '@fgv/ts-utils';
|
|
2
|
+
import * as JsonCompatible from './common';
|
|
3
|
+
import { JsonCompatibleType } from '../json';
|
|
4
|
+
/**
|
|
5
|
+
* A helper function to create a {@link Converter | Converter} which converts a supplied `unknown` value to a valid
|
|
6
|
+
* array of {@link JsonCompatibleType | JsonCompatibleType<T>}.
|
|
7
|
+
* @param converter - {@link JsonCompatible.Converter | JSON-compatible Converter<T, TC>}
|
|
8
|
+
* or {@link JsonCompatible.Validator | JSON-compatible Validator<T>} used for each item in the source array.
|
|
9
|
+
* @param onError - The error handling option to use for the conversion.
|
|
10
|
+
* @returns A {@link Converter | Converter} which returns `JsonCompatibleType<T>[]`.
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export declare function arrayOf<T, TC = unknown>(converter: JsonCompatible.Converter<T, TC> | JsonCompatible.Validator<T, TC>, onError?: Conversion.OnError): Converter<JsonCompatibleType<T>[], TC>;
|
|
14
|
+
/**
|
|
15
|
+
* A helper function to create a {@link Converter | Converter} or which converts the `string`-keyed properties
|
|
16
|
+
* using a supplied {@link JsonCompatible.Converter | JSON-compatible Converter<T, TC>} or
|
|
17
|
+
* {@link JsonCompatible.Validator | JSON-compatible Validator<T>} to produce a
|
|
18
|
+
* `Record<TK, JsonCompatibleType<T>>`.
|
|
19
|
+
* @remarks
|
|
20
|
+
* If present, the supplied {@link Converters.KeyedConverterOptions | options} can provide a strongly-typed
|
|
21
|
+
* converter for keys and/or control the handling of elements that fail conversion.
|
|
22
|
+
* @param converter - {@link JsonCompatible.Converter | JSON-compatible Converter<T, TC>}
|
|
23
|
+
* or {@link JsonCompatible.Validator | JSON-compatible Validator<T>} used for each item in the source object.
|
|
24
|
+
* @param options - Optional {@link Converters.KeyedConverterOptions | KeyedConverterOptions<TK, TC>} which
|
|
25
|
+
* supplies a key converter and/or error-handling options.
|
|
26
|
+
* @returns A {@link Converter | Converter} which returns `Record<TK, JsonCompatibleType<T>>`.
|
|
27
|
+
* @public
|
|
28
|
+
*/
|
|
29
|
+
export declare function recordOf<T, TC = unknown, TK extends string = string>(converter: JsonCompatible.Converter<T, TC> | JsonCompatible.Validator<T, TC>, options?: Converters.KeyedConverterOptions<TK, TC>): Converter<Record<TK, JsonCompatibleType<T>>, TC>;
|
|
30
|
+
/**
|
|
31
|
+
* A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
32
|
+
* @param properties - The properties to convert.
|
|
33
|
+
* @param options - The options to use for the conversion.
|
|
34
|
+
* @returns A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
35
|
+
* @public
|
|
36
|
+
*/
|
|
37
|
+
export declare function object<T, TC = unknown>(properties: Conversion.FieldConverters<JsonCompatibleType<T>, TC>, options?: Conversion.ObjectConverterOptions<JsonCompatibleType<T>>): JsonCompatible.ObjectConverter<T, TC>;
|
|
38
|
+
/**
|
|
39
|
+
* A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
40
|
+
* @param properties - The properties to convert.
|
|
41
|
+
* @param options - The options to use for the conversion.
|
|
42
|
+
* @returns A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export declare function strictObject<T, TC = unknown>(properties: Conversion.FieldConverters<JsonCompatibleType<T>, TC>, options?: Converters.StrictObjectConverterOptions<JsonCompatibleType<T>>): JsonCompatible.ObjectConverter<T, TC>;
|
|
46
|
+
/**
|
|
47
|
+
* A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
48
|
+
* @param discriminatorProp - The name of the property used to discriminate types.
|
|
49
|
+
* @param converters - The converters to use for the conversion.
|
|
50
|
+
* @returns A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
51
|
+
* @public
|
|
52
|
+
*/
|
|
53
|
+
export declare function discriminatedObject<T, TD extends string = string, TC = unknown>(discriminatorProp: string, converters: Converters.DiscriminatedObjectConverters<JsonCompatibleType<T>, TD, TC>): JsonCompatible.Converter<T, TC>;
|
|
54
|
+
//# sourceMappingURL=converters.d.ts.map
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2025 Erik Fortune
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
* copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
* SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.arrayOf = arrayOf;
|
|
25
|
+
exports.recordOf = recordOf;
|
|
26
|
+
exports.object = object;
|
|
27
|
+
exports.strictObject = strictObject;
|
|
28
|
+
exports.discriminatedObject = discriminatedObject;
|
|
29
|
+
const ts_utils_1 = require("@fgv/ts-utils");
|
|
30
|
+
/**
|
|
31
|
+
* A helper function to create a {@link Converter | Converter} which converts a supplied `unknown` value to a valid
|
|
32
|
+
* array of {@link JsonCompatibleType | JsonCompatibleType<T>}.
|
|
33
|
+
* @param converter - {@link JsonCompatible.Converter | JSON-compatible Converter<T, TC>}
|
|
34
|
+
* or {@link JsonCompatible.Validator | JSON-compatible Validator<T>} used for each item in the source array.
|
|
35
|
+
* @param onError - The error handling option to use for the conversion.
|
|
36
|
+
* @returns A {@link Converter | Converter} which returns `JsonCompatibleType<T>[]`.
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
function arrayOf(converter, onError = 'failOnError') {
|
|
40
|
+
return ts_utils_1.Converters.arrayOf(converter, onError);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* A helper function to create a {@link Converter | Converter} or which converts the `string`-keyed properties
|
|
44
|
+
* using a supplied {@link JsonCompatible.Converter | JSON-compatible Converter<T, TC>} or
|
|
45
|
+
* {@link JsonCompatible.Validator | JSON-compatible Validator<T>} to produce a
|
|
46
|
+
* `Record<TK, JsonCompatibleType<T>>`.
|
|
47
|
+
* @remarks
|
|
48
|
+
* If present, the supplied {@link Converters.KeyedConverterOptions | options} can provide a strongly-typed
|
|
49
|
+
* converter for keys and/or control the handling of elements that fail conversion.
|
|
50
|
+
* @param converter - {@link JsonCompatible.Converter | JSON-compatible Converter<T, TC>}
|
|
51
|
+
* or {@link JsonCompatible.Validator | JSON-compatible Validator<T>} used for each item in the source object.
|
|
52
|
+
* @param options - Optional {@link Converters.KeyedConverterOptions | KeyedConverterOptions<TK, TC>} which
|
|
53
|
+
* supplies a key converter and/or error-handling options.
|
|
54
|
+
* @returns A {@link Converter | Converter} which returns `Record<TK, JsonCompatibleType<T>>`.
|
|
55
|
+
* @public
|
|
56
|
+
*/
|
|
57
|
+
function recordOf(converter, options) {
|
|
58
|
+
return ts_utils_1.Converters.recordOf(converter, options !== null && options !== void 0 ? options : { onError: 'fail' });
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
62
|
+
* @param properties - The properties to convert.
|
|
63
|
+
* @param options - The options to use for the conversion.
|
|
64
|
+
* @returns A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
65
|
+
* @public
|
|
66
|
+
*/
|
|
67
|
+
function object(properties, options) {
|
|
68
|
+
return new ts_utils_1.Conversion.ObjectConverter(properties, options);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
72
|
+
* @param properties - The properties to convert.
|
|
73
|
+
* @param options - The options to use for the conversion.
|
|
74
|
+
* @returns A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
75
|
+
* @public
|
|
76
|
+
*/
|
|
77
|
+
function strictObject(properties, options) {
|
|
78
|
+
const objectOptions = Object.assign(Object.assign({}, options), { strict: true });
|
|
79
|
+
return new ts_utils_1.Conversion.ObjectConverter(properties, objectOptions);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
83
|
+
* @param discriminatorProp - The name of the property used to discriminate types.
|
|
84
|
+
* @param converters - The converters to use for the conversion.
|
|
85
|
+
* @returns A converter which converts a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
86
|
+
* @public
|
|
87
|
+
*/
|
|
88
|
+
function discriminatedObject(discriminatorProp, converters) {
|
|
89
|
+
return ts_utils_1.Converters.discriminatedObject(discriminatorProp, converters);
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=converters.js.map
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2025 Erik Fortune
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
* copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
* SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
24
|
+
if (k2 === undefined) k2 = k;
|
|
25
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
26
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
27
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
28
|
+
}
|
|
29
|
+
Object.defineProperty(o, k2, desc);
|
|
30
|
+
}) : (function(o, m, k, k2) {
|
|
31
|
+
if (k2 === undefined) k2 = k;
|
|
32
|
+
o[k2] = m[k];
|
|
33
|
+
}));
|
|
34
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
35
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
36
|
+
}) : function(o, v) {
|
|
37
|
+
o["default"] = v;
|
|
38
|
+
});
|
|
39
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
40
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
41
|
+
};
|
|
42
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
43
|
+
var ownKeys = function(o) {
|
|
44
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
45
|
+
var ar = [];
|
|
46
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
47
|
+
return ar;
|
|
48
|
+
};
|
|
49
|
+
return ownKeys(o);
|
|
50
|
+
};
|
|
51
|
+
return function (mod) {
|
|
52
|
+
if (mod && mod.__esModule) return mod;
|
|
53
|
+
var result = {};
|
|
54
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
55
|
+
__setModuleDefault(result, mod);
|
|
56
|
+
return result;
|
|
57
|
+
};
|
|
58
|
+
})();
|
|
59
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
60
|
+
exports.Validators = exports.Converters = void 0;
|
|
61
|
+
__exportStar(require("./common"), exports);
|
|
62
|
+
const Converters = __importStar(require("./converters"));
|
|
63
|
+
exports.Converters = Converters;
|
|
64
|
+
const Validators = __importStar(require("./validators"));
|
|
65
|
+
exports.Validators = Validators;
|
|
66
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { JsonCompatibleType } from '../json';
|
|
2
|
+
import * as JsonCompatible from './common';
|
|
3
|
+
import { Validation, Validators } from '@fgv/ts-utils';
|
|
4
|
+
/**
|
|
5
|
+
* A helper function to create a {@link JsonCompatible.ArrayValidator | JSON-compatible ArrayValidator<T, TC>} which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
6
|
+
* @param validateElement - The element to validate.
|
|
7
|
+
* @param params - The parameters to use for the validation.
|
|
8
|
+
* @returns A {@link JsonCompatible.ArrayValidator | JSON-compatible ArrayValidator<T, TC>} which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
export declare function arrayOf<T, TC = unknown>(validateElement: JsonCompatible.Validator<T, TC>, params?: Omit<Validation.Classes.ArrayValidatorConstructorParams<JsonCompatibleType<T>, TC>, 'validateElement'>): Validation.Classes.ArrayValidator<JsonCompatibleType<T>, TC>;
|
|
12
|
+
/**
|
|
13
|
+
* A helper function to create a {@link JsonCompatible.RecordValidator | JSON-compatible RecordValidator<T, TC>} which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
14
|
+
* @param validateElement - The element to validate.
|
|
15
|
+
* @param options - The options to use for the validation.
|
|
16
|
+
* @returns A {@link JsonCompatible.RecordValidator | JSON-compatible RecordValidator<T, TC>} which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
17
|
+
* @public
|
|
18
|
+
*/
|
|
19
|
+
export declare function recordOf<T, TC = unknown, TK extends string = string>(validateElement: JsonCompatible.Validator<T, TC>, options?: Validators.IRecordOfValidatorOptions<TK, TC>): Validation.Validator<Record<TK, JsonCompatibleType<T>>, TC>;
|
|
20
|
+
/**
|
|
21
|
+
* A helper function to create a {@link JsonCompatible.ObjectValidator | JSON-compatible ObjectValidator<T, TC>} which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
22
|
+
* @param properties - The properties to validate.
|
|
23
|
+
* @param params - The parameters to use for the validation.
|
|
24
|
+
* @returns A {@link JsonCompatible.ObjectValidator | JSON-compatible ObjectValidator<T, TC>} which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
export declare function object<T, TC = unknown>(properties: Validation.Classes.FieldValidators<JsonCompatibleType<T>, TC>, params?: Omit<Validation.Classes.ObjectValidatorConstructorParams<JsonCompatibleType<T>, TC>, 'fields'>): JsonCompatible.ObjectValidator<T, TC>;
|
|
28
|
+
//# sourceMappingURL=validators.d.ts.map
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2025 Erik Fortune
|
|
4
|
+
*
|
|
5
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
* in the Software without restriction, including without limitation the rights
|
|
8
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
* furnished to do so, subject to the following conditions:
|
|
11
|
+
*
|
|
12
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
* copies or substantial portions of the Software.
|
|
14
|
+
*
|
|
15
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
* SOFTWARE.
|
|
22
|
+
*/
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.arrayOf = arrayOf;
|
|
25
|
+
exports.recordOf = recordOf;
|
|
26
|
+
exports.object = object;
|
|
27
|
+
const ts_utils_1 = require("@fgv/ts-utils");
|
|
28
|
+
/**
|
|
29
|
+
* A helper function to create a {@link JsonCompatible.ArrayValidator | JSON-compatible ArrayValidator<T, TC>} which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
30
|
+
* @param validateElement - The element to validate.
|
|
31
|
+
* @param params - The parameters to use for the validation.
|
|
32
|
+
* @returns A {@link JsonCompatible.ArrayValidator | JSON-compatible ArrayValidator<T, TC>} which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
function arrayOf(validateElement, params) {
|
|
36
|
+
return ts_utils_1.Validators.arrayOf(validateElement, params !== null && params !== void 0 ? params : {});
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A helper function to create a {@link JsonCompatible.RecordValidator | JSON-compatible RecordValidator<T, TC>} which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
40
|
+
* @param validateElement - The element to validate.
|
|
41
|
+
* @param options - The options to use for the validation.
|
|
42
|
+
* @returns A {@link JsonCompatible.RecordValidator | JSON-compatible RecordValidator<T, TC>} which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
function recordOf(validateElement, options) {
|
|
46
|
+
return ts_utils_1.Validators.recordOf(validateElement, options !== null && options !== void 0 ? options : {});
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* A helper function to create a {@link JsonCompatible.ObjectValidator | JSON-compatible ObjectValidator<T, TC>} which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
50
|
+
* @param properties - The properties to validate.
|
|
51
|
+
* @param params - The parameters to use for the validation.
|
|
52
|
+
* @returns A {@link JsonCompatible.ObjectValidator | JSON-compatible ObjectValidator<T, TC>} which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
function object(properties, params) {
|
|
56
|
+
return ts_utils_1.Validators.object(properties, params !== null && params !== void 0 ? params : {});
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=validators.js.map
|
|
@@ -46,7 +46,7 @@ class JsonTreeHelper {
|
|
|
46
46
|
*/
|
|
47
47
|
readJsonFromTree(fileTree, filePath) {
|
|
48
48
|
return fileTree.getFile(filePath).onSuccess((file) => {
|
|
49
|
-
// Now getContents() returns
|
|
49
|
+
// Now getContents() returns JsonCompatibleType<unknown> which is assignable to JsonValue!
|
|
50
50
|
return file.getContents();
|
|
51
51
|
});
|
|
52
52
|
}
|
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-3",
|
|
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",
|
|
@@ -31,33 +31,33 @@
|
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/jest": "^29.5.14",
|
|
33
33
|
"@types/node": "^20.14.9",
|
|
34
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
35
|
-
"@typescript-eslint/parser": "^8.
|
|
36
|
-
"eslint": "^9.
|
|
34
|
+
"@typescript-eslint/eslint-plugin": "^8.46.2",
|
|
35
|
+
"@typescript-eslint/parser": "^8.46.2",
|
|
36
|
+
"eslint": "^9.39.0",
|
|
37
37
|
"eslint-plugin-import": "^2.32.0",
|
|
38
38
|
"eslint-plugin-node": "^11.1.0",
|
|
39
39
|
"eslint-plugin-promise": "^7.2.1",
|
|
40
40
|
"jest": "^29.7.0",
|
|
41
41
|
"jest-extended": "^4.0.2",
|
|
42
|
-
"rimraf": "^6.0
|
|
43
|
-
"ts-jest": "^29.4.
|
|
42
|
+
"rimraf": "^6.1.0",
|
|
43
|
+
"ts-jest": "^29.4.5",
|
|
44
44
|
"ts-node": "^10.9.2",
|
|
45
|
-
"typescript": "5.
|
|
46
|
-
"eslint-plugin-n": "^17.
|
|
47
|
-
"@rushstack/heft-node-rig": "2.
|
|
48
|
-
"@rushstack/heft": "
|
|
49
|
-
"@rushstack/heft-jest-plugin": "
|
|
45
|
+
"typescript": "5.9.3",
|
|
46
|
+
"eslint-plugin-n": "^17.23.1",
|
|
47
|
+
"@rushstack/heft-node-rig": "2.11.4",
|
|
48
|
+
"@rushstack/heft": "1.1.3",
|
|
49
|
+
"@rushstack/heft-jest-plugin": "1.1.3",
|
|
50
50
|
"@types/heft-jest": "1.0.6",
|
|
51
|
-
"@microsoft/api-documenter": "^7.
|
|
52
|
-
"@rushstack/eslint-patch": "
|
|
53
|
-
"@rushstack/eslint-config": "4.
|
|
51
|
+
"@microsoft/api-documenter": "^7.27.3",
|
|
52
|
+
"@rushstack/eslint-patch": "1.14.1",
|
|
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
|
|
57
|
-
"@fgv/ts-utils": "5.0.1-
|
|
56
|
+
"@fgv/ts-utils": "5.0.1-3",
|
|
57
|
+
"@fgv/ts-utils-jest": "5.0.1-3"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
|
-
"@fgv/ts-utils": "5.0.1-
|
|
60
|
+
"@fgv/ts-utils": "5.0.1-3"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
63
|
"luxon": "^3.7.2"
|