You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/handling-response/response.md
+59-9Lines changed: 59 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,37 @@
1
-
# Handling Responses
1
+
## Introduction
2
+
The `Serializer` class in the Ellar, is a custom class based on `pydantic` models, which provides additional functionality specific to Ellar's requirements.
2
3
3
-
**Ellar** allows you to define the schema of your responses both for validation and documentation purposes.
4
+
To use `Serializer` in Ellar, you simply need to create a class that inherits from `Serializer` and define your data model using pydantic fields.
5
+
Here's an example of how you could define a serializer class for a user model:
4
6
5
-
The response schema is defined on the HTTP method decorator and its applied
6
-
to OPENAPI documentation and validation of the data returned from route handler function.
7
+
```python
8
+
from ellar.serializer import Serializer
9
+
10
+
classUserSerializer(Serializer):
11
+
name: str
12
+
email: str
13
+
age: int
14
+
15
+
```
16
+
17
+
With this setup, you can use the `UserSerializer` class to validate incoming data and or serialize outgoing response data,
18
+
ensuring that it matches the expected format before saving it to the database or returning it to the client.
19
+
20
+
## Handling Responses
7
21
8
-
We'll create a third operation that will return information about a Fake user.
22
+
Let's see how we can use **Serializer** as a responses schema which will help us validate out data output and also provide documentation on route function response.
9
23
24
+
The response schema is defined on the HTTP method decorator.
25
+
26
+
For example:
10
27
```python
11
28
# project_name/apps/items/controllers.py
12
29
13
30
from ellar.serializer import Serializer
14
31
from ellar.common import Controller, get
15
32
from ellar.core import ControllerBase
16
33
34
+
# Define a User class with username, email, first_name, and last_name attributes
0 commit comments