Validations

Validations

The main concept of PTSQ is to create a secure API, both at the TypeScript level and at the runtime level. A validation schema is a tool that allows defining structure and types and then validating the data. The PTSQ uses the Typebox (opens in a new tab) library to define validation schemes. You might wonder why Typebox is chosen over Zod for validations. The primary reason is the seamless integration with introspection. Typebox generates JSON schemas directly, simplifying the introspection process. In contrast, Zod requires the conversion of its schemas into JSON schemas for introspection, adding an extra step and potentially complicating the process. This allows you to create a plain JSON schema using a builder with the ability to statically derive the schema type in TypeScript. Thus, the schema validates the data at the runtime level, but also provides type assumptions for the data at the type level. It is thus a "single source of truth".

Zod
z.string();
 
z.object({
  nickname: z.string().min(4),
  email: z.string().email(),
});
Typebox
Type.String();
 
Type.Object({
  nickname: Type.String({
    minLength: 4,
  }),
  email: Type.String({
    format: 'email',
  }),
});