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