@fgv/ts-json-base 5.0.0-22 → 5.0.0-23
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/package.json +5 -5
- package/src/index.ts +0 -28
- package/src/packlets/converters/converters.ts +0 -174
- package/src/packlets/converters/index.ts +0 -23
- package/src/packlets/json/common.ts +0 -187
- package/src/packlets/json/index.ts +0 -23
- package/src/packlets/json-file/file.ts +0 -97
- package/src/packlets/json-file/index.ts +0 -25
- package/src/packlets/json-file/jsonFsHelper.ts +0 -262
- package/src/packlets/json-file/jsonLike.ts +0 -56
- package/src/packlets/validators/index.ts +0 -23
- package/src/packlets/validators/validators.ts +0 -155
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fgv/ts-json-base",
|
|
3
|
-
"version": "5.0.0-
|
|
3
|
+
"version": "5.0.0-23",
|
|
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",
|
|
@@ -39,12 +39,12 @@
|
|
|
39
39
|
"@rushstack/eslint-patch": "~1.12.0",
|
|
40
40
|
"@rushstack/eslint-config": "~4.4.0",
|
|
41
41
|
"eslint-plugin-tsdoc": "~0.4.0",
|
|
42
|
-
"@fgv/ts-utils": "5.0.0-
|
|
43
|
-
"@fgv/ts-utils-jest": "5.0.0-
|
|
44
|
-
"@fgv/ts-extras": "5.0.0-
|
|
42
|
+
"@fgv/ts-utils": "5.0.0-23",
|
|
43
|
+
"@fgv/ts-utils-jest": "5.0.0-23",
|
|
44
|
+
"@fgv/ts-extras": "5.0.0-23"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
|
-
"@fgv/ts-utils": "5.0.0-
|
|
47
|
+
"@fgv/ts-utils": "5.0.0-23"
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|
|
50
50
|
"build": "heft test --clean",
|
package/src/index.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2023 Erik Fortune
|
|
3
|
-
*
|
|
4
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
* in the Software without restriction, including without limitation the rights
|
|
7
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
* furnished to do so, subject to the following conditions:
|
|
10
|
-
*
|
|
11
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
-
* copies or substantial portions of the Software.
|
|
13
|
-
*
|
|
14
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
-
* SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import * as Converters from './packlets/converters';
|
|
24
|
-
import * as JsonFile from './packlets/json-file';
|
|
25
|
-
import * as Validators from './packlets/validators';
|
|
26
|
-
|
|
27
|
-
export * from './packlets/json';
|
|
28
|
-
export { Converters, JsonFile, Validators };
|
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2023 Erik Fortune
|
|
3
|
-
*
|
|
4
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
* in the Software without restriction, including without limitation the rights
|
|
7
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
* furnished to do so, subject to the following conditions:
|
|
10
|
-
*
|
|
11
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
-
* copies or substantial portions of the Software.
|
|
13
|
-
*
|
|
14
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
-
* SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import { Conversion, Converter, Result, fail, succeed } from '@fgv/ts-utils';
|
|
24
|
-
import { JsonArray, JsonObject, JsonPrimitive, JsonValue, isJsonArray, isJsonObject } from '../json';
|
|
25
|
-
|
|
26
|
-
/* eslint-disable no-use-before-define, @typescript-eslint/no-use-before-define */
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Conversion context for JSON converters.
|
|
30
|
-
* @public
|
|
31
|
-
*/
|
|
32
|
-
export interface IJsonConverterContext {
|
|
33
|
-
ignoreUndefinedProperties?: boolean;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* An converter which converts a supplied `unknown` value to a valid {@link JsonPrimitive | JsonPrimitive}.
|
|
38
|
-
* @public
|
|
39
|
-
*/
|
|
40
|
-
export const jsonPrimitive: Converter<JsonPrimitive, IJsonConverterContext> = new Conversion.BaseConverter(
|
|
41
|
-
(
|
|
42
|
-
from: unknown,
|
|
43
|
-
__self: Converter<JsonPrimitive, IJsonConverterContext>,
|
|
44
|
-
ctx?: IJsonConverterContext
|
|
45
|
-
): Result<JsonPrimitive> => {
|
|
46
|
-
if (from === null) {
|
|
47
|
-
return succeed(null);
|
|
48
|
-
}
|
|
49
|
-
switch (typeof from) {
|
|
50
|
-
case 'boolean':
|
|
51
|
-
case 'string':
|
|
52
|
-
return succeed(from);
|
|
53
|
-
case 'number':
|
|
54
|
-
if (!Number.isNaN(from)) {
|
|
55
|
-
return succeed(from);
|
|
56
|
-
}
|
|
57
|
-
break;
|
|
58
|
-
}
|
|
59
|
-
return fail(`"${String(from)}": not a valid JSON primitive.`);
|
|
60
|
-
}
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* An copying converter which converts a supplied `unknown` value into
|
|
65
|
-
* a valid {@link JsonObject | JsonObject}. Fails by default if any properties or array elements
|
|
66
|
-
* are `undefined` - this default behavior can be overridden by supplying an appropriate
|
|
67
|
-
* {@link Converters.IJsonConverterContext | context} at runtime.
|
|
68
|
-
*
|
|
69
|
-
* Guaranteed to return a new object.
|
|
70
|
-
* @public
|
|
71
|
-
*/
|
|
72
|
-
export const jsonObject: Converter<JsonObject, IJsonConverterContext> = new Conversion.BaseConverter(
|
|
73
|
-
(
|
|
74
|
-
from: unknown,
|
|
75
|
-
__self: Converter<JsonObject, IJsonConverterContext>,
|
|
76
|
-
ctx?: IJsonConverterContext
|
|
77
|
-
): Result<JsonObject> => {
|
|
78
|
-
if (!isJsonObject(from)) {
|
|
79
|
-
return fail('not a valid JSON object.');
|
|
80
|
-
}
|
|
81
|
-
const obj: JsonObject = {};
|
|
82
|
-
const errors: string[] = [];
|
|
83
|
-
for (const [name, value] of Object.entries(from)) {
|
|
84
|
-
if (value === undefined && ctx?.ignoreUndefinedProperties === true) {
|
|
85
|
-
// optionally ignore undefined values
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
jsonValue
|
|
89
|
-
.convert(value, ctx)
|
|
90
|
-
.onSuccess((v: JsonValue) => {
|
|
91
|
-
obj[name] = v;
|
|
92
|
-
return succeed(v);
|
|
93
|
-
})
|
|
94
|
-
.onFailure((m) => {
|
|
95
|
-
errors.push(`${name}: ${m}`);
|
|
96
|
-
return fail(m);
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
if (errors.length > 0) {
|
|
100
|
-
return fail(`not a valid JSON object:\n${errors.join('\n')}`);
|
|
101
|
-
}
|
|
102
|
-
return succeed(obj);
|
|
103
|
-
}
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* An copying converter which converts a supplied `unknown` value to
|
|
108
|
-
* a valid {@link JsonArray | JsonArray}. Fails by default if any properties or array elements
|
|
109
|
-
* are `undefined` - this default behavior can be overridden by supplying an appropriate
|
|
110
|
-
* {@link Converters.IJsonConverterContext | context} at runtime.
|
|
111
|
-
*
|
|
112
|
-
* Guaranteed to return a new array.
|
|
113
|
-
* @public
|
|
114
|
-
*/
|
|
115
|
-
export const jsonArray: Converter<JsonArray, IJsonConverterContext> = new Conversion.BaseConverter(
|
|
116
|
-
(
|
|
117
|
-
from: unknown,
|
|
118
|
-
__self: Converter<JsonArray, IJsonConverterContext>,
|
|
119
|
-
ctx?: IJsonConverterContext
|
|
120
|
-
): Result<JsonArray> => {
|
|
121
|
-
if (!isJsonArray(from)) {
|
|
122
|
-
return fail('not an array');
|
|
123
|
-
}
|
|
124
|
-
const arr: JsonValue[] = [];
|
|
125
|
-
const errors: string[] = [];
|
|
126
|
-
for (let i = 0; i < from.length; i++) {
|
|
127
|
-
const value = from[i];
|
|
128
|
-
if (value === undefined && ctx?.ignoreUndefinedProperties === true) {
|
|
129
|
-
// convert undefined to 'null' for parity with JSON.stringify
|
|
130
|
-
arr.push(null);
|
|
131
|
-
continue;
|
|
132
|
-
}
|
|
133
|
-
jsonValue
|
|
134
|
-
.convert(value, ctx)
|
|
135
|
-
.onSuccess((v) => {
|
|
136
|
-
arr.push(v);
|
|
137
|
-
return succeed(v);
|
|
138
|
-
})
|
|
139
|
-
.onFailure((m) => {
|
|
140
|
-
errors.push(`${i}: ${m}`);
|
|
141
|
-
return fail(m);
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
if (errors.length > 0) {
|
|
145
|
-
return fail(`array contains non-json elements:\n${errors.join('\n')}`);
|
|
146
|
-
}
|
|
147
|
-
return succeed(arr);
|
|
148
|
-
}
|
|
149
|
-
);
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* An copying converter which converts a supplied `unknown` value to a
|
|
153
|
-
* valid {@link JsonValue | JsonValue}. Fails by default if any properties or array elements
|
|
154
|
-
* are `undefined` - this default behavior can be overridden by supplying an appropriate
|
|
155
|
-
* {@link Converters.IJsonConverterContext | context} at runtime.
|
|
156
|
-
* @public
|
|
157
|
-
*/
|
|
158
|
-
export const jsonValue: Converter<JsonValue, IJsonConverterContext> = new Conversion.BaseConverter<
|
|
159
|
-
JsonValue,
|
|
160
|
-
IJsonConverterContext
|
|
161
|
-
>(
|
|
162
|
-
(
|
|
163
|
-
from: unknown,
|
|
164
|
-
__self: Converter<JsonValue, IJsonConverterContext>,
|
|
165
|
-
ctx?: IJsonConverterContext
|
|
166
|
-
): Result<JsonValue> => {
|
|
167
|
-
if (isJsonArray(from)) {
|
|
168
|
-
return jsonArray.convert(from, ctx);
|
|
169
|
-
} else if (isJsonObject(from)) {
|
|
170
|
-
return jsonObject.convert(from, ctx);
|
|
171
|
-
}
|
|
172
|
-
return jsonPrimitive.convert(from, ctx);
|
|
173
|
-
}
|
|
174
|
-
);
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2020 Erik Fortune
|
|
3
|
-
*
|
|
4
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
* in the Software without restriction, including without limitation the rights
|
|
7
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
* furnished to do so, subject to the following conditions:
|
|
10
|
-
*
|
|
11
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
-
* copies or substantial portions of the Software.
|
|
13
|
-
*
|
|
14
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
-
* SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
export * from './converters';
|
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2020 Erik Fortune
|
|
3
|
-
*
|
|
4
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
* in the Software without restriction, including without limitation the rights
|
|
7
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
* furnished to do so, subject to the following conditions:
|
|
10
|
-
*
|
|
11
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
-
* copies or substantial portions of the Software.
|
|
13
|
-
*
|
|
14
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
-
* SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import { Result, captureResult, fail, succeed } from '@fgv/ts-utils';
|
|
24
|
-
|
|
25
|
-
/* eslint-disable no-use-before-define */
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Primitive (terminal) values allowed in by JSON.
|
|
29
|
-
* @public
|
|
30
|
-
*/
|
|
31
|
-
// eslint-disable-next-line @rushstack/no-new-null
|
|
32
|
-
export type JsonPrimitive = boolean | number | string | null;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* A {@link JsonObject | JsonObject} is a string-keyed object
|
|
36
|
-
* containing only valid {@link JsonValue | JSON values}.
|
|
37
|
-
* @public
|
|
38
|
-
*/
|
|
39
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
40
|
-
export interface JsonObject {
|
|
41
|
-
[key: string]: JsonValue;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* A {@link JsonValue | JsonValue} is one of: a {@link JsonPrimitive | JsonPrimitive},
|
|
46
|
-
* a {@link JsonObject | JsonObject} or an {@link JsonArray | JsonArray}.
|
|
47
|
-
* @public
|
|
48
|
-
*/
|
|
49
|
-
export type JsonValue = JsonPrimitive | JsonObject | JsonArray;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* A {@link JsonArray | JsonArray} is an array containing only valid {@link JsonValue | JsonValues}.
|
|
53
|
-
* @public
|
|
54
|
-
*/
|
|
55
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/naming-convention
|
|
56
|
-
export interface JsonArray extends Array<JsonValue> {}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Classes of {@link JsonValue | JsonValue}.
|
|
60
|
-
* @public
|
|
61
|
-
*/
|
|
62
|
-
export type JsonValueType = 'primitive' | 'object' | 'array';
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Test if an `unknown` is a {@link JsonValue | JsonValue}.
|
|
66
|
-
* @param from - The `unknown` to be tested
|
|
67
|
-
* @returns `true` if the supplied parameter is a valid {@link JsonPrimitive | JsonPrimitive},
|
|
68
|
-
* `false` otherwise.
|
|
69
|
-
* @public
|
|
70
|
-
*/
|
|
71
|
-
export function isJsonPrimitive(from: unknown): from is JsonPrimitive {
|
|
72
|
-
return typeof from === 'boolean' || typeof from === 'number' || typeof from === 'string' || from === null;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Test if an `unknown` is potentially a {@link JsonObject | JsonObject}.
|
|
77
|
-
* @param from - The `unknown` to be tested.
|
|
78
|
-
* @returns `true` if the supplied parameter is a non-array, non-special object,
|
|
79
|
-
* `false` otherwise.
|
|
80
|
-
* @public
|
|
81
|
-
*/
|
|
82
|
-
export function isJsonObject(from: unknown): from is JsonObject {
|
|
83
|
-
return (
|
|
84
|
-
typeof from === 'object' &&
|
|
85
|
-
from !== null &&
|
|
86
|
-
!Array.isArray(from) &&
|
|
87
|
-
!(from instanceof RegExp) &&
|
|
88
|
-
!(from instanceof Date)
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Test if an `unknown` is potentially a {@link JsonArray | JsonArray}.
|
|
94
|
-
* @param from - The `unknown` to be tested.
|
|
95
|
-
* @returns `true` if the supplied parameter is an array object,
|
|
96
|
-
* `false` otherwise.
|
|
97
|
-
* @public
|
|
98
|
-
*/
|
|
99
|
-
export function isJsonArray(from: unknown): from is JsonArray {
|
|
100
|
-
return typeof from === 'object' && Array.isArray(from);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Identifies whether some `unknown` value is a {@link JsonPrimitive | primitive},
|
|
105
|
-
* {@link JsonObject | object} or {@link JsonArray | array}. Fails for any value
|
|
106
|
-
* that cannot be converted to JSON (e.g. a function) _but_ this is a shallow test -
|
|
107
|
-
* it does not test the properties of an object or elements in an array.
|
|
108
|
-
* @param from - The `unknown` value to be tested
|
|
109
|
-
* @public
|
|
110
|
-
*/
|
|
111
|
-
export function classifyJsonValue(from: unknown): Result<JsonValueType> {
|
|
112
|
-
if (isJsonPrimitive(from)) {
|
|
113
|
-
return succeed('primitive');
|
|
114
|
-
} else if (isJsonObject(from)) {
|
|
115
|
-
return succeed('object');
|
|
116
|
-
} else if (isJsonArray(from)) {
|
|
117
|
-
return succeed('array');
|
|
118
|
-
}
|
|
119
|
-
return fail(`Invalid JSON: ${from}`);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Picks a nested field from a supplied {@link JsonObject | JsonObject}.
|
|
124
|
-
* @param src - The {@link JsonObject | object} from which the field is to be picked.
|
|
125
|
-
* @param path - Dot-separated path of the member to be picked.
|
|
126
|
-
* @returns `Success` with the property if the path is valid, `Failure`
|
|
127
|
-
* with an error message otherwise.
|
|
128
|
-
* @public
|
|
129
|
-
*/
|
|
130
|
-
export function pickJsonValue(src: JsonObject, path: string): Result<JsonValue> {
|
|
131
|
-
let result: JsonValue = src;
|
|
132
|
-
for (const part of path.split('.')) {
|
|
133
|
-
if (result && isJsonObject(result)) {
|
|
134
|
-
result = result[part];
|
|
135
|
-
if (result === undefined) {
|
|
136
|
-
return fail(`${path}: child '${part}' does not exist`);
|
|
137
|
-
}
|
|
138
|
-
} else {
|
|
139
|
-
return fail(`${path}: child '${part}' does not exist`);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
return succeed(result);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Picks a nested {@link JsonObject | JsonObject} from a supplied
|
|
147
|
-
* {@link JsonObject | JsonObject}.
|
|
148
|
-
* @param src - The {@link JsonObject | object} from which the field is
|
|
149
|
-
* to be picked.
|
|
150
|
-
* @param path - Dot-separated path of the member to be picked.
|
|
151
|
-
* @returns `Success` with the property if the path is valid and the value
|
|
152
|
-
* is an object. Returns `Failure` with details if an error occurs.
|
|
153
|
-
* @public
|
|
154
|
-
*/
|
|
155
|
-
export function pickJsonObject(src: JsonObject, path: string): Result<JsonObject> {
|
|
156
|
-
return pickJsonValue(src, path).onSuccess((v) => {
|
|
157
|
-
if (!isJsonObject(v)) {
|
|
158
|
-
return fail(`${path}: not an object`);
|
|
159
|
-
}
|
|
160
|
-
return succeed(v);
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* "Sanitizes" an `unknown` by stringifying and then parsing it. Guarantees a
|
|
166
|
-
* valid {@link JsonValue | JsonValue} but is not idempotent and gives no guarantees
|
|
167
|
-
* about fidelity. Fails if the supplied value cannot be stringified as Json.
|
|
168
|
-
* @param from - The `unknown` to be sanitized.
|
|
169
|
-
* @returns `Success` with a {@link JsonValue | JsonValue} if conversion succeeds,
|
|
170
|
-
* `Failure` with details if an error occurs.
|
|
171
|
-
* @public
|
|
172
|
-
*/
|
|
173
|
-
export function sanitizeJson(from: unknown): Result<JsonValue> {
|
|
174
|
-
return captureResult(() => JSON.parse(JSON.stringify(from)));
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Sanitizes some value using JSON stringification and parsing, returning an
|
|
179
|
-
* returning a matching strongly-typed value.
|
|
180
|
-
* @param from - The value to be sanitized.
|
|
181
|
-
* @returns `Success` with a {@link JsonObject | JsonObject} if conversion succeeds,
|
|
182
|
-
* `Failure` with details if an error occurs.
|
|
183
|
-
* @public
|
|
184
|
-
*/
|
|
185
|
-
export function sanitizeJsonObject<T>(from: T): Result<T> {
|
|
186
|
-
return captureResult(() => JSON.parse(JSON.stringify(from)));
|
|
187
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2020 Erik Fortune
|
|
3
|
-
*
|
|
4
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
* in the Software without restriction, including without limitation the rights
|
|
7
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
* furnished to do so, subject to the following conditions:
|
|
10
|
-
*
|
|
11
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
-
* copies or substantial portions of the Software.
|
|
13
|
-
*
|
|
14
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
-
* SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
export * from './common';
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2020 Erik Fortune
|
|
3
|
-
*
|
|
4
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
* in the Software without restriction, including without limitation the rights
|
|
7
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
* furnished to do so, subject to the following conditions:
|
|
10
|
-
*
|
|
11
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
-
* copies or substantial portions of the Software.
|
|
13
|
-
*
|
|
14
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
-
* SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import { Converter, Result } from '@fgv/ts-utils';
|
|
24
|
-
import { JsonValue } from '../json';
|
|
25
|
-
import {
|
|
26
|
-
DefaultJsonFsHelper,
|
|
27
|
-
IJsonFsDirectoryOptions,
|
|
28
|
-
IJsonFsDirectoryToMapOptions,
|
|
29
|
-
IReadDirectoryItem,
|
|
30
|
-
JsonFsHelper
|
|
31
|
-
} from './jsonFsHelper';
|
|
32
|
-
import { DefaultJsonLike } from './jsonLike';
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* {@inheritdoc JsonFile.JsonFsHelper.readJsonFileSync}
|
|
36
|
-
* @public
|
|
37
|
-
*/
|
|
38
|
-
export function readJsonFileSync(srcPath: string): Result<JsonValue> {
|
|
39
|
-
return DefaultJsonFsHelper.readJsonFileSync(srcPath);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Read a JSON file and apply a supplied converter.
|
|
44
|
-
* @param srcPath - Path of the file to read.
|
|
45
|
-
* @param converter - `Converter` used to convert the file contents
|
|
46
|
-
* @returns `Success` with a result of type `<T>`, or `Failure`* with a message if an error occurs.
|
|
47
|
-
* @public
|
|
48
|
-
*/
|
|
49
|
-
export function convertJsonFileSync<T, TC = unknown>(
|
|
50
|
-
srcPath: string,
|
|
51
|
-
converter: Converter<T, TC>
|
|
52
|
-
): Result<T> {
|
|
53
|
-
return DefaultJsonFsHelper.convertJsonFileSync(srcPath, converter);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Reads all JSON files from a directory and apply a supplied converter.
|
|
58
|
-
* @param srcPath - The path of the folder to be read.
|
|
59
|
-
* @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control
|
|
60
|
-
* conversion and filtering
|
|
61
|
-
* @public
|
|
62
|
-
*/
|
|
63
|
-
export function convertJsonDirectorySync<T, TC = unknown>(
|
|
64
|
-
srcPath: string,
|
|
65
|
-
options: IJsonFsDirectoryOptions<T, TC>
|
|
66
|
-
): Result<IReadDirectoryItem<T>[]> {
|
|
67
|
-
return DefaultJsonFsHelper.convertJsonDirectorySync(srcPath, options);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Reads and converts all JSON files from a directory, returning a
|
|
72
|
-
* `Map<string, T>` indexed by file base name (i.e. minus the extension)
|
|
73
|
-
* with an optional name transformation applied if present.
|
|
74
|
-
* @param srcPath - The path of the folder to be read.
|
|
75
|
-
* @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,
|
|
76
|
-
* filtering and naming.
|
|
77
|
-
* @public
|
|
78
|
-
*/
|
|
79
|
-
export function convertJsonDirectoryToMapSync<T, TC = unknown>(
|
|
80
|
-
srcPath: string,
|
|
81
|
-
options: IJsonFsDirectoryToMapOptions<T, TC>
|
|
82
|
-
): Result<Map<string, T>> {
|
|
83
|
-
return DefaultJsonFsHelper.convertJsonDirectoryToMapSync(srcPath, options);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const CompatJsonFsHelper: JsonFsHelper = new JsonFsHelper({
|
|
87
|
-
json: DefaultJsonLike,
|
|
88
|
-
allowUndefinedWrite: true // for compatibility
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* {@inheritDoc JsonFile.JsonFsHelper.writeJsonFileSync}
|
|
93
|
-
* @public
|
|
94
|
-
*/
|
|
95
|
-
export function writeJsonFileSync(srcPath: string, value: JsonValue): Result<boolean> {
|
|
96
|
-
return CompatJsonFsHelper.writeJsonFileSync(srcPath, value);
|
|
97
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2020 Erik Fortune
|
|
3
|
-
*
|
|
4
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
* in the Software without restriction, including without limitation the rights
|
|
7
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
* furnished to do so, subject to the following conditions:
|
|
10
|
-
*
|
|
11
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
-
* copies or substantial portions of the Software.
|
|
13
|
-
*
|
|
14
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
-
* SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
export * from './file';
|
|
24
|
-
export * from './jsonFsHelper';
|
|
25
|
-
export * from './jsonLike';
|
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2020 Erik Fortune
|
|
3
|
-
*
|
|
4
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
* in the Software without restriction, including without limitation the rights
|
|
7
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
* furnished to do so, subject to the following conditions:
|
|
10
|
-
*
|
|
11
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
-
* copies or substantial portions of the Software.
|
|
13
|
-
*
|
|
14
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
-
* SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import { Converter, Result, Validator, captureResult, fail, mapResults, succeed } from '@fgv/ts-utils';
|
|
24
|
-
import * as fs from 'fs';
|
|
25
|
-
import * as path from 'path';
|
|
26
|
-
|
|
27
|
-
import { JsonValue } from '../json';
|
|
28
|
-
import { DefaultJsonLike, IJsonLike } from './jsonLike';
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Options for directory conversion.
|
|
32
|
-
* TODO: add filtering, allowed and excluded.
|
|
33
|
-
* @public
|
|
34
|
-
*/
|
|
35
|
-
export interface IJsonFsDirectoryOptions<T, TC = unknown> {
|
|
36
|
-
/**
|
|
37
|
-
* The converter used to convert incoming JSON objects.
|
|
38
|
-
*/
|
|
39
|
-
converter: Converter<T, TC> | Validator<T, TC>;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Filter applied to items in the directory
|
|
43
|
-
*/
|
|
44
|
-
files?: RegExp[];
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Return value for one item in a directory conversion.
|
|
49
|
-
* @public
|
|
50
|
-
*/
|
|
51
|
-
export interface IReadDirectoryItem<T> {
|
|
52
|
-
/**
|
|
53
|
-
* Relative name of the file that was processed
|
|
54
|
-
*/
|
|
55
|
-
filename: string;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* The payload of the file.
|
|
59
|
-
*/
|
|
60
|
-
item: T;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Function to transform the name of a some entity, given an original name
|
|
65
|
-
* and the contents of the entity.
|
|
66
|
-
* @public
|
|
67
|
-
*/
|
|
68
|
-
export type ItemNameTransformFunction<T> = (name: string, item: T) => Result<string>;
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Options controlling conversion of a directory to a `Map`.
|
|
72
|
-
* @public
|
|
73
|
-
*/
|
|
74
|
-
export interface IJsonFsDirectoryToMapOptions<T, TC = unknown> extends IJsonFsDirectoryOptions<T, TC> {
|
|
75
|
-
transformName?: ItemNameTransformFunction<T>;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Function to transform the name of some entity, given only a previous name. This
|
|
80
|
-
* default implementation always returns `Success` with the value that was passed in.
|
|
81
|
-
* @param n - The name to be transformed.
|
|
82
|
-
* @returns - `Success` with the string that was passed in.
|
|
83
|
-
* @public
|
|
84
|
-
*/
|
|
85
|
-
const defaultNameTransformer = (n: string): Result<string> => succeed(n);
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Configuration for {@link JsonFile.JsonFsHelper | JsonFsHelper}.
|
|
89
|
-
* @public
|
|
90
|
-
*/
|
|
91
|
-
export interface IJsonFsHelperConfig {
|
|
92
|
-
json: IJsonLike;
|
|
93
|
-
allowUndefinedWrite: boolean;
|
|
94
|
-
defaultFiles: RegExp[];
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Initialization options for {@link JsonFile.JsonFsHelper | JsonFsHelper}.
|
|
99
|
-
* @public
|
|
100
|
-
*/
|
|
101
|
-
export type JsonFsHelperInitOptions = Partial<IJsonFsHelperConfig>;
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Default configuration for {@link JsonFile.JsonFsHelper | JsonFsHelper}.
|
|
105
|
-
* @public
|
|
106
|
-
*/
|
|
107
|
-
export const DefaultJsonFsHelperConfig: IJsonFsHelperConfig = {
|
|
108
|
-
json: DefaultJsonLike,
|
|
109
|
-
allowUndefinedWrite: false,
|
|
110
|
-
defaultFiles: [/.*.json/]
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Helper class to simplify common filesystem operations involving JSON (or JSON-like)
|
|
115
|
-
* files.
|
|
116
|
-
* @public
|
|
117
|
-
*/
|
|
118
|
-
export class JsonFsHelper {
|
|
119
|
-
/**
|
|
120
|
-
* Configuration for this {@link JsonFile.JsonFsHelper | JsonFsHelper}.
|
|
121
|
-
*/
|
|
122
|
-
public readonly config: IJsonFsHelperConfig;
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Construct a new {@link JsonFile.JsonFsHelper | JsonFsHelper}.
|
|
126
|
-
* @param json - Optional {@link JsonFile.IJsonLike | IJsonLike} used to process strings
|
|
127
|
-
* and JSON values.
|
|
128
|
-
*/
|
|
129
|
-
public constructor(init?: JsonFsHelperInitOptions) {
|
|
130
|
-
this.config = {
|
|
131
|
-
...DefaultJsonFsHelperConfig,
|
|
132
|
-
...(init ?? {})
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Read type-safe JSON from a file.
|
|
138
|
-
* @param srcPath - Path of the file to read
|
|
139
|
-
* @returns `Success` with a {@link JsonValue | JsonValue} or `Failure`
|
|
140
|
-
* with a message if an error occurs.
|
|
141
|
-
*/
|
|
142
|
-
public readJsonFileSync(srcPath: string): Result<JsonValue> {
|
|
143
|
-
return captureResult(() => {
|
|
144
|
-
const fullPath = path.resolve(srcPath);
|
|
145
|
-
const body = fs.readFileSync(fullPath, 'utf8').toString();
|
|
146
|
-
return this.config.json.parse(body) as JsonValue;
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Read a JSON file and apply a supplied converter or validator.
|
|
152
|
-
* @param srcPath - Path of the file to read.
|
|
153
|
-
* @param cv - Converter or validator used to process the file.
|
|
154
|
-
* @returns `Success` with a result of type `<T>`, or `Failure`
|
|
155
|
-
* with a message if an error occurs.
|
|
156
|
-
*/
|
|
157
|
-
public convertJsonFileSync<T, TC = unknown>(
|
|
158
|
-
srcPath: string,
|
|
159
|
-
cv: Converter<T, TC> | Validator<T, TC>,
|
|
160
|
-
context?: TC
|
|
161
|
-
): Result<T> {
|
|
162
|
-
return this.readJsonFileSync(srcPath).onSuccess((json) => {
|
|
163
|
-
return cv.convert(json, context);
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Reads all JSON files from a directory and apply a supplied converter or validator.
|
|
169
|
-
* @param srcPath - The path of the folder to be read.
|
|
170
|
-
* @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control
|
|
171
|
-
* conversion and filtering
|
|
172
|
-
*/
|
|
173
|
-
public convertJsonDirectorySync<T, TC = unknown>(
|
|
174
|
-
srcPath: string,
|
|
175
|
-
options: IJsonFsDirectoryOptions<T>,
|
|
176
|
-
context?: TC
|
|
177
|
-
): Result<IReadDirectoryItem<T>[]> {
|
|
178
|
-
return captureResult<IReadDirectoryItem<T>[]>(() => {
|
|
179
|
-
const fullPath = path.resolve(srcPath);
|
|
180
|
-
if (!fs.statSync(fullPath).isDirectory()) {
|
|
181
|
-
throw new Error(`${fullPath}: Not a directory`);
|
|
182
|
-
}
|
|
183
|
-
const files = fs.readdirSync(fullPath, { withFileTypes: true });
|
|
184
|
-
const results = files
|
|
185
|
-
.map((fi) => {
|
|
186
|
-
if (fi.isFile() && this._pathMatchesOptions(options, fi.name)) {
|
|
187
|
-
const filePath = path.resolve(fullPath, fi.name);
|
|
188
|
-
return this.convertJsonFileSync(filePath, options.converter, context)
|
|
189
|
-
.onSuccess((payload) => {
|
|
190
|
-
return succeed({
|
|
191
|
-
filename: fi.name,
|
|
192
|
-
item: payload
|
|
193
|
-
});
|
|
194
|
-
})
|
|
195
|
-
.onFailure((message) => {
|
|
196
|
-
return fail(`${fi.name}: ${message}`);
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
return undefined;
|
|
200
|
-
})
|
|
201
|
-
.filter((r): r is Result<IReadDirectoryItem<T>> => r !== undefined);
|
|
202
|
-
return mapResults(results).orThrow();
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Reads and converts or validates all JSON files from a directory, returning a
|
|
208
|
-
* `Map<string, T>` indexed by file base name (i.e. minus the extension)
|
|
209
|
-
* with an optional name transformation applied if present.
|
|
210
|
-
* @param srcPath - The path of the folder to be read.
|
|
211
|
-
* @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,
|
|
212
|
-
* filtering and naming.
|
|
213
|
-
*/
|
|
214
|
-
public convertJsonDirectoryToMapSync<T, TC = unknown>(
|
|
215
|
-
srcPath: string,
|
|
216
|
-
options: IJsonFsDirectoryToMapOptions<T, TC>,
|
|
217
|
-
context?: unknown
|
|
218
|
-
): Result<Map<string, T>> {
|
|
219
|
-
return this.convertJsonDirectorySync(srcPath, options, context).onSuccess((items) => {
|
|
220
|
-
const transformName = options.transformName ?? defaultNameTransformer;
|
|
221
|
-
return mapResults(
|
|
222
|
-
items.map((item) => {
|
|
223
|
-
const basename = path.basename(item.filename, '.json');
|
|
224
|
-
return transformName(basename, item.item).onSuccess((name) => {
|
|
225
|
-
return succeed<[string, T]>([name, item.item]);
|
|
226
|
-
});
|
|
227
|
-
})
|
|
228
|
-
).onSuccess((items) => {
|
|
229
|
-
return succeed(new Map<string, T>(items));
|
|
230
|
-
});
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* Write type-safe JSON to a file.
|
|
236
|
-
* @param srcPath - Path of the file to write.
|
|
237
|
-
* @param value - The {@link JsonValue | JsonValue} to be written.
|
|
238
|
-
*/
|
|
239
|
-
public writeJsonFileSync(srcPath: string, value: JsonValue): Result<boolean> {
|
|
240
|
-
return captureResult(() => {
|
|
241
|
-
const fullPath = path.resolve(srcPath);
|
|
242
|
-
const stringified = this.config.json.stringify(value, undefined, 2);
|
|
243
|
-
/* c8 ignore next 3 */
|
|
244
|
-
if (stringified === undefined && this.config.allowUndefinedWrite !== true) {
|
|
245
|
-
throw new Error(`Could not stringify ${value}`);
|
|
246
|
-
}
|
|
247
|
-
fs.writeFileSync(fullPath, stringified!);
|
|
248
|
-
return true;
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
protected _pathMatchesOptions<T, TC>(options: IJsonFsDirectoryOptions<T, TC>, path: string): boolean {
|
|
253
|
-
/* c8 ignore next 1 */
|
|
254
|
-
const match = options.files ?? this.config.defaultFiles;
|
|
255
|
-
return match.some((m) => m.exec(path));
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* @public
|
|
261
|
-
*/
|
|
262
|
-
export const DefaultJsonFsHelper: JsonFsHelper = new JsonFsHelper();
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2024 Erik Fortune
|
|
3
|
-
*
|
|
4
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
* in the Software without restriction, including without limitation the rights
|
|
7
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
* furnished to do so, subject to the following conditions:
|
|
10
|
-
*
|
|
11
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
-
* copies or substantial portions of the Software.
|
|
13
|
-
*
|
|
14
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
-
* SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import { JsonValue } from '../json';
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* @public
|
|
27
|
-
*/
|
|
28
|
-
export type JsonReviver = (key: string, value: JsonValue) => JsonValue;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* @public
|
|
32
|
-
*/
|
|
33
|
-
export type JsonReplacerFunction = (key: string, value: JsonValue) => JsonValue;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* @public
|
|
37
|
-
*/
|
|
38
|
-
export type JsonReplacerArray = (string | number)[];
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* @public
|
|
42
|
-
*/
|
|
43
|
-
export type JsonReplacer = JsonReplacerFunction | JsonReplacerArray;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* @public
|
|
47
|
-
*/
|
|
48
|
-
export interface IJsonLike {
|
|
49
|
-
parse(text: string, reviver?: JsonReviver, options?: unknown): JsonValue | undefined;
|
|
50
|
-
stringify(value: JsonValue, replacer?: JsonReplacer, space?: string | number): string | undefined;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* @public
|
|
55
|
-
*/
|
|
56
|
-
export const DefaultJsonLike: IJsonLike = JSON;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2020 Erik Fortune
|
|
3
|
-
*
|
|
4
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
* in the Software without restriction, including without limitation the rights
|
|
7
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
* furnished to do so, subject to the following conditions:
|
|
10
|
-
*
|
|
11
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
-
* copies or substantial portions of the Software.
|
|
13
|
-
*
|
|
14
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
-
* SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
export * from './validators';
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2023 Erik Fortune
|
|
3
|
-
*
|
|
4
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
* in the Software without restriction, including without limitation the rights
|
|
7
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
* furnished to do so, subject to the following conditions:
|
|
10
|
-
*
|
|
11
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
-
* copies or substantial portions of the Software.
|
|
13
|
-
*
|
|
14
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
-
* SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import { Failure, Validation, Validator, fail } from '@fgv/ts-utils';
|
|
24
|
-
import { JsonArray, JsonObject, JsonPrimitive, JsonValue, isJsonArray, isJsonObject } from '../json';
|
|
25
|
-
|
|
26
|
-
/* eslint-disable no-use-before-define, @typescript-eslint/no-use-before-define */
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Validation context for in-place JSON validators.
|
|
30
|
-
* @public
|
|
31
|
-
*/
|
|
32
|
-
export interface IJsonValidatorContext {
|
|
33
|
-
ignoreUndefinedProperties?: boolean;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* An in-place validator which validates that a supplied `unknown` value is
|
|
38
|
-
* a valid {@link JsonPrimitive | JsonPrimitive}.
|
|
39
|
-
* @public
|
|
40
|
-
*/
|
|
41
|
-
export const jsonPrimitive: Validator<JsonPrimitive, IJsonValidatorContext> =
|
|
42
|
-
new Validation.Base.GenericValidator({
|
|
43
|
-
validator: (
|
|
44
|
-
from: unknown,
|
|
45
|
-
ctx?: IJsonValidatorContext,
|
|
46
|
-
self?: Validator<JsonPrimitive, IJsonValidatorContext>
|
|
47
|
-
): boolean | Failure<JsonPrimitive> => {
|
|
48
|
-
if (from === null) {
|
|
49
|
-
return true;
|
|
50
|
-
}
|
|
51
|
-
switch (typeof from) {
|
|
52
|
-
case 'boolean':
|
|
53
|
-
case 'string':
|
|
54
|
-
return true;
|
|
55
|
-
case 'number':
|
|
56
|
-
if (!Number.isNaN(from)) {
|
|
57
|
-
return true;
|
|
58
|
-
}
|
|
59
|
-
break;
|
|
60
|
-
}
|
|
61
|
-
if (from === undefined && ctx?.ignoreUndefinedProperties === true) {
|
|
62
|
-
return true;
|
|
63
|
-
}
|
|
64
|
-
return fail(`"${String(from)}": not a valid JSON primitive.`);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* An in-place validator which validates that a supplied `unknown` value is
|
|
70
|
-
* a valid {@link JsonObject | JsonObject}. Fails by default if any properties or array elements
|
|
71
|
-
* are `undefined` - this default behavior can be overridden by supplying an appropriate
|
|
72
|
-
* {@link Validators.IJsonValidatorContext | context} at runtime.
|
|
73
|
-
* @public
|
|
74
|
-
*/
|
|
75
|
-
export const jsonObject: Validator<JsonObject, IJsonValidatorContext> = new Validation.Base.GenericValidator({
|
|
76
|
-
validator: (
|
|
77
|
-
from: unknown,
|
|
78
|
-
ctx?: IJsonValidatorContext,
|
|
79
|
-
self?: Validator<JsonObject, IJsonValidatorContext>
|
|
80
|
-
) => {
|
|
81
|
-
if (!isJsonObject(from)) {
|
|
82
|
-
return fail('not a valid JSON object.');
|
|
83
|
-
}
|
|
84
|
-
const errors: string[] = [];
|
|
85
|
-
for (const [name, value] of Object.entries(from)) {
|
|
86
|
-
jsonValue.validate(value, ctx).onFailure((m) => {
|
|
87
|
-
errors.push(`${name}: ${m}`);
|
|
88
|
-
return fail(m);
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
if (errors.length > 0) {
|
|
92
|
-
return fail(`not a valid JSON object:\n${errors.join('\n')}`);
|
|
93
|
-
}
|
|
94
|
-
return true;
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* An in-place validator which validates that a supplied `unknown` value is
|
|
100
|
-
* a valid {@link JsonArray | JsonArray}. Fails by default if any properties or array elements
|
|
101
|
-
* are `undefined` - this default behavior can be overridden by supplying an appropriate
|
|
102
|
-
* {@link Validators.IJsonValidatorContext | context} at runtime.
|
|
103
|
-
* @public
|
|
104
|
-
*/
|
|
105
|
-
export const jsonArray: Validator<JsonArray, IJsonValidatorContext> = new Validation.Base.GenericValidator({
|
|
106
|
-
validator: (
|
|
107
|
-
from: unknown,
|
|
108
|
-
ctx?: IJsonValidatorContext,
|
|
109
|
-
self?: Validator<JsonArray, IJsonValidatorContext>
|
|
110
|
-
) => {
|
|
111
|
-
if (!isJsonArray(from)) {
|
|
112
|
-
return fail('not an array');
|
|
113
|
-
}
|
|
114
|
-
const errors: string[] = [];
|
|
115
|
-
for (let i = 0; i < from.length; i++) {
|
|
116
|
-
const value = from[i];
|
|
117
|
-
jsonValue.validate(value, ctx).onFailure((m) => {
|
|
118
|
-
errors.push(`${i}: ${m}`);
|
|
119
|
-
return fail(m);
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
if (errors.length > 0) {
|
|
123
|
-
return fail(`array contains non-json elements:\n${errors.join('\n')}`);
|
|
124
|
-
}
|
|
125
|
-
return true;
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* An in-place validator which validates that a supplied `unknown` value is
|
|
131
|
-
* a valid {@link JsonValue | JsonValue}. Fails by default if any properties or array elements
|
|
132
|
-
* are `undefined` - this default behavior can be overridden by supplying an appropriate
|
|
133
|
-
* {@link Validators.IJsonValidatorContext | context} at runtime.
|
|
134
|
-
* @public
|
|
135
|
-
*/
|
|
136
|
-
export const jsonValue: Validator<JsonValue, IJsonValidatorContext> = new Validation.Base.GenericValidator<
|
|
137
|
-
JsonValue,
|
|
138
|
-
IJsonValidatorContext
|
|
139
|
-
>({
|
|
140
|
-
validator: (
|
|
141
|
-
from: unknown,
|
|
142
|
-
ctx?: IJsonValidatorContext,
|
|
143
|
-
self?: Validator<JsonValue, IJsonValidatorContext>
|
|
144
|
-
) => {
|
|
145
|
-
if (isJsonArray(from)) {
|
|
146
|
-
const result = jsonArray.validate(from, ctx);
|
|
147
|
-
return result.success === true ? true : result;
|
|
148
|
-
} else if (isJsonObject(from)) {
|
|
149
|
-
const result = jsonObject.validate(from, ctx);
|
|
150
|
-
return result.success === true ? true : result;
|
|
151
|
-
}
|
|
152
|
-
const result = jsonPrimitive.validate(from, ctx);
|
|
153
|
-
return result.success === true ? true : result;
|
|
154
|
-
}
|
|
155
|
-
});
|