Skip to main content

Data Validation (Zod)

This data validation is came built-in with Yelix. It uses Zod.

Initial Setup

Data Validation is built-in but by default it is not set. You need to set it in your main file.

main.ts
import { Yelix, requestDataValidationMiddleware } from 'jsr:@murat/yelix';

export async function startServer() {
const app = new Yelix();

app.setMiddleware('dataValidation', requestDataValidationMiddleware);

app.serve();
}

await startServer();

Query Parameters

hello.ts
import { Ctx, ValidationType } from "jsr:@murat/yelix";
import { z } from "npm:zod@^3.24.1";

export async function GET(ctx: Ctx) {
const requestData = ctx.get('dataValidation').user;
const query: QueryType = requestData.query;

return await ctx.text('Hello, ' + query.name, 200);
}

export const path = '/api/hello';
export const middlewares = ['dataValidation'];

export const validation: ValidationType = {
query: {
name: z.string(),
},
};
const querySchema = z.object(validation.query as z.ZodRawShape);
type QueryType = z.infer<typeof querySchema>;

Body Parameters

hello.ts
import { Ctx, ValidationType } from "jsr:@murat/yelix";
import { z } from "npm:zod@^3.24.1";

export async function POST(ctx: Ctx) {
const requestData = ctx.get('dataValidation').user;
const {
fullname,
username,
email,
password,
}: BodyType = requestData.body;

return await ctx.text('Hello, ' + fullname, 200);
}

export const path = '/api/hello';
export const middlewares = ['dataValidation'];

export const validation: ValidationType = {
body: z.object({
fullname: z.string().min(3).max(255),
username: z
.string()
.min(3)
.max(255)
.refine((value) => !/@/.test(value), {
message: 'Username should not contain @',
})
.refine((value) => !/\s/.test(value), {
message: 'Username should not contain whitespace',
})
.refine((value) => value === value.toLowerCase(), {
message: 'Username should be lowercase',
}),
email: z.string().email(),
password: z.string().min(8).max(255),
}),
};
type BodyType = z.infer<typeof validation.body>;