-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from sebastianwessel/29-fix-handling-of-array-…
…type fix: handling of array type #29
- Loading branch information
Showing
5 changed files
with
69 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { FieldDetail } from './getDetailsFromDefinition.js' | ||
|
||
export const generateZodSchemaCode = (fields: FieldDetail[], schemaName: string): string => { | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
const buildSchema = (fieldMap: { [key: string]: any }, fields: FieldDetail[]) => { | ||
for (const field of fields) { | ||
const parts = field.name.split('.').map(part => part.replace('[*]', '')) | ||
let current = fieldMap | ||
|
||
let i = 0 | ||
for (const part of parts) { | ||
if (i === parts.length - 1) { | ||
// Leaf node | ||
if (field.type?.startsWith('array')) { | ||
current[part] = `z.array(${field.zodString.replace('{}', '')}).default(${field.default ?? '[]'})` | ||
} else { | ||
const fieldDefault = field.default ? `.default(${field.default})` : '' | ||
current[part] = `${field.zodString}${fieldDefault}` | ||
} | ||
} else { | ||
// Intermediate node | ||
if (typeof current[part] === 'string') { | ||
current[part] = {} | ||
} | ||
if (!current[part]) { | ||
current[part] = {} | ||
} | ||
current = current[part] | ||
} | ||
i++ | ||
} | ||
} | ||
} | ||
|
||
const generateCode = (fieldMap: { [key: string]: unknown }, schemaName: string): string => { | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
const buildObject = (obj: { [key: string]: any }): string => { | ||
const entries = Object.entries(obj).map(([key, value]) => { | ||
if (typeof value === 'string') { | ||
return `${key}: ${value}` | ||
// biome-ignore lint/style/noUselessElse: <explanation> | ||
} else { | ||
return `${key}: z.object({\n${buildObject(value)}\n})` | ||
} | ||
}) | ||
return entries.join(',\n ') | ||
} | ||
|
||
return `const ${schemaName} = z.object({\n ${buildObject(fieldMap)}\n})` | ||
} | ||
|
||
const fieldMap: { [key: string]: unknown } = {} | ||
buildSchema(fieldMap, fields) | ||
return generateCode(fieldMap, schemaName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,13 @@ | ||
import { fieldsToObject } from './fieldsToObject.js' | ||
import { type FieldDetail, getDetailsFromDefinition } from './getDetailsFromDefinition.js' | ||
import { generateZodSchemaCode } from './generateZodSchemaCode.js' | ||
import { getDetailsFromDefinition } from './getDetailsFromDefinition.js' | ||
|
||
export const mergeNested = (fields: Record<string, string>, isInputSchema: boolean, tableName: string) => { | ||
const schemaName = `${tableName}${isInputSchema ? 'Input' : 'Output'}SchemaGen` | ||
|
||
export const mergeNested = (fields: Record<string, string>, isInputSchema: boolean) => { | ||
const inputFields = Object.entries(fields) | ||
.map(([_fname, definition]) => { | ||
return getDetailsFromDefinition(definition, isInputSchema) | ||
}) | ||
.filter(entry => !entry.skip) | ||
|
||
const obj = fieldsToObject(inputFields) | ||
|
||
const fieldMap = new Map<string, FieldDetail>() | ||
|
||
for (const field of inputFields) { | ||
fieldMap.set(field.name, field) | ||
} | ||
|
||
const mergeSchemaString = (o: object, prev?: string) => { | ||
const entries = Object.entries(o) | ||
|
||
const current = prev ? fieldMap.get(prev) : undefined | ||
|
||
if (!entries.length) { | ||
return current?.zodString ?? 'z.any()' | ||
} | ||
|
||
let res = '' | ||
|
||
for (const [name, value] of entries) { | ||
const n = prev ? `${prev}.${name}` : name | ||
res += `${name}: ${mergeSchemaString(value, n)},\n` | ||
} | ||
|
||
if (current?.zodString.startsWith('z.object({})')) { | ||
return current.zodString.replace('z.object({})', `z.object({${res}})`) | ||
} | ||
|
||
if (current?.zodString.startsWith('z.array(z.any())')) { | ||
return current.zodString.replace('z.array(z.any())', `z.array(z.object({${res}}))`) | ||
} | ||
|
||
return `z.object({${res}})` | ||
} | ||
|
||
return mergeSchemaString(obj) | ||
return generateZodSchemaCode(inputFields, schemaName) | ||
} |