json-rescue 0.2.2 → 2.0.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 +222 -0
- package/README.md +0 -114
- package/dist/fields.d.ts +92 -0
- package/dist/fields.d.ts.map +1 -0
- package/dist/fields.js +207 -0
- package/dist/fields.js.map +1 -0
- package/dist/index.d.ts +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -1
- package/dist/index.js.map +1 -1
- package/dist/repair.d.ts +16 -0
- package/dist/repair.d.ts.map +1 -1
- package/dist/repair.js +103 -1
- package/dist/repair.js.map +1 -1
- package/dist/rescue.d.ts +19 -0
- package/dist/rescue.d.ts.map +1 -1
- package/dist/rescue.js +63 -1
- package/dist/rescue.js.map +1 -1
- package/dist/schema.d.ts +93 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +254 -0
- package/dist/schema.js.map +1 -0
- package/dist/types.d.ts +4 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +4 -1
package/dist/index.js
CHANGED
|
@@ -4,9 +4,10 @@
|
|
|
4
4
|
* @packageDocumentation
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.repairSmartQuotes = exports.repairJsoncComments = exports.repairTrailingCommas = exports.autoRepair = exports.extractBalancedBraces = exports.extractFromMarkdown = exports.extractAllCandidates = exports.rescueJson = void 0;
|
|
7
|
+
exports.createValidationReport = exports.validateSchema = exports.getFieldOrDefault = exports.fieldExists = exports.extractFields = exports.extractField = exports.repairPythonLiterals = exports.repairUnquotedKeys = exports.repairSingleQuotes = exports.repairSmartQuotes = exports.repairJsoncComments = exports.repairTrailingCommas = exports.autoRepair = exports.extractBalancedBraces = exports.extractFromMarkdown = exports.extractAllCandidates = exports.rescueJsonAll = exports.rescueJson = void 0;
|
|
8
8
|
var rescue_1 = require("./rescue");
|
|
9
9
|
Object.defineProperty(exports, "rescueJson", { enumerable: true, get: function () { return rescue_1.rescueJson; } });
|
|
10
|
+
Object.defineProperty(exports, "rescueJsonAll", { enumerable: true, get: function () { return rescue_1.rescueJsonAll; } });
|
|
10
11
|
// Re-export extraction utilities
|
|
11
12
|
var extraction_1 = require("./extraction");
|
|
12
13
|
Object.defineProperty(exports, "extractAllCandidates", { enumerable: true, get: function () { return extraction_1.extractAllCandidates; } });
|
|
@@ -18,4 +19,17 @@ Object.defineProperty(exports, "autoRepair", { enumerable: true, get: function (
|
|
|
18
19
|
Object.defineProperty(exports, "repairTrailingCommas", { enumerable: true, get: function () { return repair_1.repairTrailingCommas; } });
|
|
19
20
|
Object.defineProperty(exports, "repairJsoncComments", { enumerable: true, get: function () { return repair_1.repairJsoncComments; } });
|
|
20
21
|
Object.defineProperty(exports, "repairSmartQuotes", { enumerable: true, get: function () { return repair_1.repairSmartQuotes; } });
|
|
22
|
+
Object.defineProperty(exports, "repairSingleQuotes", { enumerable: true, get: function () { return repair_1.repairSingleQuotes; } });
|
|
23
|
+
Object.defineProperty(exports, "repairUnquotedKeys", { enumerable: true, get: function () { return repair_1.repairUnquotedKeys; } });
|
|
24
|
+
Object.defineProperty(exports, "repairPythonLiterals", { enumerable: true, get: function () { return repair_1.repairPythonLiterals; } });
|
|
25
|
+
// Re-export field extraction utilities (v1.2.0)
|
|
26
|
+
var fields_1 = require("./fields");
|
|
27
|
+
Object.defineProperty(exports, "extractField", { enumerable: true, get: function () { return fields_1.extractField; } });
|
|
28
|
+
Object.defineProperty(exports, "extractFields", { enumerable: true, get: function () { return fields_1.extractFields; } });
|
|
29
|
+
Object.defineProperty(exports, "fieldExists", { enumerable: true, get: function () { return fields_1.fieldExists; } });
|
|
30
|
+
Object.defineProperty(exports, "getFieldOrDefault", { enumerable: true, get: function () { return fields_1.getFieldOrDefault; } });
|
|
31
|
+
// Re-export schema validation utilities (v2.0.0)
|
|
32
|
+
var schema_1 = require("./schema");
|
|
33
|
+
Object.defineProperty(exports, "validateSchema", { enumerable: true, get: function () { return schema_1.validateSchema; } });
|
|
34
|
+
Object.defineProperty(exports, "createValidationReport", { enumerable: true, get: function () { return schema_1.createValidationReport; } });
|
|
21
35
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,mCAAqD;AAA5C,oGAAA,UAAU,OAAA;AAAE,uGAAA,aAAa,OAAA;AAGlC,iCAAiC;AACjC,2CAAgG;AAAvF,kHAAA,oBAAoB,OAAA;AAAE,iHAAA,mBAAmB,OAAA;AAAE,mHAAA,qBAAqB,OAAA;AAEzE,6BAA6B;AAC7B,mCAQkB;AAPhB,oGAAA,UAAU,OAAA;AACV,8GAAA,oBAAoB,OAAA;AACpB,6GAAA,mBAAmB,OAAA;AACnB,2GAAA,iBAAiB,OAAA;AACjB,4GAAA,kBAAkB,OAAA;AAClB,4GAAA,kBAAkB,OAAA;AAClB,8GAAA,oBAAoB,OAAA;AAGtB,gDAAgD;AAChD,mCAAuF;AAA9E,sGAAA,YAAY,OAAA;AAAE,uGAAA,aAAa,OAAA;AAAE,qGAAA,WAAW,OAAA;AAAE,2GAAA,iBAAiB,OAAA;AAGpE,iDAAiD;AACjD,mCAAkE;AAAzD,wGAAA,cAAc,OAAA;AAAE,gHAAA,sBAAsB,OAAA"}
|
package/dist/repair.d.ts
CHANGED
|
@@ -25,6 +25,22 @@ export declare function repairJsoncComments(text: string): RepairResult;
|
|
|
25
25
|
* e.g., "hello" → "hello"
|
|
26
26
|
*/
|
|
27
27
|
export declare function repairSmartQuotes(text: string): RepairResult;
|
|
28
|
+
/**
|
|
29
|
+
* Replace single quotes with double quotes for JSON keys and string values
|
|
30
|
+
* e.g., {'key': 'value'} → {"key": "value"}
|
|
31
|
+
* Note: Only replaces quotes around identifiers and string literals at top level
|
|
32
|
+
*/
|
|
33
|
+
export declare function repairSingleQuotes(text: string): RepairResult;
|
|
34
|
+
/**
|
|
35
|
+
* Add quotes around unquoted object keys
|
|
36
|
+
* e.g., {key: "value"} → {"key": "value"}
|
|
37
|
+
*/
|
|
38
|
+
export declare function repairUnquotedKeys(text: string): RepairResult;
|
|
39
|
+
/**
|
|
40
|
+
* Convert Python literal syntax to JSON
|
|
41
|
+
* e.g., True → true, False → false, None → null
|
|
42
|
+
*/
|
|
43
|
+
export declare function repairPythonLiterals(text: string): RepairResult;
|
|
28
44
|
/**
|
|
29
45
|
* Check if text is valid JSON without throwing
|
|
30
46
|
*/
|
package/dist/repair.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"repair.d.ts","sourceRoot":"","sources":["../src/repair.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAEtC,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,WAAW,EAAE,CAAC;CACvB;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,
|
|
1
|
+
{"version":3,"file":"repair.d.ts","sourceRoot":"","sources":["../src/repair.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAEtC,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,WAAW,EAAE,CAAC;CACvB;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,CAwBrD;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,CAkB/D;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,CA2B9D;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,CAmB5D;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,CA+B7D;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,CAyB7D;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,CAgC/D;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAOjD"}
|
package/dist/repair.js
CHANGED
|
@@ -7,6 +7,9 @@ exports.autoRepair = autoRepair;
|
|
|
7
7
|
exports.repairTrailingCommas = repairTrailingCommas;
|
|
8
8
|
exports.repairJsoncComments = repairJsoncComments;
|
|
9
9
|
exports.repairSmartQuotes = repairSmartQuotes;
|
|
10
|
+
exports.repairSingleQuotes = repairSingleQuotes;
|
|
11
|
+
exports.repairUnquotedKeys = repairUnquotedKeys;
|
|
12
|
+
exports.repairPythonLiterals = repairPythonLiterals;
|
|
10
13
|
exports.isValidJson = isValidJson;
|
|
11
14
|
/**
|
|
12
15
|
* Apply all default repairs to the text
|
|
@@ -15,7 +18,14 @@ function autoRepair(text) {
|
|
|
15
18
|
let current = text;
|
|
16
19
|
const allIssues = [];
|
|
17
20
|
// Apply repairs in order
|
|
18
|
-
const repairs = [
|
|
21
|
+
const repairs = [
|
|
22
|
+
repairTrailingCommas,
|
|
23
|
+
repairJsoncComments,
|
|
24
|
+
repairSmartQuotes,
|
|
25
|
+
repairSingleQuotes,
|
|
26
|
+
repairUnquotedKeys,
|
|
27
|
+
repairPythonLiterals,
|
|
28
|
+
];
|
|
19
29
|
for (const repair of repairs) {
|
|
20
30
|
const result = repair(current);
|
|
21
31
|
current = result.text;
|
|
@@ -96,6 +106,98 @@ function repairSmartQuotes(text) {
|
|
|
96
106
|
.replace(/[\u2018\u2019]/g, "'"); // Convert curly single quotes
|
|
97
107
|
return { text: repaired, issues };
|
|
98
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* Replace single quotes with double quotes for JSON keys and string values
|
|
111
|
+
* e.g., {'key': 'value'} → {"key": "value"}
|
|
112
|
+
* Note: Only replaces quotes around identifiers and string literals at top level
|
|
113
|
+
*/
|
|
114
|
+
function repairSingleQuotes(text) {
|
|
115
|
+
const issues = [];
|
|
116
|
+
let inDoubleQuote = false;
|
|
117
|
+
let result = '';
|
|
118
|
+
let modified = false;
|
|
119
|
+
for (let i = 0; i < text.length; i++) {
|
|
120
|
+
const char = text[i];
|
|
121
|
+
// Track if we're in a double-quoted string
|
|
122
|
+
if (char === '"' && (i === 0 || text[i - 1] !== '\\')) {
|
|
123
|
+
inDoubleQuote = !inDoubleQuote;
|
|
124
|
+
result += char;
|
|
125
|
+
}
|
|
126
|
+
else if (!inDoubleQuote && char === "'" && (i === 0 || text[i - 1] !== '\\')) {
|
|
127
|
+
// Replace single quotes with double quotes when not already in a double-quoted string
|
|
128
|
+
result += '"';
|
|
129
|
+
modified = true;
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
result += char;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (modified) {
|
|
136
|
+
issues.push({
|
|
137
|
+
code: 'SINGLE_QUOTES',
|
|
138
|
+
message: 'Converted single quotes to double quotes',
|
|
139
|
+
severity: 'warning',
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
return { text: result, issues };
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Add quotes around unquoted object keys
|
|
146
|
+
* e.g., {key: "value"} → {"key": "value"}
|
|
147
|
+
*/
|
|
148
|
+
function repairUnquotedKeys(text) {
|
|
149
|
+
const issues = [];
|
|
150
|
+
// Match unquoted keys: word characters followed by colon
|
|
151
|
+
// But not inside strings
|
|
152
|
+
let result = text;
|
|
153
|
+
let modified = false;
|
|
154
|
+
// Simple regex to find unquoted keys - matches word boundaries before colons
|
|
155
|
+
const unquotedKeyRegex = /([{,]\s*)([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:/g;
|
|
156
|
+
if (unquotedKeyRegex.test(result)) {
|
|
157
|
+
modified = true;
|
|
158
|
+
result = result.replace(unquotedKeyRegex, '$1"$2":');
|
|
159
|
+
}
|
|
160
|
+
if (modified) {
|
|
161
|
+
issues.push({
|
|
162
|
+
code: 'UNQUOTED_KEYS',
|
|
163
|
+
message: 'Added quotes around unquoted object keys',
|
|
164
|
+
severity: 'warning',
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
return { text: result, issues };
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Convert Python literal syntax to JSON
|
|
171
|
+
* e.g., True → true, False → false, None → null
|
|
172
|
+
*/
|
|
173
|
+
function repairPythonLiterals(text) {
|
|
174
|
+
const issues = [];
|
|
175
|
+
let result = text;
|
|
176
|
+
let modified = false;
|
|
177
|
+
// Replace Python True with JSON true
|
|
178
|
+
if (/\bTrue\b/.test(result)) {
|
|
179
|
+
result = result.replace(/\bTrue\b/g, 'true');
|
|
180
|
+
modified = true;
|
|
181
|
+
}
|
|
182
|
+
// Replace Python False with JSON false
|
|
183
|
+
if (/\bFalse\b/.test(result)) {
|
|
184
|
+
result = result.replace(/\bFalse\b/g, 'false');
|
|
185
|
+
modified = true;
|
|
186
|
+
}
|
|
187
|
+
// Replace Python None with JSON null
|
|
188
|
+
if (/\bNone\b/.test(result)) {
|
|
189
|
+
result = result.replace(/\bNone\b/g, 'null');
|
|
190
|
+
modified = true;
|
|
191
|
+
}
|
|
192
|
+
if (modified) {
|
|
193
|
+
issues.push({
|
|
194
|
+
code: 'PYTHON_LITERALS',
|
|
195
|
+
message: 'Converted Python literals to JSON (True/False/None)',
|
|
196
|
+
severity: 'warning',
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
return { text: result, issues };
|
|
200
|
+
}
|
|
99
201
|
/**
|
|
100
202
|
* Check if text is valid JSON without throwing
|
|
101
203
|
*/
|
package/dist/repair.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"repair.js","sourceRoot":"","sources":["../src/repair.ts"],"names":[],"mappings":";AAAA;;GAEG;;AAYH,
|
|
1
|
+
{"version":3,"file":"repair.js","sourceRoot":"","sources":["../src/repair.ts"],"names":[],"mappings":";AAAA;;GAEG;;AAYH,gCAwBC;AAMD,oDAkBC;AAMD,kDA2BC;AAMD,8CAmBC;AAOD,gDA+BC;AAMD,gDAyBC;AAMD,oDAgCC;AAKD,kCAOC;AApOD;;GAEG;AACH,SAAgB,UAAU,CAAC,IAAY;IACrC,IAAI,OAAO,GAAG,IAAI,CAAC;IACnB,MAAM,SAAS,GAAkB,EAAE,CAAC;IAEpC,yBAAyB;IACzB,MAAM,OAAO,GAAG;QACd,oBAAoB;QACpB,mBAAmB;QACnB,iBAAiB;QACjB,kBAAkB;QAClB,kBAAkB;QAClB,oBAAoB;KACrB,CAAC;IAEF,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;QAC7B,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;QAC/B,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC;QACtB,SAAS,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;IACnC,CAAC;IAED,OAAO;QACL,IAAI,EAAE,OAAO;QACb,MAAM,EAAE,SAAS;KAClB,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,SAAgB,oBAAoB,CAAC,IAAY;IAC/C,MAAM,MAAM,GAAkB,EAAE,CAAC;IACjC,IAAI,QAAQ,GAAG,KAAK,CAAC;IAErB,+DAA+D;IAC/D,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,cAAc,EAAE,CAAC,KAAa,EAAE,OAAe,EAAU,EAAE;QACvF,IAAI,CAAC,QAAQ,EAAE,CAAC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C;QAC5B,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;QAC7C,QAAQ,GAAG,IAAI,CAAC;IAClB,CAAC;IAED,uCAAuC;IACvC,IAAI,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;QAC7B,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;QAC/C,QAAQ,GAAG,IAAI,CAAC;IAClB,CAAC;IAED,qCAAqC;IACrC,IAAI,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;QAC5B,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;QAC7C,QAAQ,GAAG,IAAI,CAAC;IAClB,CAAC;IAED,IAAI,QAAQ,EAAE,CAAC;QACb,MAAM,CAAC,IAAI,CAAC;YACV,IAAI,EAAE,iBAAiB;YACvB,OAAO,EAAE,qDAAqD;YAC9D,QAAQ,EAAE,SAAS;SACpB,CAAC,CAAC;IACL,CAAC;IAED,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;AAClC,CAAC;AAED;;GAEG;AACH,SAAgB,WAAW,CAAC,IAAY;IACtC,IAAI,CAAC;QACH,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACjB,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC"}
|
package/dist/rescue.d.ts
CHANGED
|
@@ -20,5 +20,24 @@ import { RescueResult, RescueOptions } from './types';
|
|
|
20
20
|
* ```
|
|
21
21
|
*/
|
|
22
22
|
export declare function rescueJson<T = unknown>(text: string, options?: RescueOptions): RescueResult<T> | RescueResult<T>[];
|
|
23
|
+
/**
|
|
24
|
+
* Convenience wrapper to get all JSON candidates
|
|
25
|
+
*
|
|
26
|
+
* @template T - The expected type of the parsed JSON
|
|
27
|
+
* @param text - The text containing JSON
|
|
28
|
+
* @param options - Configuration options (mode is always 'all')
|
|
29
|
+
* @returns An array of RescueResult
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const results = rescueJsonAll<{ name: string }>(mixedText);
|
|
34
|
+
* results.forEach(result => {
|
|
35
|
+
* if (result.success) {
|
|
36
|
+
* console.log(result.data);
|
|
37
|
+
* }
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare function rescueJsonAll<T = unknown>(text: string, options?: Omit<RescueOptions, 'mode'>): RescueResult<T>[];
|
|
23
42
|
export { RescueResult, RescueOptions, RepairIssue } from './types';
|
|
24
43
|
//# sourceMappingURL=rescue.d.ts.map
|
package/dist/rescue.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rescue.d.ts","sourceRoot":"","sources":["../src/rescue.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,YAAY,EAAE,aAAa,EAAe,MAAM,SAAS,CAAC;AAInE;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,UAAU,CAAC,CAAC,GAAG,OAAO,EACpC,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,aAAkB,GAC1B,YAAY,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"rescue.d.ts","sourceRoot":"","sources":["../src/rescue.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,YAAY,EAAE,aAAa,EAAe,MAAM,SAAS,CAAC;AAInE;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,UAAU,CAAC,CAAC,GAAG,OAAO,EACpC,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,aAAkB,GAC1B,YAAY,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,EAAE,CAqDrC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,aAAa,CAAC,CAAC,GAAG,OAAO,EACvC,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,IAAI,CAAC,aAAa,EAAE,MAAM,CAAM,GACxC,YAAY,CAAC,CAAC,CAAC,EAAE,CAGnB;AA+FD,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC"}
|
package/dist/rescue.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.rescueJson = rescueJson;
|
|
7
|
+
exports.rescueJsonAll = rescueJsonAll;
|
|
7
8
|
const extraction_1 = require("./extraction");
|
|
8
9
|
const repair_1 = require("./repair");
|
|
9
10
|
/**
|
|
@@ -40,6 +41,7 @@ function rescueJson(text, options = {}) {
|
|
|
40
41
|
],
|
|
41
42
|
raw: '',
|
|
42
43
|
repaired: '',
|
|
44
|
+
score: 0,
|
|
43
45
|
};
|
|
44
46
|
return mode === 'all' ? [emptyResult] : emptyResult;
|
|
45
47
|
}
|
|
@@ -57,9 +59,39 @@ function rescueJson(text, options = {}) {
|
|
|
57
59
|
if (mode === 'all') {
|
|
58
60
|
return results;
|
|
59
61
|
}
|
|
60
|
-
|
|
62
|
+
if (mode === 'best') {
|
|
63
|
+
// Return the result with the highest score
|
|
64
|
+
return results.reduce((best, current) => {
|
|
65
|
+
const bestScore = best.score ?? 0;
|
|
66
|
+
const currentScore = current.score ?? 0;
|
|
67
|
+
return currentScore > bestScore ? current : best;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
// Return first successful or first result (default 'first' mode)
|
|
61
71
|
return results.find((r) => r.success) || results[0];
|
|
62
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Convenience wrapper to get all JSON candidates
|
|
75
|
+
*
|
|
76
|
+
* @template T - The expected type of the parsed JSON
|
|
77
|
+
* @param text - The text containing JSON
|
|
78
|
+
* @param options - Configuration options (mode is always 'all')
|
|
79
|
+
* @returns An array of RescueResult
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* const results = rescueJsonAll<{ name: string }>(mixedText);
|
|
84
|
+
* results.forEach(result => {
|
|
85
|
+
* if (result.success) {
|
|
86
|
+
* console.log(result.data);
|
|
87
|
+
* }
|
|
88
|
+
* });
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
function rescueJsonAll(text, options = {}) {
|
|
92
|
+
const results = rescueJson(text, { ...options, mode: 'all' });
|
|
93
|
+
return Array.isArray(results) ? results : [results];
|
|
94
|
+
}
|
|
63
95
|
/**
|
|
64
96
|
* Process a single candidate: attempt repair and parse
|
|
65
97
|
*/
|
|
@@ -68,12 +100,14 @@ function processCandidate(raw, shouldRepair) {
|
|
|
68
100
|
let repaired = raw;
|
|
69
101
|
// Try to parse as-is first
|
|
70
102
|
if ((0, repair_1.isValidJson)(raw)) {
|
|
103
|
+
const score = calculateScore(raw, []);
|
|
71
104
|
return {
|
|
72
105
|
data: JSON.parse(raw),
|
|
73
106
|
success: true,
|
|
74
107
|
issues: [],
|
|
75
108
|
raw,
|
|
76
109
|
repaired: raw,
|
|
110
|
+
score,
|
|
77
111
|
};
|
|
78
112
|
}
|
|
79
113
|
// If we should repair, apply repairs
|
|
@@ -83,12 +117,14 @@ function processCandidate(raw, shouldRepair) {
|
|
|
83
117
|
allIssues.push(...repairResult.issues);
|
|
84
118
|
// Try parsing after repair
|
|
85
119
|
if ((0, repair_1.isValidJson)(repaired)) {
|
|
120
|
+
const score = calculateScore(repaired, allIssues);
|
|
86
121
|
return {
|
|
87
122
|
data: JSON.parse(repaired),
|
|
88
123
|
success: true,
|
|
89
124
|
issues: allIssues,
|
|
90
125
|
raw,
|
|
91
126
|
repaired,
|
|
127
|
+
score,
|
|
92
128
|
};
|
|
93
129
|
}
|
|
94
130
|
}
|
|
@@ -113,6 +149,32 @@ function processCandidate(raw, shouldRepair) {
|
|
|
113
149
|
],
|
|
114
150
|
raw,
|
|
115
151
|
repaired,
|
|
152
|
+
score: 0,
|
|
116
153
|
};
|
|
117
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* Calculate a confidence score for the parsed result
|
|
157
|
+
* Score ranges from 0 to 1, where 1 is perfect (no repairs needed)
|
|
158
|
+
* and 0 is a parse failure
|
|
159
|
+
*/
|
|
160
|
+
function calculateScore(text, issues) {
|
|
161
|
+
// Base score is 1.0 (perfect)
|
|
162
|
+
let score = 1.0;
|
|
163
|
+
// Penalize for each repair issue
|
|
164
|
+
// Warning issues reduce score by 0.05, error issues reduce by 0.1
|
|
165
|
+
for (const issue of issues) {
|
|
166
|
+
if (issue.severity === 'warning') {
|
|
167
|
+
score -= 0.05;
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
score -= 0.1;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// Penalize for length (longer text might have more issues)
|
|
174
|
+
// But less significantly
|
|
175
|
+
const lengthPenalty = Math.log(text.length) / 1000;
|
|
176
|
+
score -= Math.min(lengthPenalty, 0.1);
|
|
177
|
+
// Ensure score stays in valid range
|
|
178
|
+
return Math.max(0, Math.min(1, score));
|
|
179
|
+
}
|
|
118
180
|
//# sourceMappingURL=rescue.js.map
|
package/dist/rescue.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rescue.js","sourceRoot":"","sources":["../src/rescue.ts"],"names":[],"mappings":";AAAA;;GAEG;;AAuBH,
|
|
1
|
+
{"version":3,"file":"rescue.js","sourceRoot":"","sources":["../src/rescue.ts"],"names":[],"mappings":";AAAA;;GAEG;;AAuBH,gCAwDC;AAoBD,sCAMC;AAtGD,6CAAoD;AACpD,qCAAmD;AAEnD;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,UAAU,CACxB,IAAY,EACZ,UAAyB,EAAE;IAE3B,MAAM,EAAE,IAAI,GAAG,OAAO,EAAE,UAA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AK,IAAI,GAAG,CAAC;QACf,CAAC;IACH,CAAC;IAED,2DAA2D;IAC3D,yBAAyB;IACzB,MAAM,aAAa,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC;IACnD,KAAK,IAAI,IAAI,CAAC,GAAG,CAAC,aAAa,EAAE,GAAG,CAAC,CAAC;IAEtC,oCAAoC;IACpC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;AACzC,CAAC"}
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema validation API - Validate parsed JSON against a schema
|
|
3
|
+
* Provides optional schema validation with detailed error reporting
|
|
4
|
+
*/
|
|
5
|
+
import { RepairIssue } from './types';
|
|
6
|
+
/**
|
|
7
|
+
* Simple JSON schema definition (subset of JSON Schema)
|
|
8
|
+
*/
|
|
9
|
+
export interface JsonSchema {
|
|
10
|
+
type?: 'object' | 'array' | 'string' | 'number' | 'boolean' | 'null';
|
|
11
|
+
required?: string[];
|
|
12
|
+
properties?: Record<string, JsonSchema>;
|
|
13
|
+
items?: JsonSchema;
|
|
14
|
+
minLength?: number;
|
|
15
|
+
maxLength?: number;
|
|
16
|
+
minimum?: number;
|
|
17
|
+
maximum?: number;
|
|
18
|
+
pattern?: string;
|
|
19
|
+
enum?: any[];
|
|
20
|
+
description?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Result of schema validation
|
|
24
|
+
*/
|
|
25
|
+
export interface SchemaValidationResult {
|
|
26
|
+
/** Whether validation passed */
|
|
27
|
+
valid: boolean;
|
|
28
|
+
/** Validation errors if any */
|
|
29
|
+
errors: SchemaValidationError[];
|
|
30
|
+
/** Number of errors found */
|
|
31
|
+
errorCount: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Schema validation error
|
|
35
|
+
*/
|
|
36
|
+
export interface SchemaValidationError {
|
|
37
|
+
/** The path to the invalid field (dot-notation) */
|
|
38
|
+
path: string;
|
|
39
|
+
/** The validation rule that failed */
|
|
40
|
+
rule: string;
|
|
41
|
+
/** Human-readable error message */
|
|
42
|
+
message: string;
|
|
43
|
+
/** Expected value/type */
|
|
44
|
+
expected?: string;
|
|
45
|
+
/** Actual value */
|
|
46
|
+
actual?: unknown;
|
|
47
|
+
/** Error severity */
|
|
48
|
+
severity: 'error' | 'warning';
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Validate a parsed JSON object against a schema
|
|
52
|
+
*
|
|
53
|
+
* @param data - The data to validate
|
|
54
|
+
* @param schema - The schema to validate against
|
|
55
|
+
* @returns Validation result with any errors found
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const schema: JsonSchema = {
|
|
60
|
+
* type: 'object',
|
|
61
|
+
* required: ['id', 'name'],
|
|
62
|
+
* properties: {
|
|
63
|
+
* id: { type: 'number' },
|
|
64
|
+
* name: { type: 'string' },
|
|
65
|
+
* email: { type: 'string', pattern: '^[a-z0-9]+@[a-z0-9]+\\.[a-z]{2,}$' }
|
|
66
|
+
* }
|
|
67
|
+
* };
|
|
68
|
+
*
|
|
69
|
+
* const result = validateSchema(data, schema);
|
|
70
|
+
* if (!result.valid) {
|
|
71
|
+
* console.log(result.errors);
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export declare function validateSchema(data: unknown, schema: JsonSchema): SchemaValidationResult;
|
|
76
|
+
/**
|
|
77
|
+
* Combine schema validation with repair issues into a single report
|
|
78
|
+
*/
|
|
79
|
+
export interface ValidationReport {
|
|
80
|
+
/** Repair issues from JSON extraction */
|
|
81
|
+
repairIssues: RepairIssue[];
|
|
82
|
+
/** Schema validation results */
|
|
83
|
+
schemaValidation: SchemaValidationResult;
|
|
84
|
+
/** Overall success status */
|
|
85
|
+
isValid: boolean;
|
|
86
|
+
/** Total error count */
|
|
87
|
+
totalErrors: number;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Create a validation report combining repair and schema validation
|
|
91
|
+
*/
|
|
92
|
+
export declare function createValidationReport(repairIssues: RepairIssue[], schemaValidation: SchemaValidationResult): ValidationReport;
|
|
93
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAEtC;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,IAAI,CAAC,EAAE,QAAQ,GAAG,OAAO,GAAG,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,MAAM,CAAC;IACrE,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IACxC,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,gCAAgC;IAChC,KAAK,EAAE,OAAO,CAAC;IACf,+BAA+B;IAC/B,MAAM,EAAE,qBAAqB,EAAE,CAAC;IAChC,6BAA6B;IAC7B,UAAU,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,mDAAmD;IACnD,IAAI,EAAE,MAAM,CAAC;IACb,sCAA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