Skip to content

Commit

Permalink
chore: update dto docs (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
elcharitas authored Sep 14, 2024
1 parent 195b632 commit d4d6fa3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sites/docs/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function HomepageHeader() {
<div className="mt-6 sm:mt-10 flex justify-center space-x-4 text-sm">
<a
className="btn btn-primary text-center py-3 w-full md:w-fit"
href="/docs/intro"
href="/docs"
>
Get started
</a>
Expand Down
68 changes: 67 additions & 1 deletion sites/docs/versioned_docs/version-0.4/advanced/dtos.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,70 @@
sidebar_position: 1
---

# DTOs
# Data Transfer Objects (DTOs)
Data Transfer Objects (DTOs) are objects that carry data between processes. They are used to encapsulate data and send it from one part of your app to another. DTOs can be used to reduce the number of method calls and to improve performance.

In ngyn, DTOs have a couple of super powers:
- They can be used to validate data before it is handled by routes.
- They can be used to transform data before it is handled by routes.
- They can be used to serialize data before it is sent to the client (valid responses)

## Creating a DTO
To create a DTO, you need to create a serializable struct that derives the `Dto` derive macro. The `Dto` derive macro is a custom derive that implements the `Dto` trait for the struct.

```rust
use ngyn::prelude::*;

#[derive(Dto, serde::Serialize, serde::Deserialize)]
struct CreateUserDto {
username: String,
email: String,
password: String,
}
```

## Validating a DTO
In ngyn, you can either validate the data of a Dto through serde or by enabling the `validate` feature in your `Cargo.toml` file and implementing the `Validate` trait for the struct. Advanced usage of the `Validate` trait is covered in the [Validation](/docs/advanced/validation) section.

```rust examole validation with serde
use ngyn::prelude::*;

#[derive(Dto, serde::Serialize, serde::Deserialize)]
struct CreateUserDto {
#[serde(validate(length(min = 3, max = 20))]
username: String,
#[serde(validate(email))]
email: String,
#[serde(validate(length(min = 8))]
password: String,
}
```

Validating using serde has a significant limitation. Errors aren't returned as a result of the validation. Instead, the validation errors are returned as a `500 Internal Server Error` response. To get around this limitation, you can use the `validate` feature.


## Using a DTO
To use a DTO, you can simply create an instance of the struct and pass it to a route handler.

```rust
use ngyn::prelude::*;

#[derive(Dto, serde::Serialize, serde::Deserialize)]
struct CreateUserDto {
username: String,
email: String,
password: String,
}

#[controller]
struct UserController;

#[routes]
impl UserController {
#[post("/users")]
async fn create_user(dto: CreateUserDto) -> Result<u16, Error> {
// Create a user
Ok(201)
}
}
```

0 comments on commit d4d6fa3

Please sign in to comment.