@fgv/ts-json-base 5.0.1-1 → 5.0.1-10

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.
Files changed (71) hide show
  1. package/README.md +5 -5
  2. package/dist/index.browser.js +31 -0
  3. package/dist/index.js +29 -0
  4. package/dist/packlets/converters/converters.js +187 -0
  5. package/dist/packlets/converters/index.js +23 -0
  6. package/dist/packlets/file-tree/directoryItem.js +67 -0
  7. package/dist/packlets/file-tree/fileItem.js +126 -0
  8. package/dist/packlets/file-tree/fileTree.js +85 -0
  9. package/dist/packlets/file-tree/fileTreeAccessors.js +23 -0
  10. package/dist/packlets/file-tree/fileTreeHelpers.inMemory.js +28 -0
  11. package/dist/packlets/file-tree/fileTreeHelpers.js +29 -0
  12. package/dist/packlets/file-tree/fsTree.js +122 -0
  13. package/dist/packlets/file-tree/in-memory/inMemoryTree.js +177 -0
  14. package/dist/packlets/file-tree/in-memory/index.js +23 -0
  15. package/dist/packlets/file-tree/in-memory/treeBuilder.js +173 -0
  16. package/dist/packlets/file-tree/index.browser.js +34 -0
  17. package/dist/packlets/file-tree/index.js +35 -0
  18. package/dist/packlets/json/common.js +145 -0
  19. package/dist/packlets/json/index.js +23 -0
  20. package/dist/packlets/json-compatible/common.js +23 -0
  21. package/dist/packlets/json-compatible/converters.js +90 -0
  22. package/dist/packlets/json-compatible/index.js +26 -0
  23. package/dist/packlets/json-compatible/validators.js +54 -0
  24. package/dist/packlets/json-file/file.js +74 -0
  25. package/dist/packlets/json-file/index.browser.js +30 -0
  26. package/dist/packlets/json-file/index.js +30 -0
  27. package/dist/packlets/json-file/jsonFsHelper.js +166 -0
  28. package/dist/packlets/json-file/jsonLike.js +26 -0
  29. package/dist/packlets/json-file/jsonTreeHelper.js +116 -0
  30. package/dist/packlets/validators/index.js +23 -0
  31. package/dist/packlets/validators/validators.js +179 -0
  32. package/dist/test/fixtures/file-tree/config.json +1 -0
  33. package/dist/test/fixtures/file-tree/data/items.json +1 -0
  34. package/dist/test/fixtures/file-tree/docs/api/reference.json +1 -0
  35. package/dist/test/unit/data/file/bad/bad3.json +3 -0
  36. package/dist/test/unit/data/file/bad/thing1.json +4 -0
  37. package/dist/test/unit/data/file/bad/thing2.json +3 -0
  38. package/dist/test/unit/data/file/good/thing1.json +4 -0
  39. package/dist/test/unit/data/file/good/thing2.json +3 -0
  40. package/dist/test/unit/json-compatible/helpers.js +32 -0
  41. package/dist/ts-json-base.d.ts +306 -14
  42. package/dist/tsdoc-metadata.json +1 -1
  43. package/lib/index.browser.d.ts +2 -1
  44. package/lib/index.browser.js +3 -1
  45. package/lib/index.d.ts +2 -1
  46. package/lib/index.js +3 -1
  47. package/lib/packlets/converters/converters.d.ts +43 -1
  48. package/lib/packlets/converters/converters.js +59 -4
  49. package/lib/packlets/file-tree/fileTreeHelpers.d.ts +0 -21
  50. package/lib/packlets/file-tree/fileTreeHelpers.inMemory.d.ts +25 -0
  51. package/lib/packlets/file-tree/fileTreeHelpers.inMemory.js +31 -0
  52. package/lib/packlets/file-tree/fileTreeHelpers.js +0 -6
  53. package/lib/packlets/file-tree/index.browser.d.ts +1 -0
  54. package/lib/packlets/file-tree/index.browser.js +3 -0
  55. package/lib/packlets/file-tree/index.d.ts +1 -0
  56. package/lib/packlets/file-tree/index.js +3 -0
  57. package/lib/packlets/json/common.d.ts +4 -4
  58. package/lib/packlets/json-compatible/common.d.ts +43 -0
  59. package/lib/packlets/json-compatible/common.js +24 -0
  60. package/lib/packlets/json-compatible/converters.d.ts +60 -0
  61. package/lib/packlets/json-compatible/converters.js +97 -0
  62. package/lib/packlets/json-compatible/index.d.ts +5 -0
  63. package/lib/packlets/json-compatible/index.js +66 -0
  64. package/lib/packlets/json-compatible/validators.d.ts +29 -0
  65. package/lib/packlets/json-compatible/validators.js +59 -0
  66. package/lib/packlets/json-file/index.browser.d.ts +1 -1
  67. package/lib/packlets/json-file/index.browser.js +2 -2
  68. package/lib/packlets/json-file/jsonTreeHelper.js +1 -1
  69. package/lib/packlets/validators/validators.d.ts +41 -1
  70. package/lib/packlets/validators/validators.js +63 -4
  71. package/package.json +19 -17
@@ -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
  /**
@@ -47,7 +49,7 @@ exports.jsonPrimitive = new ts_utils_1.Validation.Base.GenericValidator({
47
49
  if (from === undefined && (ctx === null || ctx === void 0 ? void 0 : ctx.ignoreUndefinedProperties) === true) {
48
50
  return true;
49
51
  }
50
- return (0, ts_utils_1.fail)(`"${String(from)}": not a valid JSON primitive.`);
52
+ return (0, ts_utils_1.fail)(`"${String(from)}": invalid JSON primitive.`);
51
53
  }
52
54
  });
53
55
  /**
@@ -60,7 +62,7 @@ exports.jsonPrimitive = new ts_utils_1.Validation.Base.GenericValidator({
60
62
  exports.jsonObject = new ts_utils_1.Validation.Base.GenericValidator({
61
63
  validator: (from, ctx, self) => {
62
64
  if (!(0, json_1.isJsonObject)(from)) {
63
- return (0, ts_utils_1.fail)('not a valid JSON object.');
65
+ return (0, ts_utils_1.fail)('invalid JSON object.');
64
66
  }
65
67
  const errors = [];
66
68
  for (const [name, value] of Object.entries(from)) {
@@ -70,7 +72,7 @@ exports.jsonObject = new ts_utils_1.Validation.Base.GenericValidator({
70
72
  });
71
73
  }
72
74
  if (errors.length > 0) {
73
- return (0, ts_utils_1.fail)(`not a valid JSON object:\n${errors.join('\n')}`);
75
+ return (0, ts_utils_1.fail)(`invalid JSON object:\n${errors.join('\n')}`);
74
76
  }
75
77
  return true;
76
78
  }
@@ -122,4 +124,61 @@ exports.jsonValue = new ts_utils_1.Validation.Base.GenericValidator({
122
124
  return result.success === true ? true : result;
123
125
  }
124
126
  });
127
+ /**
128
+ * A {@link Validation.Classes.StringValidator | StringValidator} which validates a string in place.
129
+ * Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
130
+ * @public
131
+ */
132
+ exports.string = new ts_utils_1.Validation.Classes.StringValidator();
133
+ /**
134
+ * A {@link Validation.Classes.NumberValidator | NumberValidator} which validates a number in place.
135
+ * Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
136
+ * @public
137
+ */
138
+ exports.number = new ts_utils_1.Validation.Classes.NumberValidator();
139
+ /**
140
+ * A {@link Validation.Classes.BooleanValidator | BooleanValidator} which validates a boolean in place.
141
+ * Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
142
+ * @public
143
+ */
144
+ exports.boolean = new ts_utils_1.Validation.Classes.BooleanValidator();
145
+ /**
146
+ * Helper to create a validator for a literal value.
147
+ * Accepts {@link Validators.IJsonValidatorContext | IJsonValidatorContext} but ignores it.
148
+ * Mirrors the behavior of `@fgv/ts-utils`.
149
+ * @public
150
+ */
151
+ function literal(value) {
152
+ return new ts_utils_1.Validation.Base.GenericValidator({
153
+ validator: (from) => {
154
+ return from === value ? true : (0, ts_utils_1.fail)(`Expected literal ${String(value)}, found ${JSON.stringify(from)}`);
155
+ }
156
+ });
157
+ }
158
+ /**
159
+ * Helper function to create a {@link Validator | Validator} which validates `unknown` to one of a set of
160
+ * supplied enumerated values. Anything else fails.
161
+ *
162
+ * @remarks
163
+ * This JSON variant accepts an {@link Validators.IJsonValidatorContext | IJsonValidatorContext} OR
164
+ * a `ReadonlyArray<T>` as its validation context. If the context is an array, it is used to override the
165
+ * allowed values for that validation; otherwise, the original `values` supplied at creation time are used.
166
+ *
167
+ * @param values - Array of allowed values.
168
+ * @param message - Optional custom failure message.
169
+ * @returns A new {@link Validator | Validator} returning `<T>`.
170
+ * @public
171
+ */
172
+ function enumeratedValue(values, message) {
173
+ return new ts_utils_1.Validation.Base.GenericValidator({
174
+ validator: (from, context) => {
175
+ const effectiveValues = Array.isArray(context) ? context : values;
176
+ const index = effectiveValues.indexOf(from);
177
+ if (index >= 0) {
178
+ return true;
179
+ }
180
+ return (0, ts_utils_1.fail)(message !== null && message !== void 0 ? message : `Invalid enumerated value ${JSON.stringify(from)}`);
181
+ }
182
+ });
183
+ }
125
184
  //# sourceMappingURL=validators.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgv/ts-json-base",
3
- "version": "5.0.1-1",
3
+ "version": "5.0.1-10",
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,35 @@
31
31
  "devDependencies": {
32
32
  "@types/jest": "^29.5.14",
33
33
  "@types/node": "^20.14.9",
34
- "@typescript-eslint/eslint-plugin": "^8.42.0",
35
- "@typescript-eslint/parser": "^8.42.0",
36
- "eslint": "^9.35.0",
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.1",
43
- "ts-jest": "^29.4.1",
42
+ "rimraf": "^6.1.0",
43
+ "ts-jest": "^29.4.5",
44
44
  "ts-node": "^10.9.2",
45
- "typescript": "5.8.3",
46
- "eslint-plugin-n": "^17.21.3",
47
- "@rushstack/heft-node-rig": "2.9.5",
48
- "@rushstack/heft": "0.74.4",
49
- "@rushstack/heft-jest-plugin": "0.16.13",
45
+ "typescript": "5.9.3",
46
+ "eslint-plugin-n": "^17.23.1",
47
+ "@rushstack/heft": "1.1.3",
48
+ "@rushstack/heft-jest-plugin": "1.1.3",
50
49
  "@types/heft-jest": "1.0.6",
51
- "@microsoft/api-documenter": "^7.26.31",
52
- "@rushstack/eslint-patch": "~1.12.0",
53
- "@rushstack/eslint-config": "4.4.0",
50
+ "@microsoft/api-documenter": "^7.27.3",
51
+ "@rushstack/eslint-patch": "1.14.1",
52
+ "@rushstack/eslint-config": "4.5.3",
54
53
  "eslint-plugin-tsdoc": "~0.4.0",
55
54
  "@types/luxon": "^3.7.1",
56
- "@fgv/ts-utils": "5.0.1-1",
57
- "@fgv/ts-utils-jest": "5.0.1-1"
55
+ "@rushstack/heft-node-rig": "2.11.4",
56
+ "@microsoft/api-extractor": "^7.53.3",
57
+ "@fgv/ts-utils": "5.0.1-10",
58
+ "@fgv/ts-utils-jest": "5.0.1-10",
59
+ "@fgv/heft-dual-rig": "0.1.0"
58
60
  },
59
61
  "peerDependencies": {
60
- "@fgv/ts-utils": "5.0.1-1"
62
+ "@fgv/ts-utils": "5.0.1-10"
61
63
  },
62
64
  "dependencies": {
63
65
  "luxon": "^3.7.2"