Replies: 2 comments 2 replies
-
@Zamiell Hi,
You can find documentation on creating Json Schema here. All TypeBox types are Json Schema so you don't need to do anything else to create schematics (you just create types). Each const N = Type.Number() // const N = { type: 'number' }
const T = Type.Object({ // const T = {
x: Type.Number(), // type: 'object',
y: Type.Number(), // required: ['x', 'y', 'z'],
z: Type.Number() // properties: {
}) // x: { type: 'number' },
// y: { type: 'number' },
// z: { type: 'number' }
// }
// }
//
console.log(T) // If you log T, it's just Json Schema
TypeBox isn't a code generator so it has no CLI commands to run. You just write types (as above) and pass them to validators to check. import { Type } from '@sinclair/typebox'
import Ajv from 'ajv'
const T = Type.Tuple([Type.Number(), Type.Number()])
const ajv = new Ajv()
ajv.validate(T, [1]) // false
ajv.validate(T, [1, 2]) // true
ajv.validate(T, [1, 2, 3]) // false Code GenerationIf you need code generation, there are external community projects available that enable you to convert TypeScript to TypeBox, or Json Schema to TypeBox. See below for relevant links. Json Schema to TypeBoxCode Generator https://github.com/xddq/schema2typebox TypeScript to TypeBoxCode Generator https://github.com/sinclairzx81/typebox-codegen Interactive https://sinclairzx81.github.io/typebox-workbench/ Hope this helps |
Beta Was this translation helpful? Give feedback.
-
for the people who stumble upon this, if you use fastify you can just return the schema object and it will be created as the json schema |
Beta Was this translation helpful? Give feedback.
-
Hello! I am interested in using TypeBox. I've read the README file, but it doesn't explain how to actually create a JSON schema. Is that documented somewhere?
For reference, I've been using
ts-json-schema-generator
, and in my build script I do this:So what is the analogous command in TypeBox land?
A separate question: Why doesn't this package use the
typebox
name? It would make installing it a lot easier. I see that Sinclair seems to own the name on npm, so there must be some other issue?Beta Was this translation helpful? Give feedback.
All reactions