Replies: 1 comment 8 replies
-
|
@cyky Hi, Module is still supported, Import was deprecated in favor of left-side destructuring. import Type from 'typebox'
type AddressSchema = Type.Static<typeof AddressSchema>
type PersonSchema = Type.Static<typeof PersonSchema>
type PostRequestSchema = Type.Static<typeof PostRequestSchema>
export const { AddressSchema, PersonSchema, PostRequestSchema } = Type.Module({
AddressSchema: Type.Object({
street: Type.String(),
streetNumber: Type.Number(),
}),
PersonSchema: Type.Object({
name: Type.String(),
homeAddress: Type.Ref('AddressSchema'),
workAddress: Type.Ref('AddressSchema'),
}),
PostRequestSchema: Type.Object({
person: Type.Ref('PersonSchema'),
}),
})Alternatively you can use a indexed accessors to infer module sub schemas. import Type from 'typebox'
type AddressSchema = Type.Static<typeof HumanModule['AddressSchema']>
type PersonSchema = Type.Static<typeof HumanModule['PersonSchema']>
type PostRequestSchema = Type.Static<typeof HumanModule['PostRequestSchema']>
export const HumanModule = Type.Module({
AddressSchema: Type.Object({
street: Type.String(),
streetNumber: Type.Number(),
}),
PersonSchema: Type.Object({
name: Type.String(),
homeAddress: Type.Ref('AddressSchema'),
workAddress: Type.Ref('AddressSchema'),
}),
PostRequestSchema: Type.Object({
person: Type.Ref('PersonSchema'),
}),
})
// ... as per provided example
export const PersonResponseSchema = Type.Object({
status: Type.String(),
});
export const FooSchema = {
post: {
request: HumanModule.PostRequestSchema,
response: {
200: PersonResponseSchema,
},
},
};Hope this helps! |
Beta Was this translation helpful? Give feedback.
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
We used
Type.ModulewithType.Importsbefore upgrading to version 1.0 in our project.The flow that we used was kinda following:
(example copied from here: fastify/fastify-swagger#854 (comment))
This allowed us to generate correct refs in our fastify application for our schemas. Using for example
HumanModule.PostRequestSchemadoesn't seem to work in the same way as previously the Import did.Now with the new version I'm bit unsure how we can archive the similar functionality and I did not find any mentions regarding removal of Type.Import in the migration notes (https://github.com/sinclairzx81/typebox/blob/main/changelog/1.0.0-migration.md)
Beta Was this translation helpful? Give feedback.
All reactions