@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/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
- if (Array.isArray(prop.type)) {
5192
- const types = [...prop.type];
5193
- if (!types.includes("null")) types.push("null");
5194
- const propSchema = { ...prop };
5195
- delete propSchema.anyOf;
5196
- delete propSchema.type;
5197
- delete prop.type;
5198
- prop.anyOf = types.map((type) => type === "null" ? { type: "null" } : {
5199
- ...propSchema,
5200
- type
5201
- });
5202
- } else if (prop.type !== "null") {
5203
- const originalType = prop.type;
5204
- const propSchema = { ...prop };
5205
- delete propSchema.anyOf;
5206
- delete propSchema.type;
5207
- delete prop.type;
5208
- prop.anyOf = [{
5209
- ...propSchema,
5210
- type: originalType
5211
- }, { type: "null" }];
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 first non-null variant for traversal.
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 nonNull = schema.anyOf.find((s) => s.type !== "null");
5241
- if (nonNull) return nonNull;
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
  }