> ## Documentation Index
> Fetch the complete documentation index at: https://docs.molin.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Validation

> Learn how to use schemas for type-safety and payload validation

## Intro

We use [valibot](https://valibot.dev/) schemas to validate the request and response payloads. We also publish these schemas via npm at [@molin.ai/api](https://www.npmjs.com/package/@molin.ai/api) so you can use them to validate the response payloads in your code. Using the [@molin.ai/api](https://www.npmjs.com/package/@molin.ai/api) package is optional as long as your implementation is compliant and follows the schemas of each API.

## Required fields

Unless a field is marked as optional using `v.optional()`, it is required and cannot be `null` or `undefined`. Generally, all top-level fields are required, nested fields may be optional, and you may provide additional data in the `extra` field.

## Example

Install our npm package, including `valibot` because it is a required peer dependency:

```sh theme={null}
npm install --save @molin.ai/api valibot
```

Then, whenever your server receives a request from our API, make sure you run your response through the validator:

```js theme={null}
// make sure you use the correct API version
import { OrderCancellationResponseSchema } from '@molin.ai/api/v20240910.js';

// valibot is required for schema validation
import * as v from 'valibot';

// respond with the data required depending on the request received
const response = {
  cancelled: false,
  reason: 'Invalid order ID',
};

// use valibot's parse or safeParse
v.parse(OrderCancellationResponseSchema, response);

// if validation passes, your response is valid
// if it fails, please double check the schema
```
