@isdk/json-repair 0.2.0 → 0.3.0
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/CHANGELOG.md +90 -0
- package/README.cn.md +24 -9
- package/README.md +40 -12
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +269 -49
- package/dist/index.d.ts +269 -49
- package/dist/index.js +1 -1
- package/package.json +6 -5
- package/docs/README.md +0 -387
- package/docs/_media/README.cn.md +0 -333
- package/docs/classes/RepairParser.md +0 -130
- package/docs/classes/SchemaWalker.md +0 -241
- package/docs/functions/compareScores.md +0 -32
- package/docs/functions/discardedText.md +0 -42
- package/docs/functions/expectsArrayRoot.md +0 -23
- package/docs/functions/findImplicitRecordRegion.md +0 -32
- package/docs/functions/jsonRepair.md +0 -49
- package/docs/functions/jsonRepairResult.md +0 -50
- package/docs/functions/locateJsonCandidates.md +0 -33
- package/docs/functions/looksLikeSchemaEcho.md +0 -29
- package/docs/functions/renderValidatorErrors.md +0 -32
- package/docs/functions/scoreCandidate.md +0 -45
- package/docs/functions/validateValue.md +0 -38
- package/docs/globals.md +0 -43
- package/docs/interfaces/CandidateScore.md +0 -63
- package/docs/interfaces/JsonCandidate.md +0 -71
- package/docs/interfaces/LocatedJson.md +0 -48
- package/docs/interfaces/RepairOptions.md +0 -301
- package/docs/interfaces/RepairResult.md +0 -58
- package/docs/interfaces/ValidationError.md +0 -27
- package/docs/type-aliases/CommentStyle.md +0 -13
- package/docs/type-aliases/EscapeMode.md +0 -13
- package/docs/type-aliases/JSONPath.md +0 -14
- package/docs/type-aliases/LiteralDialect.md +0 -13
- package/docs/type-aliases/RepairFlag.md +0 -11
package/dist/index.d.cts
CHANGED
|
@@ -3,22 +3,26 @@
|
|
|
3
3
|
*/
|
|
4
4
|
interface RepairOptions {
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Which spellings of a declared property name the document may use.
|
|
7
7
|
*
|
|
8
8
|
* - `false` (default): Only an exactly declared key counts as a match.
|
|
9
|
-
* - `true`:
|
|
10
|
-
* `
|
|
9
|
+
* - `true`: every form on - case- and separator-insensitive, so `user_name`, `userName`,
|
|
10
|
+
* `USER-NAME` and `user name` all resolve to the declared key.
|
|
11
|
+
* - `KeyMatchSpec`: names the forms one by one (`{ forms: ['case'] }`), the separator
|
|
12
|
+
* characters, and the alternative names the Schema declares under another keyword
|
|
13
|
+
* (`aliases`) - a caller that owns its own key grammar passes that grammar here rather
|
|
14
|
+
* than inheriting this library's.
|
|
11
15
|
* - `(key: string, properties: string[]) => string | undefined`: Custom mapping logic;
|
|
12
16
|
* it receives the incoming key and the declared names, and returns the name to use
|
|
13
17
|
* (or `undefined` to fall back to the incoming key).
|
|
14
18
|
*
|
|
15
19
|
* A match renames the key to the Schema's name in the output and makes nested Schema
|
|
16
20
|
* lookups (and therefore type coercion) work for it.
|
|
17
|
-
* It also decides where greedy capture stops:
|
|
18
|
-
*
|
|
21
|
+
* It also decides where greedy capture stops: a selected form makes the variant of a declared
|
|
22
|
+
* key (`user_name:` for `userName`) end the value in front of it.
|
|
19
23
|
* When you reuse a `SchemaWalker`, pass this option to `SchemaWalker.create()`.
|
|
20
24
|
*/
|
|
21
|
-
keyMatching?: boolean | ((key: string, properties: string[]) => string | undefined);
|
|
25
|
+
keyMatching?: boolean | KeyMatchSpec | ((key: string, properties: string[]) => string | undefined);
|
|
22
26
|
/**
|
|
23
27
|
* Strategy for greedy string capture when the strict JSON path failed for a value.
|
|
24
28
|
*
|
|
@@ -59,16 +63,24 @@ interface RepairOptions {
|
|
|
59
63
|
*/
|
|
60
64
|
coerceTypes?: boolean;
|
|
61
65
|
/**
|
|
62
|
-
*
|
|
66
|
+
* The JSON Schema keyword `additionalProperties`, given as the default for every level that
|
|
67
|
+
* does not state it itself. One keyword, one rule: a Schema that writes
|
|
68
|
+
* `additionalProperties: false` on a level needs no option for that level to obey it, and a
|
|
69
|
+
* level that writes its own outranks the default here.
|
|
63
70
|
*
|
|
64
|
-
* - `
|
|
65
|
-
*
|
|
66
|
-
*
|
|
71
|
+
* - `true` (the default, and what JSON Schema assumes): keys the level does not declare are
|
|
72
|
+
* kept in the output.
|
|
73
|
+
* - `false`: such keys are dropped, listed in {@link RepairResult.discarded} and flagged
|
|
74
|
+
* `undeclared-keys-dropped`, so the value fits the Schema without content going missing.
|
|
75
|
+
* - a Schema (e.g. `{ type: 'string' }`): such keys are kept, and their values are coerced
|
|
76
|
+
* and validated against that Schema - the way to describe a free-form map (`labels`,
|
|
77
|
+
* `counts`) once instead of at every level.
|
|
67
78
|
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
79
|
+
* Dropping applies only at a level that declares `properties`. A level that declares none
|
|
80
|
+
* (`type: 'object'` alone, or no Schema at all) is free-form: it has no "extra" keys, and
|
|
81
|
+
* emptying it would destroy the answer rather than tighten it.
|
|
70
82
|
*/
|
|
71
|
-
additionalProperties?:
|
|
83
|
+
additionalProperties?: boolean | object;
|
|
72
84
|
/**
|
|
73
85
|
* Where a quoted value ends when the model left one of its quotes unescaped -
|
|
74
86
|
* `{"t": "The "Best" Paper", "y": 2020}`.
|
|
@@ -159,8 +171,10 @@ interface RepairOptions {
|
|
|
159
171
|
* exists to prevent. Dropping is the default, and the dropped text is listed in
|
|
160
172
|
* {@link RepairResult.discarded} and flagged `dangling-dropped`, so nothing disappears
|
|
161
173
|
* without a trace. Only the fragment is dropped: the container closes and a record written
|
|
162
|
-
* after it is still read, so `{"a": 1, "b"} {"c": 2}`
|
|
163
|
-
*
|
|
174
|
+
* after it is still read, so under a Schema that expects a list `{"a": 1, "b"} {"c": 2}`
|
|
175
|
+
* becomes `[{ a: 1 }, { c: 2 }]`. Without that declaration the two records are scored as
|
|
176
|
+
* competing fragments and the loser is listed in {@link RepairResult.discarded}.
|
|
177
|
+
* A broken key with a value after it is syntax to repair, not a fragment, and is left alone.
|
|
164
178
|
*/
|
|
165
179
|
dangling?: boolean;
|
|
166
180
|
/**
|
|
@@ -229,7 +243,8 @@ type JSONPath = (string | number)[];
|
|
|
229
243
|
* - Input shape: `markdown-fence`, `prose-prefix`, `trailing-chatter`,
|
|
230
244
|
* `implicit-structure`, `truncated-candidate`
|
|
231
245
|
* - Decisions taken: `sibling-objects-merged`, `extra-fragments-dropped`,
|
|
232
|
-
* `duplicate-candidates`, `ambiguous-candidates`, `
|
|
246
|
+
* `duplicate-candidates`, `ambiguous-candidates`, `undeclared-keys-dropped` (a key the
|
|
247
|
+
* level's `additionalProperties` forbids), `coerced-text-dropped` (a coercion gave up
|
|
233
248
|
* the text around the value it kept)
|
|
234
249
|
* - Content suspicions: `empty-required` (a `required` string field satisfied by `""` -
|
|
235
250
|
* schema-valid and information-free), `schema-echo-detected` (the model reproduced the
|
|
@@ -237,6 +252,35 @@ type JSONPath = (string | number)[];
|
|
|
237
252
|
* - Limits reached: `nesting-limited` (a document deeper than the parser reads, with the text
|
|
238
253
|
* it never reached listed in `discarded`)
|
|
239
254
|
*/
|
|
255
|
+
/**
|
|
256
|
+
* A difference in how a declared property name is spelled that `keyMatching` may be told to
|
|
257
|
+
* ignore. Named one at a time because a caller that owns a key grammar buys these separately:
|
|
258
|
+
* a minimal contract may accept the Schema's display name while keeping case and separators
|
|
259
|
+
* byte-exact, and `true` cannot say that.
|
|
260
|
+
*
|
|
261
|
+
* - `case`: `USER-NAME` is `user-name`.
|
|
262
|
+
* - `separators`: the written boundary between words is free, so `user_name`, `user-name` and
|
|
263
|
+
* `user name` are one name. camelCase writes that boundary as a letter-case change, so
|
|
264
|
+
* `userName` only collides with them when `case` is selected too - a boundary written as a
|
|
265
|
+
* case difference is invisible to a case-sensitive comparison.
|
|
266
|
+
*/
|
|
267
|
+
type KeyMatchForm = 'case' | 'separators';
|
|
268
|
+
/**
|
|
269
|
+
* `keyMatching` spelled out: the forms in force, which characters count as a separator, and
|
|
270
|
+
* the alternative names the Schema declares under keywords this library does not read.
|
|
271
|
+
*/
|
|
272
|
+
interface KeyMatchSpec {
|
|
273
|
+
/** Which spelling differences are not differences. `true` takes every form, `false`/`[]` none. */
|
|
274
|
+
forms?: boolean | readonly KeyMatchForm[];
|
|
275
|
+
/**
|
|
276
|
+
* The characters that count as a word separator when the `separators` form is on.
|
|
277
|
+
* Default: {@link KEY_SEPARATORS}. A caller with its own separator set passes it, so the
|
|
278
|
+
* same bytes resolve the same way in every channel it reads.
|
|
279
|
+
*/
|
|
280
|
+
separators?: readonly string[];
|
|
281
|
+
/** Canonical declared property name -> other names the document may use for it. */
|
|
282
|
+
aliases?: Record<string, readonly string[]>;
|
|
283
|
+
}
|
|
240
284
|
/** A comment style. See {@link RepairOptions.comments}. */
|
|
241
285
|
type CommentStyle = 'line' | 'block' | 'hash';
|
|
242
286
|
/** A language whose bare-token literals a model may write. See {@link RepairOptions.literals}. */
|
|
@@ -264,6 +308,11 @@ type RepairFlag = 'markdown-fence' | 'prose-prefix' | 'trailing-chatter' | 'impl
|
|
|
264
308
|
| 'invalid-escape'
|
|
265
309
|
/** A key the output never finished was dropped; its text is in `discarded`. */
|
|
266
310
|
| 'dangling-dropped'
|
|
311
|
+
/**
|
|
312
|
+
* A key the level's effective `additionalProperties` forbids was dropped; its text is in
|
|
313
|
+
* `discarded` too, so a strict Schema does not silently eat what the model wrote.
|
|
314
|
+
*/
|
|
315
|
+
| 'undeclared-keys-dropped'
|
|
267
316
|
/** The parser stopped before the end of the region it was given. */
|
|
268
317
|
| 'unparsed-tail'
|
|
269
318
|
/**
|
|
@@ -295,7 +344,10 @@ interface CandidateScore {
|
|
|
295
344
|
errors: number;
|
|
296
345
|
requiredCovered: number;
|
|
297
346
|
declaredCovered: number;
|
|
298
|
-
/**
|
|
347
|
+
/**
|
|
348
|
+
* Keys the Schema never declared, by name or by `patternProperties`. Reported, and used to
|
|
349
|
+
* rank only via `schemaEchoKeys`.
|
|
350
|
+
*/
|
|
299
351
|
undeclaredKeys: number;
|
|
300
352
|
/** How many of those undeclared keys are JSON Schema keywords - the echo signature. */
|
|
301
353
|
schemaEchoKeys: number;
|
|
@@ -335,33 +387,162 @@ interface RepairResult<T = any> {
|
|
|
335
387
|
discarded: string[];
|
|
336
388
|
}
|
|
337
389
|
|
|
390
|
+
/**
|
|
391
|
+
* The characters that count as a word separator when the `separators` form is on.
|
|
392
|
+
* camelCase is not in this list: it writes the boundary as a letter-case change, which only a
|
|
393
|
+
* case-folding comparison can see - see {@link KeyMatchForm}.
|
|
394
|
+
*/
|
|
395
|
+
declare const KEY_SEPARATORS: readonly string[];
|
|
396
|
+
/** What a `keyMatching` option resolves to. One place, so no reader of the option re-derives it. */
|
|
397
|
+
interface ResolvedKeyMatching {
|
|
398
|
+
/** The `case` form: compare without regard to letter case. */
|
|
399
|
+
foldCase: boolean;
|
|
400
|
+
/** The separator characters in force, or `null` when the `separators` form is off. */
|
|
401
|
+
separators: readonly string[] | null;
|
|
402
|
+
/** Canonical declared name -> the other names the document may use for it. */
|
|
403
|
+
aliases: Record<string, readonly string[]>;
|
|
404
|
+
/** A caller-supplied resolver; it decides itself and replaces the forms. */
|
|
405
|
+
matcher: ((key: string, properties: string[]) => string | undefined) | null;
|
|
406
|
+
}
|
|
407
|
+
|
|
338
408
|
/**
|
|
339
409
|
* Navigates through a JSON Schema based on a JSON Path.
|
|
340
410
|
* Handles $ref resolution and provides type information for specific paths.
|
|
411
|
+
*
|
|
412
|
+
* Before it is walked, the Schema is also flattened over the keywords that describe the same
|
|
413
|
+
* value from another angle - `allOf`, `anyOf` and `oneOf` - so a type or a property declared
|
|
414
|
+
* inside one of them is still seen at the level it describes.
|
|
341
415
|
*/
|
|
342
416
|
declare class SchemaWalker {
|
|
343
417
|
private dereferencedSchema;
|
|
344
418
|
private options;
|
|
419
|
+
/** `options.keyMatching`, read once. See {@link SchemaWalker.keyMatching}. */
|
|
420
|
+
private resolvedKeyMatching?;
|
|
345
421
|
private constructor();
|
|
346
422
|
/**
|
|
347
423
|
* Static factory method to create a SchemaWalker instance.
|
|
348
|
-
* Asynchronously dereferences the provided JSON Schema.
|
|
424
|
+
* Asynchronously dereferences the provided JSON Schema, then flattens its combinators.
|
|
349
425
|
*
|
|
350
426
|
* The Schema is dereferenced on a deep copy: `$RefParser.dereference()` replaces every
|
|
351
|
-
* `$ref` in place, which would otherwise rewrite the object the caller passed in.
|
|
427
|
+
* `$ref` in place, which would otherwise rewrite the object the caller passed in. Both
|
|
428
|
+
* that copy and the flattening stay inside the walker.
|
|
352
429
|
*
|
|
353
430
|
* @param schema - The JSON Schema object to walk.
|
|
354
431
|
* @param options - Repair options that concern the Schema itself (`keyMatching`).
|
|
355
432
|
* @returns A promise that resolves to a SchemaWalker instance.
|
|
356
433
|
*/
|
|
357
434
|
static create(schema: any, options?: RepairOptions): Promise<SchemaWalker>;
|
|
435
|
+
/** Keywords that describe the value at a level instead of a deeper one. */
|
|
436
|
+
private static readonly COMBINATORS;
|
|
437
|
+
/** Keywords whose value is a map of name to sub-schema. */
|
|
438
|
+
private static readonly SCHEMA_MAPS;
|
|
439
|
+
/** Keywords whose value is a sub-schema, or a list of them. */
|
|
440
|
+
private static readonly SCHEMA_NODES;
|
|
441
|
+
/**
|
|
442
|
+
* What an `anyOf` / `oneOf` branch may add to the level it describes.
|
|
443
|
+
*
|
|
444
|
+
* A branch that applies only sometimes can still say what the value may be (`type`), which
|
|
445
|
+
* names the level may hold (`properties`), what a list holds (`items`) and which values are
|
|
446
|
+
* listed (`enum`) - folding those in is what lets the walker navigate and coerce a Schema
|
|
447
|
+
* built out of branches. A branch's own constraints (`pattern`, `required`, the bounds) say
|
|
448
|
+
* something about that branch alone, so they are left where they are rather than merged into
|
|
449
|
+
* a level that may be using a different branch.
|
|
450
|
+
*/
|
|
451
|
+
private static readonly SHARED_KEYWORDS;
|
|
452
|
+
/**
|
|
453
|
+
* Folds `allOf` / `anyOf` / `oneOf` into the level they describe.
|
|
454
|
+
*
|
|
455
|
+
* Without this the node the walker hands back for a path is `{ anyOf: [{ type: 'string' }] }`:
|
|
456
|
+
* `type` is missing, so `coerceValue()` leaves the scalar exactly as the document wrote it and
|
|
457
|
+
* `validateValue()` finds nothing to complain about - the same value coerces under
|
|
458
|
+
* `{ type: 'string' }` and not under a branch that says the same thing.
|
|
459
|
+
*
|
|
460
|
+
* - `allOf`: every branch applies, so a branch merges into the level completely, its
|
|
461
|
+
* constraints included.
|
|
462
|
+
* - `anyOf` / `oneOf`: one branch applies, so only {@link SHARED_KEYWORDS} merge.
|
|
463
|
+
*
|
|
464
|
+
* Where a level and a branch say different things about one thing, the types are listed
|
|
465
|
+
* together (`type: ['string', 'number']`) - the Schema allows either, so a value that already
|
|
466
|
+
* reads as one of them is left alone and only a value that matches none gets coerced.
|
|
467
|
+
*
|
|
468
|
+
* The pass runs once per Schema and keeps a recursive Schema cyclic instead of endless:
|
|
469
|
+
* a node already seen is reused, and a branch is never merged into a node twice.
|
|
470
|
+
*/
|
|
471
|
+
private static flatten;
|
|
472
|
+
/** The sub-schemas held by a map keyword (`properties`, `$defs`, ...). */
|
|
473
|
+
private static flattenMap;
|
|
474
|
+
/**
|
|
475
|
+
* The flattened form of one Schema node: its own keywords, its sub-schemas flattened below
|
|
476
|
+
* them, and its branches applied. `const`, `enum` and `default` hold data rather than
|
|
477
|
+
* sub-schemas, so they are copied and never walked - a document's own value may well contain
|
|
478
|
+
* the word `allOf`.
|
|
479
|
+
*/
|
|
480
|
+
private static flattenNode;
|
|
481
|
+
/**
|
|
482
|
+
* Applies one branch to the level it describes: `every` when the branch always applies
|
|
483
|
+
* (`allOf`), `one` when it applies only if chosen (`anyOf` / `oneOf`).
|
|
484
|
+
*/
|
|
485
|
+
private static mergeInto;
|
|
486
|
+
/** The two lists together, without repeating what both already say. */
|
|
487
|
+
private static unique;
|
|
358
488
|
/**
|
|
359
489
|
* Retrieves the sub-schema for a given JSON path.
|
|
360
490
|
*
|
|
491
|
+
* A key the Schema does not name is described by a `patternProperties` entry that matches it,
|
|
492
|
+
* or by the level's effective `additionalProperties` when that is a Schema itself - which is
|
|
493
|
+
* how one declaration covers a whole free-form map.
|
|
494
|
+
*
|
|
361
495
|
* @param path - The path to navigate to.
|
|
362
496
|
* @returns The sub-schema at the specified path, or undefined if not found.
|
|
363
497
|
*/
|
|
364
498
|
getSchemaForPath(path: JSONPath): any;
|
|
499
|
+
/**
|
|
500
|
+
* The `additionalProperties` value that applies at `path`: what the Schema says there, or
|
|
501
|
+
* what {@link RepairOptions.additionalProperties} says for every level that does not.
|
|
502
|
+
*
|
|
503
|
+
* Neither speaking means the JSON Schema default: any value is allowed.
|
|
504
|
+
*/
|
|
505
|
+
additionalPropertiesAt(path: JSONPath): unknown;
|
|
506
|
+
/**
|
|
507
|
+
* The keyword value that governs the children of a Schema node - the node's own if it states
|
|
508
|
+
* one, the option's otherwise, and JSON Schema's own assumption (`true`) when neither does.
|
|
509
|
+
*/
|
|
510
|
+
private effectiveAdditionalProperties;
|
|
511
|
+
/**
|
|
512
|
+
* The sub-schema one of those children gets when the node names no property for it, or
|
|
513
|
+
* `undefined` when the effective keyword names none (`true`, `false`).
|
|
514
|
+
*
|
|
515
|
+
* This is what lets one Schema describe a whole free-form map: the value of an undeclared
|
|
516
|
+
* key is coerced and validated against it, exactly as a declared property's value is.
|
|
517
|
+
*/
|
|
518
|
+
private valueSchemaOf;
|
|
519
|
+
/**
|
|
520
|
+
* The `patternProperties` Schema that names `key` at `path` - the first pattern it matches.
|
|
521
|
+
*
|
|
522
|
+
* A pattern is a key the level describes by shape rather than by name, so it carries the same
|
|
523
|
+
* three things a declared property does: what the value is, whether it fits, and whether the
|
|
524
|
+
* key belongs here.
|
|
525
|
+
*/
|
|
526
|
+
patternSchemaFor(path: JSONPath, key: string | number): any;
|
|
527
|
+
/** What one Schema node's `patternProperties` says about `key`, or `undefined`. */
|
|
528
|
+
private patternSchemaForNode;
|
|
529
|
+
/**
|
|
530
|
+
* Whether a `patternProperties` entry at `path` covers `key`, so it is not an extra one.
|
|
531
|
+
*
|
|
532
|
+
* A pattern the host engine cannot compile covers every key and describes none: failing to
|
|
533
|
+
* read the guard is no reason to call the document's key extra (and drop it under
|
|
534
|
+
* `additionalProperties: false`), and no reason to invent a type for it either.
|
|
535
|
+
*/
|
|
536
|
+
matchesKeyPattern(path: JSONPath, key: string): boolean;
|
|
537
|
+
/** Whether the level at `path` describes keys by shape at all. */
|
|
538
|
+
hasKeyPatterns(path: JSONPath): boolean;
|
|
539
|
+
private static patternEntries;
|
|
540
|
+
/**
|
|
541
|
+
* The compiled pattern, or `null` when the host regex engine cannot compile it. The same
|
|
542
|
+
* patterns are asked again for every key of every document, so they are kept compiled.
|
|
543
|
+
*/
|
|
544
|
+
private static readonly patterns;
|
|
545
|
+
private static patternOf;
|
|
365
546
|
/**
|
|
366
547
|
* Determines if a property key is valid for the given path according to the Schema.
|
|
367
548
|
*
|
|
@@ -370,13 +551,24 @@ declare class SchemaWalker {
|
|
|
370
551
|
* @returns True if the property is defined in the Schema or allowed by additionalProperties.
|
|
371
552
|
*/
|
|
372
553
|
isValidProperty(path: JSONPath, key: string): boolean;
|
|
554
|
+
/**
|
|
555
|
+
* Whether the level at `path` may hold `key` at all: it is declared there, or the effective
|
|
556
|
+
* {@link additionalPropertiesAt} allows keys the Schema does not name.
|
|
557
|
+
*
|
|
558
|
+
* A level that declares no property names is free-form: none of its keys is an "extra" one,
|
|
559
|
+
* so `additionalProperties: false` never empties it. That is the one place the repair reads
|
|
560
|
+
* the keyword more loosely than strict JSON Schema does, and it is deliberate - emptying a
|
|
561
|
+
* `labels` or `metadata` map is destroying the answer, not tightening it.
|
|
562
|
+
*/
|
|
563
|
+
allowsProperty(path: JSONPath, key: string): boolean;
|
|
373
564
|
/**
|
|
374
565
|
* Resolves an incoming property name to the name declared by the Schema at `path`.
|
|
375
566
|
*
|
|
376
|
-
* Without `keyMatching` this only accepts an exact declaration.
|
|
377
|
-
*
|
|
378
|
-
* (`user_name` / `userName` / `USER NAME`)
|
|
379
|
-
*
|
|
567
|
+
* Without `keyMatching` this only accepts an exact declaration. The selected forms say what
|
|
568
|
+
* may differ from it: `case` ignores letter case, `separators` ignores the word boundary
|
|
569
|
+
* (`user_name` / `userName` / `USER NAME`), and `aliases` adds the alternative names the
|
|
570
|
+
* Schema declares under a keyword this library does not read. A function matcher decides
|
|
571
|
+
* itself and may return a name the Schema does not declare.
|
|
380
572
|
*
|
|
381
573
|
* Besides renaming, the result is what makes a key a breakpoint for greedy string capture:
|
|
382
574
|
* the parser calls this while deciding whether an upcoming `key:` ends the value in front of it.
|
|
@@ -387,14 +579,19 @@ declare class SchemaWalker {
|
|
|
387
579
|
*/
|
|
388
580
|
matchProperty(path: JSONPath, key: string): string | null;
|
|
389
581
|
/**
|
|
390
|
-
* The
|
|
582
|
+
* The resolved `options.keyMatching` this walker was built with - the one place the option is
|
|
583
|
+
* read. The parser asks for it rather than reading the option itself: `keyMatching` describes
|
|
584
|
+
* how the Schema's names may be spelled, so a walker built with it (the documented way to
|
|
585
|
+
* reuse a Schema) has to give the same answer to every reader, both the key lookup above and
|
|
586
|
+
* the parser's scan for a declared key written across several words.
|
|
587
|
+
*
|
|
588
|
+
* Cached: it does not change while this walker lives.
|
|
391
589
|
*/
|
|
392
|
-
|
|
590
|
+
get keyMatching(): ResolvedKeyMatching;
|
|
393
591
|
/**
|
|
394
|
-
*
|
|
395
|
-
* snake_case, kebab-case, camelCase and spaced keys collide.
|
|
592
|
+
* The property names the Schema declares at `path` (empty when the level is free-form).
|
|
396
593
|
*/
|
|
397
|
-
|
|
594
|
+
declaredPropertyNames(path: JSONPath): string[];
|
|
398
595
|
/**
|
|
399
596
|
* Whether a property name is explicitly declared in the Schema at the given path.
|
|
400
597
|
*
|
|
@@ -407,6 +604,8 @@ declare class SchemaWalker {
|
|
|
407
604
|
* Whether the Schema at the given path declares any property names at all.
|
|
408
605
|
* Used to decide whether key matching can be strict or has to stay lenient.
|
|
409
606
|
*
|
|
607
|
+
* `patternProperties` declares keys of another kind - see {@link hasKeyPatterns}.
|
|
608
|
+
*
|
|
410
609
|
* @param path - The current parent path.
|
|
411
610
|
*/
|
|
412
611
|
hasDeclaredProperties(path: JSONPath): boolean;
|
|
@@ -549,23 +748,16 @@ declare class RepairParser {
|
|
|
549
748
|
/**
|
|
550
749
|
* Whether the Schema describes an object at the current path.
|
|
551
750
|
*
|
|
552
|
-
* `SchemaWalker` navigates on `type === 'object' || properties`, so
|
|
553
|
-
* detection has to use the same rule: a level that declares `properties`
|
|
554
|
-
* `type: ['object', ...]` union) is an object even when it never spells
|
|
751
|
+
* `SchemaWalker` navigates on `type === 'object' || properties || patternProperties`, so
|
|
752
|
+
* implicit-object detection has to use the same rule: a level that declares `properties`
|
|
753
|
+
* or key shapes (or a `type: ['object', ...]` union) is an object even when it never spells
|
|
754
|
+
* out `type`.
|
|
555
755
|
*/
|
|
556
756
|
private static declaresObject;
|
|
557
757
|
/**
|
|
558
758
|
* Parses a JSON object, handling both standard and implicit (no braces) structures.
|
|
559
759
|
*/
|
|
560
760
|
private consumeObject;
|
|
561
|
-
/**
|
|
562
|
-
* Whether a property belongs in the output, according to `RepairOptions.additionalProperties`.
|
|
563
|
-
*
|
|
564
|
-
* `'remove'` only drops keys at a level whose Schema declares property names: a level
|
|
565
|
-
* that is free-form (`type: 'object'` without `properties`, or no Schema at all) has no
|
|
566
|
-
* notion of an "extra" key, so dropping everything there would be destructive.
|
|
567
|
-
*/
|
|
568
|
-
private isKeptProperty;
|
|
569
761
|
/**
|
|
570
762
|
* Assigns an own enumerable property without going through setters.
|
|
571
763
|
*
|
|
@@ -631,8 +823,9 @@ declare class RepairParser {
|
|
|
631
823
|
private static readonly WORD_CHAR;
|
|
632
824
|
/**
|
|
633
825
|
* Compiled "declared key literally here" patterns, shared across instances.
|
|
634
|
-
* Keyed by `
|
|
635
|
-
* come from the Schema in play, and a long-lived process sees many of
|
|
826
|
+
* Keyed by `foldCase` + name because the `i` flag follows the `case` form of `keyMatching`,
|
|
827
|
+
* and bounded: the names come from the Schema in play, and a long-lived process sees many of
|
|
828
|
+
* them.
|
|
636
829
|
*/
|
|
637
830
|
private static readonly declaredKeyPatterns;
|
|
638
831
|
/**
|
|
@@ -646,12 +839,19 @@ declare class RepairParser {
|
|
|
646
839
|
* The property names declared at `path` that contain whitespace, with a pattern that matches
|
|
647
840
|
* them literally: the name escaped, internal whitespace runs relaxed to `\s+`, and a `:`
|
|
648
841
|
* required after it. Single-word names are deliberately excluded - the generic key-shaped
|
|
649
|
-
* regex of {@link RepairParser.matchKey} already covers them.
|
|
842
|
+
* regex of {@link RepairParser.matchKey} already covers them. The alternative spellings a
|
|
843
|
+
* caller declares through `keyMatching.aliases` are included: for this scanner they are just
|
|
844
|
+
* as multi-word as the name they stand for.
|
|
845
|
+
*
|
|
846
|
+
* The forms in force come from the walker, not from this parser's options: `keyMatching`
|
|
847
|
+
* belongs to the Schema, and a caller who hands one walker to many `jsonRepair()` calls has
|
|
848
|
+
* stated it there. Reading it twice, once per place, is what lets one key in a document match
|
|
849
|
+
* while the next one does not.
|
|
650
850
|
*/
|
|
651
851
|
private declaredMultiWordKeys;
|
|
652
852
|
/**
|
|
653
853
|
* Matches a Schema-declared property name that contains whitespace at `startPos`, literally
|
|
654
|
-
* (case as declared, unless `keyMatching` folds
|
|
854
|
+
* (case as declared, unless the `case` form of `keyMatching` folds it).
|
|
655
855
|
*
|
|
656
856
|
* The generic key scanner cannot help here: its bare-key branch excludes whitespace, so an
|
|
657
857
|
* unquoted `Official language:` in the document is never seen as one key. Declaring the name
|
|
@@ -796,6 +996,18 @@ declare class RepairParser {
|
|
|
796
996
|
* Consumes a standard JSON number.
|
|
797
997
|
*/
|
|
798
998
|
private consumeNumber;
|
|
999
|
+
/**
|
|
1000
|
+
* Consumes one of JSON's own literals - `true`, `false`, `null` - only when the whole token
|
|
1001
|
+
* is that word: the character after it has to end the token, not continue it.
|
|
1002
|
+
*
|
|
1003
|
+
* A bare `n` is the model's word, not `null`. Reading a prefix as the literal invents a missing
|
|
1004
|
+
* value where the document wrote text, and inventing `null` is the one mistake no caller
|
|
1005
|
+
* notices: it is exactly what a truncated answer usually looks like. {@link consumeValue}
|
|
1006
|
+
* falls back to greedy capture when nothing is consumed here, so the text comes back whole.
|
|
1007
|
+
*
|
|
1008
|
+
* @returns whether the token was the literal.
|
|
1009
|
+
*/
|
|
1010
|
+
private consumeKeyword;
|
|
799
1011
|
/**
|
|
800
1012
|
* Consumes a standard JSON boolean.
|
|
801
1013
|
*/
|
|
@@ -888,11 +1100,19 @@ declare function discardedText(text: string, chosen: LocatedJson, others: Locate
|
|
|
888
1100
|
* walking this library already does.
|
|
889
1101
|
*
|
|
890
1102
|
* The keywords covered are the ones that describe a shape an LLM can get wrong: `type`
|
|
891
|
-
* (including `integer` and unions), `required`, `properties`, `
|
|
892
|
-
* `
|
|
893
|
-
*
|
|
894
|
-
*
|
|
895
|
-
*
|
|
1103
|
+
* (including `integer` and unions), `required`, `properties`, `patternProperties`,
|
|
1104
|
+
* `additionalProperties` (the Schema's own, or the default `RepairOptions.additionalProperties`
|
|
1105
|
+
* gives it), `items`,
|
|
1106
|
+
* `enum`, `const`, `pattern`, `format` for dates and times, numeric bounds, string length and
|
|
1107
|
+
* array length. Unknown keywords are ignored, and so is everything below a Schema that says
|
|
1108
|
+
* nothing.
|
|
1109
|
+
*
|
|
1110
|
+
* `allOf` / `anyOf` / `oneOf` are read where they describe the level - `SchemaWalker` folds
|
|
1111
|
+
* their types, declared names, `items` and listed values into it before this validator sees it,
|
|
1112
|
+
* and every `allOf` branch contributes its constraints. What is still not evaluated is what
|
|
1113
|
+
* belongs to one branch of an `anyOf` / `oneOf` (`pattern`, the bounds, `required`) and
|
|
1114
|
+
* `if` / `then` / `else`: a constraint that only one alternative makes is not a constraint on
|
|
1115
|
+
* the value, and reporting it would be reporting an error the Schema does not make.
|
|
896
1116
|
*/
|
|
897
1117
|
declare function validateValue(value: any, walker: SchemaWalker): ValidationError[];
|
|
898
1118
|
/**
|
|
@@ -942,4 +1162,4 @@ declare function looksLikeSchemaEcho(fragment: unknown): boolean;
|
|
|
942
1162
|
*/
|
|
943
1163
|
declare function compareScores(a: CandidateScore, b: CandidateScore): number;
|
|
944
1164
|
|
|
945
|
-
export { type CandidateScore, type CommentStyle, type EscapeMode, type JSONPath, type JsonCandidate, type LiteralDialect, type LocatedJson, type RepairFlag, type RepairOptions, RepairParser, type RepairResult, SchemaWalker, type ValidationError, compareScores, discardedText, expectsArrayRoot, findImplicitRecordRegion, jsonRepair, jsonRepairResult, locateJsonCandidates, looksLikeSchemaEcho, renderValidatorErrors, scoreCandidate, validateValue };
|
|
1165
|
+
export { type CandidateScore, type CommentStyle, type EscapeMode, type JSONPath, type JsonCandidate, KEY_SEPARATORS, type KeyMatchForm, type KeyMatchSpec, type LiteralDialect, type LocatedJson, type RepairFlag, type RepairOptions, RepairParser, type RepairResult, type ResolvedKeyMatching, SchemaWalker, type ValidationError, compareScores, discardedText, expectsArrayRoot, findImplicitRecordRegion, jsonRepair, jsonRepairResult, locateJsonCandidates, looksLikeSchemaEcho, renderValidatorErrors, scoreCandidate, validateValue };
|