@mastra/schema-compat 1.3.6-alpha.0 → 1.3.6-alpha.3

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
@@ -6,8 +6,138 @@ const require_schema = require("./schema-Cjk6OMG9.cjs");
6
6
  let zod_v3 = require("zod/v3");
7
7
  let zod = require("zod");
8
8
  let zod_from_json_schema = require("zod-from-json-schema");
9
- let zod_from_json_schema_v3 = require("zod-from-json-schema-v3");
10
9
  let zod_v4 = require("zod/v4");
10
+ //#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@/zod-from-json-schema/0.0.5/8e273a13436172e49bb0898e3f9caf0fd3966851d12111764522ea958a085aeb/node_modules/zod-from-json-schema/dist/index.mjs
11
+ function convertJsonSchemaToZod$1(schema) {
12
+ function addMetadata(zodSchema, jsonSchema) {
13
+ if (jsonSchema.description) zodSchema = zodSchema.describe(jsonSchema.description);
14
+ return zodSchema;
15
+ }
16
+ if (schema.const !== void 0) {
17
+ if (typeof schema.const === "string") return addMetadata(zod.z.literal(schema.const), schema);
18
+ else if (typeof schema.const === "number") return addMetadata(zod.z.literal(schema.const), schema);
19
+ else if (typeof schema.const === "boolean") return addMetadata(zod.z.literal(schema.const), schema);
20
+ else if (schema.const === null) return addMetadata(zod.z.null(), schema);
21
+ return addMetadata(zod.z.literal(schema.const), schema);
22
+ }
23
+ if (schema.type) switch (schema.type) {
24
+ case "string": {
25
+ if (schema.enum) {
26
+ if (schema.enum.length === 0) return addMetadata(zod.z.string(), schema);
27
+ return addMetadata(zod.z.enum(schema.enum), schema);
28
+ }
29
+ let stringSchema = zod.z.string();
30
+ if (schema.minLength !== void 0) stringSchema = stringSchema.min(schema.minLength);
31
+ if (schema.maxLength !== void 0) stringSchema = stringSchema.max(schema.maxLength);
32
+ if (schema.pattern !== void 0) {
33
+ const regex = new RegExp(schema.pattern);
34
+ stringSchema = stringSchema.regex(regex);
35
+ }
36
+ return addMetadata(stringSchema, schema);
37
+ }
38
+ case "number":
39
+ case "integer": {
40
+ if (schema.enum) {
41
+ if (schema.enum.length === 0) return addMetadata(zod.z.number(), schema);
42
+ const options = schema.enum.map((val) => zod.z.literal(val));
43
+ if (options.length === 1) return addMetadata(options[0], schema);
44
+ if (options.length >= 2) return addMetadata(zod.z.union([
45
+ options[0],
46
+ options[1],
47
+ ...options.slice(2)
48
+ ]), schema);
49
+ }
50
+ let numberSchema = schema.type === "integer" ? zod.z.number().int() : zod.z.number();
51
+ if (schema.minimum !== void 0) numberSchema = numberSchema.min(schema.minimum);
52
+ if (schema.maximum !== void 0) numberSchema = numberSchema.max(schema.maximum);
53
+ if (schema.exclusiveMinimum !== void 0) numberSchema = numberSchema.gt(schema.exclusiveMinimum);
54
+ if (schema.exclusiveMaximum !== void 0) numberSchema = numberSchema.lt(schema.exclusiveMaximum);
55
+ if (schema.multipleOf !== void 0) numberSchema = numberSchema.multipleOf(schema.multipleOf);
56
+ return addMetadata(numberSchema, schema);
57
+ }
58
+ case "boolean":
59
+ if (schema.enum) {
60
+ if (schema.enum.length === 0) return addMetadata(zod.z.boolean(), schema);
61
+ const options = schema.enum.map((val) => zod.z.literal(val));
62
+ if (options.length === 1) return addMetadata(options[0], schema);
63
+ if (options.length >= 2) return addMetadata(zod.z.union([
64
+ options[0],
65
+ options[1],
66
+ ...options.slice(2)
67
+ ]), schema);
68
+ }
69
+ return addMetadata(zod.z.boolean(), schema);
70
+ case "null": return addMetadata(zod.z.null(), schema);
71
+ case "object":
72
+ if (schema.properties) {
73
+ const shape = {};
74
+ for (const [key, propSchema] of Object.entries(schema.properties)) shape[key] = convertJsonSchemaToZod$1(propSchema);
75
+ if (schema.required && Array.isArray(schema.required)) {
76
+ const required = new Set(schema.required);
77
+ for (const key of Object.keys(shape)) if (!required.has(key)) shape[key] = shape[key].optional();
78
+ } else for (const key of Object.keys(shape)) shape[key] = shape[key].optional();
79
+ let zodSchema;
80
+ if (schema.additionalProperties !== false) zodSchema = zod.z.object(shape).passthrough();
81
+ else zodSchema = zod.z.object(shape);
82
+ return addMetadata(zodSchema, schema);
83
+ }
84
+ return addMetadata(zod.z.object({}), schema);
85
+ case "array": {
86
+ let arraySchema;
87
+ if (schema.items) arraySchema = zod.z.array(convertJsonSchemaToZod$1(schema.items));
88
+ else arraySchema = zod.z.array(zod.z.any());
89
+ if (schema.minItems !== void 0) arraySchema = arraySchema.min(schema.minItems);
90
+ if (schema.maxItems !== void 0) arraySchema = arraySchema.max(schema.maxItems);
91
+ if (schema.uniqueItems === true) arraySchema = arraySchema.refine((items) => {
92
+ const seen = /* @__PURE__ */ new Set();
93
+ return items.every((item) => {
94
+ if (typeof item === "string" || typeof item === "number" || typeof item === "boolean") {
95
+ if (seen.has(item)) return false;
96
+ seen.add(item);
97
+ return true;
98
+ }
99
+ const serialized = JSON.stringify(item);
100
+ if (seen.has(serialized)) return false;
101
+ seen.add(serialized);
102
+ return true;
103
+ });
104
+ }, { message: "Array items must be unique" });
105
+ return addMetadata(arraySchema, schema);
106
+ }
107
+ }
108
+ if (schema.enum) {
109
+ if (schema.enum.length === 0) return addMetadata(zod.z.never(), schema);
110
+ if (schema.enum.every((val) => typeof val === "string")) return addMetadata(zod.z.enum(schema.enum), schema);
111
+ else {
112
+ const options = schema.enum.map((val) => zod.z.literal(val));
113
+ if (options.length === 1) return addMetadata(options[0], schema);
114
+ if (options.length >= 2) return addMetadata(zod.z.union([
115
+ options[0],
116
+ options[1],
117
+ ...options.slice(2)
118
+ ]), schema);
119
+ }
120
+ }
121
+ if (schema.anyOf && schema.anyOf.length >= 2) {
122
+ const schemas = schema.anyOf.map(convertJsonSchemaToZod$1);
123
+ return addMetadata(zod.z.union([
124
+ schemas[0],
125
+ schemas[1],
126
+ ...schemas.slice(2)
127
+ ]), schema);
128
+ }
129
+ if (schema.allOf) return addMetadata(schema.allOf.reduce((acc, s) => zod.z.intersection(acc, convertJsonSchemaToZod$1(s)), zod.z.object({})), schema);
130
+ if (schema.oneOf && schema.oneOf.length >= 2) {
131
+ const schemas = schema.oneOf.map(convertJsonSchemaToZod$1);
132
+ return addMetadata(zod.z.union([
133
+ schemas[0],
134
+ schemas[1],
135
+ ...schemas.slice(2)
136
+ ]), schema);
137
+ }
138
+ return addMetadata(zod.z.any(), schema);
139
+ }
140
+ //#endregion
11
141
  //#region ../_vendored/ai_v4/dist/dist-Cm5DZgxA.js
12
142
  var marker$1 = "vercel.ai.error";
13
143
  var symbol$1 = Symbol.for(marker$1);
@@ -3333,7 +3463,7 @@ function convertSchemaToZod(schema) {
3333
3463
  const jsonSchemaToConvert = "jsonSchema" in schema ? schema.jsonSchema : schema;
3334
3464
  try {
3335
3465
  if ("toJSONSchema" in zod.z) return (0, zod_from_json_schema.convertJsonSchemaToZod)(jsonSchemaToConvert);
3336
- else return (0, zod_from_json_schema_v3.convertJsonSchemaToZod)(jsonSchemaToConvert);
3466
+ else return convertJsonSchemaToZod$1(jsonSchemaToConvert);
3337
3467
  } catch (e) {
3338
3468
  const errorMessage = `[Schema Builder] Failed to convert schema parameters to Zod. Original schema: ${JSON.stringify(jsonSchemaToConvert)}`;
3339
3469
  console.error(errorMessage, e);
@@ -5052,6 +5182,7 @@ var OpenAISchemaCompatLayer = class extends SchemaCompatLayer {
5052
5182
  }
5053
5183
  if (isObjectSchema(schema)) {
5054
5184
  schema.additionalProperties = false;
5185
+ delete schema.propertyNames;
5055
5186
  if (schema.properties) for (const key of Object.keys(schema.properties)) {
5056
5187
  const prop = schema.properties[key];
5057
5188
  if (!schema.required) schema.required = [];
@@ -5318,28 +5449,29 @@ var AnthropicSchemaCompatLayer = class extends SchemaCompatLayer {
5318
5449
  return this.defaultUnsupportedZodTypeHandler(value);
5319
5450
  }
5320
5451
  processToAISDKSchema(zodSchema) {
5321
- const transformedJsonSchema = require_schema.standardSchemaToJSONSchema(this.processToCompatSchema(zodSchema));
5322
- return jsonSchema(transformedJsonSchema, { validate: (value) => {
5323
- const transformed = this.#traverse(value, transformedJsonSchema);
5324
- const result = zodSchema.safeParse(transformed);
5325
- return result.success ? {
5326
- success: true,
5327
- value: result.data
5328
- } : {
5452
+ const compat = this.processToCompatSchema(zodSchema);
5453
+ return jsonSchema(require_schema.standardSchemaToJSONSchema(compat), { validate: (value) => {
5454
+ const result = compat["~standard"].validate(value);
5455
+ if (result instanceof Promise) throw new Error("Async validation is not supported");
5456
+ return "issues" in result && result.issues ? {
5329
5457
  success: false,
5330
- error: result.error
5458
+ error: new Error(result.issues.map((i) => i.message).join(", "))
5459
+ } : {
5460
+ success: true,
5461
+ value: result.value
5331
5462
  };
5332
5463
  } });
5333
5464
  }
5334
5465
  processToCompatSchema(schema) {
5335
5466
  const originalStandardSchema = require_schema.toStandardSchema(schema);
5467
+ const validationStandardSchema = this.#getCompatValidationStandardSchema(schema, originalStandardSchema);
5336
5468
  return { "~standard": {
5337
5469
  version: 1,
5338
5470
  vendor: "mastra",
5339
5471
  validate: (value) => {
5340
5472
  const transformedJsonSchema = this.processToJSONSchema(schema, "input");
5341
5473
  const transformed = this.#traverse(value, transformedJsonSchema);
5342
- return originalStandardSchema["~standard"].validate(transformed);
5474
+ return validationStandardSchema["~standard"].validate(transformed);
5343
5475
  },
5344
5476
  jsonSchema: {
5345
5477
  input: () => {
@@ -5375,6 +5507,242 @@ var AnthropicSchemaCompatLayer = class extends SchemaCompatLayer {
5375
5507
  for (const key in obj) if (properties[key]) obj[key] = this.#traverse(obj[key], properties[key]);
5376
5508
  return obj;
5377
5509
  }
5510
+ #isHaikuModel() {
5511
+ return this.getModel().modelId.includes("claude-3.5-haiku");
5512
+ }
5513
+ #getCompatValidationStandardSchema(schema, originalStandardSchema) {
5514
+ if (!this.#isHaikuModel() || !isZodType(schema)) return originalStandardSchema;
5515
+ return require_schema.toStandardSchema(this.#stripHaikuStringLengthConstraints(schema));
5516
+ }
5517
+ /**
5518
+ * Strip Haiku-ignored string min/max from a Zod schema for execute-time validation.
5519
+ * Preserves object-level refinements and wrapper semantics unlike full-tree processZodType.
5520
+ */
5521
+ #stripHaikuStringLengthConstraints(value) {
5522
+ const schemas = /* @__PURE__ */ new WeakMap();
5523
+ if ("_zod" in value) return this.#stripHaikuStringLengthConstraintsV4(value, schemas);
5524
+ return this.#stripHaikuStringLengthConstraintsV3(value, schemas);
5525
+ }
5526
+ #stripHaikuStringLengthConstraintsV4(value, schemas) {
5527
+ const cached = schemas.get(value);
5528
+ if (cached) return cached;
5529
+ const schema = value;
5530
+ const def = schema._zod.def;
5531
+ const strip = (child) => this.#stripHaikuStringLengthConstraintsV4(child, schemas);
5532
+ let nextDef = def;
5533
+ const replace = (key) => {
5534
+ const current = def[key];
5535
+ if (!current) return;
5536
+ const stripped = strip(current);
5537
+ if (stripped !== current) nextDef = {
5538
+ ...nextDef,
5539
+ [key]: stripped
5540
+ };
5541
+ };
5542
+ switch (def.type) {
5543
+ case "string": {
5544
+ const checks = (def.checks ?? []).filter((check) => check._zod.def.check !== "min_length" && check._zod.def.check !== "max_length");
5545
+ if (checks.length !== (def.checks?.length ?? 0)) nextDef = {
5546
+ ...def,
5547
+ checks
5548
+ };
5549
+ break;
5550
+ }
5551
+ case "object": {
5552
+ const shape = def.shape;
5553
+ const nextShape = Object.fromEntries(Object.entries(shape).map(([key, child]) => [key, strip(child)]));
5554
+ if (Object.keys(shape).some((key) => nextShape[key] !== shape[key])) nextDef = {
5555
+ ...nextDef,
5556
+ shape: nextShape
5557
+ };
5558
+ replace("catchall");
5559
+ break;
5560
+ }
5561
+ case "array":
5562
+ replace("element");
5563
+ break;
5564
+ case "union": {
5565
+ const options = def.options;
5566
+ const nextOptions = options.map(strip);
5567
+ if (nextOptions.some((option, index) => option !== options[index])) nextDef = {
5568
+ ...nextDef,
5569
+ options: nextOptions
5570
+ };
5571
+ break;
5572
+ }
5573
+ case "intersection":
5574
+ replace("left");
5575
+ replace("right");
5576
+ break;
5577
+ case "tuple": {
5578
+ const items = def.items;
5579
+ const nextItems = items.map(strip);
5580
+ if (nextItems.some((item, index) => item !== items[index])) nextDef = {
5581
+ ...nextDef,
5582
+ items: nextItems
5583
+ };
5584
+ replace("rest");
5585
+ break;
5586
+ }
5587
+ case "record":
5588
+ case "map":
5589
+ replace("keyType");
5590
+ replace("valueType");
5591
+ break;
5592
+ case "set":
5593
+ replace("valueType");
5594
+ break;
5595
+ case "optional":
5596
+ case "nullable":
5597
+ case "default":
5598
+ case "prefault":
5599
+ case "nonoptional":
5600
+ case "success":
5601
+ case "catch":
5602
+ case "readonly":
5603
+ replace("innerType");
5604
+ break;
5605
+ case "pipe":
5606
+ replace("in");
5607
+ replace("out");
5608
+ break;
5609
+ case "promise":
5610
+ replace("innerType");
5611
+ break;
5612
+ case "lazy":
5613
+ nextDef = {
5614
+ ...def,
5615
+ getter: () => strip(def.getter())
5616
+ };
5617
+ break;
5618
+ }
5619
+ if (nextDef === def) {
5620
+ schemas.set(value, value);
5621
+ return value;
5622
+ }
5623
+ let stripped = schema.clone(nextDef);
5624
+ const metadata = schema.meta();
5625
+ if (metadata) stripped = stripped.meta(metadata);
5626
+ schemas.set(value, stripped);
5627
+ return stripped;
5628
+ }
5629
+ #stripHaikuStringLengthConstraintsV3(value, schemas) {
5630
+ const cached = schemas.get(value);
5631
+ if (cached) return cached;
5632
+ const schema = value;
5633
+ const def = schema._def;
5634
+ const strip = (child) => this.#stripHaikuStringLengthConstraintsV3(child, schemas);
5635
+ let nextDef = def;
5636
+ const replace = (key) => {
5637
+ const current = def[key];
5638
+ if (!current) return;
5639
+ const stripped = strip(current);
5640
+ if (stripped !== current) nextDef = {
5641
+ ...nextDef,
5642
+ [key]: stripped
5643
+ };
5644
+ };
5645
+ switch (def.typeName) {
5646
+ case "ZodString": {
5647
+ const checks = (def.checks ?? []).filter((check) => check.kind !== "min" && check.kind !== "max");
5648
+ if (checks.length !== (def.checks?.length ?? 0)) nextDef = {
5649
+ ...def,
5650
+ checks
5651
+ };
5652
+ break;
5653
+ }
5654
+ case "ZodObject": {
5655
+ const shape = def.shape();
5656
+ const nextShape = Object.fromEntries(Object.entries(shape).map(([key, child]) => [key, strip(child)]));
5657
+ if (Object.keys(shape).some((key) => nextShape[key] !== shape[key])) nextDef = {
5658
+ ...nextDef,
5659
+ shape: () => nextShape
5660
+ };
5661
+ replace("catchall");
5662
+ break;
5663
+ }
5664
+ case "ZodArray":
5665
+ case "ZodPromise":
5666
+ case "ZodBranded":
5667
+ replace("type");
5668
+ break;
5669
+ case "ZodUnion": {
5670
+ const options = def.options;
5671
+ const nextOptions = options.map(strip);
5672
+ if (nextOptions.some((option, index) => option !== options[index])) nextDef = {
5673
+ ...nextDef,
5674
+ options: nextOptions
5675
+ };
5676
+ break;
5677
+ }
5678
+ case "ZodDiscriminatedUnion": {
5679
+ const options = def.options;
5680
+ const nextOptions = options.map(strip);
5681
+ if (nextOptions.some((option, index) => option !== options[index])) {
5682
+ const optionsMap = new Map(def.optionsMap);
5683
+ for (const [key, option] of optionsMap) {
5684
+ const index = options.indexOf(option);
5685
+ if (index !== -1) optionsMap.set(key, nextOptions[index]);
5686
+ }
5687
+ nextDef = {
5688
+ ...nextDef,
5689
+ options: nextOptions,
5690
+ optionsMap
5691
+ };
5692
+ }
5693
+ break;
5694
+ }
5695
+ case "ZodIntersection":
5696
+ replace("left");
5697
+ replace("right");
5698
+ break;
5699
+ case "ZodTuple": {
5700
+ const items = def.items;
5701
+ const nextItems = items.map(strip);
5702
+ if (nextItems.some((item, index) => item !== items[index])) nextDef = {
5703
+ ...nextDef,
5704
+ items: nextItems
5705
+ };
5706
+ replace("rest");
5707
+ break;
5708
+ }
5709
+ case "ZodRecord":
5710
+ case "ZodMap":
5711
+ replace("keyType");
5712
+ replace("valueType");
5713
+ break;
5714
+ case "ZodSet":
5715
+ replace("valueType");
5716
+ break;
5717
+ case "ZodOptional":
5718
+ case "ZodNullable":
5719
+ case "ZodDefault":
5720
+ case "ZodCatch":
5721
+ case "ZodReadonly":
5722
+ replace("innerType");
5723
+ break;
5724
+ case "ZodEffects":
5725
+ replace("schema");
5726
+ break;
5727
+ case "ZodPipeline":
5728
+ replace("in");
5729
+ replace("out");
5730
+ break;
5731
+ case "ZodLazy":
5732
+ nextDef = {
5733
+ ...def,
5734
+ getter: () => strip(def.getter())
5735
+ };
5736
+ break;
5737
+ }
5738
+ if (nextDef === def) {
5739
+ schemas.set(value, value);
5740
+ return value;
5741
+ }
5742
+ const stripped = new schema.constructor(nextDef);
5743
+ schemas.set(value, stripped);
5744
+ return stripped;
5745
+ }
5378
5746
  #resolveSchemaForValue(schema, value) {
5379
5747
  if (!Array.isArray(schema.anyOf)) return schema;
5380
5748
  const variants = schema.anyOf;