@fgv/ts-json-base 5.0.1-8 → 5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.browser.js +31 -0
- package/dist/index.js +29 -0
- package/dist/packlets/converters/converters.js +187 -0
- package/dist/packlets/converters/index.js +23 -0
- package/dist/packlets/file-tree/directoryItem.js +67 -0
- package/dist/packlets/file-tree/fileItem.js +126 -0
- package/dist/packlets/file-tree/fileTree.js +85 -0
- package/dist/packlets/file-tree/fileTreeAccessors.js +23 -0
- package/dist/packlets/file-tree/fileTreeHelpers.inMemory.js +28 -0
- package/dist/packlets/file-tree/fileTreeHelpers.js +29 -0
- package/dist/packlets/file-tree/fsTree.js +122 -0
- package/dist/packlets/file-tree/in-memory/inMemoryTree.js +177 -0
- package/dist/packlets/file-tree/in-memory/index.js +23 -0
- package/dist/packlets/file-tree/in-memory/treeBuilder.js +173 -0
- package/dist/packlets/file-tree/index.browser.js +34 -0
- package/dist/packlets/file-tree/index.js +35 -0
- package/dist/packlets/json/common.js +145 -0
- package/dist/packlets/json/index.js +23 -0
- package/dist/packlets/json-compatible/common.js +23 -0
- package/dist/packlets/json-compatible/converters.js +90 -0
- package/dist/packlets/json-compatible/index.js +26 -0
- package/dist/packlets/json-compatible/validators.js +54 -0
- package/dist/packlets/json-file/file.js +74 -0
- package/dist/packlets/json-file/index.browser.js +30 -0
- package/dist/packlets/json-file/index.js +30 -0
- package/dist/packlets/json-file/jsonFsHelper.js +166 -0
- package/dist/packlets/json-file/jsonLike.js +26 -0
- package/dist/packlets/json-file/jsonTreeHelper.js +116 -0
- package/dist/packlets/validators/index.js +23 -0
- package/dist/packlets/validators/validators.js +179 -0
- package/dist/test/fixtures/file-tree/config.json +1 -0
- package/dist/test/fixtures/file-tree/data/items.json +1 -0
- package/dist/test/fixtures/file-tree/docs/api/reference.json +1 -0
- package/dist/test/unit/data/file/bad/bad3.json +3 -0
- package/dist/test/unit/data/file/bad/thing1.json +4 -0
- package/dist/test/unit/data/file/bad/thing2.json +3 -0
- package/dist/test/unit/data/file/good/thing1.json +4 -0
- package/dist/test/unit/data/file/good/thing2.json +3 -0
- package/dist/test/unit/json-compatible/helpers.js +32 -0
- package/dist/ts-json-base.d.ts +51 -1
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/packlets/json-file/index.browser.d.ts +1 -1
- package/lib/packlets/json-file/index.browser.js +2 -2
- package/lib/packlets/validators/validators.d.ts +41 -1
- package/lib/packlets/validators/validators.js +60 -1
- package/package.json +7 -5
|
@@ -0,0 +1,26 @@
|
|
|
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
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
export const DefaultJsonLike = JSON;
|
|
26
|
+
//# sourceMappingURL=jsonLike.js.map
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 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
|
+
import { mapResults, succeed } from '@fgv/ts-utils';
|
|
23
|
+
import { DefaultJsonLike } from './jsonLike';
|
|
24
|
+
/**
|
|
25
|
+
* Helper class to work with JSON files using FileTree API (web-compatible).
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export class JsonTreeHelper {
|
|
29
|
+
/**
|
|
30
|
+
* Construct a new JsonTreeHelper.
|
|
31
|
+
* @param json - Optional {@link JsonFile.IJsonLike | IJsonLike} used to process strings
|
|
32
|
+
* and JSON values.
|
|
33
|
+
*/
|
|
34
|
+
constructor(json) {
|
|
35
|
+
this.json = json !== null && json !== void 0 ? json : DefaultJsonLike;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Read type-safe JSON from a file in a FileTree.
|
|
39
|
+
* @param fileTree - The FileTree to read from
|
|
40
|
+
* @param filePath - Path of the file to read within the tree
|
|
41
|
+
* @returns `Success` with a {@link JsonValue | JsonValue} or `Failure`
|
|
42
|
+
* with a message if an error occurs.
|
|
43
|
+
*/
|
|
44
|
+
readJsonFromTree(fileTree, filePath) {
|
|
45
|
+
return fileTree.getFile(filePath).onSuccess((file) => {
|
|
46
|
+
// Now getContents() returns JsonCompatibleType<unknown> which is assignable to JsonValue!
|
|
47
|
+
return file.getContents();
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Read a JSON file from a FileTree and apply a supplied converter or validator.
|
|
52
|
+
* @param fileTree - The FileTree to read from
|
|
53
|
+
* @param filePath - Path of the file to read within the tree
|
|
54
|
+
* @param cv - Converter or validator used to process the file.
|
|
55
|
+
* @param context - Optional context for the converter/validator
|
|
56
|
+
* @returns `Success` with a result of type `<T>`, or `Failure`
|
|
57
|
+
* with a message if an error occurs.
|
|
58
|
+
*/
|
|
59
|
+
convertJsonFromTree(fileTree, filePath, cv, context) {
|
|
60
|
+
return this.readJsonFromTree(fileTree, filePath).onSuccess((json) => {
|
|
61
|
+
return cv.convert(json, context);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Reads all JSON files from a directory in a FileTree and applies a converter or validator.
|
|
66
|
+
* @param fileTree - The FileTree to read from
|
|
67
|
+
* @param dirPath - The path of the directory within the tree
|
|
68
|
+
* @param cv - Converter or validator to apply to each JSON file
|
|
69
|
+
* @param filePattern - Optional regex pattern to filter files (defaults to .json files)
|
|
70
|
+
* @param context - Optional context for the converter/validator
|
|
71
|
+
* @returns Array of items with filename and converted content
|
|
72
|
+
*/
|
|
73
|
+
convertJsonDirectoryFromTree(fileTree, dirPath, cv, filePattern, context) {
|
|
74
|
+
const pattern = filePattern !== null && filePattern !== void 0 ? filePattern : /\.json$/;
|
|
75
|
+
return fileTree.getDirectory(dirPath).onSuccess((dir) => {
|
|
76
|
+
return dir.getChildren().onSuccess((children) => {
|
|
77
|
+
const results = children
|
|
78
|
+
.filter((child) => child.type === 'file' && pattern.test(child.name))
|
|
79
|
+
.map((file) => {
|
|
80
|
+
return this.convertJsonFromTree(fileTree, file.absolutePath, cv, context).onSuccess((item) => {
|
|
81
|
+
return succeed({
|
|
82
|
+
filename: file.name,
|
|
83
|
+
item
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
return mapResults(results);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Reads and converts all JSON files from a directory in a FileTree,
|
|
93
|
+
* returning a Map indexed by file base name.
|
|
94
|
+
* @param fileTree - The FileTree to read from
|
|
95
|
+
* @param dirPath - The path of the directory within the tree
|
|
96
|
+
* @param cv - Converter or validator to apply to each JSON file
|
|
97
|
+
* @param filePattern - Optional regex pattern to filter files
|
|
98
|
+
* @param context - Optional context for the converter/validator
|
|
99
|
+
* @returns Map of basename to converted content
|
|
100
|
+
*/
|
|
101
|
+
convertJsonDirectoryToMapFromTree(fileTree, dirPath, cv, filePattern, context) {
|
|
102
|
+
return this.convertJsonDirectoryFromTree(fileTree, dirPath, cv, filePattern, context).onSuccess((items) => {
|
|
103
|
+
const map = new Map();
|
|
104
|
+
for (const item of items) {
|
|
105
|
+
const basename = item.filename.replace(/\.json$/, '');
|
|
106
|
+
map.set(basename, item.item);
|
|
107
|
+
}
|
|
108
|
+
return succeed(map);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* @public
|
|
114
|
+
*/
|
|
115
|
+
export const DefaultJsonTreeHelper = new JsonTreeHelper();
|
|
116
|
+
//# sourceMappingURL=jsonTreeHelper.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
export * from './validators';
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,179 @@
|
|
|
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
|
+
import { Validation, fail } from '@fgv/ts-utils';
|
|
23
|
+
import { isJsonArray, isJsonObject } from '../json';
|
|
24
|
+
/**
|
|
25
|
+
* An in-place validator which validates that a supplied `unknown` value is
|
|
26
|
+
* a valid {@link JsonPrimitive | JsonPrimitive}.
|
|
27
|
+
* @public
|
|
28
|
+
*/
|
|
29
|
+
export const jsonPrimitive = new Validation.Base.GenericValidator({
|
|
30
|
+
validator: (from, ctx, self) => {
|
|
31
|
+
if (from === null) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
switch (typeof from) {
|
|
35
|
+
case 'boolean':
|
|
36
|
+
case 'string':
|
|
37
|
+
return true;
|
|
38
|
+
case 'number':
|
|
39
|
+
if (!Number.isNaN(from)) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
if (from === undefined && (ctx === null || ctx === void 0 ? void 0 : ctx.ignoreUndefinedProperties) === true) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
return fail(`"${String(from)}": invalid JSON primitive.`);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
/**
|
|
51
|
+
* An in-place validator which validates that a supplied `unknown` value is
|
|
52
|
+
* a valid {@link JsonObject | JsonObject}. Fails by default if any properties or array elements
|
|
53
|
+
* are `undefined` - this default behavior can be overridden by supplying an appropriate
|
|
54
|
+
* {@link Validators.IJsonValidatorContext | context} at runtime.
|
|
55
|
+
* @public
|
|
56
|
+
*/
|
|
57
|
+
export const jsonObject = new Validation.Base.GenericValidator({
|
|
58
|
+
validator: (from, ctx, self) => {
|
|
59
|
+
if (!isJsonObject(from)) {
|
|
60
|
+
return fail('invalid JSON object.');
|
|
61
|
+
}
|
|
62
|
+
const errors = [];
|
|
63
|
+
for (const [name, value] of Object.entries(from)) {
|
|
64
|
+
jsonValue.validate(value, ctx).onFailure((m) => {
|
|
65
|
+
errors.push(`${name}: ${m}`);
|
|
66
|
+
return fail(m);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
if (errors.length > 0) {
|
|
70
|
+
return fail(`invalid JSON object:\n${errors.join('\n')}`);
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
/**
|
|
76
|
+
* An in-place validator which validates that a supplied `unknown` value is
|
|
77
|
+
* a valid {@link JsonArray | JsonArray}. Fails by default if any properties or array elements
|
|
78
|
+
* are `undefined` - this default behavior can be overridden by supplying an appropriate
|
|
79
|
+
* {@link Validators.IJsonValidatorContext | context} at runtime.
|
|
80
|
+
* @public
|
|
81
|
+
*/
|
|
82
|
+
export const jsonArray = new Validation.Base.GenericValidator({
|
|
83
|
+
validator: (from, ctx, self) => {
|
|
84
|
+
if (!isJsonArray(from)) {
|
|
85
|
+
return fail('not an array');
|
|
86
|
+
}
|
|
87
|
+
const errors = [];
|
|
88
|
+
for (let i = 0; i < from.length; i++) {
|
|
89
|
+
const value = from[i];
|
|
90
|
+
jsonValue.validate(value, ctx).onFailure((m) => {
|
|
91
|
+
errors.push(`${i}: ${m}`);
|
|
92
|
+
return fail(m);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (errors.length > 0) {
|
|
96
|
+
return fail(`array contains non-json elements:\n${errors.join('\n')}`);
|
|
97
|
+
}
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
/**
|
|
102
|
+
* An in-place validator which validates that a supplied `unknown` value is
|
|
103
|
+
* a valid {@link JsonValue | JsonValue}. Fails by default if any properties or array elements
|
|
104
|
+
* are `undefined` - this default behavior can be overridden by supplying an appropriate
|
|
105
|
+
* {@link Validators.IJsonValidatorContext | context} at runtime.
|
|
106
|
+
* @public
|
|
107
|
+
*/
|
|
108
|
+
export const jsonValue = new Validation.Base.GenericValidator({
|
|
109
|
+
validator: (from, ctx, self) => {
|
|
110
|
+
if (isJsonArray(from)) {
|
|
111
|
+
const result = jsonArray.validate(from, ctx);
|
|
112
|
+
return result.success === true ? true : result;
|
|
113
|
+
}
|
|
114
|
+
else if (isJsonObject(from)) {
|
|
115
|
+
const result = jsonObject.validate(from, ctx);
|
|
116
|
+
return result.success === true ? true : result;
|
|
117
|
+
}
|
|
118
|
+
const result = jsonPrimitive.validate(from, ctx);
|
|
119
|
+
return result.success === true ? true : result;
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
/**
|
|
123
|
+
* A {@link Validation.Classes.StringValidator | StringValidator} which validates a string in place.
|
|
124
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
125
|
+
* @public
|
|
126
|
+
*/
|
|
127
|
+
export const string = new Validation.Classes.StringValidator();
|
|
128
|
+
/**
|
|
129
|
+
* A {@link Validation.Classes.NumberValidator | NumberValidator} which validates a number in place.
|
|
130
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
131
|
+
* @public
|
|
132
|
+
*/
|
|
133
|
+
export const number = new Validation.Classes.NumberValidator();
|
|
134
|
+
/**
|
|
135
|
+
* A {@link Validation.Classes.BooleanValidator | BooleanValidator} which validates a boolean in place.
|
|
136
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
137
|
+
* @public
|
|
138
|
+
*/
|
|
139
|
+
export const boolean = new Validation.Classes.BooleanValidator();
|
|
140
|
+
/**
|
|
141
|
+
* Helper to create a validator for a literal value.
|
|
142
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
143
|
+
* Mirrors the behavior of `@fgv/ts-utils`.
|
|
144
|
+
* @public
|
|
145
|
+
*/
|
|
146
|
+
export function literal(value) {
|
|
147
|
+
return new Validation.Base.GenericValidator({
|
|
148
|
+
validator: (from) => {
|
|
149
|
+
return from === value ? true : fail(`Expected literal ${String(value)}, found ${JSON.stringify(from)}`);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Helper function to create a {@link Validator | Validator} which validates `unknown` to one of a set of
|
|
155
|
+
* supplied enumerated values. Anything else fails.
|
|
156
|
+
*
|
|
157
|
+
* @remarks
|
|
158
|
+
* This JSON variant accepts an {@link Validators.IJsonValidatorContext | IJsonValidatorContext} OR
|
|
159
|
+
* a `ReadonlyArray<T>` as its validation context. If the context is an array, it is used to override the
|
|
160
|
+
* allowed values for that validation; otherwise, the original `values` supplied at creation time are used.
|
|
161
|
+
*
|
|
162
|
+
* @param values - Array of allowed values.
|
|
163
|
+
* @param message - Optional custom failure message.
|
|
164
|
+
* @returns A new {@link Validator | Validator} returning `<T>`.
|
|
165
|
+
* @public
|
|
166
|
+
*/
|
|
167
|
+
export function enumeratedValue(values, message) {
|
|
168
|
+
return new Validation.Base.GenericValidator({
|
|
169
|
+
validator: (from, context) => {
|
|
170
|
+
const effectiveValues = Array.isArray(context) ? context : values;
|
|
171
|
+
const index = effectiveValues.indexOf(from);
|
|
172
|
+
if (index >= 0) {
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
return fail(message !== null && message !== void 0 ? message : `Invalid enumerated value ${JSON.stringify(from)}`);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
//# sourceMappingURL=validators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "name": "test", "enabled": true }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
[1, 2, 3]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "endpoints": [] }
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2025 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
|
+
// Factory helpers for runtime sections
|
|
23
|
+
export function makeUser(overrides = {}) {
|
|
24
|
+
return Object.assign({ id: 'u1', name: 'Test User', tags: ['a', 'b'], meta: { active: true, score: 10 } }, overrides);
|
|
25
|
+
}
|
|
26
|
+
export function makeAlpha() {
|
|
27
|
+
return { kind: 'alpha', value: 'ok' };
|
|
28
|
+
}
|
|
29
|
+
export function makeBeta() {
|
|
30
|
+
return { kind: 'beta', count: 3 };
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=helpers.js.map
|
package/dist/ts-json-base.d.ts
CHANGED
|
@@ -47,6 +47,13 @@ declare type ArrayValidator<T, TC = unknown> = Validation.Classes.ArrayValidator
|
|
|
47
47
|
*/
|
|
48
48
|
declare const boolean: Converter<boolean, IJsonConverterContext>;
|
|
49
49
|
|
|
50
|
+
/**
|
|
51
|
+
* A {@link Validation.Classes.BooleanValidator | BooleanValidator} which validates a boolean in place.
|
|
52
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
declare const boolean_2: Validator<boolean, IJsonValidatorContext>;
|
|
56
|
+
|
|
50
57
|
/**
|
|
51
58
|
* Identifies whether some `unknown` value is a {@link JsonPrimitive | primitive},
|
|
52
59
|
* {@link JsonObject | object} or {@link JsonArray | array}. Fails for any value
|
|
@@ -217,6 +224,22 @@ declare function discriminatedObject<T, TD extends string = string, TC = unknown
|
|
|
217
224
|
*/
|
|
218
225
|
declare function enumeratedValue<T>(values: ReadonlyArray<T>, message?: string): Converter<T, IJsonConverterContext | ReadonlyArray<T>>;
|
|
219
226
|
|
|
227
|
+
/**
|
|
228
|
+
* Helper function to create a {@link Validator | Validator} which validates `unknown` to one of a set of
|
|
229
|
+
* supplied enumerated values. Anything else fails.
|
|
230
|
+
*
|
|
231
|
+
* @remarks
|
|
232
|
+
* This JSON variant accepts an {@link Validators.IJsonValidatorContext | IJsonValidatorContext} OR
|
|
233
|
+
* a `ReadonlyArray<T>` as its validation context. If the context is an array, it is used to override the
|
|
234
|
+
* allowed values for that validation; otherwise, the original `values` supplied at creation time are used.
|
|
235
|
+
*
|
|
236
|
+
* @param values - Array of allowed values.
|
|
237
|
+
* @param message - Optional custom failure message.
|
|
238
|
+
* @returns A new {@link Validator | Validator} returning `<T>`.
|
|
239
|
+
* @public
|
|
240
|
+
*/
|
|
241
|
+
declare function enumeratedValue_2<T>(values: ReadonlyArray<T>, message?: string): Validator<T, IJsonValidatorContext | ReadonlyArray<T>>;
|
|
242
|
+
|
|
220
243
|
/**
|
|
221
244
|
* Class representing a file in a file tree.
|
|
222
245
|
* @public
|
|
@@ -1184,6 +1207,14 @@ export declare type JsonValueType = 'primitive' | 'object' | 'array';
|
|
|
1184
1207
|
*/
|
|
1185
1208
|
declare function literal<T>(value: T): Converter<T, IJsonConverterContext>;
|
|
1186
1209
|
|
|
1210
|
+
/**
|
|
1211
|
+
* Helper to create a validator for a literal value.
|
|
1212
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
1213
|
+
* Mirrors the behavior of `@fgv/ts-utils`.
|
|
1214
|
+
* @public
|
|
1215
|
+
*/
|
|
1216
|
+
declare function literal_2<T>(value: T): Validator<T, IJsonValidatorContext>;
|
|
1217
|
+
|
|
1187
1218
|
/**
|
|
1188
1219
|
* A {@link Converter | Converter} which converts `unknown` to a `number`.
|
|
1189
1220
|
* Accepts {@link Converters.IJsonConverterContext | IJsonConverterContext} but ignores it.
|
|
@@ -1192,6 +1223,13 @@ declare function literal<T>(value: T): Converter<T, IJsonConverterContext>;
|
|
|
1192
1223
|
*/
|
|
1193
1224
|
declare const number: Converter<number, IJsonConverterContext>;
|
|
1194
1225
|
|
|
1226
|
+
/**
|
|
1227
|
+
* A {@link Validation.Classes.NumberValidator | NumberValidator} which validates a number in place.
|
|
1228
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
1229
|
+
* @public
|
|
1230
|
+
*/
|
|
1231
|
+
declare const number_2: Validator<number, IJsonValidatorContext>;
|
|
1232
|
+
|
|
1195
1233
|
/**
|
|
1196
1234
|
* A helper function to create a {@link JsonCompatible.ObjectConverter | JSON-compatible ObjectConverter<T, TC>} which converts a
|
|
1197
1235
|
* supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatibleType<T>} value.
|
|
@@ -1331,6 +1369,13 @@ declare function strictObject<T, TC = unknown>(properties: Conversion.FieldConve
|
|
|
1331
1369
|
*/
|
|
1332
1370
|
declare const string: StringConverter<string, IJsonConverterContext>;
|
|
1333
1371
|
|
|
1372
|
+
/**
|
|
1373
|
+
* A {@link Validation.Classes.StringValidator | StringValidator} which validates a string in place.
|
|
1374
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
1375
|
+
* @public
|
|
1376
|
+
*/
|
|
1377
|
+
declare const string_2: Validation.Classes.StringValidator<string, IJsonValidatorContext>;
|
|
1378
|
+
|
|
1334
1379
|
/**
|
|
1335
1380
|
* A validator which validates a supplied `unknown` value to a valid {@link JsonCompatibleType | JsonCompatible} value.
|
|
1336
1381
|
* @public
|
|
@@ -1339,11 +1384,16 @@ declare type Validator_2<T, TC = unknown> = Validator<JsonCompatibleType<T>, TC>
|
|
|
1339
1384
|
|
|
1340
1385
|
declare namespace Validators {
|
|
1341
1386
|
export {
|
|
1387
|
+
literal_2 as literal,
|
|
1388
|
+
enumeratedValue_2 as enumeratedValue,
|
|
1342
1389
|
IJsonValidatorContext,
|
|
1343
1390
|
jsonPrimitive_2 as jsonPrimitive,
|
|
1344
1391
|
jsonObject_2 as jsonObject,
|
|
1345
1392
|
jsonArray_2 as jsonArray,
|
|
1346
|
-
jsonValue_2 as jsonValue
|
|
1393
|
+
jsonValue_2 as jsonValue,
|
|
1394
|
+
string_2 as string,
|
|
1395
|
+
number_2 as number,
|
|
1396
|
+
boolean_2 as boolean
|
|
1347
1397
|
}
|
|
1348
1398
|
}
|
|
1349
1399
|
export { Validators }
|
package/dist/tsdoc-metadata.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from './file';
|
|
2
1
|
export * from './jsonLike';
|
|
3
2
|
export * from './jsonTreeHelper';
|
|
3
|
+
export type { IJsonFsDirectoryOptions, IReadDirectoryItem, ItemNameTransformFunction, IJsonFsDirectoryToMapOptions, IJsonFsHelperConfig, JsonFsHelperInitOptions } from './jsonFsHelper';
|
|
4
4
|
//# sourceMappingURL=index.browser.d.ts.map
|
|
@@ -36,11 +36,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
36
36
|
};
|
|
37
37
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
38
|
// Browser-safe JSON file exports - excludes Node.js filesystem dependencies
|
|
39
|
-
// Export core
|
|
40
|
-
__exportStar(require("./file"), exports);
|
|
39
|
+
// Export browser-safe core functionality
|
|
41
40
|
__exportStar(require("./jsonLike"), exports);
|
|
42
41
|
// Export FileTree-based helper (web-compatible)
|
|
43
42
|
__exportStar(require("./jsonTreeHelper"), exports);
|
|
44
43
|
// Exclude:
|
|
44
|
+
// - file.ts (wraps jsonFsHelper - requires Node.js fs/path)
|
|
45
45
|
// - jsonFsHelper (requires Node.js fs/path)
|
|
46
46
|
//# sourceMappingURL=index.browser.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Validator } from '@fgv/ts-utils';
|
|
1
|
+
import { Validation, Validator } from '@fgv/ts-utils';
|
|
2
2
|
import { JsonArray, JsonObject, JsonPrimitive, JsonValue } from '../json';
|
|
3
3
|
/**
|
|
4
4
|
* Validation context for in-place JSON validators.
|
|
@@ -37,4 +37,44 @@ export declare const jsonArray: Validator<JsonArray, IJsonValidatorContext>;
|
|
|
37
37
|
* @public
|
|
38
38
|
*/
|
|
39
39
|
export declare const jsonValue: Validator<JsonValue, IJsonValidatorContext>;
|
|
40
|
+
/**
|
|
41
|
+
* A {@link Validation.Classes.StringValidator | StringValidator} which validates a string in place.
|
|
42
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export declare const string: Validation.Classes.StringValidator<string, IJsonValidatorContext>;
|
|
46
|
+
/**
|
|
47
|
+
* A {@link Validation.Classes.NumberValidator | NumberValidator} which validates a number in place.
|
|
48
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
49
|
+
* @public
|
|
50
|
+
*/
|
|
51
|
+
export declare const number: Validator<number, IJsonValidatorContext>;
|
|
52
|
+
/**
|
|
53
|
+
* A {@link Validation.Classes.BooleanValidator | BooleanValidator} which validates a boolean in place.
|
|
54
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
55
|
+
* @public
|
|
56
|
+
*/
|
|
57
|
+
export declare const boolean: Validator<boolean, IJsonValidatorContext>;
|
|
58
|
+
/**
|
|
59
|
+
* Helper to create a validator for a literal value.
|
|
60
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
61
|
+
* Mirrors the behavior of `@fgv/ts-utils`.
|
|
62
|
+
* @public
|
|
63
|
+
*/
|
|
64
|
+
export declare function literal<T>(value: T): Validator<T, IJsonValidatorContext>;
|
|
65
|
+
/**
|
|
66
|
+
* Helper function to create a {@link Validator | Validator} which validates `unknown` to one of a set of
|
|
67
|
+
* supplied enumerated values. Anything else fails.
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* This JSON variant accepts an {@link Validators.IJsonValidatorContext | IJsonValidatorContext} OR
|
|
71
|
+
* a `ReadonlyArray<T>` as its validation context. If the context is an array, it is used to override the
|
|
72
|
+
* allowed values for that validation; otherwise, the original `values` supplied at creation time are used.
|
|
73
|
+
*
|
|
74
|
+
* @param values - Array of allowed values.
|
|
75
|
+
* @param message - Optional custom failure message.
|
|
76
|
+
* @returns A new {@link Validator | Validator} returning `<T>`.
|
|
77
|
+
* @public
|
|
78
|
+
*/
|
|
79
|
+
export declare function enumeratedValue<T>(values: ReadonlyArray<T>, message?: string): Validator<T, IJsonValidatorContext | ReadonlyArray<T>>;
|
|
40
80
|
//# sourceMappingURL=validators.d.ts.map
|
|
@@ -21,7 +21,9 @@
|
|
|
21
21
|
* SOFTWARE.
|
|
22
22
|
*/
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
-
exports.jsonValue = exports.jsonArray = exports.jsonObject = exports.jsonPrimitive = void 0;
|
|
24
|
+
exports.boolean = exports.number = exports.string = exports.jsonValue = exports.jsonArray = exports.jsonObject = exports.jsonPrimitive = void 0;
|
|
25
|
+
exports.literal = literal;
|
|
26
|
+
exports.enumeratedValue = enumeratedValue;
|
|
25
27
|
const ts_utils_1 = require("@fgv/ts-utils");
|
|
26
28
|
const json_1 = require("../json");
|
|
27
29
|
/**
|
|
@@ -122,4 +124,61 @@ exports.jsonValue = new ts_utils_1.Validation.Base.GenericValidator({
|
|
|
122
124
|
return result.success === true ? true : result;
|
|
123
125
|
}
|
|
124
126
|
});
|
|
127
|
+
/**
|
|
128
|
+
* A {@link Validation.Classes.StringValidator | StringValidator} which validates a string in place.
|
|
129
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
130
|
+
* @public
|
|
131
|
+
*/
|
|
132
|
+
exports.string = new ts_utils_1.Validation.Classes.StringValidator();
|
|
133
|
+
/**
|
|
134
|
+
* A {@link Validation.Classes.NumberValidator | NumberValidator} which validates a number in place.
|
|
135
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
136
|
+
* @public
|
|
137
|
+
*/
|
|
138
|
+
exports.number = new ts_utils_1.Validation.Classes.NumberValidator();
|
|
139
|
+
/**
|
|
140
|
+
* A {@link Validation.Classes.BooleanValidator | BooleanValidator} which validates a boolean in place.
|
|
141
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
142
|
+
* @public
|
|
143
|
+
*/
|
|
144
|
+
exports.boolean = new ts_utils_1.Validation.Classes.BooleanValidator();
|
|
145
|
+
/**
|
|
146
|
+
* Helper to create a validator for a literal value.
|
|
147
|
+
* Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
|
|
148
|
+
* Mirrors the behavior of `@fgv/ts-utils`.
|
|
149
|
+
* @public
|
|
150
|
+
*/
|
|
151
|
+
function literal(value) {
|
|
152
|
+
return new ts_utils_1.Validation.Base.GenericValidator({
|
|
153
|
+
validator: (from) => {
|
|
154
|
+
return from === value ? true : (0, ts_utils_1.fail)(`Expected literal ${String(value)}, found ${JSON.stringify(from)}`);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Helper function to create a {@link Validator | Validator} which validates `unknown` to one of a set of
|
|
160
|
+
* supplied enumerated values. Anything else fails.
|
|
161
|
+
*
|
|
162
|
+
* @remarks
|
|
163
|
+
* This JSON variant accepts an {@link Validators.IJsonValidatorContext | IJsonValidatorContext} OR
|
|
164
|
+
* a `ReadonlyArray<T>` as its validation context. If the context is an array, it is used to override the
|
|
165
|
+
* allowed values for that validation; otherwise, the original `values` supplied at creation time are used.
|
|
166
|
+
*
|
|
167
|
+
* @param values - Array of allowed values.
|
|
168
|
+
* @param message - Optional custom failure message.
|
|
169
|
+
* @returns A new {@link Validator | Validator} returning `<T>`.
|
|
170
|
+
* @public
|
|
171
|
+
*/
|
|
172
|
+
function enumeratedValue(values, message) {
|
|
173
|
+
return new ts_utils_1.Validation.Base.GenericValidator({
|
|
174
|
+
validator: (from, context) => {
|
|
175
|
+
const effectiveValues = Array.isArray(context) ? context : values;
|
|
176
|
+
const index = effectiveValues.indexOf(from);
|
|
177
|
+
if (index >= 0) {
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
return (0, ts_utils_1.fail)(message !== null && message !== void 0 ? message : `Invalid enumerated value ${JSON.stringify(from)}`);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
125
184
|
//# sourceMappingURL=validators.js.map
|