-
Notifications
You must be signed in to change notification settings - Fork 0
fix: reduce safe runtime config validation noise #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Nuxt Safe Runtime Config | ||
|
|
||
| Use this when modifying a Nuxt app that depends on `nuxt-safe-runtime-config`, or when adding typed runtime config validation to a Nuxt app. | ||
|
|
||
| ## Core model | ||
|
|
||
| - Configure the module with `safeRuntimeConfig.$schema`. | ||
| - The schema must implement Standard Schema. Zod, Valibot, ArkType, and other Standard Schema compatible libraries are supported. | ||
| - Build-time validation is enabled by default. Runtime validation is opt-in with `validateAtRuntime: true`. | ||
| - `useSafeRuntimeConfig()` is the typed accessor for app code, server routes, and server utilities. | ||
| - Generated types live under `.nuxt/types/safe-runtime-config.d.ts`; never edit generated files directly. | ||
|
|
||
| ## Editing workflow | ||
|
|
||
| 1. When adding or renaming a runtime config key, update both `runtimeConfig` and the Standard Schema in `safeRuntimeConfig.$schema`. | ||
| 2. Prefer `useSafeRuntimeConfig()` over `useRuntimeConfig()` wherever typed access matters. | ||
| 3. If an editor or utility file runs outside Nuxt auto-import context, import the composable from `#imports`. | ||
| 4. Do not ask consumers to install JSON Schema converter packages for Zod or Valibot; the module includes the conversion path. | ||
| 5. After config or schema changes, run `nuxi prepare` or the project typecheck so generated types refresh. | ||
|
|
||
| ## Validation behavior | ||
|
|
||
| - `onError: 'throw'` is the default and should stay in CI-facing validation paths. | ||
| - Use `onError: 'warn'` only for migration periods where missing values are expected. | ||
| - Use `validateAtRuntime: true` when deployment-time environment variables can differ from build-time values. | ||
| - Treat validation failures as configuration bugs; do not patch around them by casting the config to `any`. | ||
|
|
||
| ## Type-safety checks | ||
|
|
||
| - `config.public.*` should reflect public runtime config keys. | ||
| - Private runtime config keys should stay off `config.public`. | ||
| - Unknown keys should fail typechecking after the generated declarations are refreshed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "skills": [ | ||
| { | ||
| "name": "nuxt-safe-runtime-config", | ||
| "description": "Use Nuxt Safe Runtime Config to validate Nuxt runtimeConfig with Standard Schema and consume it through typed generated helpers.", | ||
| "files": ["SKILL.md"] | ||
| } | ||
| ] | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
docs/public/.well-known/skills/nuxt-safe-runtime-config/SKILL.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Nuxt Safe Runtime Config | ||
|
|
||
| Use this when modifying a Nuxt app that depends on `nuxt-safe-runtime-config`, or when adding typed runtime config validation to a Nuxt app. | ||
|
|
||
| ## Core model | ||
|
|
||
| - Configure the module with `safeRuntimeConfig.$schema`. | ||
| - The schema must implement Standard Schema. Zod, Valibot, ArkType, and other Standard Schema compatible libraries are supported. | ||
| - Build-time validation is enabled by default. Runtime validation is opt-in with `validateAtRuntime: true`. | ||
| - `useSafeRuntimeConfig()` is the typed accessor for app code, server routes, and server utilities. | ||
| - Generated types live under `.nuxt/types/safe-runtime-config.d.ts`; never edit generated files directly. | ||
|
|
||
| ## Editing workflow | ||
|
|
||
| 1. When adding or renaming a runtime config key, update both `runtimeConfig` and the Standard Schema in `safeRuntimeConfig.$schema`. | ||
| 2. Prefer `useSafeRuntimeConfig()` over `useRuntimeConfig()` wherever typed access matters. | ||
| 3. If an editor or utility file runs outside Nuxt auto-import context, import the composable from `#imports`. | ||
| 4. Do not ask consumers to install JSON Schema converter packages for Zod or Valibot; the module includes the conversion path. | ||
| 5. After config or schema changes, run `nuxi prepare` or the project typecheck so generated types refresh. | ||
|
|
||
| ## Validation behavior | ||
|
|
||
| - `onError: 'throw'` is the default and should stay in CI-facing validation paths. | ||
| - Use `onError: 'warn'` only for migration periods where missing values are expected. | ||
| - Use `validateAtRuntime: true` when deployment-time environment variables can differ from build-time values. | ||
| - Treat validation failures as configuration bugs; do not patch around them by casting the config to `any`. | ||
|
|
||
| ## Type-safety checks | ||
|
|
||
| - `config.public.*` should reflect public runtime config keys. | ||
| - Private runtime config keys should stay off `config.public`. | ||
| - Unknown keys should fail typechecking after the generated declarations are refreshed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,43 @@ | ||
| import type { StandardJSONSchemaV1, StandardSchemaV1 } from '@standard-schema/spec' | ||
| import { toJsonSchema } from '@standard-community/standard-json' | ||
| import { errorMessage } from './error' | ||
|
|
||
| type NativeJsonSchemaOutput = (options: { | ||
| target: StandardJSONSchemaV1.Target | ||
| }) => Record<string, unknown> | Promise<Record<string, unknown>> | ||
|
|
||
| interface StandardSchemaWithNativeJsonSchema extends StandardSchemaV1 { | ||
| '~standard': StandardSchemaV1['~standard'] & { | ||
| jsonSchema?: { | ||
| output?: NativeJsonSchemaOutput | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function getNativeJSONSchemaOutput(schema: StandardSchemaV1): NativeJsonSchemaOutput | undefined { | ||
| const output = (schema as StandardSchemaWithNativeJsonSchema)['~standard']?.jsonSchema?.output | ||
| return typeof output === 'function' ? output : undefined | ||
| } | ||
|
|
||
| export function hasNativeJSONSchema(schema: StandardSchemaV1): boolean { | ||
| return typeof (schema?.['~standard'] as any)?.jsonSchema?.output === 'function' | ||
| return Boolean(getNativeJSONSchemaOutput(schema)) | ||
| } | ||
|
|
||
| export async function getJSONSchema( | ||
| schema: StandardSchemaV1, | ||
| target: StandardJSONSchemaV1.Target = 'draft-2020-12', | ||
| onFallback?: (message: string) => void, | ||
| ): Promise<Record<string, unknown>> { | ||
| if (hasNativeJSONSchema(schema)) { | ||
| const nativeOutput = getNativeJSONSchemaOutput(schema) | ||
|
|
||
| if (nativeOutput) { | ||
| try { | ||
| return (schema['~standard'] as any).jsonSchema.output({ target }) | ||
| return await nativeOutput({ target }) | ||
| } | ||
| catch { | ||
| onFallback?.(`Native JSON Schema failed for target "${target}", using fallback`) | ||
| catch (error) { | ||
| onFallback?.(`Native JSON Schema failed for target "${target}" (${errorMessage(error)}), using @standard-community/standard-json fallback`) | ||
| } | ||
| } | ||
| else { | ||
| onFallback?.('Schema does not support native JSON Schema, using @standard-community/standard-json fallback') | ||
| } | ||
|
|
||
| return await toJsonSchema(schema as any) as Record<string, unknown> | ||
| return await toJsonSchema(schema) as Record<string, unknown> | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { existsSync, readFileSync } from 'node:fs' | ||
| import { join } from 'node:path' | ||
| import { fileURLToPath } from 'node:url' | ||
| import { describe, expect, it } from 'vitest' | ||
|
|
||
| const rootDir = fileURLToPath(new URL('..', import.meta.url)) | ||
|
|
||
| describe('agent context metadata', () => { | ||
| it('advertises docs from package metadata', () => { | ||
| const pkg = JSON.parse(readFileSync(join(rootDir, 'package.json'), 'utf8')) as { | ||
| docs?: string | ||
| homepage?: string | ||
| } | ||
|
|
||
| expect(pkg.homepage).toBe('https://nuxt-safe-runtime-config.onmax.me') | ||
| expect(pkg.docs).toBe('https://nuxt-safe-runtime-config.onmax.me') | ||
| }) | ||
|
|
||
| it('publishes a well-known skill for module-specific context', () => { | ||
| const indexPath = join(rootDir, 'docs/public/.well-known/skills/index.json') | ||
| const compatibilitySkillPath = join(rootDir, 'docs/public/.well-known/skills/SKILL.md') | ||
| const skillPath = join(rootDir, 'docs/public/.well-known/skills/nuxt-safe-runtime-config/SKILL.md') | ||
| const index = JSON.parse(readFileSync(indexPath, 'utf8')) as { | ||
| skills?: Array<{ name?: string, description?: string, files?: string[] }> | ||
| } | ||
|
|
||
| expect(existsSync(skillPath)).toBe(true) | ||
| expect(existsSync(compatibilitySkillPath)).toBe(true) | ||
| expect(index.skills).toEqual([ | ||
| { | ||
| name: 'nuxt-safe-runtime-config', | ||
| description: 'Use Nuxt Safe Runtime Config to validate Nuxt runtimeConfig with Standard Schema and consume it through typed generated helpers.', | ||
| files: ['SKILL.md'], | ||
| }, | ||
| ]) | ||
|
|
||
| const skill = readFileSync(skillPath, 'utf8') | ||
| const compatibilitySkill = readFileSync(compatibilitySkillPath, 'utf8') | ||
| expect(compatibilitySkill).toBe(skill) | ||
| expect(skill).toContain('useSafeRuntimeConfig()') | ||
| expect(skill).toContain('safeRuntimeConfig.$schema') | ||
| expect(skill.toLowerCase()).not.toContain('shelve') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.