# Request Validation in This OpenAPI Server

This project uses **Fastify** as the underlying web server, plus an **OpenAPI-driven** approach to routes and validation. Here’s how it works:

1. **OpenAPI Document**  
   You define your endpoints, request/response schemas, etc. in `example/openapi.yaml`. For example:

   ```yaml
   paths:
     /user/create:
       post:
         x-controller: user.controller
         x-action: create
         summary: Create a new User
         requestBody:
           required: true
           content:
             application/json:
               schema:
                 $ref: '#/components/schemas/NewUser'
         responses:
           '200':
             description: Success
   components:
     schemas:
       NewUser:
         type: object
         required:
           - email
           - password
         properties:
           email:
             type: string
             format: email
           password:
             type: string
   ```

2. **Route Generation**  
   - Our framework loads your `openapi.yaml` using the [`OpenAPIParser`](./framework/openapi/parser.ts).  
   - The [`FastifyServerImplentation.createRoutes()`](./framework/fastify/FastifyServerImplentation.ts) method reads each path’s schema and automatically sets up Fastify routes with a `schema` property that represents your request params, query, and body definitions.  
   - Fastify’s built-in validation uses [Ajv](https://ajv.js.org/) behind the scenes to validate inbound requests against those schemas.

3. **Validation Pipeline**  
   - When a client sends a request to `/user/create`, Fastify matches it to the route defined in `openapi.yaml`.  
   - Fastify checks if any `parameters` (for path/query) or `requestBody` data are provided in the correct shape.  
   - If the data doesn’t match the schema, **Fastify** automatically returns an error (e.g. `400`).  
   - If it’s valid, the request proceeds to your controller method (like `UserController.create`).

4. **Schema Inference**  
   - For `requestBody`, the schema is taken from `requestBody.content['application/json'].schema`.  
   - For path or query parameters, the schema is taken from `parameters[].schema` in `openapi.yaml`.  
   - You can also define response schemas if you want response validation.

5. **Example**  
   Let’s look at the `NewUser` schema. It declares `email` as a string with an email format, and `password` as required. If a request is missing those, you’ll get an error like:

   ```json
   {
     "statusCode": 400,
     "error": "Bad Request",
     "message": "body must have required property 'password'"
   }
   ```

6. **No Extra Controller Code Needed**  
   - Since Fastify handles validation, your controllers only receive valid data.  
   - If the request is invalid, the request never reaches your controller logic.

7. **Where to Edit Schemas**  
   - Any time you need to change request/response shapes, update `example/openapi.yaml`.  
   - The changes automatically reflect in the generated routes and validation rules.

8. **Advanced Validation**  
   - If you need custom rules that aren’t possible in JSON Schema, you can add a [Fastify or Ajv plugin](https://www.fastify.io/docs/latest/Reference/Validation-and-Serialization/#adding-new-keywords).  
   - But generally, the base JSON Schema approach covers most cases.

