C
CanxJS
v1.6.2
  • Learn
  • Blog
  • Showcase
C
CanxJS

Ultra-fast async MVC backend framework for Bun. Build production-ready APIs with elegance and speed.

Resources

  • Documentation
  • Learn
  • Blog
  • Showcase

Documentation

  • Introduction
  • Installation
  • Core Concepts
  • CLI Commands
  • API Reference

Legal

  • Privacy Policy
  • Terms of Service

© 2026 CanxJS. All rights reserved.

Built with ❤️ for Candra Kirana

  • Routing
  • Controllers
  • API Resources
  • Dependency Injection
  • Service Providers
  • Middleware
  • Request & Response
  • Authentication
  • Validation
  • Session
  • I18n
  • Utilities
Core Concepts

Validation

Ensure data integrity with CanxJS's powerful validation system. Use simple inline validation or dedicate FormRequest classes for complex logic.

Basic Validation

The validate helper provides a quick way to validate arbitrary data against a set of rules.

basic-validation.ts
1import { validate } from "canxjs";
2
3app.post("/users", async (req, res) => {
4 const data = await req.body();
5
6 const rules = {
7 name: ["required", "string", "min:3"],
8 email: ["required", "email"],
9 age: ["number", "min:18"],
10 role: ["in:admin,user,editor"]
11 };
12
13 const validation = validate(data, rules);
14
15 if (!validation.valid) {
16 return res.status(422).json({
17 error: "Validation Failed",
18 details: Object.fromEntries(validation.errors)
19 });
20 }
21
22 // Use validated data
23 const user = await User.create(validation.data);
24 return res.json(user);
25});

Form Requests

For more complex scenarios, Form Requests encapsulate validation and authorization logic in a single class, keeping your controllers clean.

src/requests/CreateUserRequest.ts
1// src/requests/CreateUserRequest.ts
2import { FormRequest } from "canxjs";
3
4export class CreateUserRequest extends FormRequest {
5 /**
6 * Determine if the user is authorized to make this request.
7 */
8 authorize(): boolean {
9 // Check permissions, roles, etc.
10 return true;
11 }
12
13 /**
14 * Get the validation rules that apply to the request.
15 */
16 rules() {
17 return {
18 name: ["required", "string", "min:3"],
19 email: ["required", "email", "unique:users,email"],
20 password: ["required", "string", "min:8", "confirmed"],
21 };
22 }
23
24 /**
25 * Custom error messages (optional)
26 */
27 messages() {
28 return {
29 "email.unique": "This email is already registered.",
30 "password.confirmed": "Passwords do not match."
31 };
32 }
33}

Using in Controllers

Use the @ValidateWith decorator to automatically validate requests before they hit your controller method.

src/controllers/UserController.ts
1// src/controllers/UserController.ts
2import { Controller } from "canxjs/controller";
3import { ValidateWith } from "canxjs";
4import { CreateUserRequest } from "../requests/CreateUserRequest";
5
6export class UserController extends Controller {
7
8 @ValidateWith(CreateUserRequest)
9 async store(req: CanxRequest, res: CanxResponse) {
10 // Code execution only reaches here if validation passes.
11
12 // Get validated data safely
13 const data = req.context.get("validated");
14
15 const user = await User.create(data);
16 return res.status(201).json(user);
17 }
18}

Available Rules

requiredField must be present and not empty
stringMust be a string
numberMust be a number
booleanMust be true or false
emailMust be a valid email format
min:valueMinimum length (string) or value (number)
max:valueMaximum length (string) or value (number)
in:foo,barMust be one of the given values
urlMust be a valid URL
alphaOnly alphabetic characters
alphanumOnly alphanumeric characters
confirmedField must match {field}_confirmation

Next Steps

Now that your data is safe, learn how to store it in the database.