TypeBox to Model #23
Replies: 1 comment 1 reply
-
@TomHiller-swd Hi, The TypeBoxModel is essentially an array of TypeBox types, meaning there isn't really a need to convert TypeBox into the Model. The Model does however mandate that Types have This is maybe best illustrated with a quick example. import * as Codegen from '@sinclair/typebox-codegen'
import { Type } from '@sinclair/typebox'
// ------------------------------------------------------------------
// Types
// ------------------------------------------------------------------
const T = Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number(),
}, { $id: 'T'})
const U = Type.Object({
position: Type.Ref(T),
normal: Type.Ref(T),
}, { $id: 'U' })
// ------------------------------------------------------------------
// Model
// ------------------------------------------------------------------
const Model: Codegen.TypeBoxModel = {
exports: new Map(), // unused
types: [T, U] // sort in order of definition
}
// ------------------------------------------------------------------
// Generate
// ------------------------------------------------------------------
const G = Codegen.ModelToZod.Generate(Model)
// where G is:
//
// import z from 'zod'
//
// export type T = z.infer<typeof T>
// export const T = z.object({
// x: z.number(),
// y: z.number(),
// z: z.number()
// })
//
// export type U = z.infer<typeof U>
// export const U = z.object({
// position: T,
// normal: T
// }) I do need to write some documentation for the TypeBoxModel at some point, but is currently pending some upstream functionality to support either cyclic references or topological sorted types (haven't decided which approach to take at this stage), so the Model is currently treated as an opaque data structure that may be expanded upon in later revisions. For now, you can use the above if you need this functionality |
Beta Was this translation helpful? Give feedback.
-
I apologize if this already exists. Conversions from Typescript to both TypeBox and TypeBox models exist as well as conversions from TypeBox models to a variety of formats but cannot find where TypeBox can be converted to TypeBox Models.
This may seem like an odd request. I am trying to unify existing Typescript types and JSON Schemas to ensure changes made to one are not made to the other. TypeBox seems to be the perfect way to do this. Although, due to JSON Schema needed in non-Typescript services and third parties needing access to Typescript types without additional dependency, we need to convert these to other formats.
It looks like the logic already exists in src/typescript/typescript-to-model.ts if Generate did not call
TypeScriptToTypeBox.Generate
or provided the option to setuseTypeBoxImport
to true. I am new to TypeBox so there may be some nuance I am not familiar with that makes this much more difficult. If it is this straight forward, could the option to convert TypeBox to Models be abstracted out?Thank you in advance.
Beta Was this translation helpful? Give feedback.
All reactions