@mastra/schema-compat 1.3.11 → 1.3.12-alpha.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/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { r as __toESM$1, t as require_json_schema_traverse } from "./json-schema-traverse-B35mHRM8.js";
2
2
  import { ensureAllPropertiesRequired, prepareJsonSchemaForOpenAIStrictMode, zodToJsonSchema as zodToJsonSchema$1 } from "./zod-to-json.js";
3
- import { a as standardSchemaToJSONSchema, i as isStandardSchemaWithJSON, n as isStandardJSONSchema, o as toStandardSchema, r as isStandardSchema, t as JSON_SCHEMA_LIBRARY_OPTIONS } from "./schema-BwlBcZRB.js";
3
+ import { a as standardSchemaToJSONSchema, i as isStandardSchemaWithJSON, n as isStandardJSONSchema, o as toStandardSchema, r as isStandardSchema, t as JSON_SCHEMA_LIBRARY_OPTIONS } from "./schema-e0VHpMt0.js";
4
4
  import { ZodArray, ZodDate, ZodDefault, ZodFirstPartyTypeKind, ZodIntersection, ZodNull, ZodNullable, ZodNumber, ZodObject, ZodOptional, ZodString, ZodUnion, z } from "zod/v3";
5
5
  import { z as z$1 } from "zod";
6
6
  import { convertJsonSchemaToZod } from "zod-from-json-schema";
@@ -4970,6 +4970,13 @@ var SchemaCompatLayer = class {
4970
4970
  */
4971
4971
  function transformNullToUndefined(value, jsonSchema) {
4972
4972
  if (value === null || value === void 0) return value;
4973
+ const branches = jsonSchema.anyOf ?? jsonSchema.oneOf;
4974
+ if (Array.isArray(branches)) {
4975
+ const { anyOf: _anyOf, oneOf: _oneOf, ...parent } = jsonSchema;
4976
+ const branch = findMatchingBranch(value, branches);
4977
+ if (!branch) return "properties" in parent || "items" in parent ? transformNullToUndefined(value, parent) : value;
4978
+ return transformNullToUndefined(value, mergeSchemas(parent, branch));
4979
+ }
4973
4980
  if (typeof value !== "object" || Array.isArray(value)) {
4974
4981
  if (Array.isArray(value) && jsonSchema.items && typeof jsonSchema.items === "object") return value.map((item) => transformNullToUndefined(item, jsonSchema.items));
4975
4982
  return value;
@@ -4984,6 +4991,58 @@ function transformNullToUndefined(value, jsonSchema) {
4984
4991
  return result;
4985
4992
  }
4986
4993
  /**
4994
+ * Picks the non-null union branch (from `anyOf`/`oneOf`) whose shape matches the value,
4995
+ * e.g. the object branch of a `.nullable()` object. For objects, branches whose required
4996
+ * keys are missing or whose `const`/`enum` properties (discriminators) disagree are skipped.
4997
+ */
4998
+ function findMatchingBranch(value, branches) {
4999
+ const isArray = Array.isArray(value);
5000
+ if (typeof value !== "object" && !isArray) return;
5001
+ return branches.find((branch) => {
5002
+ if (!branch || typeof branch !== "object") return false;
5003
+ const types = Array.isArray(branch.type) ? branch.type : [branch.type];
5004
+ if (isArray) {
5005
+ if (!("items" in branch || types.includes("array"))) return false;
5006
+ const items = branch.items;
5007
+ return !items || typeof items !== "object" || value.every((item) => itemMatches(item, items));
5008
+ }
5009
+ if (!("properties" in branch || "anyOf" in branch || "oneOf" in branch || types.includes("object"))) return false;
5010
+ return objectMatchesBranch(value, branch);
5011
+ });
5012
+ }
5013
+ function itemMatches(item, schema) {
5014
+ if (item === null || typeof item !== "object" || Array.isArray(item)) return true;
5015
+ const branches = schema.anyOf ?? schema.oneOf;
5016
+ if (Array.isArray(branches)) return findMatchingBranch(item, branches) !== void 0;
5017
+ return objectMatchesBranch(item, schema);
5018
+ }
5019
+ function objectMatchesBranch(value, branch) {
5020
+ if ((branch.required ?? []).some((key) => !(key in value))) return false;
5021
+ const properties = branch.properties ?? {};
5022
+ return Object.entries(properties).every(([key, prop]) => {
5023
+ if (!prop || typeof prop !== "object" || !(key in value)) return true;
5024
+ if ("const" in prop) return prop.const === value[key];
5025
+ if (Array.isArray(prop.enum)) return prop.enum.includes(value[key]);
5026
+ return true;
5027
+ });
5028
+ }
5029
+ function mergeSchemas(parent, branch) {
5030
+ const merged = {
5031
+ ...parent,
5032
+ ...branch
5033
+ };
5034
+ if (parent.items && !branch.items) merged.items = parent.items;
5035
+ if (!parent.properties) return merged;
5036
+ return {
5037
+ ...merged,
5038
+ properties: {
5039
+ ...parent.properties,
5040
+ ...branch.properties
5041
+ },
5042
+ required: [.../* @__PURE__ */ new Set([...parent.required ?? [], ...branch.required ?? []])]
5043
+ };
5044
+ }
5045
+ /**
4987
5046
  * Wraps a StandardSchemaWithJSON to transform null values to undefined for
4988
5047
  * optional fields before validation. This is a schema-agnostic solution for
4989
5048
  * OpenAI strict mode, which sends null for optional fields.
@@ -5227,6 +5286,15 @@ var OpenAISchemaCompatLayer = class extends SchemaCompatLayer {
5227
5286
  "x-optional"
5228
5287
  ];
5229
5288
  const arrayKeywords = ["items"];
5289
+ const numericKeywords = [
5290
+ "format",
5291
+ "minimum",
5292
+ "maximum",
5293
+ "exclusiveMinimum",
5294
+ "exclusiveMaximum",
5295
+ "multipleOf"
5296
+ ];
5297
+ const isNumericType = (type) => type === "integer" || type === "number";
5230
5298
  const genericConstraintKeywords = [
5231
5299
  "const",
5232
5300
  "enum",
@@ -5276,10 +5344,26 @@ var OpenAISchemaCompatLayer = class extends SchemaCompatLayer {
5276
5344
  prop.anyOf = types.map((type) => {
5277
5345
  if (type === "null") return { type: "null" };
5278
5346
  const branch = { type };
5279
- for (const keyword of typeSpecificKeywords(type)) if (keyword in prop) branch[keyword] = prop[keyword];
5347
+ const numericFormat = typeof prop.format === "string" && [
5348
+ "int32",
5349
+ "int64",
5350
+ "float",
5351
+ "double"
5352
+ ].includes(prop.format);
5353
+ const formatOwner = types.includes("string") && !numericFormat ? type === "string" : isNumericType(type);
5354
+ for (const keyword of [
5355
+ ...typeSpecificKeywords(type),
5356
+ ...isNumericType(type) ? numericKeywords.filter((k) => k !== "format") : [],
5357
+ ...formatOwner ? ["format"] : []
5358
+ ]) if (keyword in prop) branch[keyword] = prop[keyword];
5280
5359
  return Object.assign(branch, branchConstraints);
5281
5360
  });
5282
- for (const keyword of [...objectKeywords, ...arrayKeywords]) delete prop[keyword];
5361
+ const numericBranchExists = types.some(isNumericType);
5362
+ for (const keyword of [
5363
+ ...objectKeywords,
5364
+ ...arrayKeywords,
5365
+ ...numericBranchExists ? numericKeywords : []
5366
+ ]) delete prop[keyword];
5283
5367
  } else if (prop.type && prop.type !== "null") {
5284
5368
  const originalType = prop.type;
5285
5369
  const keywords = typeSpecificKeywords(originalType);
@@ -5300,6 +5384,7 @@ var OpenAISchemaCompatLayer = class extends SchemaCompatLayer {
5300
5384
  };
5301
5385
  if (isRedundantNullableUnion(originalType)) delete propSchema.anyOf;
5302
5386
  for (const keyword of [...genericConstraintKeywords, "anyOf"]) delete prop[keyword];
5387
+ if (isNumericType(originalType)) for (const keyword of numericKeywords) delete prop[keyword];
5303
5388
  delete prop.type;
5304
5389
  prop.anyOf = [propSchema, { type: "null" }];
5305
5390
  }