@isdk/json-repair 0.1.3 → 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 +65 -9
- package/README.cn.md +143 -9
- package/README.md +181 -12
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +828 -41
- package/dist/index.d.ts +828 -41
- package/dist/index.js +1 -1
- package/package.json +30 -7
- package/.prettierignore +0 -12
- package/.prettierrc +0 -8
- package/.versionrc +0 -36
- package/TODO +0 -0
- package/docs/README.md +0 -246
- package/docs/_media/README.cn.md +0 -214
- package/docs/classes/RepairParser.md +0 -64
- package/docs/classes/SchemaWalker.md +0 -241
- package/docs/functions/jsonRepair.md +0 -48
- package/docs/globals.md +0 -24
- package/docs/interfaces/RepairOptions.md +0 -96
- package/docs/type-aliases/JSONPath.md +0 -14
- package/eslint.config.mjs +0 -65
- package/setupVitest.mjs +0 -15
- package/tsconfig.json +0 -20
- package/tsconfig.release.json +0 -21
- package/tsup.config.ts +0 -38
- package/typedoc.config.cjs +0 -9
- package/vite.config.mjs +0 -21
package/docs/_media/README.cn.md
DELETED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
# @isdk/json-repair
|
|
2
|
-
|
|
3
|
-
> 【[English](./README.md)|中文】
|
|
4
|
-
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
一个强大的、基于 Schema 引导的 JSON 修复库,专为修复大语言模型 (LLM) 生成的“破碎” JSON 而设计。
|
|
8
|
-
|
|
9
|
-
[](https://opensource.org/licenses/MIT)
|
|
10
|
-
[](https://www.npmjs.com/package/@isdk/json-repair)
|
|
11
|
-
|
|
12
|
-
## 🚀 为什么选择这个库?
|
|
13
|
-
|
|
14
|
-
LLM 生成的 JSON 经常包含语法错误或语义噪声。常见问题包括:
|
|
15
|
-
|
|
16
|
-
- 键或值缺少引号。
|
|
17
|
-
- 字符串内部引号未转义(例如 `"a"b"`)。
|
|
18
|
-
- 缺少逗号或结尾花括号。
|
|
19
|
-
- 值中混合了自然语言(例如 `"age": "大约 30 岁"`)。
|
|
20
|
-
- 隐式结构(例如数组中的对象缺少花括号)。
|
|
21
|
-
|
|
22
|
-
传统的 `json-repair` 库仅在语法层面打补丁。**@isdk/json-repair** 利用 **JSON Schema** 作为导航图,智能地解析、修复并将数据强制转换为符合规范的结构化对象。
|
|
23
|
-
|
|
24
|
-
## ✨ 核心特性
|
|
25
|
-
|
|
26
|
-
- **Schema 引导修复**:利用 Schema 解决歧义(例如通过判断冒号后面是否跟着 Schema 定义的 Key 来决定它是定界符还是字符串内容)。
|
|
27
|
-
- **贪婪字符串捕获**:智能捕获未加引号或破碎的字符串,直到遇到下一个合法的 Schema 键名。
|
|
28
|
-
- **平衡定界符追踪**:正确处理 DSL 风格字符串(如 `@func(a, b)`)内部嵌套在括号、方括号或花括号中的结构化符号(逗号、冒号等)。
|
|
29
|
-
- **“奇偶判定法”**:完美解决嵌套引号歧义(例如准确区分 `"A" OR "B"` 和 `"a"b"`)。
|
|
30
|
-
- **语义强制转换 (Coercion)**:
|
|
31
|
-
- **枚举模糊匹配**:如果 Schema 定义了枚举,能将 `"Processing!"` 自动匹配为 `"processing"`。
|
|
32
|
-
- **噪声数字提取**:能从 `"约 1,200.50 元"` 中提取出 `1200.5`。
|
|
33
|
-
- **布尔值变体**:识别 `yes/no`、`on/off`、`1/0`、`确定/取消`、`是/否` 为布尔值。
|
|
34
|
-
- **声明为 string 的值保持文本**:`{ Population: 12 }` 在 `{ type: 'string' }` 下得到 `"12"`;数字取自文档原文,所以 `{ id: 01234 }` 得到 `"01234"`。
|
|
35
|
-
- **隐式结构支持**:自动修复对象或数组中缺失的花括号。
|
|
36
|
-
- **稳当的属性边界**:带引号的键名可以含空格与标点(`"Official language": "Italian"`);Schema 声明过的多词键名,即使文档里没加引号也能被识别;能完整解析的值不会被重新切分,因此合法 JSON 的产物与 `JSON.parse` 完全一致。
|
|
37
|
-
- **模糊键名匹配**:`user_name`、`userName`、`USER NAME` 都会归位到 Schema 声明的属性名,输出里用的就是声明的名字。
|
|
38
|
-
- **原型安全的输出**:`__proto__`、`constructor` 这类键名存为自身的普通属性,与 `JSON.parse` 的处理一致。
|
|
39
|
-
- **高性能**:支持复用 `SchemaWalker` 实例,适用于高吞吐量的批处理场景。
|
|
40
|
-
|
|
41
|
-
## 📦 安装
|
|
42
|
-
|
|
43
|
-
```bash
|
|
44
|
-
pnpm add @isdk/json-repair @apidevtools/json-schema-ref-parser
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
## 🛠 用法
|
|
48
|
-
|
|
49
|
-
### 1. Schema 引导修复 (推荐)
|
|
50
|
-
|
|
51
|
-
这是最强大的使用方式。提供 Schema 可以开启语义强制转换、贪婪捕获和更精准的歧义消除等高级特性。
|
|
52
|
-
|
|
53
|
-
```typescript
|
|
54
|
-
import { jsonRepair } from '@isdk/json-repair'
|
|
55
|
-
|
|
56
|
-
const schema = {
|
|
57
|
-
type: 'object',
|
|
58
|
-
properties: {
|
|
59
|
-
query: { type: 'string' },
|
|
60
|
-
status: { enum: ['success', 'error'] },
|
|
61
|
-
},
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const brokenJson = '{ query: "python" OR "js", status: Success! }'
|
|
65
|
-
|
|
66
|
-
const result = await jsonRepair(brokenJson, schema)
|
|
67
|
-
console.log(result)
|
|
68
|
-
// 输出: { query: '"python" OR "js"', status: 'success' }
|
|
69
|
-
|
|
70
|
-
// Schema 声明过的多词键名,即使文档里没加引号也能被识别。
|
|
71
|
-
const countrySchema = {
|
|
72
|
-
type: 'object',
|
|
73
|
-
properties: {
|
|
74
|
-
Capital: { type: 'string' },
|
|
75
|
-
'Official language': { type: 'string' },
|
|
76
|
-
},
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
console.log(
|
|
80
|
-
await jsonRepair(
|
|
81
|
-
'{ Capital: Berlin, Official language: Italian }',
|
|
82
|
-
countrySchema
|
|
83
|
-
)
|
|
84
|
-
)
|
|
85
|
-
// 输出: { Capital: 'Berlin', 'Official language': 'Italian' }
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
### 2. 简单用法 (不使用 Schema)
|
|
89
|
-
|
|
90
|
-
对于基础的语法修复,你可以不提供 Schema:
|
|
91
|
-
|
|
92
|
-
```typescript
|
|
93
|
-
import { jsonRepair } from '@isdk/json-repair'
|
|
94
|
-
|
|
95
|
-
const result = await jsonRepair('{ name: 张三, age: 30 }')
|
|
96
|
-
console.log(result) // { name: '张三', age: 30 }
|
|
97
|
-
|
|
98
|
-
// 带引号的键名可以包含空格与标点;合法 JSON 会被原样保留。
|
|
99
|
-
const country = await jsonRepair(
|
|
100
|
-
'{ "Capital": "Berlin", "Official language": "Italian" }'
|
|
101
|
-
)
|
|
102
|
-
console.log(country) // { Capital: 'Berlin', 'Official language': 'Italian' }
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
### 高级技巧:复用 SchemaWalker (批处理)
|
|
106
|
-
|
|
107
|
-
如果你需要使用相同的 Schema 处理大量数据,预解析 Schema 可以显著提升性能:
|
|
108
|
-
|
|
109
|
-
```typescript
|
|
110
|
-
import { jsonRepair, SchemaWalker } from '@isdk/json-repair'
|
|
111
|
-
|
|
112
|
-
// 与 Schema 本身相关的选项(`keyMatching`)属于 walker。
|
|
113
|
-
const walker = await SchemaWalker.create(mySchema, { keyMatching: true })
|
|
114
|
-
|
|
115
|
-
for (const item of brokenItems) {
|
|
116
|
-
const result = await jsonRepair(item, walker)
|
|
117
|
-
// ...
|
|
118
|
-
}
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
## 🧭 属性边界是如何判定的
|
|
122
|
-
|
|
123
|
-
在修复一个值的过程中,解析器必须在每个位置上判断接下来的 `"foo":` 是**下一个属性**的起点,还是**当前值**的一部分。它分为两种模式:
|
|
124
|
-
|
|
125
|
-
1. **结构模式 (Structural mode)** —— 适用于刚刚由严格 JSON 路径解析成功的值(成对的 `"string"`、格式合法的数字、`true`/`false`/`null`)。
|
|
126
|
-
此时任何“键形状 + `:`”都被视为下一个属性,**包括含空格或标点的引号键名**(如 `"Official language":`、`"a, b":`、`"a: b":`)。
|
|
127
|
-
一次成功的严格解析**永远不会被丢弃**,这正是“合法 JSON 的产物与 `JSON.parse` 一致”这条不变量的来源。
|
|
128
|
-
2. **贪婪模式 (Greedy mode)** —— 适用于无法严格解析的值(无引号文本、内部引号未转义、`"A" OR "B"` 表达式、`@func(a="x, y")` 这类 DSL 片段)。
|
|
129
|
-
- 如果 Schema 在当前层级声明了属性名,则**只在已声明的键名处断开**。仅仅是“看起来像键”的文本会留在值里:
|
|
130
|
-
`{ "query": he said "hello there": loudly, "intent": "x" }` 会得到 `query = 'he said "hello there": loudly'`。
|
|
131
|
-
- 如果当前层级没有任何声明的属性名(无 Schema、只有 `type: "object"` 没有 `properties`、或只写了 `additionalProperties`),则退回到“第一个键形状的 token 即结束值”。
|
|
132
|
-
- **已声明**的键名还会被按字面量搜索,因此即使文档里没加引号、且键名内部含空格,它依然是断点(也是键)。通用键扫描器看不出裸的 `Official language:` 是一个键,而 Schema 能告诉你:
|
|
133
|
-
`{ Capital: Berlin, Official language: Italian }` 会得到 `{ Capital: 'Berlin', 'Official language': 'Italian' }`。开启 `keyMatching` 后,已声明键名的分隔符与大小写变体(`user_name` 对应 `userName`)同样会结束一个值。
|
|
134
|
-
|
|
135
|
-
将 `stringCaptureStrategy: 'conservative'` 可以让贪婪模式不管 Schema 声明与否,都在任意“键形状 + `:`”处结束(参见[修复选项](#-修复选项))。
|
|
136
|
-
|
|
137
|
-
### 如何理解这套启发式
|
|
138
|
-
|
|
139
|
-
歧义的本质是信息缺失,而不是魔法:`{ "note": see "foo bar": baz here, "b": 2 }` 在没有 Schema 时无法判定,因为两种解读都说得通。解析器遵循的原则是:
|
|
140
|
-
|
|
141
|
-
- **能干净解析**的值会被信任;它后面的东西被当作结构。
|
|
142
|
-
- **本身已破碎**的值,只要 Schema 能给出真正的锚点,就尽量作为文本保留。
|
|
143
|
-
- Schema **声明过**的键名会被按字面量匹配,所以多词键名在文档里不加引号也能被识别;而 Schema 未声明的多词文本永远不会被当成键。
|
|
144
|
-
- 因此,**为当前层级声明 `properties` 是引导修复效果最显著的手段** —— 已声明的键名是贪婪捕获的断点,已声明的 `type`/`enum` 才能开启语义强制转换。
|
|
145
|
-
- 只要 Schema 在该层级声明了 `properties`(或 `type: 'object'`),该层级就被当作对象;因此省略 `type: 'object'` 也不会让无花括号的文档退化成标量。
|
|
146
|
-
- 如果值里可能合法地出现 `word:` 或 `"phrase":`,要么让模型输出正确转义的 JSON(严格解析直接胜出),要么在 Schema 里声明该层级的属性。
|
|
147
|
-
- 如果某个值在“看起来像键”的地方被截断,通常是你的 Schema 在该层级缺少 `properties` —— 补上后重试。
|
|
148
|
-
|
|
149
|
-
数字字面量支持科学计数法(`1e5`、`-2E2`),也容忍 LLM 常见的 `+5`、`.5`、`5.`。而 `1.2.3` 这种不完整的字面量会先作为字符串保留,再按 Schema 做强制转换。
|
|
150
|
-
|
|
151
|
-
反方向同样成立:Schema 声明 `type: 'string'` 时 `12` 会被转成 `"12"`;并且文本取自文档原文而非解析后的数字,所以前导零不会丢失(`{ id: 01234 }` → `"01234"`)。`null` 与对象/数组不会被转成字符串。
|
|
152
|
-
|
|
153
|
-
## ⚠️ 注意事项与歧义说明
|
|
154
|
-
|
|
155
|
-
- **“奇偶判定法”逻辑**:当字符串被引号包裹且内部包含引号时,我们会计算内部引号的数量。
|
|
156
|
-
- 奇数个 (如 `"a"b"`):认为外层是定界符 -> 结果 `a"b`。
|
|
157
|
-
- 偶数个 (如 `"A" OR "B"`):认为外层是内容的一部分 -> 结果 `"A" OR "B"`。
|
|
158
|
-
- **局限性**:极少数极端模式如 `"a"b"c"` 可能仍存在歧义。
|
|
159
|
-
- **破碎值内部“看起来像键”的文本(无 Schema 引导时)**:当前层级没有声明任何属性名时,第一个键形状的 token 就会结束该值。
|
|
160
|
-
因此无 Schema 的 `{ "note": see "foo bar": baz here, "b": 2 }` 会被读成 `{ note: 'see', 'foo bar': 'baz here', b: 2 }`。
|
|
161
|
-
声明该层级的 `properties` 后,贪婪捕获只在真正的键名处断开,才能还原成 `{ note: 'see "foo bar": baz here', b: 2 }`(参见[如何理解这套启发式](#如何理解这套启发式))。
|
|
162
|
-
- **未加引号的多词键名必须有声明。** `{ Capital: Berlin, Official language: Italian }` 能拆成两个属性,是因为 Schema 声明了 `Official language`。若文档里的带空格键名 _没有_ 声明,它就不是键:`{ Capital: Berlin, Official Lang: Italian }` 在默认的 `'aggressive'` 策略下会把整段都留在 `Capital` 里;改用 `'conservative'` 则会在空格处断开(`{ Capital: 'Berlin, Official', Lang: 'Italian' }`)。
|
|
163
|
-
- **值里重复出现已声明的键名时会被截断。** 声明过的键就是断点,所以 `{ summary: the Official language: Italian is wrong, Official language: Italian }` 会读成 `summary = 'the'`。如果值确实可能重复某个已声明的键名,就让模型给键加引号,或者换一个不会碰撞的属性名。
|
|
164
|
-
- **Markdown 代码块**:本库专注于 JSON 内容修复。如果 LLM 的输出包含 `json ...` 标记,请在传入前自行剥离。
|
|
165
|
-
|
|
166
|
-
## ⚙️ 修复选项
|
|
167
|
-
|
|
168
|
-
`jsonRepair(input, schema, options)` 的第三个参数就是 `RepairOptions`:
|
|
169
|
-
|
|
170
|
-
| 选项 | 默认值 | 作用 |
|
|
171
|
-
| ----------------------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
172
|
-
| `coerceTypes` | `true` | 按 Schema 声明的类型做强制转换:`"约 1,200.50 元"` → `1200.5`、`"yes"` → `true`、枚举内的 `"Processing!"` → `"processing"`、`type: 'string'` 下的 `12` → `"12"`(数字取自文档原文,故 `{ id: 01234 }` → `"01234"`)。`null` 与对象/数组不会被转成字符串。 |
|
|
173
|
-
| `keyMatching` | `false` | 把输入键名解析成 Schema 声明的属性名。`true` 忽略大小写和分隔符,`user_name`、`userName`、`USER NAME` 均匹配 `userName`;也可以传 `(key, properties) => string \| undefined` 自定义映射(返回 `undefined` 则保留原键名)。命中后输出里的键名会被改写,该子树的类型转换也随之生效。开启 `true` 时它同时决定贪婪捕获在哪里断开:Schema 声明了 `userName`,那么 `user_name:` 就会结束一个值。 |
|
|
174
|
-
| `additionalProperties` | `'keep'` | `'remove'` 丢弃 Schema 未声明的键,且只在确实声明了 `properties` 的层级生效 —— 自由结构没有“多余键”的概念,保持原样。它与 JSON Schema 关键字 `additionalProperties: false` 互不影响,后者单独生效。 |
|
|
175
|
-
| `stringCaptureStrategy` | `'aggressive'` | `'conservative'`:贪婪捕获在任意“键形状 + `:`”处结束,而不再只在 Schema 声明的键名处结束。代价是值更容易被截断,好处是一个破碎的值不会吞掉后面的一大片内容。 |
|
|
176
|
-
|
|
177
|
-
```typescript
|
|
178
|
-
const schema = {
|
|
179
|
-
type: 'object',
|
|
180
|
-
properties: {
|
|
181
|
-
userName: { type: 'string' },
|
|
182
|
-
userAge: { type: 'number' },
|
|
183
|
-
},
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
await jsonRepair('{ user_name: "Ada", user_age: "36" }', schema, {
|
|
187
|
-
keyMatching: true,
|
|
188
|
-
coerceTypes: true,
|
|
189
|
-
})
|
|
190
|
-
// { userName: 'Ada', userAge: 36 }
|
|
191
|
-
|
|
192
|
-
await jsonRepair('{ userName: "Ada", reasoning: "让我想想…" }', schema, {
|
|
193
|
-
additionalProperties: 'remove',
|
|
194
|
-
})
|
|
195
|
-
// { userName: 'Ada' }
|
|
196
|
-
|
|
197
|
-
const querySchema = {
|
|
198
|
-
type: 'object',
|
|
199
|
-
properties: { query: { type: 'string' }, intent: { type: 'string' } },
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
await jsonRepair(
|
|
203
|
-
'{ "query": he said "hello there": loudly, "intent": "x" }',
|
|
204
|
-
querySchema,
|
|
205
|
-
{ stringCaptureStrategy: 'conservative' }
|
|
206
|
-
)
|
|
207
|
-
// { query: 'he said', 'hello there': 'loudly', intent: 'x' }
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
复用 walker 时,`keyMatching` 取自 `SchemaWalker.create()` 的选项;其余选项在每次 `jsonRepair()` 调用时读取。
|
|
211
|
-
|
|
212
|
-
## 📄 开源协议
|
|
213
|
-
|
|
214
|
-
MIT © Riceball LEE
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
[**@isdk/json-repair**](../README.md)
|
|
2
|
-
|
|
3
|
-
***
|
|
4
|
-
|
|
5
|
-
[@isdk/json-repair](../globals.md) / RepairParser
|
|
6
|
-
|
|
7
|
-
# Class: RepairParser
|
|
8
|
-
|
|
9
|
-
Defined in: [parser.ts:8](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/parser.ts#L8)
|
|
10
|
-
|
|
11
|
-
A state-machine based parser that repairs and extracts structured data from broken JSON strings.
|
|
12
|
-
It uses a JSON Schema to guide the parsing process, especially for handling ambiguous or missing syntax.
|
|
13
|
-
|
|
14
|
-
## Constructors
|
|
15
|
-
|
|
16
|
-
### Constructor
|
|
17
|
-
|
|
18
|
-
> **new RepairParser**(`walker`, `options?`): `RepairParser`
|
|
19
|
-
|
|
20
|
-
Defined in: [parser.ts:21](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/parser.ts#L21)
|
|
21
|
-
|
|
22
|
-
Creates a new RepairParser instance.
|
|
23
|
-
|
|
24
|
-
#### Parameters
|
|
25
|
-
|
|
26
|
-
##### walker
|
|
27
|
-
|
|
28
|
-
[`SchemaWalker`](SchemaWalker.md)
|
|
29
|
-
|
|
30
|
-
An initialized SchemaWalker instance for Schema guidance.
|
|
31
|
-
|
|
32
|
-
##### options?
|
|
33
|
-
|
|
34
|
-
[`RepairOptions`](../interfaces/RepairOptions.md) = `{}`
|
|
35
|
-
|
|
36
|
-
Configuration options for the repair process.
|
|
37
|
-
|
|
38
|
-
#### Returns
|
|
39
|
-
|
|
40
|
-
`RepairParser`
|
|
41
|
-
|
|
42
|
-
## Methods
|
|
43
|
-
|
|
44
|
-
### parse()
|
|
45
|
-
|
|
46
|
-
> **parse**(`input`): `any`
|
|
47
|
-
|
|
48
|
-
Defined in: [parser.ts:35](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/parser.ts#L35)
|
|
49
|
-
|
|
50
|
-
Parses the input string and returns the repaired JavaScript object.
|
|
51
|
-
|
|
52
|
-
#### Parameters
|
|
53
|
-
|
|
54
|
-
##### input
|
|
55
|
-
|
|
56
|
-
`string`
|
|
57
|
-
|
|
58
|
-
The potentially broken JSON string.
|
|
59
|
-
|
|
60
|
-
#### Returns
|
|
61
|
-
|
|
62
|
-
`any`
|
|
63
|
-
|
|
64
|
-
The parsed and repaired object.
|
|
@@ -1,241 +0,0 @@
|
|
|
1
|
-
[**@isdk/json-repair**](../README.md)
|
|
2
|
-
|
|
3
|
-
***
|
|
4
|
-
|
|
5
|
-
[@isdk/json-repair](../globals.md) / SchemaWalker
|
|
6
|
-
|
|
7
|
-
# Class: SchemaWalker
|
|
8
|
-
|
|
9
|
-
Defined in: [walker/schema-walker.ts:9](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/walker/schema-walker.ts#L9)
|
|
10
|
-
|
|
11
|
-
Navigates through a JSON Schema based on a JSON Path.
|
|
12
|
-
Handles $ref resolution and provides type information for specific paths.
|
|
13
|
-
|
|
14
|
-
## Methods
|
|
15
|
-
|
|
16
|
-
### declaredPropertyNames()
|
|
17
|
-
|
|
18
|
-
> **declaredPropertyNames**(`path`): `string`[]
|
|
19
|
-
|
|
20
|
-
Defined in: [walker/schema-walker.ts:130](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/walker/schema-walker.ts#L130)
|
|
21
|
-
|
|
22
|
-
The property names the Schema declares at `path` (empty when the level is free-form).
|
|
23
|
-
|
|
24
|
-
#### Parameters
|
|
25
|
-
|
|
26
|
-
##### path
|
|
27
|
-
|
|
28
|
-
[`JSONPath`](../type-aliases/JSONPath.md)
|
|
29
|
-
|
|
30
|
-
#### Returns
|
|
31
|
-
|
|
32
|
-
`string`[]
|
|
33
|
-
|
|
34
|
-
***
|
|
35
|
-
|
|
36
|
-
### getExpectedTypes()
|
|
37
|
-
|
|
38
|
-
> **getExpectedTypes**(`path`, `key`): `string`[]
|
|
39
|
-
|
|
40
|
-
Defined in: [walker/schema-walker.ts:176](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/walker/schema-walker.ts#L176)
|
|
41
|
-
|
|
42
|
-
Gets the expected types for a specific property at a given path.
|
|
43
|
-
|
|
44
|
-
#### Parameters
|
|
45
|
-
|
|
46
|
-
##### path
|
|
47
|
-
|
|
48
|
-
[`JSONPath`](../type-aliases/JSONPath.md)
|
|
49
|
-
|
|
50
|
-
The current parent path.
|
|
51
|
-
|
|
52
|
-
##### key
|
|
53
|
-
|
|
54
|
-
`string`
|
|
55
|
-
|
|
56
|
-
The property key.
|
|
57
|
-
|
|
58
|
-
#### Returns
|
|
59
|
-
|
|
60
|
-
`string`[]
|
|
61
|
-
|
|
62
|
-
An array of expected type strings (e.g., ["string", "number"]).
|
|
63
|
-
|
|
64
|
-
***
|
|
65
|
-
|
|
66
|
-
### getSchemaForPath()
|
|
67
|
-
|
|
68
|
-
> **getSchemaForPath**(`path`): `any`
|
|
69
|
-
|
|
70
|
-
Defined in: [walker/schema-walker.ts:48](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/walker/schema-walker.ts#L48)
|
|
71
|
-
|
|
72
|
-
Retrieves the sub-schema for a given JSON path.
|
|
73
|
-
|
|
74
|
-
#### Parameters
|
|
75
|
-
|
|
76
|
-
##### path
|
|
77
|
-
|
|
78
|
-
[`JSONPath`](../type-aliases/JSONPath.md)
|
|
79
|
-
|
|
80
|
-
The path to navigate to.
|
|
81
|
-
|
|
82
|
-
#### Returns
|
|
83
|
-
|
|
84
|
-
`any`
|
|
85
|
-
|
|
86
|
-
The sub-schema at the specified path, or undefined if not found.
|
|
87
|
-
|
|
88
|
-
***
|
|
89
|
-
|
|
90
|
-
### hasDeclaredProperties()
|
|
91
|
-
|
|
92
|
-
> **hasDeclaredProperties**(`path`): `boolean`
|
|
93
|
-
|
|
94
|
-
Defined in: [walker/schema-walker.ts:164](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/walker/schema-walker.ts#L164)
|
|
95
|
-
|
|
96
|
-
Whether the Schema at the given path declares any property names at all.
|
|
97
|
-
Used to decide whether key matching can be strict or has to stay lenient.
|
|
98
|
-
|
|
99
|
-
#### Parameters
|
|
100
|
-
|
|
101
|
-
##### path
|
|
102
|
-
|
|
103
|
-
[`JSONPath`](../type-aliases/JSONPath.md)
|
|
104
|
-
|
|
105
|
-
The current parent path.
|
|
106
|
-
|
|
107
|
-
#### Returns
|
|
108
|
-
|
|
109
|
-
`boolean`
|
|
110
|
-
|
|
111
|
-
***
|
|
112
|
-
|
|
113
|
-
### isDeclaredProperty()
|
|
114
|
-
|
|
115
|
-
> **isDeclaredProperty**(`path`, `key`): `boolean`
|
|
116
|
-
|
|
117
|
-
Defined in: [walker/schema-walker.ts:150](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/walker/schema-walker.ts#L150)
|
|
118
|
-
|
|
119
|
-
Whether a property name is explicitly declared in the Schema at the given path.
|
|
120
|
-
|
|
121
|
-
#### Parameters
|
|
122
|
-
|
|
123
|
-
##### path
|
|
124
|
-
|
|
125
|
-
[`JSONPath`](../type-aliases/JSONPath.md)
|
|
126
|
-
|
|
127
|
-
The current parent path.
|
|
128
|
-
|
|
129
|
-
##### key
|
|
130
|
-
|
|
131
|
-
`string`
|
|
132
|
-
|
|
133
|
-
The property key to check.
|
|
134
|
-
|
|
135
|
-
#### Returns
|
|
136
|
-
|
|
137
|
-
`boolean`
|
|
138
|
-
|
|
139
|
-
True only when `key` appears in the Schema `properties` of that path.
|
|
140
|
-
|
|
141
|
-
***
|
|
142
|
-
|
|
143
|
-
### isValidProperty()
|
|
144
|
-
|
|
145
|
-
> **isValidProperty**(`path`, `key`): `boolean`
|
|
146
|
-
|
|
147
|
-
Defined in: [walker/schema-walker.ts:75](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/walker/schema-walker.ts#L75)
|
|
148
|
-
|
|
149
|
-
Determines if a property key is valid for the given path according to the Schema.
|
|
150
|
-
|
|
151
|
-
#### Parameters
|
|
152
|
-
|
|
153
|
-
##### path
|
|
154
|
-
|
|
155
|
-
[`JSONPath`](../type-aliases/JSONPath.md)
|
|
156
|
-
|
|
157
|
-
The current parent path.
|
|
158
|
-
|
|
159
|
-
##### key
|
|
160
|
-
|
|
161
|
-
`string`
|
|
162
|
-
|
|
163
|
-
The property key to check.
|
|
164
|
-
|
|
165
|
-
#### Returns
|
|
166
|
-
|
|
167
|
-
`boolean`
|
|
168
|
-
|
|
169
|
-
True if the property is defined in the Schema or allowed by additionalProperties.
|
|
170
|
-
|
|
171
|
-
***
|
|
172
|
-
|
|
173
|
-
### matchProperty()
|
|
174
|
-
|
|
175
|
-
> **matchProperty**(`path`, `key`): `string` \| `null`
|
|
176
|
-
|
|
177
|
-
Defined in: [walker/schema-walker.ts:108](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/walker/schema-walker.ts#L108)
|
|
178
|
-
|
|
179
|
-
Resolves an incoming property name to the name declared by the Schema at `path`.
|
|
180
|
-
|
|
181
|
-
Without `keyMatching` this only accepts an exact declaration. With `keyMatching: true`
|
|
182
|
-
the comparison ignores case and the separators used by the two common naming styles
|
|
183
|
-
(`user_name` / `userName` / `USER NAME`). A function matcher decides itself and may
|
|
184
|
-
return a name the Schema does not declare.
|
|
185
|
-
|
|
186
|
-
Besides renaming, the result is what makes a key a breakpoint for greedy string capture:
|
|
187
|
-
the parser calls this while deciding whether an upcoming `key:` ends the value in front of it.
|
|
188
|
-
|
|
189
|
-
#### Parameters
|
|
190
|
-
|
|
191
|
-
##### path
|
|
192
|
-
|
|
193
|
-
[`JSONPath`](../type-aliases/JSONPath.md)
|
|
194
|
-
|
|
195
|
-
The current parent path.
|
|
196
|
-
|
|
197
|
-
##### key
|
|
198
|
-
|
|
199
|
-
`string`
|
|
200
|
-
|
|
201
|
-
The property key found in the broken document.
|
|
202
|
-
|
|
203
|
-
#### Returns
|
|
204
|
-
|
|
205
|
-
`string` \| `null`
|
|
206
|
-
|
|
207
|
-
The canonical Schema property name, or `null` when nothing matches.
|
|
208
|
-
|
|
209
|
-
***
|
|
210
|
-
|
|
211
|
-
### create()
|
|
212
|
-
|
|
213
|
-
> `static` **create**(`schema`, `options?`): `Promise`\<`SchemaWalker`\>
|
|
214
|
-
|
|
215
|
-
Defined in: [walker/schema-walker.ts:30](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/walker/schema-walker.ts#L30)
|
|
216
|
-
|
|
217
|
-
Static factory method to create a SchemaWalker instance.
|
|
218
|
-
Asynchronously dereferences the provided JSON Schema.
|
|
219
|
-
|
|
220
|
-
The Schema is dereferenced on a deep copy: `$RefParser.dereference()` replaces every
|
|
221
|
-
`$ref` in place, which would otherwise rewrite the object the caller passed in.
|
|
222
|
-
|
|
223
|
-
#### Parameters
|
|
224
|
-
|
|
225
|
-
##### schema
|
|
226
|
-
|
|
227
|
-
`any`
|
|
228
|
-
|
|
229
|
-
The JSON Schema object to walk.
|
|
230
|
-
|
|
231
|
-
##### options?
|
|
232
|
-
|
|
233
|
-
[`RepairOptions`](../interfaces/RepairOptions.md) = `{}`
|
|
234
|
-
|
|
235
|
-
Repair options that concern the Schema itself (`keyMatching`).
|
|
236
|
-
|
|
237
|
-
#### Returns
|
|
238
|
-
|
|
239
|
-
`Promise`\<`SchemaWalker`\>
|
|
240
|
-
|
|
241
|
-
A promise that resolves to a SchemaWalker instance.
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
[**@isdk/json-repair**](../README.md)
|
|
2
|
-
|
|
3
|
-
***
|
|
4
|
-
|
|
5
|
-
[@isdk/json-repair](../globals.md) / jsonRepair
|
|
6
|
-
|
|
7
|
-
# Function: jsonRepair()
|
|
8
|
-
|
|
9
|
-
> **jsonRepair**(`input`, `schema?`, `options?`): `Promise`\<`any`\>
|
|
10
|
-
|
|
11
|
-
Defined in: [repair.ts:21](https://github.com/isdk/json-repair.js/blob/bd999430e5a6ec831a040451c76463ff7791a09a/src/repair.ts#L21)
|
|
12
|
-
|
|
13
|
-
Repairs and parses a JSON string generated by an LLM, guided by a JSON Schema.
|
|
14
|
-
This is the main entry point for the library.
|
|
15
|
-
|
|
16
|
-
## Parameters
|
|
17
|
-
|
|
18
|
-
### input
|
|
19
|
-
|
|
20
|
-
`string`
|
|
21
|
-
|
|
22
|
-
The potentially broken JSON string to be repaired.
|
|
23
|
-
|
|
24
|
-
### schema?
|
|
25
|
-
|
|
26
|
-
`any`
|
|
27
|
-
|
|
28
|
-
Either a plain JSON Schema object or a pre-initialized SchemaWalker instance for better performance in batch processing.
|
|
29
|
-
|
|
30
|
-
### options?
|
|
31
|
-
|
|
32
|
-
[`RepairOptions`](../interfaces/RepairOptions.md) = `{}`
|
|
33
|
-
|
|
34
|
-
Optional configuration to tune the repair process.
|
|
35
|
-
|
|
36
|
-
## Returns
|
|
37
|
-
|
|
38
|
-
`Promise`\<`any`\>
|
|
39
|
-
|
|
40
|
-
A promise that resolves to the repaired and parsed JavaScript object.
|
|
41
|
-
|
|
42
|
-
## Example
|
|
43
|
-
|
|
44
|
-
```typescript
|
|
45
|
-
const schema = { type: 'object', properties: { name: { type: 'string' } } };
|
|
46
|
-
const result = await jsonRepair('{ name: John }', schema);
|
|
47
|
-
console.log(result); // { name: 'John' }
|
|
48
|
-
```
|
package/docs/globals.md
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
[**@isdk/json-repair**](README.md)
|
|
2
|
-
|
|
3
|
-
***
|
|
4
|
-
|
|
5
|
-
# @isdk/json-repair
|
|
6
|
-
|
|
7
|
-
A schema-guided JSON repair library specifically designed for LLM-generated outputs.
|
|
8
|
-
|
|
9
|
-
## Classes
|
|
10
|
-
|
|
11
|
-
- [RepairParser](classes/RepairParser.md)
|
|
12
|
-
- [SchemaWalker](classes/SchemaWalker.md)
|
|
13
|
-
|
|
14
|
-
## Interfaces
|
|
15
|
-
|
|
16
|
-
- [RepairOptions](interfaces/RepairOptions.md)
|
|
17
|
-
|
|
18
|
-
## Type Aliases
|
|
19
|
-
|
|
20
|
-
- [JSONPath](type-aliases/JSONPath.md)
|
|
21
|
-
|
|
22
|
-
## Functions
|
|
23
|
-
|
|
24
|
-
- [jsonRepair](functions/jsonRepair.md)
|