Closed mblandr closed 1 month ago
@mblandr Hi,
TypeBox doesn't configure any strings formats by default, you will need to configure them yourself. The following configures the email
format, you can add additional format checkers if you need.
import { Type, FormatRegistry } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'
FormatRegistry.Set('email', value => /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i.test(value))
const Email = Type.String({ format: 'email' })
Value.Check(Email, "user@domain.com") // true
Value.Check(Email, "not-a-email") // false
You can find some common formats here Hope this helps S
Ok. thanks
import { Type } from '@sinclair/typebox'; import { Value } from '@sinclair/typebox/value';
const T = Type.String({ format: 'email' }); const a = 'test@gmail.com'; const res = Value.Check(T, a); console.log(res); result is false.Why?