---
title: "NUXT_B5031"
description: "运行时配置 schema 无法转换为 JSON Schema。"
canonical_url: "https://nuxt.zhcndoc.com/docs/4.x/errors/b5031"
---
# B5031

加载配置时，Nuxt 会从 [Standard JSON Schema](https://standardschema.dev/json-schema) 中读取[运行时配置 schema](https://nuxt.zhcndoc.com/docs/4.x/guide/going-further/runtime-config#validating-with-a-schema)的类型和默认值。schema 本身不会包含在服务器 bundle 中。

当 schema 未实现 Standard JSON Schema，或 schema 库无法将其转换时（例如使用了转换操作的 schema），就会报告此错误。用于整个 `runtimeConfig` 的 schema 还必须描述单个对象，不能是联合类型。

## 解决方法

请使用实现了 Standard JSON Schema 的 schema 库，例如 [Zod](https://zod.dev) 或 [ArkType](https://arktype.io)。对于 [Valibot](https://valibot.dev) schema，请使用 `toStandardJsonSchema()` 进行包装：

```ts [nuxt.config.ts]
import * as v from 'valibot'
import { toStandardJsonSchema } from '@valibot/to-json-schema'

export default defineNuxtConfig({
  runtimeConfig: {
    apiSecret: toStandardJsonSchema(v.string()),
  },
})
```

请从运行时配置 schema 中移除转换操作，改为在读取值时进行转换：

```ts [nuxt.config.ts]
import { z } from 'zod'

export default defineNuxtConfig({
  runtimeConfig: {
    allowedHosts: z.string().transform(value => value.split(',')), // [!code --]
    allowedHosts: z.array(z.string()).default([]), // [!code ++]
  },
})
```


## Sitemap

See the full [sitemap](https://nuxt.zhcndoc.com/sitemap.md) for all pages.
