@mastra/schema-compat 1.3.12-alpha.0 → 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.cjs CHANGED
@@ -4971,6 +4971,13 @@ var SchemaCompatLayer = class {
4971
4971
  */
4972
4972
  function transformNullToUndefined(value, jsonSchema) {
4973
4973
  if (value === null || value === void 0) return value;
4974
+ const branches = jsonSchema.anyOf ?? jsonSchema.oneOf;
4975
+ if (Array.isArray(branches)) {
4976
+ const { anyOf: _anyOf, oneOf: _oneOf, ...parent } = jsonSchema;
4977
+ const branch = findMatchingBranch(value, branches);
4978
+ if (!branch) return "properties" in parent || "items" in parent ? transformNullToUndefined(value, parent) : value;
4979
+ return transformNullToUndefined(value, mergeSchemas(parent, branch));
4980
+ }
4974
4981
  if (typeof value !== "object" || Array.isArray(value)) {
4975
4982
  if (Array.isArray(value) && jsonSchema.items && typeof jsonSchema.items === "object") return value.map((item) => transformNullToUndefined(item, jsonSchema.items));
4976
4983
  return value;
@@ -4985,6 +4992,58 @@ function transformNullToUndefined(value, jsonSchema) {
4985
4992
  return result;
4986
4993
  }
4987
4994
  /**
4995
+ * Picks the non-null union branch (from `anyOf`/`oneOf`) whose shape matches the value,
4996
+ * e.g. the object branch of a `.nullable()` object. For objects, branches whose required
4997
+ * keys are missing or whose `const`/`enum` properties (discriminators) disagree are skipped.
4998
+ */
4999
+ function findMatchingBranch(value, branches) {
5000
+ const isArray = Array.isArray(value);
5001
+ if (typeof value !== "object" && !isArray) return;
5002
+ return branches.find((branch) => {
5003
+ if (!branch || typeof branch !== "object") return false;
5004
+ const types = Array.isArray(branch.type) ? branch.type : [branch.type];
5005
+ if (isArray) {
5006
+ if (!("items" in branch || types.includes("array"))) return false;
5007
+ const items = branch.items;
5008
+ return !items || typeof items !== "object" || value.every((item) => itemMatches(item, items));
5009
+ }
5010
+ if (!("properties" in branch || "anyOf" in branch || "oneOf" in branch || types.includes("object"))) return false;
5011
+ return objectMatchesBranch(value, branch);
5012
+ });
5013
+ }
5014
+ function itemMatches(item, schema) {
5015
+ if (item === null || typeof item !== "object" || Array.isArray(item)) return true;
5016
+ const branches = schema.anyOf ?? schema.oneOf;
5017
+ if (Array.isArray(branches)) return findMatchingBranch(item, branches) !== void 0;
5018
+ return objectMatchesBranch(item, schema);
5019
+ }
5020
+ function objectMatchesBranch(value, branch) {
5021
+ if ((branch.required ?? []).some((key) => !(key in value))) return false;
5022
+ const properties = branch.properties ?? {};
5023
+ return Object.entries(properties).every(([key, prop]) => {
5024
+ if (!prop || typeof prop !== "object" || !(key in value)) return true;
5025
+ if ("const" in prop) return prop.const === value[key];
5026
+ if (Array.isArray(prop.enum)) return prop.enum.includes(value[key]);
5027
+ return true;
5028
+ });
5029
+ }
5030
+ function mergeSchemas(parent, branch) {
5031
+ const merged = {
5032
+ ...parent,
5033
+ ...branch
5034
+ };
5035
+ if (parent.items && !branch.items) merged.items = parent.items;
5036
+ if (!parent.properties) return merged;
5037
+ return {
5038
+ ...merged,
5039
+ properties: {
5040
+ ...parent.properties,
5041
+ ...branch.properties
5042
+ },
5043
+ required: [.../* @__PURE__ */ new Set([...parent.required ?? [], ...branch.required ?? []])]
5044
+ };
5045
+ }
5046
+ /**
4988
5047
  * Wraps a StandardSchemaWithJSON to transform null values to undefined for
4989
5048
  * optional fields before validation. This is a schema-agnostic solution for
4990
5049
  * OpenAI strict mode, which sends null for optional fields.