A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/nuxt/nuxt/discussions/32301 below:

Enable Standard Schema Validation in Nuxt Config · nuxt/nuxt · Discussion #32301 · GitHub

Introduction

Nuxt 3's existing $schema unifies core configuration types and documentation. However, it only supports JSON schema, and is primarily intended for module authors.

There is no built-in support for 'Standard Schema' (Zod, Valibot, ArkType, etc.) to validate the options. This RFC therefore proposes extending the $schema (and module hooks) to accept standard schema objects and merge them into the global configuration schema. This would enable partial build-time validation.

For reference, see:

Proposal 1. Allow $schema to be a Standard Schema

In nuxt.config.ts, if $schema is a Zod/Valibot object, Nuxt will:

  1. Detect it (via a simple isStandardSchema check).
  2. Convert it → JSON Schema fragment.
  3. Deep‐merge that fragment under the specified keys (leaving other keys unvalidated).
Example
import { object, string } from 'valibot'

export default defineNuxtConfig({
  runtimeConfig: {
    baseApi: 'https://api.com',
    token: 12345  // ❌ fails validation,
  },
  $schema: object({
    runtimeConfig: object({
      baseApi: string(),
      token: string()
    })
  })
 })

Here only runtimeConfig.baseApi and runtimeConfig.token are validated.

2. Module Authors Can Use schema:extend with $standardSchema

In a Nuxt module, hook into schema:extend and push:

nuxt.hook('schema:extend', (schemas) => {
  schemas.push({
    myModule: {
      $standardSchema: z.object({
        endpoint: z.string().url(),
        timeout: z.number().int().positive().optional()
      })
    }
  })
})

At build time, Nuxt converts that Standard schema → JSON Schema under config.myModule and merges/validates accordingly.

3. Partial‐Validation Semantics 4. Error Reporting

During nuxi dev/build/generate, run merged JSON Schema validation against nuxt.options.

If errors occur, abort and log:

❌ [Nuxt Schema] Validation failed at "runtimeConfig.token":
Expected string, received number (12345)
More Examples Module‐Level Schema
// modules/nuxt-my-feature/module.ts
import { defineNuxtModule } from '@nuxt/kit'
import { z } from 'zod'

export default defineNuxtModule({
  meta: { name: 'nuxt-my-feature' },
  setup(_, nuxt) {
    nuxt.hook('schema:extend', (schemas) => {
      schemas.push({
        myFeature: {
          $standardSchema: z.object({
            endpoint: z.string().url(),
            timeout: z.number().positive().optional()
          })
        }
      })
    })
  }
})

If a user sets:

export default defineNuxtConfig({
  modules: ['nuxt-my-feature'],
  myFeature: {
    endpoint: 'not-a-url', // ❌ fails
    timeout: 5000          // ✅ okay
  }
 })

Nuxt will error on myFeature.endpoint.

App‐Level Partial Validation
// nuxt.config.ts
import { object, string } from 'valibot'

export default defineNuxtConfig({
  runtimeConfig: {
    baseApi: 42,         // ❌ fails: expects string
    token: 'abc123',     // ✅ okay
    extra: true          // ✅ allowed (ignored)
  },
  $schema: object({
    runtimeConfig: object({
      baseApi: string(),
      token: string()
    })
  })
})

    Build aborts with:

     [Nuxt Schema] Validation failed at "runtimeConfig.baseApi":
       Expected string, received number (42)
Conclusion

By accepting Standard Schema in $schema and in module hooks ($standardSchema), Nuxt can:

  1. Validate arbitrary subtrees (e.g., only runtimeConfig or module options) at build time.
  2. Merge user‐provided schemas without forcing a full JSON Schema for every key.
  3. Preserve backward compatibility with existing JSON Schema usage.

I would love to collaborate on implementing this feature (from schema‐detection to merge/validation).


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4