0
Fork 0
mirror of https://github.com/mastra-ai/mastra.git synced 2026-11-17 08:44:46 +00:00

(fix) Support union types on jsonSchemaToModel func (#3223)

# Handle array of types in JSON Schema conversion

## Description
Updated the `jsonSchemaPropertiesToTSTypes` function to properly handle
JSON Schema definitions where `type` can be an array of strings.
Previously, the function only handled single string types, but according
to the JSON Schema specification, `type` can be an array of possible
types.

## Changes
- Added support for array of types in `jsonSchemaPropertiesToTSTypes`
- Creates a Zod union type when multiple types are specified
- Preserves descriptions and examples on the union type

## Example
The following JSON Schema can now be properly converted:

```json
{
  "type": "object",
  "properties": {
    "mixedField": {
      "type": ["string", "null"],
      "description": "A field that can be either a string or null",
      "examples": ["example text", null]
    },
    "multiType": {
      "type": ["number", "string", "boolean"],
      "description": "A field that accepts multiple types"
    }
  }
}
```

Will be converted to the equivalent Zod schema:

```typescript
z.object({
  mixedField: z.union([
    z.string(),
    z.null()
  ]).describe("A field that can be either a string or null\nExamples: example text, null"),
  multiType: z.union([
    z.number(),
    z.string(),
    z.boolean()
  ]).describe("A field that accepts multiple types")
})
```

---------

Signed-off-by: Marcos Candeia <marrcooos@gmail.com>
Co-authored-by: Tyler Barnes <tylerdbarnes@gmail.com>
This commit is contained in:
Marcos Candeia 2025-03-25 11:30:13 -03:00 • committed by GitHub
commit 27439adb5d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 0 deletions