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:
parent
6ab066f78c
commit
27439adb5d
2 changed files with 15 additions and 0 deletions
Loading…
Add table
Add a link
Reference in a new issue