@ai-sdk-tool/parser 3.2.1 → 3.3.1
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/README.md +24 -116
- package/dist/{chunk-DCK5APVO.js → chunk-7E6UFDFQ.js} +15 -12
- package/dist/chunk-7E6UFDFQ.js.map +1 -0
- package/dist/chunk-EW3A6Y7O.js +2216 -0
- package/dist/chunk-EW3A6Y7O.js.map +1 -0
- package/dist/chunk-IX4FJELL.js +671 -0
- package/dist/chunk-IX4FJELL.js.map +1 -0
- package/dist/chunk-OUGMLYAW.js +389 -0
- package/dist/chunk-OUGMLYAW.js.map +1 -0
- package/dist/community.cjs +3442 -276
- package/dist/community.cjs.map +1 -1
- package/dist/community.js +4 -1
- package/dist/community.js.map +1 -1
- package/dist/index.cjs +3505 -254
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +11 -1
- package/dist/rjson.cjs +697 -0
- package/dist/rjson.cjs.map +1 -0
- package/dist/rjson.d.cts +108 -0
- package/dist/rjson.d.ts +108 -0
- package/dist/rjson.js +11 -0
- package/dist/rjson.js.map +1 -0
- package/dist/rxml.cjs +2620 -0
- package/dist/rxml.cjs.map +1 -0
- package/dist/rxml.d.cts +90 -0
- package/dist/rxml.d.ts +90 -0
- package/dist/rxml.js +10 -0
- package/dist/rxml.js.map +1 -0
- package/dist/schema-coerce.cjs +415 -0
- package/dist/schema-coerce.cjs.map +1 -0
- package/dist/schema-coerce.d.cts +5 -0
- package/dist/schema-coerce.d.ts +5 -0
- package/dist/schema-coerce.js +11 -0
- package/dist/schema-coerce.js.map +1 -0
- package/package.json +46 -15
- package/dist/chunk-DCK5APVO.js.map +0 -1
package/dist/rxml.d.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core types for the robust-xml parser
|
|
3
|
+
* Based on TXML structure but enhanced for schema-aware parsing
|
|
4
|
+
*/
|
|
5
|
+
type OnErrorFn = (message: string, metadata?: Record<string, unknown>) => void;
|
|
6
|
+
/**
|
|
7
|
+
* Represents a parsed XML node in the DOM tree
|
|
8
|
+
*/
|
|
9
|
+
interface RXMLNode {
|
|
10
|
+
tagName: string;
|
|
11
|
+
attributes: Record<string, string | null>;
|
|
12
|
+
children: (RXMLNode | string)[];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Options for XML parsing
|
|
16
|
+
*/
|
|
17
|
+
interface ParseOptions {
|
|
18
|
+
/** Position to start parsing from (for streaming) */
|
|
19
|
+
pos?: number;
|
|
20
|
+
/** Array of tag names that don't have children and don't need to be closed */
|
|
21
|
+
noChildNodes?: string[];
|
|
22
|
+
/** Whether to set position information in result */
|
|
23
|
+
setPos?: boolean;
|
|
24
|
+
/** Keep comments in the parsed result */
|
|
25
|
+
keepComments?: boolean;
|
|
26
|
+
/** Keep whitespace like spaces, tabs and line breaks as string content */
|
|
27
|
+
keepWhitespace?: boolean;
|
|
28
|
+
/** Name of the text node property (default: "#text") */
|
|
29
|
+
textNodeName?: string;
|
|
30
|
+
/** Whether to throw on duplicate string tags */
|
|
31
|
+
throwOnDuplicateStringTags?: boolean;
|
|
32
|
+
/** Error handling callback */
|
|
33
|
+
onError?: OnErrorFn;
|
|
34
|
+
/**
|
|
35
|
+
* Apply internal repair heuristics for fragment parsing.
|
|
36
|
+
* When enabled, parsing attempts normalization/repair before throwing.
|
|
37
|
+
*/
|
|
38
|
+
repair?: boolean;
|
|
39
|
+
/** Maximum reparses when repair heuristics are enabled */
|
|
40
|
+
maxReparses?: number;
|
|
41
|
+
/** Whether to parse a single node instead of children */
|
|
42
|
+
parseNode?: boolean;
|
|
43
|
+
/** Filter function for nodes */
|
|
44
|
+
filter?: (node: RXMLNode, index: number, depth: number, path: string) => boolean;
|
|
45
|
+
/** Simplify the result structure */
|
|
46
|
+
simplify?: boolean;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Options for XML stringification
|
|
50
|
+
*/
|
|
51
|
+
interface StringifyOptions {
|
|
52
|
+
/** Whether to format the output with indentation */
|
|
53
|
+
format?: boolean;
|
|
54
|
+
/** Whether to suppress empty nodes */
|
|
55
|
+
suppressEmptyNode?: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Whether to use minimal escaping per XML 1.0:
|
|
58
|
+
* - In character data: escape '&' and '<' (and '>' only in ']]>' sequence)
|
|
59
|
+
* - In attribute values: escape '&', '<', and only the wrapping quote
|
|
60
|
+
* Defaults to false (conservative escaping of &, <, >, ", ')
|
|
61
|
+
*/
|
|
62
|
+
minimalEscaping?: boolean;
|
|
63
|
+
/** Error handling callback */
|
|
64
|
+
onError?: OnErrorFn;
|
|
65
|
+
/**
|
|
66
|
+
* Whether to serialize boolean-like attributes (value === null)
|
|
67
|
+
* as name="name" to follow strict XML attribute rules.
|
|
68
|
+
* When false (default), serialize as a convenience flag without value
|
|
69
|
+
* (e.g., <item checked>), for compatibility with existing outputs.
|
|
70
|
+
*/
|
|
71
|
+
strictBooleanAttributes?: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Whether to include the XML declaration
|
|
74
|
+
*/
|
|
75
|
+
declaration?: boolean;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* XML stringification based on TXML's stringify approach
|
|
80
|
+
* Replaces the fast-xml-parser XMLBuilder with a native implementation
|
|
81
|
+
*/
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Stringify an object to XML
|
|
85
|
+
*/
|
|
86
|
+
declare function stringify(rootTag: string, obj: unknown, options?: StringifyOptions): string;
|
|
87
|
+
|
|
88
|
+
declare function parse(xml: string, schema: unknown, options?: ParseOptions): Record<string, unknown>;
|
|
89
|
+
|
|
90
|
+
export { type ParseOptions, type StringifyOptions, parse, stringify };
|
package/dist/rxml.js
ADDED
package/dist/rxml.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/schema-coerce/index.ts
|
|
21
|
+
var schema_coerce_exports = {};
|
|
22
|
+
__export(schema_coerce_exports, {
|
|
23
|
+
coerceBySchema: () => coerceBySchema,
|
|
24
|
+
getSchemaType: () => getSchemaType,
|
|
25
|
+
unwrapJsonSchema: () => unwrapJsonSchema
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(schema_coerce_exports);
|
|
28
|
+
var NUMERIC_REGEX = /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/;
|
|
29
|
+
var EMPTY_OBJECT_REGEX = /^\{\s*\}$/s;
|
|
30
|
+
var NEWLINE_SPLIT_REGEX = /\n+/;
|
|
31
|
+
var COMMA_SPLIT_REGEX = /,\s*/;
|
|
32
|
+
var DIGIT_KEY_REGEX = /^\d+$/;
|
|
33
|
+
function unwrapJsonSchema(schema) {
|
|
34
|
+
if (!schema || typeof schema !== "object") {
|
|
35
|
+
return schema;
|
|
36
|
+
}
|
|
37
|
+
const s = schema;
|
|
38
|
+
if (s.jsonSchema && typeof s.jsonSchema === "object") {
|
|
39
|
+
return unwrapJsonSchema(s.jsonSchema);
|
|
40
|
+
}
|
|
41
|
+
return schema;
|
|
42
|
+
}
|
|
43
|
+
function getSchemaType(schema) {
|
|
44
|
+
const unwrapped = unwrapJsonSchema(schema);
|
|
45
|
+
if (!unwrapped || typeof unwrapped !== "object") {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const t = unwrapped.type;
|
|
49
|
+
if (typeof t === "string") {
|
|
50
|
+
return t;
|
|
51
|
+
}
|
|
52
|
+
if (Array.isArray(t)) {
|
|
53
|
+
const preferred = [
|
|
54
|
+
"object",
|
|
55
|
+
"array",
|
|
56
|
+
"boolean",
|
|
57
|
+
"number",
|
|
58
|
+
"integer",
|
|
59
|
+
"string"
|
|
60
|
+
];
|
|
61
|
+
for (const p of preferred) {
|
|
62
|
+
if (t.includes(p)) {
|
|
63
|
+
return p;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const s = unwrapped;
|
|
68
|
+
if (s && typeof s === "object" && (s.properties || s.additionalProperties)) {
|
|
69
|
+
return "object";
|
|
70
|
+
}
|
|
71
|
+
if (s && typeof s === "object" && (s.items || s.prefixItems)) {
|
|
72
|
+
return "array";
|
|
73
|
+
}
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
function schemaAllowsPropertyViaCombinators(s, key, depth) {
|
|
77
|
+
const anyOfValues = s.anyOf;
|
|
78
|
+
const oneOfValues = s.oneOf;
|
|
79
|
+
const allOfValues = s.allOf;
|
|
80
|
+
let hasCombinator = false;
|
|
81
|
+
let anyOfAllows = true;
|
|
82
|
+
let oneOfAllows = true;
|
|
83
|
+
let allOfAllows = true;
|
|
84
|
+
if (Array.isArray(anyOfValues)) {
|
|
85
|
+
hasCombinator = true;
|
|
86
|
+
anyOfAllows = anyOfValues.some(
|
|
87
|
+
(sub) => schemaHasProperty(sub, key, depth + 1)
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
if (Array.isArray(oneOfValues)) {
|
|
91
|
+
hasCombinator = true;
|
|
92
|
+
oneOfAllows = oneOfValues.some(
|
|
93
|
+
(sub) => schemaHasProperty(sub, key, depth + 1)
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
if (Array.isArray(allOfValues)) {
|
|
97
|
+
hasCombinator = true;
|
|
98
|
+
allOfAllows = allOfValues.every(
|
|
99
|
+
(sub) => schemaHasProperty(sub, key, depth + 1)
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
if (!hasCombinator) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
return anyOfAllows && oneOfAllows && allOfAllows;
|
|
106
|
+
}
|
|
107
|
+
function schemaHasPropertyDirectly(s, key) {
|
|
108
|
+
const props = s.properties;
|
|
109
|
+
if (props && typeof props === "object" && !Array.isArray(props) && Object.hasOwn(props, key) && props[key] !== false) {
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
const required = s.required;
|
|
113
|
+
if (Array.isArray(required) && required.includes(key)) {
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
const patternSchemas = getPatternSchemasForKey(s.patternProperties, key);
|
|
117
|
+
return patternSchemas.some((schema) => schema !== false);
|
|
118
|
+
}
|
|
119
|
+
function schemaHasPropertyViaAdditional(s) {
|
|
120
|
+
const additional = s.additionalProperties;
|
|
121
|
+
if (additional === true || additional && typeof additional === "object" && !Array.isArray(additional)) {
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
if (Object.hasOwn(s, "additionalProperties")) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
const type = s.type;
|
|
128
|
+
const isObjectType = type === "object" || Array.isArray(type) && type.includes("object");
|
|
129
|
+
const hasObjectKeywords = s.properties && typeof s.properties === "object" && !Array.isArray(s.properties) || s.patternProperties && typeof s.patternProperties === "object" && !Array.isArray(s.patternProperties) || Array.isArray(s.required) && s.required.length > 0;
|
|
130
|
+
return !!(isObjectType || hasObjectKeywords);
|
|
131
|
+
}
|
|
132
|
+
function schemaDisallowsPropertyDirectly(s, key) {
|
|
133
|
+
const props = s.properties;
|
|
134
|
+
if (props && typeof props === "object" && !Array.isArray(props) && Object.hasOwn(props, key) && props[key] === false) {
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
const patternSchemas = getPatternSchemasForKey(s.patternProperties, key);
|
|
138
|
+
return patternSchemas.some((schema) => schema === false);
|
|
139
|
+
}
|
|
140
|
+
function schemaHasProperty(schema, key, depth = 0) {
|
|
141
|
+
if (depth > 5) {
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
const unwrapped = unwrapJsonSchema(schema);
|
|
145
|
+
if (schemaIsUnconstrained(unwrapped)) {
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
if (!unwrapped || typeof unwrapped !== "object") {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
const s = unwrapped;
|
|
152
|
+
if (schemaDisallowsPropertyDirectly(s, key)) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
if (schemaHasPropertyDirectly(s, key)) {
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
if (schemaHasPropertyViaAdditional(s)) {
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
return schemaAllowsPropertyViaCombinators(s, key, depth);
|
|
162
|
+
}
|
|
163
|
+
function schemaIsUnconstrained(schema) {
|
|
164
|
+
const unwrapped = unwrapJsonSchema(schema);
|
|
165
|
+
if (unwrapped == null || unwrapped === true) {
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
if (typeof unwrapped !== "object" || Array.isArray(unwrapped)) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
return Object.keys(unwrapped).length === 0;
|
|
172
|
+
}
|
|
173
|
+
function getPatternSchemasForKey(patternProperties, key) {
|
|
174
|
+
if (!patternProperties || typeof patternProperties !== "object" || Array.isArray(patternProperties)) {
|
|
175
|
+
return [];
|
|
176
|
+
}
|
|
177
|
+
const schemas = [];
|
|
178
|
+
for (const [pattern, schema] of Object.entries(
|
|
179
|
+
patternProperties
|
|
180
|
+
)) {
|
|
181
|
+
try {
|
|
182
|
+
const regex = new RegExp(pattern);
|
|
183
|
+
if (regex.test(key)) {
|
|
184
|
+
schemas.push(schema);
|
|
185
|
+
}
|
|
186
|
+
} catch (e) {
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return schemas;
|
|
190
|
+
}
|
|
191
|
+
function coerceValueForKey(value, key, unwrapped) {
|
|
192
|
+
const schemas = [];
|
|
193
|
+
const props = unwrapped.properties;
|
|
194
|
+
if (props && Object.hasOwn(props, key)) {
|
|
195
|
+
schemas.push(props[key]);
|
|
196
|
+
}
|
|
197
|
+
const patternSchemas = getPatternSchemasForKey(
|
|
198
|
+
unwrapped.patternProperties,
|
|
199
|
+
key
|
|
200
|
+
);
|
|
201
|
+
if (patternSchemas.length > 0) {
|
|
202
|
+
schemas.push(...patternSchemas);
|
|
203
|
+
}
|
|
204
|
+
if (schemas.length > 0) {
|
|
205
|
+
let out = value;
|
|
206
|
+
for (const schema of schemas) {
|
|
207
|
+
if (typeof schema === "boolean") {
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
out = coerceBySchema(out, schema);
|
|
211
|
+
}
|
|
212
|
+
return out;
|
|
213
|
+
}
|
|
214
|
+
const additional = unwrapped.additionalProperties;
|
|
215
|
+
if (additional && typeof additional === "object" && !Array.isArray(additional)) {
|
|
216
|
+
return coerceBySchema(value, additional);
|
|
217
|
+
}
|
|
218
|
+
if (additional === true || additional === false) {
|
|
219
|
+
return value;
|
|
220
|
+
}
|
|
221
|
+
return coerceBySchema(value, void 0);
|
|
222
|
+
}
|
|
223
|
+
function coerceStringWithoutSchema(value) {
|
|
224
|
+
const s = value.trim();
|
|
225
|
+
const lower = s.toLowerCase();
|
|
226
|
+
if (lower === "true") {
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
if (lower === "false") {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
if (NUMERIC_REGEX.test(s)) {
|
|
233
|
+
const num = Number(s);
|
|
234
|
+
if (Number.isFinite(num)) {
|
|
235
|
+
return num;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (s.startsWith("{") && s.endsWith("}") || s.startsWith("[") && s.endsWith("]")) {
|
|
239
|
+
try {
|
|
240
|
+
const parsed = JSON.parse(s);
|
|
241
|
+
return coerceBySchema(parsed, void 0);
|
|
242
|
+
} catch (e) {
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return value;
|
|
246
|
+
}
|
|
247
|
+
function coerceStringToObject(s, unwrapped) {
|
|
248
|
+
try {
|
|
249
|
+
let normalized = s.replace(/'/g, '"');
|
|
250
|
+
normalized = normalized.replace(EMPTY_OBJECT_REGEX, "{}");
|
|
251
|
+
const obj = JSON.parse(normalized);
|
|
252
|
+
if (obj && typeof obj === "object" && !Array.isArray(obj)) {
|
|
253
|
+
return coerceObjectToObject(obj, unwrapped);
|
|
254
|
+
}
|
|
255
|
+
} catch (e) {
|
|
256
|
+
}
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
function coerceStringToArray(s, unwrapped) {
|
|
260
|
+
const prefixItems = Array.isArray(unwrapped.prefixItems) ? unwrapped.prefixItems : void 0;
|
|
261
|
+
const itemsSchema = unwrapped.items;
|
|
262
|
+
try {
|
|
263
|
+
const normalized = s.replace(/'/g, '"');
|
|
264
|
+
const arr = JSON.parse(normalized);
|
|
265
|
+
if (Array.isArray(arr)) {
|
|
266
|
+
if (prefixItems && arr.length === prefixItems.length) {
|
|
267
|
+
return arr.map((v, i) => coerceBySchema(v, prefixItems[i]));
|
|
268
|
+
}
|
|
269
|
+
return arr.map((v) => coerceBySchema(v, itemsSchema));
|
|
270
|
+
}
|
|
271
|
+
} catch (e) {
|
|
272
|
+
const csv = s.includes("\n") ? s.split(NEWLINE_SPLIT_REGEX) : s.split(COMMA_SPLIT_REGEX);
|
|
273
|
+
const trimmed = csv.map((x) => x.trim()).filter((x) => x.length > 0);
|
|
274
|
+
if (prefixItems && trimmed.length === prefixItems.length) {
|
|
275
|
+
return trimmed.map((x, i) => coerceBySchema(x, prefixItems[i]));
|
|
276
|
+
}
|
|
277
|
+
return trimmed.map((x) => coerceBySchema(x, itemsSchema));
|
|
278
|
+
}
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
function coerceObjectToObject(value, unwrapped) {
|
|
282
|
+
const out = {};
|
|
283
|
+
for (const [k, v] of Object.entries(value)) {
|
|
284
|
+
out[k] = coerceValueForKey(v, k, unwrapped);
|
|
285
|
+
}
|
|
286
|
+
return out;
|
|
287
|
+
}
|
|
288
|
+
function coerceArrayToArray(value, prefixItems, itemsSchema) {
|
|
289
|
+
if (prefixItems && value.length === prefixItems.length) {
|
|
290
|
+
return value.map((v, i) => coerceBySchema(v, prefixItems[i]));
|
|
291
|
+
}
|
|
292
|
+
return value.map((v) => coerceBySchema(v, itemsSchema));
|
|
293
|
+
}
|
|
294
|
+
function coerceObjectToArray(maybe, prefixItems, itemsSchema) {
|
|
295
|
+
if (Object.hasOwn(maybe, "item")) {
|
|
296
|
+
const items = maybe.item;
|
|
297
|
+
const arr = Array.isArray(items) ? items : [items];
|
|
298
|
+
return coerceArrayToArray(arr, prefixItems, itemsSchema);
|
|
299
|
+
}
|
|
300
|
+
const keys = Object.keys(maybe);
|
|
301
|
+
if (keys.length > 0 && keys.every((k) => DIGIT_KEY_REGEX.test(k))) {
|
|
302
|
+
const arr = keys.sort((a, b) => Number(a) - Number(b)).map((k) => maybe[k]);
|
|
303
|
+
return coerceArrayToArray(arr, prefixItems, itemsSchema);
|
|
304
|
+
}
|
|
305
|
+
if (keys.length === 1) {
|
|
306
|
+
const singleKey = keys[0];
|
|
307
|
+
if (!(schemaIsUnconstrained(itemsSchema) || schemaHasProperty(itemsSchema, singleKey))) {
|
|
308
|
+
const singleValue = maybe[singleKey];
|
|
309
|
+
if (Array.isArray(singleValue)) {
|
|
310
|
+
return singleValue.map((v) => coerceBySchema(v, itemsSchema));
|
|
311
|
+
}
|
|
312
|
+
if (singleValue && typeof singleValue === "object") {
|
|
313
|
+
return [coerceBySchema(singleValue, itemsSchema)];
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return null;
|
|
318
|
+
}
|
|
319
|
+
function coercePrimitiveToArray(value, prefixItems, itemsSchema) {
|
|
320
|
+
if (prefixItems && prefixItems.length > 0) {
|
|
321
|
+
return [coerceBySchema(value, prefixItems[0])];
|
|
322
|
+
}
|
|
323
|
+
return [coerceBySchema(value, itemsSchema)];
|
|
324
|
+
}
|
|
325
|
+
function coerceStringToPrimitive(s, schemaType) {
|
|
326
|
+
if (schemaType === "boolean") {
|
|
327
|
+
const lower = s.toLowerCase();
|
|
328
|
+
if (lower === "true") {
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
if (lower === "false") {
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
if ((schemaType === "number" || schemaType === "integer") && NUMERIC_REGEX.test(s)) {
|
|
336
|
+
const num = Number(s);
|
|
337
|
+
if (Number.isFinite(num)) {
|
|
338
|
+
return num;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return null;
|
|
342
|
+
}
|
|
343
|
+
function coerceStringValue(value, schemaType, u) {
|
|
344
|
+
const s = value.trim();
|
|
345
|
+
if (schemaType === "object") {
|
|
346
|
+
const result = coerceStringToObject(s, u);
|
|
347
|
+
if (result !== null) {
|
|
348
|
+
return result;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (schemaType === "array") {
|
|
352
|
+
const result = coerceStringToArray(s, u);
|
|
353
|
+
if (result !== null) {
|
|
354
|
+
return result;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
const primitiveResult = coerceStringToPrimitive(s, schemaType);
|
|
358
|
+
if (primitiveResult !== null) {
|
|
359
|
+
return primitiveResult;
|
|
360
|
+
}
|
|
361
|
+
return value;
|
|
362
|
+
}
|
|
363
|
+
function coerceArrayValue(value, prefixItems, itemsSchema) {
|
|
364
|
+
if (Array.isArray(value)) {
|
|
365
|
+
return coerceArrayToArray(value, prefixItems, itemsSchema);
|
|
366
|
+
}
|
|
367
|
+
if (value && typeof value === "object") {
|
|
368
|
+
const result = coerceObjectToArray(
|
|
369
|
+
value,
|
|
370
|
+
prefixItems,
|
|
371
|
+
itemsSchema
|
|
372
|
+
);
|
|
373
|
+
if (result !== null) {
|
|
374
|
+
return result;
|
|
375
|
+
}
|
|
376
|
+
if (getSchemaType(itemsSchema) === "array") {
|
|
377
|
+
return [value];
|
|
378
|
+
}
|
|
379
|
+
return [coerceBySchema(value, itemsSchema)];
|
|
380
|
+
}
|
|
381
|
+
if (value == null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
382
|
+
return coercePrimitiveToArray(value, prefixItems, itemsSchema);
|
|
383
|
+
}
|
|
384
|
+
return [value];
|
|
385
|
+
}
|
|
386
|
+
function coerceBySchema(value, schema) {
|
|
387
|
+
const unwrapped = unwrapJsonSchema(schema);
|
|
388
|
+
if (!unwrapped || typeof unwrapped !== "object") {
|
|
389
|
+
if (typeof value === "string") {
|
|
390
|
+
return coerceStringWithoutSchema(value);
|
|
391
|
+
}
|
|
392
|
+
return value;
|
|
393
|
+
}
|
|
394
|
+
const schemaType = getSchemaType(unwrapped);
|
|
395
|
+
const u = unwrapped;
|
|
396
|
+
if (typeof value === "string") {
|
|
397
|
+
return coerceStringValue(value, schemaType, u);
|
|
398
|
+
}
|
|
399
|
+
if (schemaType === "object" && value && typeof value === "object" && !Array.isArray(value)) {
|
|
400
|
+
return coerceObjectToObject(value, u);
|
|
401
|
+
}
|
|
402
|
+
if (schemaType === "array") {
|
|
403
|
+
const prefixItems = Array.isArray(u.prefixItems) ? u.prefixItems : void 0;
|
|
404
|
+
const itemsSchema = u.items;
|
|
405
|
+
return coerceArrayValue(value, prefixItems, itemsSchema);
|
|
406
|
+
}
|
|
407
|
+
return value;
|
|
408
|
+
}
|
|
409
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
410
|
+
0 && (module.exports = {
|
|
411
|
+
coerceBySchema,
|
|
412
|
+
getSchemaType,
|
|
413
|
+
unwrapJsonSchema
|
|
414
|
+
});
|
|
415
|
+
//# sourceMappingURL=schema-coerce.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/schema-coerce/index.ts"],"sourcesContent":["// Regex constants for performance\nconst NUMERIC_REGEX = /^-?\\d+(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$/;\nconst EMPTY_OBJECT_REGEX = /^\\{\\s*\\}$/s;\nconst NEWLINE_SPLIT_REGEX = /\\n+/;\nconst COMMA_SPLIT_REGEX = /,\\s*/;\nconst DIGIT_KEY_REGEX = /^\\d+$/;\n\nexport function unwrapJsonSchema(schema: unknown): unknown {\n if (!schema || typeof schema !== \"object\") {\n return schema;\n }\n const s = schema as Record<string, unknown>;\n if (s.jsonSchema && typeof s.jsonSchema === \"object\") {\n return unwrapJsonSchema(s.jsonSchema);\n }\n return schema;\n}\n\nexport function getSchemaType(schema: unknown): string | undefined {\n const unwrapped = unwrapJsonSchema(schema);\n if (!unwrapped || typeof unwrapped !== \"object\") {\n return;\n }\n const t: unknown = (unwrapped as Record<string, unknown>).type;\n if (typeof t === \"string\") {\n return t;\n }\n if (Array.isArray(t)) {\n const preferred = [\n \"object\",\n \"array\",\n \"boolean\",\n \"number\",\n \"integer\",\n \"string\",\n ];\n for (const p of preferred) {\n if (t.includes(p)) {\n return p;\n }\n }\n }\n const s = unwrapped as Record<string, unknown>;\n if (s && typeof s === \"object\" && (s.properties || s.additionalProperties)) {\n return \"object\";\n }\n if (\n s &&\n typeof s === \"object\" &&\n (s.items || (s as Record<string, unknown>).prefixItems)\n ) {\n return \"array\";\n }\n return;\n}\n\n/**\n * Checks if a property is allowed through schema combinators (anyOf, oneOf, allOf).\n *\n * @param s - The schema object to check\n * @param key - The property key to look for\n * @param depth - Current recursion depth\n * @returns `true` if at least one combinator exists AND allows the property;\n * `false` if no combinators exist OR none allow the property.\n * When no combinators are present, returns `false` so the caller can\n * fall back to other property-checking methods.\n *\n * **oneOf semantics**: JSON Schema's `oneOf` requires exactly one schema to match,\n * but for coercion heuristics we treat it like `anyOf` (at least one allows).\n * This is intentional because:\n * 1. We're determining if a property CAN exist, not validating exact matches\n * 2. Coercion should be permissive - if any branch allows the property, we allow it\n * 3. Strict oneOf validation would require runtime value inspection, not just schema analysis\n */\nfunction schemaAllowsPropertyViaCombinators(\n s: Record<string, unknown>,\n key: string,\n depth: number\n): boolean {\n const anyOfValues = s.anyOf;\n const oneOfValues = s.oneOf;\n const allOfValues = s.allOf;\n\n let hasCombinator = false;\n let anyOfAllows = true;\n let oneOfAllows = true;\n let allOfAllows = true;\n\n if (Array.isArray(anyOfValues)) {\n hasCombinator = true;\n anyOfAllows = anyOfValues.some((sub) =>\n schemaHasProperty(sub, key, depth + 1)\n );\n }\n\n if (Array.isArray(oneOfValues)) {\n hasCombinator = true;\n oneOfAllows = oneOfValues.some((sub) =>\n schemaHasProperty(sub, key, depth + 1)\n );\n }\n\n if (Array.isArray(allOfValues)) {\n hasCombinator = true;\n allOfAllows = allOfValues.every((sub) =>\n schemaHasProperty(sub, key, depth + 1)\n );\n }\n\n if (!hasCombinator) {\n return false;\n }\n\n return anyOfAllows && oneOfAllows && allOfAllows;\n}\n\nfunction schemaHasPropertyDirectly(\n s: Record<string, unknown>,\n key: string\n): boolean {\n const props = s.properties;\n if (\n props &&\n typeof props === \"object\" &&\n !Array.isArray(props) &&\n Object.hasOwn(props, key) &&\n (props as Record<string, unknown>)[key] !== false\n ) {\n return true;\n }\n const required = s.required;\n if (Array.isArray(required) && required.includes(key)) {\n return true;\n }\n const patternSchemas = getPatternSchemasForKey(s.patternProperties, key);\n return patternSchemas.some((schema) => schema !== false);\n}\n\n/**\n * Checks if a schema allows additional properties beyond those explicitly defined.\n *\n * JSON Schema behavior for additionalProperties:\n * - `additionalProperties: true` or `additionalProperties: { schema }`: Explicitly allows additional properties\n * - `additionalProperties: false`: Explicitly disallows additional properties\n * - `additionalProperties` not specified: Defaults to allowing additional properties (JSON Schema spec)\n *\n * When `additionalProperties` is not explicitly set, this function returns `true` if the schema\n * appears to be an object schema (has `type: \"object\"`, `properties`, `patternProperties`, or `required`).\n * This follows the JSON Schema specification where omitting `additionalProperties` is equivalent to `true`.\n *\n * **Important**: This means schemas like `{ type: \"object\", properties: { foo: ... } }` without\n * `additionalProperties: false` will be treated as allowing any additional property, which affects\n * single-key object unwrapping behavior in array coercion.\n *\n * @param s - The schema object to check\n * @returns `true` if the schema allows additional properties, `false` otherwise\n */\nfunction schemaHasPropertyViaAdditional(s: Record<string, unknown>): boolean {\n const additional = s.additionalProperties;\n if (\n additional === true ||\n (additional && typeof additional === \"object\" && !Array.isArray(additional))\n ) {\n return true;\n }\n if (Object.hasOwn(s, \"additionalProperties\")) {\n return false;\n }\n const type = s.type;\n const isObjectType =\n type === \"object\" || (Array.isArray(type) && type.includes(\"object\"));\n const hasObjectKeywords =\n (s.properties &&\n typeof s.properties === \"object\" &&\n !Array.isArray(s.properties)) ||\n (s.patternProperties &&\n typeof s.patternProperties === \"object\" &&\n !Array.isArray(s.patternProperties)) ||\n (Array.isArray(s.required) && s.required.length > 0);\n return !!(isObjectType || hasObjectKeywords);\n}\n\nfunction schemaDisallowsPropertyDirectly(\n s: Record<string, unknown>,\n key: string\n): boolean {\n const props = s.properties;\n if (\n props &&\n typeof props === \"object\" &&\n !Array.isArray(props) &&\n Object.hasOwn(props, key) &&\n (props as Record<string, unknown>)[key] === false\n ) {\n return true;\n }\n const patternSchemas = getPatternSchemasForKey(s.patternProperties, key);\n return patternSchemas.some((schema) => schema === false);\n}\n\n/**\n * Checks if a schema allows a specific property key.\n *\n * Recursively checks through schema combinators (allOf, anyOf, oneOf) to determine\n * if the given key is allowed by the schema.\n *\n * @param schema - The JSON Schema to check\n * @param key - The property key to check for\n * @param depth - Current recursion depth (default: 0)\n * @returns `true` if the schema allows the property, `false` otherwise\n *\n * @remarks\n * The depth limit of 5 prevents infinite recursion in deeply nested or circular\n * schema references. This limit is sufficient for most real-world schemas while\n * protecting against pathological cases. When the limit is exceeded, the function\n * conservatively returns `true` to prevent unwrapping - it's safer to keep a\n * wrapper key than to incorrectly remove it and lose data.\n */\nfunction schemaHasProperty(schema: unknown, key: string, depth = 0): boolean {\n if (depth > 5) {\n return true;\n }\n const unwrapped = unwrapJsonSchema(schema);\n // Unconstrained schemas (true, null, {}) allow any property\n if (schemaIsUnconstrained(unwrapped)) {\n return true;\n }\n if (!unwrapped || typeof unwrapped !== \"object\") {\n return false;\n }\n const s = unwrapped as Record<string, unknown>;\n\n if (schemaDisallowsPropertyDirectly(s, key)) {\n return false;\n }\n if (schemaHasPropertyDirectly(s, key)) {\n return true;\n }\n if (schemaHasPropertyViaAdditional(s)) {\n return true;\n }\n return schemaAllowsPropertyViaCombinators(s, key, depth);\n}\n\nfunction schemaIsUnconstrained(schema: unknown): boolean {\n const unwrapped = unwrapJsonSchema(schema);\n if (unwrapped == null || unwrapped === true) {\n return true;\n }\n if (typeof unwrapped !== \"object\" || Array.isArray(unwrapped)) {\n return false;\n }\n return Object.keys(unwrapped).length === 0;\n}\n\n/**\n * Gets all schemas from patternProperties that match the given key.\n *\n * @param patternProperties - The patternProperties object from a JSON Schema\n * @param key - The property key to match against patterns\n * @returns Array of schemas whose patterns match the key\n *\n * @remarks\n * **Security consideration**: This function executes regex patterns from the schema.\n * In typical usage (AI SDK tool parsing), schemas come from trusted application code.\n * However, if schemas can originate from untrusted sources, be aware of potential\n * ReDoS (Regular Expression Denial of Service) with malicious patterns like `(a+)+$`.\n * Consider adding regex timeout or safe-regex validation if processing untrusted schemas.\n */\nfunction getPatternSchemasForKey(\n patternProperties: unknown,\n key: string\n): unknown[] {\n if (\n !patternProperties ||\n typeof patternProperties !== \"object\" ||\n Array.isArray(patternProperties)\n ) {\n return [];\n }\n const schemas: unknown[] = [];\n for (const [pattern, schema] of Object.entries(\n patternProperties as Record<string, unknown>\n )) {\n try {\n const regex = new RegExp(pattern);\n if (regex.test(key)) {\n schemas.push(schema);\n }\n } catch {\n // Ignore invalid regex patterns.\n }\n }\n return schemas;\n}\n\nfunction coerceValueForKey(\n value: unknown,\n key: string,\n unwrapped: Record<string, unknown>\n): unknown {\n const schemas: unknown[] = [];\n const props = unwrapped.properties as Record<string, unknown> | undefined;\n if (props && Object.hasOwn(props, key)) {\n schemas.push(props[key]);\n }\n const patternSchemas = getPatternSchemasForKey(\n unwrapped.patternProperties,\n key\n );\n if (patternSchemas.length > 0) {\n schemas.push(...patternSchemas);\n }\n\n if (schemas.length > 0) {\n let out = value;\n for (const schema of schemas) {\n if (typeof schema === \"boolean\") {\n continue;\n }\n out = coerceBySchema(out, schema);\n }\n return out;\n }\n\n const additional = unwrapped.additionalProperties;\n if (\n additional &&\n typeof additional === \"object\" &&\n !Array.isArray(additional)\n ) {\n return coerceBySchema(value, additional);\n }\n if (additional === true || additional === false) {\n return value;\n }\n\n return coerceBySchema(value, undefined);\n}\n\n/**\n * Coerce string value without schema information\n */\nfunction coerceStringWithoutSchema(value: string): unknown {\n const s = value.trim();\n const lower = s.toLowerCase();\n if (lower === \"true\") {\n return true;\n }\n if (lower === \"false\") {\n return false;\n }\n if (NUMERIC_REGEX.test(s)) {\n const num = Number(s);\n if (Number.isFinite(num)) {\n return num;\n }\n }\n\n // Fallback: try parsing JSON-like strings when no schema info\n if (\n (s.startsWith(\"{\") && s.endsWith(\"}\")) ||\n (s.startsWith(\"[\") && s.endsWith(\"]\"))\n ) {\n try {\n const parsed = JSON.parse(s);\n return coerceBySchema(parsed, undefined);\n } catch {\n // If parsing fails, return original value\n }\n }\n return value;\n}\n\n/**\n * Coerce string to object using schema\n */\nfunction coerceStringToObject(\n s: string,\n unwrapped: Record<string, unknown>\n): unknown {\n try {\n let normalized = s.replace(/'/g, '\"');\n normalized = normalized.replace(EMPTY_OBJECT_REGEX, \"{}\");\n\n const obj = JSON.parse(normalized);\n if (obj && typeof obj === \"object\" && !Array.isArray(obj)) {\n return coerceObjectToObject(obj as Record<string, unknown>, unwrapped);\n }\n } catch {\n // fallthrough\n }\n return null;\n}\n\n/**\n * Coerce string to array using schema\n */\nfunction coerceStringToArray(\n s: string,\n unwrapped: Record<string, unknown>\n): unknown {\n const prefixItems = Array.isArray(unwrapped.prefixItems)\n ? (unwrapped.prefixItems as unknown[])\n : undefined;\n const itemsSchema = unwrapped.items as unknown;\n\n try {\n const normalized = s.replace(/'/g, '\"');\n const arr = JSON.parse(normalized);\n if (Array.isArray(arr)) {\n if (prefixItems && arr.length === prefixItems.length) {\n return arr.map((v, i) => coerceBySchema(v, prefixItems[i]));\n }\n return arr.map((v) => coerceBySchema(v, itemsSchema));\n }\n } catch {\n const csv = s.includes(\"\\n\")\n ? s.split(NEWLINE_SPLIT_REGEX)\n : s.split(COMMA_SPLIT_REGEX);\n const trimmed = csv.map((x) => x.trim()).filter((x) => x.length > 0);\n if (prefixItems && trimmed.length === prefixItems.length) {\n return trimmed.map((x, i) => coerceBySchema(x, prefixItems[i]));\n }\n return trimmed.map((x) => coerceBySchema(x, itemsSchema));\n }\n return null;\n}\n\n/**\n * Coerce object to object using schema\n */\nfunction coerceObjectToObject(\n value: Record<string, unknown>,\n unwrapped: Record<string, unknown>\n): Record<string, unknown> {\n const out: Record<string, unknown> = {};\n for (const [k, v] of Object.entries(value)) {\n out[k] = coerceValueForKey(v, k, unwrapped);\n }\n return out;\n}\n\n/**\n * Coerce array to array using schema\n */\nfunction coerceArrayToArray(\n value: unknown[],\n prefixItems: unknown[] | undefined,\n itemsSchema: unknown\n): unknown[] {\n if (prefixItems && value.length === prefixItems.length) {\n return value.map((v, i) => coerceBySchema(v, prefixItems[i]));\n }\n return value.map((v) => coerceBySchema(v, itemsSchema));\n}\n\n/**\n * Coerce object to array using schema\n */\nfunction coerceObjectToArray(\n maybe: Record<string, unknown>,\n prefixItems: unknown[] | undefined,\n itemsSchema: unknown\n): unknown {\n if (Object.hasOwn(maybe, \"item\")) {\n const items = maybe.item as unknown;\n const arr = Array.isArray(items) ? items : [items];\n return coerceArrayToArray(arr, prefixItems, itemsSchema);\n }\n\n const keys = Object.keys(maybe);\n\n // Check for numeric keys (traditional tuple handling)\n if (keys.length > 0 && keys.every((k) => DIGIT_KEY_REGEX.test(k))) {\n const arr = keys.sort((a, b) => Number(a) - Number(b)).map((k) => maybe[k]);\n return coerceArrayToArray(arr, prefixItems, itemsSchema);\n }\n\n // Check for single field that contains an array or object (common XML pattern)\n // This handles both: { user: [{ name: \"A\" }, { name: \"B\" }] } and { user: { name: \"A\" } }\n if (keys.length === 1) {\n const singleKey = keys[0];\n if (\n !(\n schemaIsUnconstrained(itemsSchema) ||\n schemaHasProperty(itemsSchema, singleKey)\n )\n ) {\n const singleValue = maybe[singleKey];\n if (Array.isArray(singleValue)) {\n return singleValue.map((v) => coerceBySchema(v, itemsSchema));\n }\n // Also extract when single key's value is an object and wrap in array (single/multiple element consistency)\n if (singleValue && typeof singleValue === \"object\") {\n return [coerceBySchema(singleValue, itemsSchema)];\n }\n }\n }\n\n return null;\n}\n\n/**\n * Coerce primitive to array using schema\n */\nfunction coercePrimitiveToArray(\n value: unknown,\n prefixItems: unknown[] | undefined,\n itemsSchema: unknown\n): unknown[] {\n if (prefixItems && prefixItems.length > 0) {\n return [coerceBySchema(value, prefixItems[0])];\n }\n return [coerceBySchema(value, itemsSchema)];\n}\n\n/**\n * Coerce string to primitive type using schema\n */\nfunction coerceStringToPrimitive(\n s: string,\n schemaType: string | undefined\n): unknown {\n if (schemaType === \"boolean\") {\n const lower = s.toLowerCase();\n if (lower === \"true\") {\n return true;\n }\n if (lower === \"false\") {\n return false;\n }\n }\n if (\n (schemaType === \"number\" || schemaType === \"integer\") &&\n NUMERIC_REGEX.test(s)\n ) {\n const num = Number(s);\n if (Number.isFinite(num)) {\n return num;\n }\n }\n return null;\n}\n\nfunction coerceStringValue(\n value: string,\n schemaType: string | undefined,\n u: Record<string, unknown>\n): unknown {\n const s = value.trim();\n\n if (schemaType === \"object\") {\n const result = coerceStringToObject(s, u);\n if (result !== null) {\n return result;\n }\n }\n\n if (schemaType === \"array\") {\n const result = coerceStringToArray(s, u);\n if (result !== null) {\n return result;\n }\n }\n\n const primitiveResult = coerceStringToPrimitive(s, schemaType);\n if (primitiveResult !== null) {\n return primitiveResult;\n }\n\n return value;\n}\n\nfunction coerceArrayValue(\n value: unknown,\n prefixItems: unknown[] | undefined,\n itemsSchema: unknown\n): unknown {\n if (Array.isArray(value)) {\n return coerceArrayToArray(value, prefixItems, itemsSchema);\n }\n\n if (value && typeof value === \"object\") {\n const result = coerceObjectToArray(\n value as Record<string, unknown>,\n prefixItems,\n itemsSchema\n );\n if (result !== null) {\n return result;\n }\n // To prevent infinite recursion, check if the itemsSchema is also for an array.\n // If so, just wrap the object. Otherwise, coerce it against the itemsSchema.\n if (getSchemaType(itemsSchema) === \"array\") {\n return [value];\n }\n return [coerceBySchema(value, itemsSchema)];\n }\n\n if (\n value == null ||\n typeof value === \"string\" ||\n typeof value === \"number\" ||\n typeof value === \"boolean\"\n ) {\n return coercePrimitiveToArray(value, prefixItems, itemsSchema);\n }\n\n return [value];\n}\n\nexport function coerceBySchema(value: unknown, schema?: unknown): unknown {\n const unwrapped = unwrapJsonSchema(schema);\n if (!unwrapped || typeof unwrapped !== \"object\") {\n if (typeof value === \"string\") {\n return coerceStringWithoutSchema(value);\n }\n return value;\n }\n\n const schemaType = getSchemaType(unwrapped);\n const u = unwrapped as Record<string, unknown>;\n\n // Handle string values\n if (typeof value === \"string\") {\n return coerceStringValue(value, schemaType, u);\n }\n\n // Handle object to object coercion\n if (\n schemaType === \"object\" &&\n value &&\n typeof value === \"object\" &&\n !Array.isArray(value)\n ) {\n return coerceObjectToObject(value as Record<string, unknown>, u);\n }\n\n // Handle array coercion\n if (schemaType === \"array\") {\n const prefixItems = Array.isArray(u.prefixItems)\n ? (u.prefixItems as unknown[])\n : undefined;\n const itemsSchema = u.items as unknown;\n\n return coerceArrayValue(value, prefixItems, itemsSchema);\n }\n\n return value;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,IAAM,gBAAgB;AACtB,IAAM,qBAAqB;AAC3B,IAAM,sBAAsB;AAC5B,IAAM,oBAAoB;AAC1B,IAAM,kBAAkB;AAEjB,SAAS,iBAAiB,QAA0B;AACzD,MAAI,CAAC,UAAU,OAAO,WAAW,UAAU;AACzC,WAAO;AAAA,EACT;AACA,QAAM,IAAI;AACV,MAAI,EAAE,cAAc,OAAO,EAAE,eAAe,UAAU;AACpD,WAAO,iBAAiB,EAAE,UAAU;AAAA,EACtC;AACA,SAAO;AACT;AAEO,SAAS,cAAc,QAAqC;AACjE,QAAM,YAAY,iBAAiB,MAAM;AACzC,MAAI,CAAC,aAAa,OAAO,cAAc,UAAU;AAC/C;AAAA,EACF;AACA,QAAM,IAAc,UAAsC;AAC1D,MAAI,OAAO,MAAM,UAAU;AACzB,WAAO;AAAA,EACT;AACA,MAAI,MAAM,QAAQ,CAAC,GAAG;AACpB,UAAM,YAAY;AAAA,MAChB;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AACA,eAAW,KAAK,WAAW;AACzB,UAAI,EAAE,SAAS,CAAC,GAAG;AACjB,eAAO;AAAA,MACT;AAAA,IACF;AAAA,EACF;AACA,QAAM,IAAI;AACV,MAAI,KAAK,OAAO,MAAM,aAAa,EAAE,cAAc,EAAE,uBAAuB;AAC1E,WAAO;AAAA,EACT;AACA,MACE,KACA,OAAO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O,cAAc,UAAU;AAC/C,QAAI,OAAO,UAAU,UAAU;AAC7B,aAAO,0BAA0B,KAAK;AAAA,IACxC;AACA,WAAO;AAAA,EACT;AAEA,QAAM,aAAa,cAAc,SAAS;AAC1C,QAAM,IAAI;AAGV,MAAI,OAAO,UAAU,UAAU;AAC7B,WAAO,kBAAkB,OAAO,YAAY,CAAC;AAAA,EAC/C;AAGA,MACE,eAAe,YACf,SACA,OAAO,UAAU,YACjB,CAAC,MAAM,QAAQ,KAAK,GACpB;AACA,WAAO,qBAAqB,OAAkC,CAAC;AAAA,EACjE;AAGA,MAAI,eAAe,SAAS;AAC1B,UAAM,cAAc,MAAM,QAAQ,EAAE,WAAW,IAC1C,EAAE,cACH;AACJ,UAAM,cAAc,EAAE;AAEtB,WAAO,iBAAiB,OAAO,aAAa,WAAW;AAAA,EACzD;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
declare function unwrapJsonSchema(schema: unknown): unknown;
|
|
2
|
+
declare function getSchemaType(schema: unknown): string | undefined;
|
|
3
|
+
declare function coerceBySchema(value: unknown, schema?: unknown): unknown;
|
|
4
|
+
|
|
5
|
+
export { coerceBySchema, getSchemaType, unwrapJsonSchema };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
declare function unwrapJsonSchema(schema: unknown): unknown;
|
|
2
|
+
declare function getSchemaType(schema: unknown): string | undefined;
|
|
3
|
+
declare function coerceBySchema(value: unknown, schema?: unknown): unknown;
|
|
4
|
+
|
|
5
|
+
export { coerceBySchema, getSchemaType, unwrapJsonSchema };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|