Skip to main content
Kit provides a simple pattern for handling incoming request data with automatic validation. The #[request] attribute works with both JSON and form-urlencoded data, making it suitable for REST APIs and HTML forms alike.

Overview

Requests combine three concerns into a single, declarative struct:
  1. Data parsing - Automatically parse JSON or form-urlencoded data
  2. Validation - Validate fields using the validator crate
  3. Authorization - Optionally check if the request is authorized

The #[handler] Attribute

All controller methods in Kit use the #[handler] attribute. This enables automatic extraction and validation of request data:
When combined with validated request types, validation happens automatically:

Defining a Request

The #[request] attribute automatically adds Deserialize and Validate derives:

Validation Rules

Kit uses the validator crate for validation. Here are common validation rules:

String Validations

Numeric Validations

Nested and Collection Validations

Common Validation Attributes

Validation Error Response

When validation fails, Kit automatically returns a 422 response with Laravel/Inertia-compatible error format:
This format integrates seamlessly with Inertia.js form handling on the frontend.

Complete Example

Here’s a complete example of a user registration endpoint: Define the request:
Create the controller:
Register the routes:

Authorization

You can override the authorize method to add authorization checks:
If authorize returns false, the request is rejected with a 403 Forbidden response:

Request Content Types

Requests automatically detect and parse the content type:
  • application/json - Parsed as JSON
  • application/x-www-form-urlencoded - Parsed as form data
The parsing is handled automatically based on the Content-Type header.

Using Request with Validated Data

If you need access to both the validated data and the original request (for headers, params, etc.), you can still access request information in your controller:

File Organization

The standard structure for requests:
src/requests/mod.rs:

End-to-End Type Safety with Inertia

Requests can also derive InertiaProps to generate TypeScript types, enabling end-to-end type safety from your Rust backend to your React frontend.

Generating TypeScript Types for Requests

Add InertiaProps derive alongside #[request]:
Run type generation:
This generates TypeScript types in frontend/src/types/inertia-props.ts:

Type-Safe Forms with Inertia

Use Inertia’s <Form> component for the cleanest form handling:
For more control, combine <Form> with the useForm hook and your generated types:

Benefits of End-to-End Type Safety

  1. Compile-time checks: TypeScript catches field name typos and type mismatches
  2. IDE autocomplete: Full IntelliSense for form fields in your editor
  3. Validation alignment: Your TypeScript types match your Rust validation rules
  4. Refactoring safety: Rename a field in Rust, TypeScript errors show where to update

Workflow

  1. Define request with validation in Rust
  2. Add #[derive(InertiaProps)] to the struct
  3. Run kit generate-types to generate TypeScript
  4. Use the generated type with useForm<RequestType>
  5. Get full type safety and validation error handling
For more information on TypeScript type generation, see TypeScript Types.

Summary