String formats do not work #853
-
@sinclairzx81 String validation using the list of available formats does not work. Here's a reproducible stackblitz link. Run |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
@adrtivv Hi, Unfortunately, TypeBox doesn't provide any string formats by default. But you can still register these formats manually via the FormatRegistry. import { Type, FormatRegistry } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'
import { randomUUID } from 'node:crypto';
import { IsEmail, IsUuid } from './formats' // see below
FormatRegistry.Set('email', value => IsEmail(value))
FormatRegistry.Set('uuid', value => IsUuid(value))
const T = Type.Object({
email: Type.String({ format: 'email' })
uuid: Type.String({ format: 'uuid' })
})
const R = Value.Check(T, {
email: '[email protected]',
uuid: randomUUID()
}) You can find a set of common formats (including email and uuid) in the examples/formats directory of this repo. These formats are ported from the Hope this helps! |
Beta Was this translation helpful? Give feedback.
Hiya,
Keep in mind that TypeBox is primarily a Json Schema builder whose types are designed to be checked by any Json Schema compliant validator. Because of this, the format options are provided to be representative of the Json Schema specification formats (listed here), but TypeBox makes no assumptions as to if you're using Ajv, Hyperjump, TypeCompiler or other library. It also makes no assumptions with respect to the capabilities of each validator.
With respect to formats s…