# Pydantic Support and Validation

This allows you to define structured schemas for incoming data and ensures that requests are validated automatically before reaching your route logic.

## Why Use Validation?

Validation helps you:

* Ensure incoming data is correctly structured
* Automatically parse and type-check request bodies
* Reduce manual validation logic
* Return consistent error responses

## Basic Usage

Define a Pydantic model:

```python
from pydantic import BaseModel

class UserCreate(BaseModel):
    name: str
    age: int
```

Use it in your route:

```python
@app.route("/users", methods=["POST"])
def create_user(request, body: UserCreate):
    return {
        "name": body.name,
        "age": body.age
    }
```

## How It Works

* Slush inspects the function signature
* Detects parameters annotated with Pydantic models
* Parses `request.json` into that model
* Injects the validated object into your function

## Valid Request Example

```json
{
  "name": "John Doe",
  "age": 25
}
```

Response:

```json
{
  "name": "John Doe",
  "age": 25
}
```

## Invalid Request Example

```json
{
  "name": "John Doe",
  "age": "invalid"
}
```

Response:

```json
{
  "error": "Validation Error",
  "details": [
    {
      "loc": ["age"],
      "msg": "Input should be a valid integer",
      "type": "int_parsing"
    }
  ]
}
```

Status code: `422 Unprocessable Entity`

## Invalid JSON Body

If the request body is not valid JSON:

```
{bad json
```

Response:

```json
{
  "error": "Invalid JSON body",
  "details": [
    {
      "loc": ["body"],
      "msg": "Request body must be valid JSON",
      "type": "json_invalid"
    }
  ]
}
```

## Key Rules

* Only one request body model is supported per route
* The model must inherit from `BaseModel`
* Data is automatically validated before the handler executes
* If validation fails, the route handler is never called

## Combining Path Params + Validation

```python
@app.route("/users/<int:id>", methods=["POST"])
def update_user(id: int, request, body: UserCreate):
    return {
        "id": id,
        "name": body.name
    }
```

## Summary

With Pydantic validation, Slush allows you to:

* Define strict input schemas
* Automatically parse request data
* Return consistent validation errors
* Keep route logic clean and minimal


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://slush.gitbook.io/docs/documentation/basics/pydantic-support-and-validation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
