Extending TypeBuilder and the compiler #263
-
Hi and thanks for this awesome lib. We tested it in our code base as a replacement for Unfortunately we hit a blocker when we tried to implement validation for javascript Extending the Are there any plans to get support for this in the future or are you open to add the Or maybe if the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
@giggo1604 Hi! Thanks :) Custom Schemas (Date objects)At this stage, the TypeCompiler only supports extending custom string Formats (so would only support serializable Happy to discuss more in this thread if you have some thoughts on exposing an API for custom schemas through the TypeCompiler. Mongo SchematicsIf you're working against MongoDB, it's actually possible to use TypeBox to model Mongo's native Example// ------------------------------------------------------------------------------
//
// TypeBox: MongoDB BsonType Reference
//
// ------------------------------------------------------------------------------
import { Type, Static, TUnsafe } from '@sinclair/typebox'
import { ObjectId, Binary } from 'mongodb'
export interface BsonObjectOptions {
minProperties?: number
maxProperties?: number
additionalProperties?: boolean
}
export interface BsonArrayOptions {
additionalItems?: boolean
minItems?: number
maxItems?: number
uniqueItems?: number
}
export namespace BsonType {
export function Boolean() {
return Type.Unsafe<boolean>({ bsonType: 'bool' })
}
export function String() {
return Type.Unsafe<string>({ bsonType: 'string' })
}
export function Double() {
return Type.Unsafe<number>({ bsonType: 'double' })
}
export function Decimal() {
return Type.Unsafe<number>({ bsonType: 'decimal' })
}
export function Uuid() {
return Type.Unsafe<string>({ bsonType: 'uuid' })
}
export function BinData() {
return Type.Unsafe<Binary>({ bsonType: 'binData' })
}
export function Long() {
return Type.Unsafe<number>({ bsonType: 'long' })
}
export function Null() {
return Type.Unsafe<number>({ bsonType: 'null' })
}
export function Date() {
return Type.Unsafe<Date>({ bsonType: 'date' })
}
export function ObjectId() {
return Type.Unsafe<ObjectId>({ bsonType: 'objectId' })
}
export function Array<T extends TUnsafe<any>>(schema: T, options: BsonArrayOptions = {}) {
return Type.Unsafe<Static<T>[]>({
...options,
bsonType: 'array',
items: schema
})
}
export function Object<T extends Record<string, TUnsafe<any>>>(properties: T, options: BsonObjectOptions = {}) {
return Type.Unsafe<{[K in keyof T]: Static<T[K]>}>({
...options,
bsonType: 'object',
properties
})
}
}
// ------------------------------------------------------------------------------
//
// Example
//
// ------------------------------------------------------------------------------
type T = Static<typeof T>
const T = BsonType.Object({
id: BsonType.ObjectId(),
name: BsonType.String(),
date: BsonType.Date(),
items: BsonType.Array(Type.Object({
x: BsonType.Double(),
y: BsonType.Double(),
z: BsonType.Double(),
}))
}) Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
@giggo1604 Hi, Just an update, TypeBox now provides the ability to extend the compiler and value API's with user defined types. Documentation on this feature can be found here. The following example shows creating a user defined Date type. import { TypeCompiler } from '@sinclair/typebox/compiler'
import { Type, Kind } from '@sinclair/typebox'
import { Custom } from '@sinclair/typebox/custom'
Custom.Set('Date', (schema, value) => value instanceof Date) // custom type check
const T = Type.Unsafe<Date>({ [Kind]: 'Date' }) // custom type schema
const R = TypeCompiler.Compile(T).Check(new Date()) // true As this functionality has landed a little earlier than expected, I'm going to let it settle till early next year, but will likely look to deprecate both Will mark this thread as resolved, but happy to discuss moving from |
Beta Was this translation helpful? Give feedback.
@giggo1604 Hi,
Just an update, TypeBox now provides the ability to extend the compiler and value API's with user defined types. Documentation on this feature can be found here.
The following example shows creating a user defined Date type.
TypeScript Link Here
As this functionality has landed a little ea…