@mastra/schema-compat 1.3.6 → 1.3.7-alpha.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 +6 -0
- package/dist/index.cjs +60 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +60 -28
- package/dist/index.js.map +1 -1
- package/dist/provider-compats/openai.d.ts.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -5187,36 +5187,48 @@ var OpenAISchemaCompatLayer = class extends SchemaCompatLayer {
|
|
|
5187
5187
|
if (!schema.required?.includes(key)) {
|
|
5188
5188
|
schema["x-optional"] = [...schema["x-optional"] || [], key];
|
|
5189
5189
|
schema.required?.push(key);
|
|
5190
|
-
if (prop.type) {
|
|
5191
|
-
|
|
5192
|
-
|
|
5193
|
-
|
|
5194
|
-
|
|
5195
|
-
|
|
5196
|
-
|
|
5197
|
-
|
|
5198
|
-
prop.
|
|
5199
|
-
|
|
5200
|
-
|
|
5201
|
-
|
|
5202
|
-
|
|
5203
|
-
|
|
5204
|
-
|
|
5205
|
-
|
|
5206
|
-
|
|
5207
|
-
|
|
5208
|
-
|
|
5209
|
-
|
|
5210
|
-
|
|
5211
|
-
|
|
5212
|
-
|
|
5190
|
+
if (Array.isArray(prop.type)) {
|
|
5191
|
+
const types = [...prop.type];
|
|
5192
|
+
if (!types.includes("null")) types.push("null");
|
|
5193
|
+
delete prop.anyOf;
|
|
5194
|
+
delete prop.type;
|
|
5195
|
+
if ("const" in prop) {
|
|
5196
|
+
const constValue = prop.const;
|
|
5197
|
+
prop.enum = !prop.enum || prop.enum.some((value) => JSON.stringify(value) === JSON.stringify(constValue)) ? [constValue, null] : [null];
|
|
5198
|
+
delete prop.const;
|
|
5199
|
+
} else if (prop.enum && !prop.enum.includes(null)) prop.enum = [...prop.enum, null];
|
|
5200
|
+
const objectKeywords = [
|
|
5201
|
+
"properties",
|
|
5202
|
+
"required",
|
|
5203
|
+
"additionalProperties",
|
|
5204
|
+
"x-optional"
|
|
5205
|
+
];
|
|
5206
|
+
const arrayKeywords = ["items"];
|
|
5207
|
+
prop.anyOf = types.map((type) => {
|
|
5208
|
+
if (type === "null") return { type: "null" };
|
|
5209
|
+
const branch = { type };
|
|
5210
|
+
const keywords = type === "object" ? objectKeywords : type === "array" ? arrayKeywords : [];
|
|
5211
|
+
for (const keyword of keywords) if (keyword in prop) branch[keyword] = prop[keyword];
|
|
5212
|
+
return branch;
|
|
5213
|
+
});
|
|
5214
|
+
for (const keyword of [...objectKeywords, ...arrayKeywords]) delete prop[keyword];
|
|
5215
|
+
} else if (prop.type && prop.type !== "null") {
|
|
5216
|
+
const originalType = prop.type;
|
|
5217
|
+
const propSchema = { ...prop };
|
|
5218
|
+
delete propSchema.anyOf;
|
|
5219
|
+
delete propSchema.type;
|
|
5220
|
+
delete prop.type;
|
|
5221
|
+
prop.anyOf = [{
|
|
5222
|
+
...propSchema,
|
|
5223
|
+
type: originalType
|
|
5224
|
+
}, { type: "null" }];
|
|
5213
5225
|
}
|
|
5214
5226
|
}
|
|
5215
5227
|
}
|
|
5216
5228
|
}
|
|
5217
5229
|
}
|
|
5218
5230
|
#traverse(value, schema) {
|
|
5219
|
-
const resolved = this.#resolveAnyOf(schema);
|
|
5231
|
+
const resolved = this.#resolveAnyOf(schema, value);
|
|
5220
5232
|
if ((isDateFormat(resolved) || resolved["x-date"] === true) && typeof value === "string") return new Date(value);
|
|
5221
5233
|
if (resolved.type === "array" || Array.isArray(resolved.type) && resolved.type.includes("array")) {
|
|
5222
5234
|
if (!Array.isArray(value)) return value;
|
|
@@ -5232,13 +5244,33 @@ var OpenAISchemaCompatLayer = class extends SchemaCompatLayer {
|
|
|
5232
5244
|
return obj;
|
|
5233
5245
|
}
|
|
5234
5246
|
/**
|
|
5235
|
-
* If schema has anyOf, return the
|
|
5247
|
+
* If schema has anyOf, return the variant whose type matches the value's shape
|
|
5248
|
+
* (branches are type-specific), falling back to the first non-null variant.
|
|
5236
5249
|
* Otherwise return the schema itself.
|
|
5237
5250
|
*/
|
|
5238
|
-
#resolveAnyOf(schema) {
|
|
5251
|
+
#resolveAnyOf(schema, value) {
|
|
5239
5252
|
if (Array.isArray(schema.anyOf)) {
|
|
5240
|
-
const
|
|
5241
|
-
|
|
5253
|
+
const nonNullVariants = schema.anyOf.filter((s) => s && s.type !== "null");
|
|
5254
|
+
const valueType = Array.isArray(value) ? "array" : value !== null && typeof value === "object" ? "object" : typeof value === "number" ? Number.isInteger(value) ? "integer" : "number" : typeof value === "string" || typeof value === "boolean" ? typeof value : void 0;
|
|
5255
|
+
if (valueType) {
|
|
5256
|
+
const hasType = (variant, type) => (Array.isArray(variant.type) ? variant.type : [variant.type]).includes(type);
|
|
5257
|
+
const exactMatch = nonNullVariants.find((variant) => hasType(variant, valueType));
|
|
5258
|
+
if (exactMatch) return {
|
|
5259
|
+
...schema,
|
|
5260
|
+
...exactMatch
|
|
5261
|
+
};
|
|
5262
|
+
if (valueType === "integer") {
|
|
5263
|
+
const numberMatch = nonNullVariants.find((variant) => hasType(variant, "number"));
|
|
5264
|
+
if (numberMatch) return {
|
|
5265
|
+
...schema,
|
|
5266
|
+
...numberMatch
|
|
5267
|
+
};
|
|
5268
|
+
}
|
|
5269
|
+
}
|
|
5270
|
+
if (nonNullVariants[0]) return {
|
|
5271
|
+
...schema,
|
|
5272
|
+
...nonNullVariants[0]
|
|
5273
|
+
};
|
|
5242
5274
|
}
|
|
5243
5275
|
return schema;
|
|
5244
5276
|
}
|