# flatten

> This document is the Markdown version of [valibot.dev/api/flatten/](https://valibot.dev/api/flatten/). For the complete documentation index, see [llms.txt](https://valibot.dev/llms.txt).

Flatten the error messages of issues.

```ts
const errors = v.flatten<TSchema>(issues);
```

## Generics

- `TSchema` `extends BaseSchema<unknown, unknown, BaseIssue<unknown>> | BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>`

## Parameters

- `issues` `[InferIssue<TSchema>, ...InferIssue<TSchema>[]]`

### Explanation

The error messages of issues without a path that belong to the root of the schema are added to the `.root` key.

The error messages of issues with a path that belong to the nested parts of the schema and can be converted to a dot path are added to the `.nested` key.

Some issue paths, for example for complex data types like `Set` and `Map`, have no key or a key that cannot be converted to a dot path. These error messages are added to the `.other` key.

## Returns

- `errors` `FlatErrors<TSchema>`

## Examples

The following example show how `flatten` can be used.

```ts
const Schema = v.object({
  nested: v.object({
    foo: v.string('Value of "nested.foo" is invalid.'),
  }),
});

const result = v.safeParse(Schema, { nested: { foo: null } });

if (result.issues) {
  const flatErrors = v.flatten<typeof Schema>(result.issues);

  // ...
}
```

## Related

The following APIs can be combined with `flatten`.

### Methods

[`parse`](/api/parse.md), [`parser`](/api/parser.md), [`safeParse`](/api/safeParse.md)
