@isdk/json-repair 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierignore +8 -0
- package/CHANGELOG.md +16 -0
- package/README.cn.md +126 -14
- package/README.md +152 -14
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +207 -20
- package/dist/index.d.ts +207 -20
- package/dist/index.js +1 -1
- package/docs/README.md +152 -14
- package/docs/_media/README.cn.md +126 -14
- package/docs/classes/RepairParser.md +5 -5
- package/docs/classes/SchemaWalker.md +126 -6
- package/docs/functions/jsonRepair.md +1 -1
- package/docs/interfaces/RepairOptions.md +52 -19
- package/docs/type-aliases/JSONPath.md +1 -1
- package/eslint.config.mjs +60 -26
- package/package.json +13 -9
- package/setupVitest.mjs +1 -1
- package/tsconfig.json +9 -8
- package/tsconfig.release.json +21 -0
- package/tsup.config.ts +13 -9
- package/typedoc.config.cjs +3 -1
- package/vite.config.mjs +13 -8
- package/tsconfig.spec.json +0 -19
package/dist/index.d.cts
CHANGED
|
@@ -3,31 +3,64 @@
|
|
|
3
3
|
*/
|
|
4
4
|
interface RepairOptions {
|
|
5
5
|
/**
|
|
6
|
-
* Fuzzy matching
|
|
6
|
+
* Fuzzy matching of property keys against the Schema's declared `properties`.
|
|
7
7
|
*
|
|
8
|
-
* - `false` (default):
|
|
9
|
-
* - `true`:
|
|
10
|
-
* -
|
|
8
|
+
* - `false` (default): Only an exactly declared key counts as a match.
|
|
9
|
+
* - `true`: Case-insensitive and separator-insensitive matching, so `user_name`,
|
|
10
|
+
* `userName`, `USER-NAME` and `user name` all resolve to the declared key.
|
|
11
|
+
* - `(key: string, properties: string[]) => string | undefined`: Custom mapping logic;
|
|
12
|
+
* it receives the incoming key and the declared names, and returns the name to use
|
|
13
|
+
* (or `undefined` to fall back to the incoming key).
|
|
14
|
+
*
|
|
15
|
+
* A match renames the key to the Schema's name in the output and makes nested Schema
|
|
16
|
+
* lookups (and therefore type coercion) work for it.
|
|
17
|
+
* It also decides where greedy capture stops: with `true` a separator or case variant of a
|
|
18
|
+
* declared key (`user_name:` for `userName`) ends the value in front of it.
|
|
19
|
+
* When you reuse a `SchemaWalker`, pass this option to `SchemaWalker.create()`.
|
|
11
20
|
*/
|
|
12
21
|
keyMatching?: boolean | ((key: string, properties: string[]) => string | undefined);
|
|
13
22
|
/**
|
|
14
|
-
* Strategy for greedy string capture when
|
|
23
|
+
* Strategy for greedy string capture when the strict JSON path failed for a value.
|
|
24
|
+
*
|
|
25
|
+
* - `'aggressive'` (default): When the Schema declares property names for the current
|
|
26
|
+
* level, the captured value only ends at one of those declared keys. Text that merely
|
|
27
|
+
* looks like `"some words":` stays inside the value.
|
|
28
|
+
* - `'conservative'`: Any text matching the `"key":` / `key:` pattern ends the value,
|
|
29
|
+
* which keeps the structure of the document at the cost of cutting values short.
|
|
15
30
|
*
|
|
16
|
-
* -
|
|
17
|
-
*
|
|
31
|
+
* A whitespace-containing key the Schema declares is a breakpoint in both strategies even
|
|
32
|
+
* when the document left it unquoted (`{ Capital: Berlin, Official language: Italian }`),
|
|
33
|
+
* because it can only be recognised from the declaration.
|
|
34
|
+
* Neither strategy affects values that were parsed by the strict JSON path; the
|
|
35
|
+
* boundary rules described in the README apply to those unchanged.
|
|
18
36
|
*/
|
|
19
37
|
stringCaptureStrategy?: 'conservative' | 'aggressive';
|
|
20
38
|
/**
|
|
21
|
-
* Whether to
|
|
39
|
+
* Whether to coerce values to the type the Schema declares. Enabled by default.
|
|
40
|
+
*
|
|
41
|
+
* - `true` (default): `"Approx. 1,200.50 USD"` becomes `1200.5`, `"yes"` becomes `true`,
|
|
42
|
+
* `"Processing!"` becomes the enum entry it fuzzy matches, `12` becomes `"12"` when the
|
|
43
|
+
* Schema says `type: 'string'`.
|
|
44
|
+
* - `false`: values are returned exactly as they were parsed.
|
|
45
|
+
*
|
|
46
|
+
* Coercion needs the Schema of the property the value belongs to; a level that declares
|
|
47
|
+
* no types is left alone.
|
|
22
48
|
*
|
|
23
|
-
*
|
|
49
|
+
* Coercing to a declared `string` reuses the text the document wrote, not the number it
|
|
50
|
+
* parsed to, so a leading zero survives: `{ id: 01234 }` becomes `"01234"`.
|
|
51
|
+
* `null` and structures (`{}` / `[]`) are never stringified - `"null"` and
|
|
52
|
+
* `"[object Object]"` are not what a `type: 'string'` declaration asks for.
|
|
24
53
|
*/
|
|
25
54
|
coerceTypes?: boolean;
|
|
26
55
|
/**
|
|
27
56
|
* Defines how to handle properties not defined in the Schema.
|
|
28
57
|
*
|
|
29
58
|
* - `'keep'` (default): Keeps these extra properties in the output.
|
|
30
|
-
* - `'remove'`:
|
|
59
|
+
* - `'remove'`: Drops them. Only applies at a level whose Schema declares `properties`;
|
|
60
|
+
* a free-form level has no "extra" keys and is left untouched.
|
|
61
|
+
*
|
|
62
|
+
* This option is independent from the JSON Schema keyword `additionalProperties: false`,
|
|
63
|
+
* which is respected on its own when validating incoming keys.
|
|
31
64
|
*/
|
|
32
65
|
additionalProperties?: 'keep' | 'remove';
|
|
33
66
|
}
|
|
@@ -43,15 +76,20 @@ type JSONPath = (string | number)[];
|
|
|
43
76
|
*/
|
|
44
77
|
declare class SchemaWalker {
|
|
45
78
|
private dereferencedSchema;
|
|
79
|
+
private options;
|
|
46
80
|
private constructor();
|
|
47
81
|
/**
|
|
48
82
|
* Static factory method to create a SchemaWalker instance.
|
|
49
83
|
* Asynchronously dereferences the provided JSON Schema.
|
|
50
84
|
*
|
|
85
|
+
* The Schema is dereferenced on a deep copy: `$RefParser.dereference()` replaces every
|
|
86
|
+
* `$ref` in place, which would otherwise rewrite the object the caller passed in.
|
|
87
|
+
*
|
|
51
88
|
* @param schema - The JSON Schema object to walk.
|
|
89
|
+
* @param options - Repair options that concern the Schema itself (`keyMatching`).
|
|
52
90
|
* @returns A promise that resolves to a SchemaWalker instance.
|
|
53
91
|
*/
|
|
54
|
-
static create(schema: any): Promise<SchemaWalker>;
|
|
92
|
+
static create(schema: any, options?: RepairOptions): Promise<SchemaWalker>;
|
|
55
93
|
/**
|
|
56
94
|
* Retrieves the sub-schema for a given JSON path.
|
|
57
95
|
*
|
|
@@ -67,6 +105,46 @@ declare class SchemaWalker {
|
|
|
67
105
|
* @returns True if the property is defined in the Schema or allowed by additionalProperties.
|
|
68
106
|
*/
|
|
69
107
|
isValidProperty(path: JSONPath, key: string): boolean;
|
|
108
|
+
/**
|
|
109
|
+
* Resolves an incoming property name to the name declared by the Schema at `path`.
|
|
110
|
+
*
|
|
111
|
+
* Without `keyMatching` this only accepts an exact declaration. With `keyMatching: true`
|
|
112
|
+
* the comparison ignores case and the separators used by the two common naming styles
|
|
113
|
+
* (`user_name` / `userName` / `USER NAME`). A function matcher decides itself and may
|
|
114
|
+
* return a name the Schema does not declare.
|
|
115
|
+
*
|
|
116
|
+
* Besides renaming, the result is what makes a key a breakpoint for greedy string capture:
|
|
117
|
+
* the parser calls this while deciding whether an upcoming `key:` ends the value in front of it.
|
|
118
|
+
*
|
|
119
|
+
* @param path - The current parent path.
|
|
120
|
+
* @param key - The property key found in the broken document.
|
|
121
|
+
* @returns The canonical Schema property name, or `null` when nothing matches.
|
|
122
|
+
*/
|
|
123
|
+
matchProperty(path: JSONPath, key: string): string | null;
|
|
124
|
+
/**
|
|
125
|
+
* The property names the Schema declares at `path` (empty when the level is free-form).
|
|
126
|
+
*/
|
|
127
|
+
declaredPropertyNames(path: JSONPath): string[];
|
|
128
|
+
/**
|
|
129
|
+
* Folding used for fuzzy key matching: case-insensitive and separator-insensitive, so
|
|
130
|
+
* snake_case, kebab-case, camelCase and spaced keys collide.
|
|
131
|
+
*/
|
|
132
|
+
private static normalizeName;
|
|
133
|
+
/**
|
|
134
|
+
* Whether a property name is explicitly declared in the Schema at the given path.
|
|
135
|
+
*
|
|
136
|
+
* @param path - The current parent path.
|
|
137
|
+
* @param key - The property key to check.
|
|
138
|
+
* @returns True only when `key` appears in the Schema `properties` of that path.
|
|
139
|
+
*/
|
|
140
|
+
isDeclaredProperty(path: JSONPath, key: string): boolean;
|
|
141
|
+
/**
|
|
142
|
+
* Whether the Schema at the given path declares any property names at all.
|
|
143
|
+
* Used to decide whether key matching can be strict or has to stay lenient.
|
|
144
|
+
*
|
|
145
|
+
* @param path - The current parent path.
|
|
146
|
+
*/
|
|
147
|
+
hasDeclaredProperties(path: JSONPath): boolean;
|
|
70
148
|
/**
|
|
71
149
|
* Gets the expected types for a specific property at a given path.
|
|
72
150
|
*
|
|
@@ -106,33 +184,112 @@ declare class RepairParser {
|
|
|
106
184
|
* Uses Schema guidance to decide whether to use standard parsing or greedy capture.
|
|
107
185
|
*/
|
|
108
186
|
private consumeValue;
|
|
187
|
+
/**
|
|
188
|
+
* Attempts a strict JSON scalar parse (string / number / boolean / null) at the current
|
|
189
|
+
* position and rewinds when the token is not followed by a boundary.
|
|
190
|
+
*
|
|
191
|
+
* The boundary test runs in `'structural'` mode: a cleanly parsed value is trusted, so
|
|
192
|
+
* anything that looks like the next key is treated as structure and the successful parse
|
|
193
|
+
* is never discarded in favour of greedy capture. This is what guarantees that valid JSON
|
|
194
|
+
* always parses exactly like `JSON.parse`.
|
|
195
|
+
*/
|
|
196
|
+
private tryConsumeStrictValue;
|
|
197
|
+
/**
|
|
198
|
+
* Whether the Schema describes an object at the current path.
|
|
199
|
+
*
|
|
200
|
+
* `SchemaWalker` navigates on `type === 'object' || properties`, so implicit-object
|
|
201
|
+
* detection has to use the same rule: a level that declares `properties` (or a
|
|
202
|
+
* `type: ['object', ...]` union) is an object even when it never spells out `type`.
|
|
203
|
+
*/
|
|
204
|
+
private static declaresObject;
|
|
109
205
|
/**
|
|
110
206
|
* Parses a JSON object, handling both standard and implicit (no braces) structures.
|
|
111
207
|
*/
|
|
112
208
|
private consumeObject;
|
|
209
|
+
/**
|
|
210
|
+
* Whether a property belongs in the output, according to `RepairOptions.additionalProperties`.
|
|
211
|
+
*
|
|
212
|
+
* `'remove'` only drops keys at a level whose Schema declares property names: a level
|
|
213
|
+
* that is free-form (`type: 'object'` without `properties`, or no Schema at all) has no
|
|
214
|
+
* notion of an "extra" key, so dropping everything there would be destructive.
|
|
215
|
+
*/
|
|
216
|
+
private isKeptProperty;
|
|
217
|
+
/**
|
|
218
|
+
* Assigns an own enumerable property without going through setters.
|
|
219
|
+
*
|
|
220
|
+
* `obj[key] = value` with a key such as `__proto__` rewrites the prototype of the result
|
|
221
|
+
* instead of storing a value (`JSON.parse` keeps it as a plain data property), which both
|
|
222
|
+
* loses the key and leaks inherited properties into the repaired document.
|
|
223
|
+
*/
|
|
224
|
+
private static setOwn;
|
|
113
225
|
/**
|
|
114
226
|
* Parses a JSON array.
|
|
115
227
|
*/
|
|
116
228
|
private consumeArray;
|
|
117
229
|
/**
|
|
118
230
|
* Consumes a property key, handling both quoted and unquoted keys.
|
|
231
|
+
*
|
|
232
|
+
* A quoted key keeps its exact content, including whitespace and punctuation
|
|
233
|
+
* (`"Official language"`); a bare key stops at quote characters so that a malformed
|
|
234
|
+
* document can never produce keys such as `language"`.
|
|
235
|
+
*
|
|
236
|
+
* A bare key that the Schema declares verbatim at `path` wins over the token scan, so a
|
|
237
|
+
* multi-word declaration (`Official language`) is read as one key even when the document
|
|
238
|
+
* left it unquoted (see {@link RepairParser.matchDeclaredKey}).
|
|
239
|
+
*
|
|
240
|
+
* @param path - The parent path the key belongs to; `pathStack` (the key is pushed later).
|
|
241
|
+
* @returns the key, or `null` when no key is present at all (an empty quoted key `""`
|
|
242
|
+
* is a valid key and returns the empty string).
|
|
119
243
|
*/
|
|
120
244
|
private consumeKey;
|
|
121
245
|
/**
|
|
122
|
-
* Captures content greedily until it encounters a delimiter that marks the start of the
|
|
123
|
-
*
|
|
246
|
+
* Captures content greedily until it encounters a delimiter that marks the start of the
|
|
247
|
+
* next property or the end of a structure. This is the fallback used when the strict JSON
|
|
248
|
+
* path failed, so the boundary test runs in `'greedy'` mode: when the Schema declares
|
|
249
|
+
* property names for this level, capture stops only at a declared key (see
|
|
250
|
+
* {@link RepairParser.isBoundaryAhead}).
|
|
251
|
+
*
|
|
252
|
+
* It also tracks balanced delimiters (parentheses, brackets, braces) and quotes, so that
|
|
253
|
+
* structural markers inside DSL-like strings (e.g. `@weather(location="SH, CN")`) stay
|
|
254
|
+
* part of the captured value, and implements the "Parity Rule" for quote stripping.
|
|
255
|
+
*/
|
|
256
|
+
private consumeGreedyString;
|
|
257
|
+
/**
|
|
258
|
+
* Characters that make the text before a candidate key part of a longer word.
|
|
259
|
+
*/
|
|
260
|
+
private static readonly WORD_CHAR;
|
|
261
|
+
/**
|
|
262
|
+
* Compiled "declared key literally here" patterns, shared across instances.
|
|
263
|
+
* Keyed by `fuzzy` + name because the `i` flag depends on `keyMatching`.
|
|
124
264
|
*/
|
|
265
|
+
private static readonly declaredKeyPatterns;
|
|
125
266
|
/**
|
|
126
|
-
*
|
|
267
|
+
* Memo of the last path the multi-word declarations were collected for.
|
|
268
|
+
* Greedy capture probes every character of a value while the path stays the same, so this
|
|
269
|
+
* avoids re-walking the Schema (and re-keying a Map) thousands of times per document.
|
|
270
|
+
*/
|
|
271
|
+
private cachedPath;
|
|
272
|
+
private cachedDeclaredKeys;
|
|
273
|
+
/**
|
|
274
|
+
* The property names declared at `path` that contain whitespace, with a pattern that matches
|
|
275
|
+
* them literally: the name escaped, internal whitespace runs relaxed to `\s+`, and a `:`
|
|
276
|
+
* required after it. Single-word names are deliberately excluded - the generic key-shaped
|
|
277
|
+
* regex of {@link RepairParser.matchKey} already covers them.
|
|
278
|
+
*/
|
|
279
|
+
private declaredMultiWordKeys;
|
|
280
|
+
/**
|
|
281
|
+
* Matches a Schema-declared property name that contains whitespace at `startPos`, literally
|
|
282
|
+
* (case as declared, unless `keyMatching` folds case).
|
|
127
283
|
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
284
|
+
* The generic key scanner cannot help here: its bare-key branch excludes whitespace, so an
|
|
285
|
+
* unquoted `Official language:` in the document is never seen as one key. Declaring the name
|
|
286
|
+
* in the Schema supplies the missing information, and this method turns it into both a
|
|
287
|
+
* breakpoint for greedy capture and a readable key. A candidate glued to a longer word
|
|
288
|
+
* (`de-factoOfficial language:`) is rejected, so the breakpoint stays anchored on real keys.
|
|
131
289
|
*
|
|
132
|
-
*
|
|
133
|
-
* consuming the rest of the JSON.
|
|
290
|
+
* @returns the declared name and the amount of text it covers, or `null`.
|
|
134
291
|
*/
|
|
135
|
-
private
|
|
292
|
+
private matchDeclaredKey;
|
|
136
293
|
/**
|
|
137
294
|
* Probes whether the current position marks a structural delimiter (end or next property).
|
|
138
295
|
*/
|
|
@@ -141,15 +298,45 @@ declare class RepairParser {
|
|
|
141
298
|
* Checks if the upcoming text looks like a property key defined in the Schema at the given path.
|
|
142
299
|
*/
|
|
143
300
|
private isLookingAtKey;
|
|
301
|
+
/**
|
|
302
|
+
* Matches a property key at `startPos`, including quoted keys that contain
|
|
303
|
+
* whitespace/commas/colons (`"Official language":`), and returns the decoded key.
|
|
304
|
+
*/
|
|
305
|
+
private matchKey;
|
|
306
|
+
/**
|
|
307
|
+
* Is a property boundary (a key followed by ':') ahead of `startPos`?
|
|
308
|
+
*
|
|
309
|
+
* - 'structural': trust any key-shaped token. Used right after a value was parsed by the
|
|
310
|
+
* strict JSON path, so a successful parse is never thrown away.
|
|
311
|
+
* - 'greedy': when the Schema declares property names at this level, stop only at a
|
|
312
|
+
* declared key; otherwise text that merely looks like `"foo bar":` would cut the value.
|
|
313
|
+
*
|
|
314
|
+
* A whitespace-containing name declared by the Schema is always a breakpoint, quoted or not,
|
|
315
|
+
* because it can only be recognised literally (see {@link RepairParser.matchDeclaredKey});
|
|
316
|
+
* in greedy mode other keys count as breakpoints through `keyMatching`, so a declared key
|
|
317
|
+
* written as `user_name` still breaks a value when the Schema declares `userName`.
|
|
318
|
+
*/
|
|
319
|
+
private isBoundaryAhead;
|
|
144
320
|
/**
|
|
145
321
|
* Performs type coercion and semantic repair based on the Schema.
|
|
146
322
|
* Handles fuzzy enum matching, noisy number extraction, and boolean variants.
|
|
323
|
+
*
|
|
324
|
+
* @param raw - The exact text the strict JSON path consumed for `val`, when it came from
|
|
325
|
+
* there. Coercing to `string` uses it so that `{ zip: 01234 }` yields `'01234'`.
|
|
147
326
|
*/
|
|
148
327
|
private coerceValue;
|
|
149
328
|
/**
|
|
150
329
|
* Consumes a standard JSON string.
|
|
151
330
|
*/
|
|
152
331
|
private consumeString;
|
|
332
|
+
/**
|
|
333
|
+
* Matches a JSON number literal at the current position.
|
|
334
|
+
*
|
|
335
|
+
* Scientific notation (`1e5`, `-2E2`) and a leading `+` are accepted; the match always
|
|
336
|
+
* stops at the end of a well-formed literal so that leftovers such as `1.2.3` remain
|
|
337
|
+
* available for greedy capture instead of being silently truncated.
|
|
338
|
+
*/
|
|
339
|
+
private static readonly NUMBER_PATTERN;
|
|
153
340
|
/**
|
|
154
341
|
* Consumes a standard JSON number.
|
|
155
342
|
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -3,31 +3,64 @@
|
|
|
3
3
|
*/
|
|
4
4
|
interface RepairOptions {
|
|
5
5
|
/**
|
|
6
|
-
* Fuzzy matching
|
|
6
|
+
* Fuzzy matching of property keys against the Schema's declared `properties`.
|
|
7
7
|
*
|
|
8
|
-
* - `false` (default):
|
|
9
|
-
* - `true`:
|
|
10
|
-
* -
|
|
8
|
+
* - `false` (default): Only an exactly declared key counts as a match.
|
|
9
|
+
* - `true`: Case-insensitive and separator-insensitive matching, so `user_name`,
|
|
10
|
+
* `userName`, `USER-NAME` and `user name` all resolve to the declared key.
|
|
11
|
+
* - `(key: string, properties: string[]) => string | undefined`: Custom mapping logic;
|
|
12
|
+
* it receives the incoming key and the declared names, and returns the name to use
|
|
13
|
+
* (or `undefined` to fall back to the incoming key).
|
|
14
|
+
*
|
|
15
|
+
* A match renames the key to the Schema's name in the output and makes nested Schema
|
|
16
|
+
* lookups (and therefore type coercion) work for it.
|
|
17
|
+
* It also decides where greedy capture stops: with `true` a separator or case variant of a
|
|
18
|
+
* declared key (`user_name:` for `userName`) ends the value in front of it.
|
|
19
|
+
* When you reuse a `SchemaWalker`, pass this option to `SchemaWalker.create()`.
|
|
11
20
|
*/
|
|
12
21
|
keyMatching?: boolean | ((key: string, properties: string[]) => string | undefined);
|
|
13
22
|
/**
|
|
14
|
-
* Strategy for greedy string capture when
|
|
23
|
+
* Strategy for greedy string capture when the strict JSON path failed for a value.
|
|
24
|
+
*
|
|
25
|
+
* - `'aggressive'` (default): When the Schema declares property names for the current
|
|
26
|
+
* level, the captured value only ends at one of those declared keys. Text that merely
|
|
27
|
+
* looks like `"some words":` stays inside the value.
|
|
28
|
+
* - `'conservative'`: Any text matching the `"key":` / `key:` pattern ends the value,
|
|
29
|
+
* which keeps the structure of the document at the cost of cutting values short.
|
|
15
30
|
*
|
|
16
|
-
* -
|
|
17
|
-
*
|
|
31
|
+
* A whitespace-containing key the Schema declares is a breakpoint in both strategies even
|
|
32
|
+
* when the document left it unquoted (`{ Capital: Berlin, Official language: Italian }`),
|
|
33
|
+
* because it can only be recognised from the declaration.
|
|
34
|
+
* Neither strategy affects values that were parsed by the strict JSON path; the
|
|
35
|
+
* boundary rules described in the README apply to those unchanged.
|
|
18
36
|
*/
|
|
19
37
|
stringCaptureStrategy?: 'conservative' | 'aggressive';
|
|
20
38
|
/**
|
|
21
|
-
* Whether to
|
|
39
|
+
* Whether to coerce values to the type the Schema declares. Enabled by default.
|
|
40
|
+
*
|
|
41
|
+
* - `true` (default): `"Approx. 1,200.50 USD"` becomes `1200.5`, `"yes"` becomes `true`,
|
|
42
|
+
* `"Processing!"` becomes the enum entry it fuzzy matches, `12` becomes `"12"` when the
|
|
43
|
+
* Schema says `type: 'string'`.
|
|
44
|
+
* - `false`: values are returned exactly as they were parsed.
|
|
45
|
+
*
|
|
46
|
+
* Coercion needs the Schema of the property the value belongs to; a level that declares
|
|
47
|
+
* no types is left alone.
|
|
22
48
|
*
|
|
23
|
-
*
|
|
49
|
+
* Coercing to a declared `string` reuses the text the document wrote, not the number it
|
|
50
|
+
* parsed to, so a leading zero survives: `{ id: 01234 }` becomes `"01234"`.
|
|
51
|
+
* `null` and structures (`{}` / `[]`) are never stringified - `"null"` and
|
|
52
|
+
* `"[object Object]"` are not what a `type: 'string'` declaration asks for.
|
|
24
53
|
*/
|
|
25
54
|
coerceTypes?: boolean;
|
|
26
55
|
/**
|
|
27
56
|
* Defines how to handle properties not defined in the Schema.
|
|
28
57
|
*
|
|
29
58
|
* - `'keep'` (default): Keeps these extra properties in the output.
|
|
30
|
-
* - `'remove'`:
|
|
59
|
+
* - `'remove'`: Drops them. Only applies at a level whose Schema declares `properties`;
|
|
60
|
+
* a free-form level has no "extra" keys and is left untouched.
|
|
61
|
+
*
|
|
62
|
+
* This option is independent from the JSON Schema keyword `additionalProperties: false`,
|
|
63
|
+
* which is respected on its own when validating incoming keys.
|
|
31
64
|
*/
|
|
32
65
|
additionalProperties?: 'keep' | 'remove';
|
|
33
66
|
}
|
|
@@ -43,15 +76,20 @@ type JSONPath = (string | number)[];
|
|
|
43
76
|
*/
|
|
44
77
|
declare class SchemaWalker {
|
|
45
78
|
private dereferencedSchema;
|
|
79
|
+
private options;
|
|
46
80
|
private constructor();
|
|
47
81
|
/**
|
|
48
82
|
* Static factory method to create a SchemaWalker instance.
|
|
49
83
|
* Asynchronously dereferences the provided JSON Schema.
|
|
50
84
|
*
|
|
85
|
+
* The Schema is dereferenced on a deep copy: `$RefParser.dereference()` replaces every
|
|
86
|
+
* `$ref` in place, which would otherwise rewrite the object the caller passed in.
|
|
87
|
+
*
|
|
51
88
|
* @param schema - The JSON Schema object to walk.
|
|
89
|
+
* @param options - Repair options that concern the Schema itself (`keyMatching`).
|
|
52
90
|
* @returns A promise that resolves to a SchemaWalker instance.
|
|
53
91
|
*/
|
|
54
|
-
static create(schema: any): Promise<SchemaWalker>;
|
|
92
|
+
static create(schema: any, options?: RepairOptions): Promise<SchemaWalker>;
|
|
55
93
|
/**
|
|
56
94
|
* Retrieves the sub-schema for a given JSON path.
|
|
57
95
|
*
|
|
@@ -67,6 +105,46 @@ declare class SchemaWalker {
|
|
|
67
105
|
* @returns True if the property is defined in the Schema or allowed by additionalProperties.
|
|
68
106
|
*/
|
|
69
107
|
isValidProperty(path: JSONPath, key: string): boolean;
|
|
108
|
+
/**
|
|
109
|
+
* Resolves an incoming property name to the name declared by the Schema at `path`.
|
|
110
|
+
*
|
|
111
|
+
* Without `keyMatching` this only accepts an exact declaration. With `keyMatching: true`
|
|
112
|
+
* the comparison ignores case and the separators used by the two common naming styles
|
|
113
|
+
* (`user_name` / `userName` / `USER NAME`). A function matcher decides itself and may
|
|
114
|
+
* return a name the Schema does not declare.
|
|
115
|
+
*
|
|
116
|
+
* Besides renaming, the result is what makes a key a breakpoint for greedy string capture:
|
|
117
|
+
* the parser calls this while deciding whether an upcoming `key:` ends the value in front of it.
|
|
118
|
+
*
|
|
119
|
+
* @param path - The current parent path.
|
|
120
|
+
* @param key - The property key found in the broken document.
|
|
121
|
+
* @returns The canonical Schema property name, or `null` when nothing matches.
|
|
122
|
+
*/
|
|
123
|
+
matchProperty(path: JSONPath, key: string): string | null;
|
|
124
|
+
/**
|
|
125
|
+
* The property names the Schema declares at `path` (empty when the level is free-form).
|
|
126
|
+
*/
|
|
127
|
+
declaredPropertyNames(path: JSONPath): string[];
|
|
128
|
+
/**
|
|
129
|
+
* Folding used for fuzzy key matching: case-insensitive and separator-insensitive, so
|
|
130
|
+
* snake_case, kebab-case, camelCase and spaced keys collide.
|
|
131
|
+
*/
|
|
132
|
+
private static normalizeName;
|
|
133
|
+
/**
|
|
134
|
+
* Whether a property name is explicitly declared in the Schema at the given path.
|
|
135
|
+
*
|
|
136
|
+
* @param path - The current parent path.
|
|
137
|
+
* @param key - The property key to check.
|
|
138
|
+
* @returns True only when `key` appears in the Schema `properties` of that path.
|
|
139
|
+
*/
|
|
140
|
+
isDeclaredProperty(path: JSONPath, key: string): boolean;
|
|
141
|
+
/**
|
|
142
|
+
* Whether the Schema at the given path declares any property names at all.
|
|
143
|
+
* Used to decide whether key matching can be strict or has to stay lenient.
|
|
144
|
+
*
|
|
145
|
+
* @param path - The current parent path.
|
|
146
|
+
*/
|
|
147
|
+
hasDeclaredProperties(path: JSONPath): boolean;
|
|
70
148
|
/**
|
|
71
149
|
* Gets the expected types for a specific property at a given path.
|
|
72
150
|
*
|
|
@@ -106,33 +184,112 @@ declare class RepairParser {
|
|
|
106
184
|
* Uses Schema guidance to decide whether to use standard parsing or greedy capture.
|
|
107
185
|
*/
|
|
108
186
|
private consumeValue;
|
|
187
|
+
/**
|
|
188
|
+
* Attempts a strict JSON scalar parse (string / number / boolean / null) at the current
|
|
189
|
+
* position and rewinds when the token is not followed by a boundary.
|
|
190
|
+
*
|
|
191
|
+
* The boundary test runs in `'structural'` mode: a cleanly parsed value is trusted, so
|
|
192
|
+
* anything that looks like the next key is treated as structure and the successful parse
|
|
193
|
+
* is never discarded in favour of greedy capture. This is what guarantees that valid JSON
|
|
194
|
+
* always parses exactly like `JSON.parse`.
|
|
195
|
+
*/
|
|
196
|
+
private tryConsumeStrictValue;
|
|
197
|
+
/**
|
|
198
|
+
* Whether the Schema describes an object at the current path.
|
|
199
|
+
*
|
|
200
|
+
* `SchemaWalker` navigates on `type === 'object' || properties`, so implicit-object
|
|
201
|
+
* detection has to use the same rule: a level that declares `properties` (or a
|
|
202
|
+
* `type: ['object', ...]` union) is an object even when it never spells out `type`.
|
|
203
|
+
*/
|
|
204
|
+
private static declaresObject;
|
|
109
205
|
/**
|
|
110
206
|
* Parses a JSON object, handling both standard and implicit (no braces) structures.
|
|
111
207
|
*/
|
|
112
208
|
private consumeObject;
|
|
209
|
+
/**
|
|
210
|
+
* Whether a property belongs in the output, according to `RepairOptions.additionalProperties`.
|
|
211
|
+
*
|
|
212
|
+
* `'remove'` only drops keys at a level whose Schema declares property names: a level
|
|
213
|
+
* that is free-form (`type: 'object'` without `properties`, or no Schema at all) has no
|
|
214
|
+
* notion of an "extra" key, so dropping everything there would be destructive.
|
|
215
|
+
*/
|
|
216
|
+
private isKeptProperty;
|
|
217
|
+
/**
|
|
218
|
+
* Assigns an own enumerable property without going through setters.
|
|
219
|
+
*
|
|
220
|
+
* `obj[key] = value` with a key such as `__proto__` rewrites the prototype of the result
|
|
221
|
+
* instead of storing a value (`JSON.parse` keeps it as a plain data property), which both
|
|
222
|
+
* loses the key and leaks inherited properties into the repaired document.
|
|
223
|
+
*/
|
|
224
|
+
private static setOwn;
|
|
113
225
|
/**
|
|
114
226
|
* Parses a JSON array.
|
|
115
227
|
*/
|
|
116
228
|
private consumeArray;
|
|
117
229
|
/**
|
|
118
230
|
* Consumes a property key, handling both quoted and unquoted keys.
|
|
231
|
+
*
|
|
232
|
+
* A quoted key keeps its exact content, including whitespace and punctuation
|
|
233
|
+
* (`"Official language"`); a bare key stops at quote characters so that a malformed
|
|
234
|
+
* document can never produce keys such as `language"`.
|
|
235
|
+
*
|
|
236
|
+
* A bare key that the Schema declares verbatim at `path` wins over the token scan, so a
|
|
237
|
+
* multi-word declaration (`Official language`) is read as one key even when the document
|
|
238
|
+
* left it unquoted (see {@link RepairParser.matchDeclaredKey}).
|
|
239
|
+
*
|
|
240
|
+
* @param path - The parent path the key belongs to; `pathStack` (the key is pushed later).
|
|
241
|
+
* @returns the key, or `null` when no key is present at all (an empty quoted key `""`
|
|
242
|
+
* is a valid key and returns the empty string).
|
|
119
243
|
*/
|
|
120
244
|
private consumeKey;
|
|
121
245
|
/**
|
|
122
|
-
* Captures content greedily until it encounters a delimiter that marks the start of the
|
|
123
|
-
*
|
|
246
|
+
* Captures content greedily until it encounters a delimiter that marks the start of the
|
|
247
|
+
* next property or the end of a structure. This is the fallback used when the strict JSON
|
|
248
|
+
* path failed, so the boundary test runs in `'greedy'` mode: when the Schema declares
|
|
249
|
+
* property names for this level, capture stops only at a declared key (see
|
|
250
|
+
* {@link RepairParser.isBoundaryAhead}).
|
|
251
|
+
*
|
|
252
|
+
* It also tracks balanced delimiters (parentheses, brackets, braces) and quotes, so that
|
|
253
|
+
* structural markers inside DSL-like strings (e.g. `@weather(location="SH, CN")`) stay
|
|
254
|
+
* part of the captured value, and implements the "Parity Rule" for quote stripping.
|
|
255
|
+
*/
|
|
256
|
+
private consumeGreedyString;
|
|
257
|
+
/**
|
|
258
|
+
* Characters that make the text before a candidate key part of a longer word.
|
|
259
|
+
*/
|
|
260
|
+
private static readonly WORD_CHAR;
|
|
261
|
+
/**
|
|
262
|
+
* Compiled "declared key literally here" patterns, shared across instances.
|
|
263
|
+
* Keyed by `fuzzy` + name because the `i` flag depends on `keyMatching`.
|
|
124
264
|
*/
|
|
265
|
+
private static readonly declaredKeyPatterns;
|
|
125
266
|
/**
|
|
126
|
-
*
|
|
267
|
+
* Memo of the last path the multi-word declarations were collected for.
|
|
268
|
+
* Greedy capture probes every character of a value while the path stays the same, so this
|
|
269
|
+
* avoids re-walking the Schema (and re-keying a Map) thousands of times per document.
|
|
270
|
+
*/
|
|
271
|
+
private cachedPath;
|
|
272
|
+
private cachedDeclaredKeys;
|
|
273
|
+
/**
|
|
274
|
+
* The property names declared at `path` that contain whitespace, with a pattern that matches
|
|
275
|
+
* them literally: the name escaped, internal whitespace runs relaxed to `\s+`, and a `:`
|
|
276
|
+
* required after it. Single-word names are deliberately excluded - the generic key-shaped
|
|
277
|
+
* regex of {@link RepairParser.matchKey} already covers them.
|
|
278
|
+
*/
|
|
279
|
+
private declaredMultiWordKeys;
|
|
280
|
+
/**
|
|
281
|
+
* Matches a Schema-declared property name that contains whitespace at `startPos`, literally
|
|
282
|
+
* (case as declared, unless `keyMatching` folds case).
|
|
127
283
|
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
284
|
+
* The generic key scanner cannot help here: its bare-key branch excludes whitespace, so an
|
|
285
|
+
* unquoted `Official language:` in the document is never seen as one key. Declaring the name
|
|
286
|
+
* in the Schema supplies the missing information, and this method turns it into both a
|
|
287
|
+
* breakpoint for greedy capture and a readable key. A candidate glued to a longer word
|
|
288
|
+
* (`de-factoOfficial language:`) is rejected, so the breakpoint stays anchored on real keys.
|
|
131
289
|
*
|
|
132
|
-
*
|
|
133
|
-
* consuming the rest of the JSON.
|
|
290
|
+
* @returns the declared name and the amount of text it covers, or `null`.
|
|
134
291
|
*/
|
|
135
|
-
private
|
|
292
|
+
private matchDeclaredKey;
|
|
136
293
|
/**
|
|
137
294
|
* Probes whether the current position marks a structural delimiter (end or next property).
|
|
138
295
|
*/
|
|
@@ -141,15 +298,45 @@ declare class RepairParser {
|
|
|
141
298
|
* Checks if the upcoming text looks like a property key defined in the Schema at the given path.
|
|
142
299
|
*/
|
|
143
300
|
private isLookingAtKey;
|
|
301
|
+
/**
|
|
302
|
+
* Matches a property key at `startPos`, including quoted keys that contain
|
|
303
|
+
* whitespace/commas/colons (`"Official language":`), and returns the decoded key.
|
|
304
|
+
*/
|
|
305
|
+
private matchKey;
|
|
306
|
+
/**
|
|
307
|
+
* Is a property boundary (a key followed by ':') ahead of `startPos`?
|
|
308
|
+
*
|
|
309
|
+
* - 'structural': trust any key-shaped token. Used right after a value was parsed by the
|
|
310
|
+
* strict JSON path, so a successful parse is never thrown away.
|
|
311
|
+
* - 'greedy': when the Schema declares property names at this level, stop only at a
|
|
312
|
+
* declared key; otherwise text that merely looks like `"foo bar":` would cut the value.
|
|
313
|
+
*
|
|
314
|
+
* A whitespace-containing name declared by the Schema is always a breakpoint, quoted or not,
|
|
315
|
+
* because it can only be recognised literally (see {@link RepairParser.matchDeclaredKey});
|
|
316
|
+
* in greedy mode other keys count as breakpoints through `keyMatching`, so a declared key
|
|
317
|
+
* written as `user_name` still breaks a value when the Schema declares `userName`.
|
|
318
|
+
*/
|
|
319
|
+
private isBoundaryAhead;
|
|
144
320
|
/**
|
|
145
321
|
* Performs type coercion and semantic repair based on the Schema.
|
|
146
322
|
* Handles fuzzy enum matching, noisy number extraction, and boolean variants.
|
|
323
|
+
*
|
|
324
|
+
* @param raw - The exact text the strict JSON path consumed for `val`, when it came from
|
|
325
|
+
* there. Coercing to `string` uses it so that `{ zip: 01234 }` yields `'01234'`.
|
|
147
326
|
*/
|
|
148
327
|
private coerceValue;
|
|
149
328
|
/**
|
|
150
329
|
* Consumes a standard JSON string.
|
|
151
330
|
*/
|
|
152
331
|
private consumeString;
|
|
332
|
+
/**
|
|
333
|
+
* Matches a JSON number literal at the current position.
|
|
334
|
+
*
|
|
335
|
+
* Scientific notation (`1e5`, `-2E2`) and a leading `+` are accepted; the match always
|
|
336
|
+
* stops at the end of a well-formed literal so that leftovers such as `1.2.3` remain
|
|
337
|
+
* available for greedy capture instead of being silently truncated.
|
|
338
|
+
*/
|
|
339
|
+
private static readonly NUMBER_PATTERN;
|
|
153
340
|
/**
|
|
154
341
|
* Consumes a standard JSON number.
|
|
155
342
|
*/
|