@mastra/schema-compat 1.3.12-alpha.0 → 1.3.12
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 +59 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +59 -0
- package/dist/index.js.map +1 -1
- package/dist/null-to-undefined.d.ts.map +1 -1
- package/package.json +8 -8
package/dist/index.js
CHANGED
|
@@ -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.
|