|
1 | | -import { createContext } from 'unctx' |
| 1 | +import { getContext } from 'unctx' |
| 2 | +import type { Draft07 } from '../types' |
2 | 3 |
|
3 | 4 | type ContextKey = 'zod3' | 'zod4' | 'valibot' | 'unknown' |
4 | 5 |
|
5 | | -const nuxtContentContext = { |
6 | | - zod3: { |
7 | | - toJSONSchema: (_schema: unknown, _name: string) => { |
8 | | - throw new Error( |
9 | | - 'It seems you are using Zod version 3 for collection schema, but Zod is not installed, ' |
10 | | - + 'Nuxt Content does not ship with zod, install `zod` and `zod-to-json-schema` and it will work.', |
11 | | - ) |
| 6 | +type SchemaHandler = { toJSONSchema: (schema: unknown, name: string) => Draft07 } |
| 7 | + |
| 8 | +type NuxtContentContext = { |
| 9 | + zod3: SchemaHandler |
| 10 | + zod4: SchemaHandler |
| 11 | + valibot: SchemaHandler |
| 12 | + unknown: SchemaHandler |
| 13 | + set: (key: ContextKey, value: unknown) => void |
| 14 | + get: (key: ContextKey) => SchemaHandler |
| 15 | +} |
| 16 | + |
| 17 | +const ctx = getContext<NuxtContentContext>('@nuxt/content:validators-context') |
| 18 | + |
| 19 | +/** |
| 20 | + * Initialize the context if it hasn't been set yet. |
| 21 | + */ |
| 22 | +if (!ctx.tryUse()) { |
| 23 | + ctx.set({ |
| 24 | + zod3: { |
| 25 | + toJSONSchema: (_schema: unknown, _name: string) => { |
| 26 | + throw new Error( |
| 27 | + 'It seems you are using Zod version 3 for collection schema, but Zod is not installed, ' |
| 28 | + + 'Nuxt Content does not ship with zod, install `zod` and `zod-to-json-schema` and it will work.', |
| 29 | + ) |
| 30 | + }, |
12 | 31 | }, |
13 | | - }, |
14 | | - zod4: { |
15 | | - toJSONSchema: (_schema: unknown, _name: string) => { |
16 | | - throw new Error( |
17 | | - 'It seems you are using Zod version 4 for collection schema, but Zod is not installed, ' |
18 | | - + 'Nuxt Content does not ship with zod, install `zod` and it will work.', |
19 | | - ) |
| 32 | + zod4: { |
| 33 | + toJSONSchema: (_schema: unknown, _name: string) => { |
| 34 | + throw new Error( |
| 35 | + 'It seems you are using Zod version 4 for collection schema, but Zod is not installed, ' |
| 36 | + + 'Nuxt Content does not ship with zod, install `zod` and it will work.', |
| 37 | + ) |
| 38 | + }, |
20 | 39 | }, |
21 | | - }, |
22 | | - valibot: { |
23 | | - toJSONSchema: (_schema: unknown, _name: string) => { |
24 | | - throw new Error( |
25 | | - 'It seems you are using Valibot for collection schema, but Valibot is not installed, ' |
26 | | - + 'Nuxt Content does not ship with valibot, install `valibot` and `@valibot/to-json-schema` and it will work.', |
27 | | - ) |
| 40 | + valibot: { |
| 41 | + toJSONSchema: (_schema: unknown, _name: string) => { |
| 42 | + throw new Error( |
| 43 | + 'It seems you are using Valibot for collection schema, but Valibot is not installed, ' |
| 44 | + + 'Nuxt Content does not ship with valibot, install `valibot` and `@valibot/to-json-schema` and it will work.', |
| 45 | + ) |
| 46 | + }, |
28 | 47 | }, |
29 | | - }, |
30 | | - unknown: { |
31 | | - toJSONSchema: (_schema: unknown, _name: string) => { |
32 | | - throw new Error('Unknown schema vendor') |
| 48 | + unknown: { |
| 49 | + toJSONSchema: (_schema: unknown, _name: string) => { |
| 50 | + throw new Error('Unknown schema vendor') |
| 51 | + }, |
33 | 52 | }, |
34 | | - }, |
35 | | - set: (key: ContextKey, value: unknown) => { |
36 | | - nuxtContentContext[key] = value as typeof nuxtContentContext[ContextKey] |
37 | | - }, |
38 | | - get: (key: ContextKey) => { |
39 | | - return nuxtContentContext[key] |
40 | | - }, |
| 53 | + set(key: ContextKey, value: unknown) { |
| 54 | + (this as unknown as Record<ContextKey, unknown>)[key] = value |
| 55 | + }, |
| 56 | + get(key: ContextKey) { |
| 57 | + return (this as unknown as Record<ContextKey, SchemaHandler>)[key] |
| 58 | + }, |
| 59 | + }) |
41 | 60 | } |
42 | 61 |
|
43 | | -const ctx = createContext<typeof nuxtContentContext>() |
44 | | -ctx.set(nuxtContentContext) |
45 | | - |
46 | 62 | export default ctx.use |
0 commit comments