Skip to content

feat: use json schema instead of zod for internal routines #3347

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 5 commits into from
Jun 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -71,6 +71,7 @@
"destr": "^2.0.5",
"git-url-parse": "^16.1.0",
"jiti": "^2.4.2",
"json-schema-to-typescript": "^15.0.4",
"knitwork": "^1.2.0",
"listhen": "^1.9.0",
"mdast-util-to-hast": "^13.2.0",
@@ -101,8 +102,7 @@
"unist-util-visit": "^5.0.0",
"ws": "^8.18.2",
"zod": "^3.25.48",
"zod-to-json-schema": "^3.24.5",
"zod-to-ts": "^1.2.0"
"zod-to-json-schema": "^3.24.5"
},
"peerDependencies": {
"@electric-sql/pglite": "*",
54 changes: 40 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -11,5 +11,6 @@ onlyBuiltDependencies:
- esbuild
- sharp
- sqlite3
- unrs-resolver
- vue-demi
- workerd
2 changes: 1 addition & 1 deletion src/runtime/internal/preview/collection.ts
Original file line number Diff line number Diff line change
@@ -115,7 +115,7 @@ function computeValuesBasedOnCollectionSchema(collection: CollectionInfo, data:
const fields: string[] = []
const values: Array<string | number | boolean> = []
const properties = (collection.schema.definitions![collection.name] as JsonSchema7ObjectType).properties
const sortedKeys = getOrderedSchemaKeys(properties)
const sortedKeys = getOrderedSchemaKeys(collection.schema)

sortedKeys.forEach((key) => {
const value = (properties)[key]
117 changes: 115 additions & 2 deletions src/runtime/internal/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import type { ZodRawShape } from 'zod'
import type { Draft07, Draft07DefinitionProperty, Draft07DefinitionPropertyAllOf, Draft07DefinitionPropertyAnyOf } from '@nuxt/content'

export function getOrderedSchemaKeys(shape: ZodRawShape | Record<string, unknown>) {
const propertyTypes = {
string: 'VARCHAR',
number: 'INT',
boolean: 'BOOLEAN',
date: 'DATE',
enum: 'VARCHAR',
object: 'TEXT',
}

export function getOrderedSchemaKeys(schema: Draft07) {
const shape = Object.values(schema.definitions)[0].properties
const keys = new Set([
shape.id ? 'id' : undefined,
shape.title ? 'title' : undefined,
@@ -9,3 +19,106 @@ export function getOrderedSchemaKeys(shape: ZodRawShape | Record<string, unknown

return Array.from(keys) as string[]
}

function isJSONProperty(property: Draft07DefinitionProperty | Draft07DefinitionPropertyAllOf | Draft07DefinitionPropertyAnyOf) {
const propertyType = (property as Draft07DefinitionProperty).type
return propertyType === 'object'
|| propertyType === 'array'
|| !!(property as Draft07DefinitionPropertyAnyOf).anyOf
|| !!(property as Draft07DefinitionPropertyAllOf).allOf
}

function getPropertyType(property: Draft07DefinitionProperty | Draft07DefinitionPropertyAllOf | Draft07DefinitionPropertyAnyOf) {
const propertyType = (property as Draft07DefinitionProperty).type
let type = propertyTypes[propertyType as keyof typeof propertyTypes] || 'TEXT'

if ((property as Draft07DefinitionProperty).format === 'date-time') {
type = 'DATE'
}

if ((property as Draft07DefinitionPropertyAllOf).allOf) {
type = 'TEXT'
}

if ((property as Draft07DefinitionPropertyAnyOf).anyOf) {
type = 'TEXT'

const anyOf = (property as Draft07DefinitionPropertyAnyOf).anyOf
const nullIndex = anyOf.findIndex(t => t.type === 'null')
if (anyOf.length === 2 && nullIndex !== -1) {
type = nullIndex === 0
? getPropertyType(anyOf[1])
: getPropertyType(anyOf[0])
}
}

if (Array.isArray(propertyType) && propertyType.includes('null') && propertyType.length === 2) {
type = propertyType[0] === 'null'
? propertyTypes[propertyType[1] as keyof typeof propertyTypes] || 'TEXT'
: propertyTypes[propertyType[0] as keyof typeof propertyTypes] || 'TEXT'
}

return type
}

export function describeProperty(schema: Draft07, property: string) {
const def = Object.values(schema.definitions)[0]
const shape = def.properties
if (!shape[property]) {
throw new Error(`Property ${property} not found in schema`)
}

const type = (shape[property] as Draft07DefinitionProperty).type

const result: { name: string, sqlType: string, type?: string, default?: unknown, nullable: boolean, maxLength?: number, enum?: string[], json?: boolean } = {
name: property,
sqlType: getPropertyType(shape[property]),
type,
nullable: false,
maxLength: (shape[property] as Draft07DefinitionProperty).maxLength,
enum: (shape[property] as Draft07DefinitionProperty).enum,
json: isJSONProperty(shape[property]),
}
if ((shape[property] as Draft07DefinitionPropertyAnyOf).anyOf) {
if (((shape[property] as Draft07DefinitionPropertyAnyOf).anyOf).find(t => t.type === 'null')) {
result.nullable = true
}
}

if (Array.isArray(type) && type.includes('null')) {
result.nullable = true
}

// default value
if ('default' in shape[property]) {
result.default = shape[property].default
}
else if (!def.required.includes(property)) {
result.nullable = true
}

return result
}

export function getCollectionFieldsTypes(schema: Draft07) {
const sortedKeys = getOrderedSchemaKeys(schema)
return sortedKeys.reduce((acc, key) => {
const property = describeProperty(schema, key)
if (property.json) {
acc[key] = 'json'
}
else if (property.sqlType === 'BOOLEAN') {
acc[key] = 'boolean'
}
else if (property.sqlType === 'DATE') {
acc[key] = 'date'
}
else if (property.sqlType === 'INT') {
acc[key] = 'number'
}
else {
acc[key] = 'string'
}
return acc
}, {} as Record<string, 'string' | 'number' | 'boolean' | 'date' | 'json'>)
}
17 changes: 6 additions & 11 deletions src/types/collection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ZodObject, ZodRawShape } from 'zod'
import type { zodToJsonSchema } from 'zod-to-json-schema'
import type { Draft07 } from '../types/schema'
import type { MarkdownRoot } from './content'

export interface PageCollections {}
@@ -52,22 +52,17 @@ export interface DataCollection<T extends ZodRawShape = ZodRawShape> {

export type Collection<T extends ZodRawShape = ZodRawShape> = PageCollection<T> | DataCollection<T>

export interface DefinedCollection<T extends ZodRawShape = ZodRawShape> {
export interface DefinedCollection {
type: CollectionType
source: ResolvedCollectionSource[] | undefined
schema: ZodObject<T>
extendedSchema: ZodObject<T>
schema: Draft07
extendedSchema: Draft07
fields: Record<string, 'string' | 'number' | 'boolean' | 'date' | 'json'>
}

export interface ResolvedCollection<T extends ZodRawShape = ZodRawShape> {
export interface ResolvedCollection extends DefinedCollection {
name: string
tableName: string
type: CollectionType
source: ResolvedCollectionSource[] | undefined
schema: ZodObject<T>
extendedSchema: ZodObject<T>
fields: Record<string, 'string' | 'number' | 'boolean' | 'date' | 'json'>
/**
* Whether the collection is private or not.
* Private collections will not be available in the runtime.
@@ -81,7 +76,7 @@ export interface CollectionInfo {
tableName: string
source: ResolvedCollectionSource[]
type: CollectionType
schema: ReturnType<typeof zodToJsonSchema>
schema: Draft07
fields: Record<string, 'string' | 'number' | 'boolean' | 'date' | 'json'>
tableDefinition: string
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -8,3 +8,4 @@ export type * from './content'
export type * from './tree'
export type * from './database'
export type * from './preview'
export type * from './schema'
37 changes: 37 additions & 0 deletions src/types/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export interface Draft07 {
$schema: 'http://json-schema.org/draft-07/schema#'
$ref: string
definitions: Record<string, Draft07Definition>
}

export interface Draft07Definition {
type: string
properties: Record<string, Draft07DefinitionProperty | Draft07DefinitionPropertyAnyOf | Draft07DefinitionPropertyAllOf>
required: string[]
additionalProperties: boolean
}

export interface Draft07DefinitionProperty {
type?: string // missing type means any
properties?: Record<string, Draft07DefinitionProperty>
required?: string[]
default?: unknown
maxLength?: number
format?: string
enum?: string[]
additionalProperties?: boolean | Record<string, Draft07DefinitionProperty>
$content?: {
editor?: {
input?: 'media' | 'icon' // Override the default input for the field
hidden?: boolean // Do not display the field in the editor
}
}
}

export interface Draft07DefinitionPropertyAnyOf {
anyOf: Draft07DefinitionProperty[]
}

export interface Draft07DefinitionPropertyAllOf {
allOf: Draft07DefinitionProperty[]
}
134 changes: 53 additions & 81 deletions src/utils/collection.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,36 @@
import type { ZodObject, ZodOptionalDef, ZodRawShape, ZodStringDef, ZodType } from 'zod'
import type { ZodRawShape } from 'zod'
import { hash } from 'ohash'
import type { Collection, ResolvedCollection, CollectionSource, DefinedCollection, ResolvedCollectionSource, CustomCollectionSource, ResolvedCustomCollectionSource } from '../types/collection'
import { getOrderedSchemaKeys } from '../runtime/internal/schema'
import type { ParsedContentFile } from '../types'
import { getOrderedSchemaKeys, describeProperty, getCollectionFieldsTypes } from '../runtime/internal/schema'
import type { Draft07, ParsedContentFile } from '../types'
import { defineLocalSource, defineGitHubSource, defineBitbucketSource } from './source'
import { metaSchema, pageSchema } from './schema'
import type { ZodFieldType } from './zod'
import { getUnderlyingType, ZodToSqlFieldTypes, z, getUnderlyingTypeName } from './zod'
import { emptyStandardSchema, mergeStandardSchema, metaStandardSchema, pageStandardSchema } from './schema'
import { z, zodToStandardSchema } from './zod'
import { logger } from './dev'

const JSON_FIELDS_TYPES = ['ZodObject', 'ZodArray', 'ZodRecord', 'ZodIntersection', 'ZodUnion', 'ZodAny', 'ZodMap']

export function getTableName(name: string) {
return `_content_${name}`
}

export function defineCollection<T extends ZodRawShape>(collection: Collection<T>): DefinedCollection {
let schema = collection.schema || z.object({})
let standardSchema: Draft07 = emptyStandardSchema
if (collection.schema instanceof z.ZodObject) {
standardSchema = zodToStandardSchema(collection.schema, '__SCHEMA__')
}

let extendedSchema: Draft07 = standardSchema
if (collection.type === 'page') {
schema = pageSchema.extend((schema as ZodObject<ZodRawShape>).shape)
extendedSchema = mergeStandardSchema(pageStandardSchema, extendedSchema)
}

schema = metaSchema.extend((schema as ZodObject<ZodRawShape>).shape)
extendedSchema = mergeStandardSchema(metaStandardSchema, extendedSchema)

return {
type: collection.type,
source: resolveSource(collection.source),
schema: collection.schema || z.object({}),
extendedSchema: schema,
fields: Object.keys(schema.shape).reduce((acc, key) => {
const underlyingType = getUnderlyingTypeName(schema.shape[key as keyof typeof schema.shape])
if (JSON_FIELDS_TYPES.includes(underlyingType)) {
acc[key] = 'json'
}
else if (['ZodString'].includes(underlyingType)) {
acc[key] = 'string'
}
else if (['ZodDate'].includes(underlyingType)) {
acc[key] = 'date'
}
else if (underlyingType === 'ZodBoolean') {
acc[key] = 'boolean'
}
else if (underlyingType === 'ZodNumber') {
acc[key] = 'number'
}
else {
acc[key] = 'string'
}
return acc
}, {} as Record<string, 'string' | 'number' | 'boolean' | 'date' | 'json'>),
schema: standardSchema,
extendedSchema: extendedSchema,
fields: getCollectionFieldsTypes(extendedSchema),
}
}

@@ -85,21 +66,17 @@ export function resolveCollection(name: string, collection: DefinedCollection):
}

export function resolveCollections(collections: Record<string, DefinedCollection>): ResolvedCollection[] {
const infoSchema = zodToStandardSchema(z.object({
id: z.string(),
version: z.string(),
structureVersion: z.string(),
ready: z.boolean(),
}), 'info')
collections.info = {
type: 'data',
source: undefined,
schema: z.object({
id: z.string(),
version: z.string(),
structureVersion: z.string(),
ready: z.boolean(),
}),
extendedSchema: z.object({
id: z.string(),
version: z.string(),
structureVersion: z.string(),
ready: z.boolean(),
}),
schema: infoSchema,
extendedSchema: infoSchema,
fields: {},
}

@@ -154,13 +131,14 @@ export const SLICE_SIZE = 70000
export function generateCollectionInsert(collection: ResolvedCollection, data: ParsedContentFile): { queries: string[], hash: string } {
const fields: string[] = []
const values: Array<string | number | boolean> = []
const sortedKeys = getOrderedSchemaKeys((collection.extendedSchema).shape)

const sortedKeys = getOrderedSchemaKeys(collection.extendedSchema)

sortedKeys.forEach((key) => {
const value = (collection.extendedSchema).shape[key]
const underlyingType = getUnderlyingType(value as ZodType<unknown, ZodOptionalDef>)
const property = describeProperty(collection.extendedSchema, key)
// const value = (collection.extendedSchema).shape[key]

const defaultValue = value?._def.defaultValue ? value?._def.defaultValue() : 'NULL'
const defaultValue = property?.default ? property.default : 'NULL'

const valueToInsert = (typeof data[key] === 'undefined' || String(data[key]) === 'null')
? defaultValue
@@ -173,23 +151,26 @@ export function generateCollectionInsert(collection: ResolvedCollection, data: P
return
}

if (collection.fields[key] === 'json') {
if (property?.json) {
values.push(`'${JSON.stringify(valueToInsert).replace(/'/g, '\'\'')}'`)
}
else if (underlyingType.constructor.name === 'ZodEnum') {
values.push(`'${String(valueToInsert).replace(/\n/g, '\\n').replace(/'/g, '\'\'')}'`)
else if (property?.sqlType === 'BOOLEAN') {
values.push(!!valueToInsert)
}
else if (underlyingType.constructor.name === 'ZodString') {
values.push(`'${String(valueToInsert).replace(/'/g, '\'\'')}'`)
else if (property?.sqlType === 'INT') {
values.push(Number(valueToInsert))
}
else if (collection.fields[key] === 'date') {
else if (property?.sqlType === 'DATE') {
values.push(`'${new Date(valueToInsert as string).toISOString()}'`)
}
else if (collection.fields[key] === 'boolean') {
values.push(!!valueToInsert)
else if (property?.enum) {
values.push(`'${String(valueToInsert).replace(/\n/g, '\\n').replace(/'/g, '\'\'')}'`)
}
else if ((property?.sqlType || '').match(/^(VARCHAR|TEXT)/)) {
values.push(`'${String(valueToInsert).replace(/'/g, '\'\'')}'`)
}
else {
values.push(valueToInsert)
values.push(String(valueToInsert))
}
})

@@ -250,40 +231,31 @@ export function generateCollectionInsert(collection: ResolvedCollection, data: P

// Convert a collection with Zod schema to SQL table definition
export function generateCollectionTableDefinition(collection: ResolvedCollection, opts: { drop?: boolean } = {}) {
const sortedKeys = getOrderedSchemaKeys((collection.extendedSchema).shape)
const sortedKeys = getOrderedSchemaKeys(collection.extendedSchema)
const sqlFields = sortedKeys.map((key) => {
const type = (collection.extendedSchema).shape[key]!
const underlyingType = getUnderlyingType(type)

if (key === 'id') return `${key} TEXT PRIMARY KEY`

let sqlType: string = ZodToSqlFieldTypes[underlyingType.constructor.name as ZodFieldType]
const property = describeProperty(collection.extendedSchema, key)

// Convert nested objects to TEXT
if (JSON_FIELDS_TYPES.includes(underlyingType.constructor.name)) {
sqlType = 'TEXT'
}
let sqlType = property?.sqlType

if (!sqlType) throw new Error(`Unsupported Zod type: ${underlyingType.constructor.name}`)
if (!sqlType) throw new Error(`Unsupported Zod type: ${property?.type}`)

// Handle string length
if (underlyingType.constructor.name === 'ZodString') {
const checks = (underlyingType._def as ZodStringDef).checks || []
if (checks.some(check => check.kind === 'max')) {
sqlType += `(${checks.find(check => check.kind === 'max')?.value})`
}
if (property.sqlType === 'VARCHAR' && property.maxLength) {
sqlType += `(${property.maxLength})`
}

// Handle optional fields
const constraints = [
type.isNullable() ? ' NULL' : '',
const constraints: string[] = [
property?.nullable ? ' NULL' : '',
]

// Handle default values
if (type._def.defaultValue !== undefined) {
let defaultValue = typeof type._def.defaultValue() === 'string'
? `'${type._def.defaultValue()}'`
: type._def.defaultValue()
if ('default' in property) {
let defaultValue = typeof property.default === 'string'
? `'${property.default}'`
: property.default

if (!(defaultValue instanceof Date) && typeof defaultValue === 'object') {
defaultValue = `'${JSON.stringify(defaultValue)}'`
11 changes: 6 additions & 5 deletions src/utils/content/index.ts
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ import { visit } from 'unist-util-visit'
import type { ResolvedCollection } from '../../types/collection'
import type { FileAfterParseHook, FileBeforeParseHook, ModuleOptions, ContentFile, ContentTransformer, ParsedContentFile } from '../../types'
import { logger } from '../dev'
import { getOrderedSchemaKeys } from '../../runtime/internal/schema'
import { transformContent } from './transformers'

let parserOptions = {
@@ -169,10 +170,10 @@ export async function createParser(collection: ResolvedCollection, nuxt?: Nuxt)
transformers: extraTransformers,
})
const { id: id, __metadata, ...parsedContentFields } = parsedContent
const result = { id } as typeof collection.extendedSchema._type
const result = { id } as ParsedContentFile
const meta = {} as Record<string, unknown>

const collectionKeys = Object.keys(collection.extendedSchema.shape)
const collectionKeys = getOrderedSchemaKeys(collection.extendedSchema)
for (const key of Object.keys(parsedContentFields)) {
if (collectionKeys.includes(key)) {
result[key] = parsedContent[key]
@@ -192,9 +193,9 @@ export async function createParser(collection: ResolvedCollection, nuxt?: Nuxt)
}

if (collectionKeys.includes('seo')) {
result.seo = result.seo || {}
result.seo.title = result.seo.title || result.title
result.seo.description = result.seo.description || result.description
const seo = result.seo = (result.seo || {}) as Record<string, unknown>
seo.title = seo.title || result.title
seo.description = seo.description || result.description
}

const afterParseCtx: FileAfterParseHook = { file: hookedFile, content: result as ParsedContentFile, collection }
22 changes: 15 additions & 7 deletions src/utils/database.ts
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@ import { addDependency } from 'nypm'
import cloudflareD1Connector from 'db0/connectors/cloudflare-d1'
import { isAbsolute, join, dirname } from 'pathe'
import { isWebContainer } from '@webcontainer/env'
import { z } from 'zod'
import type { CacheEntry, D1DatabaseConfig, LocalDevelopmentDatabase, ResolvedCollection, SqliteDatabaseConfig } from '../types'
import type { ModuleOptions, SQLiteConnector } from '../types/module'
import { logger } from './dev'
@@ -71,14 +70,23 @@ const _localDatabase: Record<string, Connector> = {}
export async function getLocalDatabase(database: SqliteDatabaseConfig | D1DatabaseConfig, { connector, sqliteConnector }: { connector?: Connector, nativeSqlite?: boolean, sqliteConnector?: SQLiteConnector } = {}): Promise<LocalDevelopmentDatabase> {
const databaseLocation = database.type === 'sqlite' ? database.filename : database.bindingName
const db = _localDatabase[databaseLocation] || connector || await getDatabase(database, { sqliteConnector })

const cacheCollection = {
tableName: '_development_cache',
extendedSchema: z.object({
id: z.string(),
value: z.string(),
checksum: z.string(),
}),
extendedSchema: {
$schema: 'http://json-schema.org/draft-07/schema#',
$ref: '#/definitions/cache',
definitions: {
cache: {
type: 'object',
properties: {
id: { type: 'string' },
value: { type: 'string' },
checksum: { type: 'string' },
},
required: ['id', 'value', 'checksum'],
},
},
},
fields: {
id: 'string',
value: 'string',
167 changes: 166 additions & 1 deletion src/utils/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as z from 'zod'
import { ContentFileExtension } from '../types/content'
import { getEnumValues } from './zod'
import type { Draft07 } from '../types'

export function getEnumValues<T extends Record<string, unknown>>(obj: T) {
return Object.values(obj) as [(typeof obj)[keyof T]]
}

export const metaSchema = z.object({
id: z.string(),
@@ -9,6 +13,58 @@ export const metaSchema = z.object({
meta: z.record(z.string(), z.any()),
})

export const emptyStandardSchema: Draft07 = {
$schema: 'http://json-schema.org/draft-07/schema#',
$ref: '#/definitions/__SCHEMA__',
definitions: {
__SCHEMA__: {
type: 'object',
properties: {},
required: [],
additionalProperties: false,
},
},
}

export const metaStandardSchema: Draft07 = {
$schema: 'http://json-schema.org/draft-07/schema#',
$ref: '#/definitions/__SCHEMA__',
definitions: {
__SCHEMA__: {
type: 'object',
properties: {
id: {
type: 'string',
},
stem: {
type: 'string',
},
extension: {
type: 'string',
enum: [
'md',
'yaml',
'yml',
'json',
'csv',
'xml',
],
},
meta: {
type: 'object',
additionalProperties: {},
},
},
required: [
'id',
'stem',
'extension',
'meta',
],
additionalProperties: false,
},
},
}
export const pageSchema = z.object({
path: z.string(),
title: z.string(),
@@ -34,3 +90,112 @@ export const pageSchema = z.object({
}),
]).optional().default(true),
})

export const pageStandardSchema: Draft07 = {
$schema: 'http://json-schema.org/draft-07/schema#',
$ref: '#/definitions/__SCHEMA__',
definitions: {
__SCHEMA__: {
type: 'object',
properties: {
path: {
type: 'string',
},
title: {
type: 'string',
},
description: {
type: 'string',
},
seo: {
allOf: [
{
type: 'object',
properties: {
title: {
type: 'string',
},
description: {
type: 'string',
},
},
},
{
type: 'object',
additionalProperties: {},
},
],
default: {},
},
body: {
type: 'object',
properties: {
type: {
type: 'string',
},
children: {},
toc: {},
},
required: [
'type',
],
additionalProperties: false,
},
navigation: {
anyOf: [
{
type: 'boolean',
},
{
type: 'object',
properties: {
title: {
type: 'string',
},
description: {
type: 'string',
},
icon: {
type: 'string',
},
},
required: [
'title',
'description',
'icon',
],
additionalProperties: false,
},
],
default: true,
},
},
required: [
'path',
'title',
'description',
'body',
],
additionalProperties: false,
},
},
}

export function mergeStandardSchema(s1: Draft07, s2: Draft07): Draft07 {
return {
$schema: s1.$schema,
$ref: s1.$ref,
definitions: Object.fromEntries(
Object.entries(s1.definitions).map(([key, def1]) => {
const def2 = s2.definitions[key]
if (!def2) return [key, def1]

return [key, {
...def1,
properties: { ...def1.properties, ...def2.properties },
required: [...new Set([...def1.required, ...(def2.required || [])])],
}]
}),
),
}
}
45 changes: 10 additions & 35 deletions src/utils/templates.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import { gzip } from 'node:zlib'
import { printNode, zodToTs } from 'zod-to-ts'
import { zodToJsonSchema, ignoreOverride } from 'zod-to-json-schema'
import type { NuxtTemplate } from '@nuxt/schema'
import { isAbsolute, join, relative } from 'pathe'
import { genDynamicImport } from 'knitwork'
import { compile as jsonSchemaToTypescript, type JSONSchema } from 'json-schema-to-typescript'
import { pascalCase } from 'scule'
import type { Schema } from 'untyped'
import { createDefu } from 'defu'
import type { CollectionInfo, ResolvedCollection } from '../types/collection'
import type { Manifest } from '../types/manifest'
import type { GitInfo } from './git'
import { generateCollectionTableDefinition } from './collection'

const defu = createDefu((obj, key, value) => {
if (Array.isArray(obj[key]) && Array.isArray(value)) {
obj[key] = value
return true
}
})

const compress = (text: string): Promise<string> => {
return new Promise((resolve, reject) => gzip(text, (err, buff) => {
if (err) {
@@ -47,7 +38,7 @@ export const moduleTemplates = {

export const contentTypesTemplate = (collections: ResolvedCollection[]) => ({
filename: moduleTemplates.types as `${string}.d.ts`,
getContents: ({ options }) => {
getContents: async ({ options }) => {
const publicCollections = (options.collections as ResolvedCollection[]).filter(c => !c.private)
const pagesCollections = publicCollections.filter(c => c.type === 'page')

@@ -56,9 +47,13 @@ export const contentTypesTemplate = (collections: ResolvedCollection[]) => ({
'import type { PageCollectionItemBase, DataCollectionItemBase } from \'@nuxt/content\'',
'',
'declare module \'@nuxt/content\' {',
...publicCollections.map(c =>
indentLines(`interface ${pascalCase(c.name)}CollectionItem extends ${parentInterface(c)} ${printNode(zodToTs(c.schema, pascalCase(c.name)).node)}`),
),
...(await Promise.all(
publicCollections.map(async (c) => {
const type = await jsonSchemaToTypescript(c.schema as JSONSchema, 'CLASS')
.then(code => code.replace('export interface CLASS', `interface ${pascalCase(c.name)}CollectionItem extends ${parentInterface(c)}`))
return indentLines(` ${type}`)
}),
)),
'',
' interface PageCollections {',
...pagesCollections.map(c => indentLines(`${c.name}: ${pascalCase(c.name)}CollectionItem`, 4)),
@@ -208,7 +203,7 @@ export const previewTemplate = (collections: ResolvedCollection[], gitInfo: GitI
source: collection.source?.filter(source => source.repository ? undefined : collection.source) || [],
type: collection.type,
fields: collection.fields,
schema: generateJsonSchema(collection),
schema: collection.schema,
tableDefinition: generateCollectionTableDefinition(collection),
}
return acc
@@ -231,23 +226,3 @@ export const previewTemplate = (collections: ResolvedCollection[], gitInfo: GitI
},
write: true,
})

function generateJsonSchema(collection: ResolvedCollection): ReturnType<typeof zodToJsonSchema> {
const jsonSchema = zodToJsonSchema(collection.extendedSchema, collection.name)
const jsonSchemaWithEditorMeta = zodToJsonSchema(
collection.extendedSchema,
{
name: collection.name,
override: (def) => {
if (def.editor) {
return {
editor: def.editor,
} as never
}

return ignoreOverride
},
})

return defu(jsonSchema, jsonSchemaWithEditorMeta) as ReturnType<typeof zodToJsonSchema>
}
46 changes: 34 additions & 12 deletions src/utils/zod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import type { ZodOptionalDef, ZodType } from 'zod'
import { zodToJsonSchema, ignoreOverride } from 'zod-to-json-schema'
import { z as zod } from 'zod'
import { createDefu } from 'defu'
import type { Draft07 } from '../types'

const defu = createDefu((obj, key, value) => {
if (Array.isArray(obj[key]) && Array.isArray(value)) {
obj[key] = value
return true
}
})

declare module 'zod' {
interface ZodTypeDef {
@@ -27,18 +37,6 @@ export type SqlFieldType = 'VARCHAR' | 'INT' | 'BOOLEAN' | 'DATE' | 'TEXT'

export const z = zod

export const ZodToSqlFieldTypes: Record<ZodFieldType, SqlFieldType> = {
ZodString: 'VARCHAR',
ZodNumber: 'INT',
ZodBoolean: 'BOOLEAN',
ZodDate: 'DATE',
ZodEnum: 'VARCHAR',
} as const

export function getEnumValues<T extends Record<string, unknown>>(obj: T) {
return Object.values(obj) as [(typeof obj)[keyof T]]
}

// Function to get the underlying Zod type
export function getUnderlyingType(zodType: ZodType): ZodType {
while ((zodType._def as ZodOptionalDef).innerType) {
@@ -50,3 +48,27 @@ export function getUnderlyingType(zodType: ZodType): ZodType {
export function getUnderlyingTypeName(zodType: ZodType): string {
return getUnderlyingType(zodType).constructor.name
}

export function zodToStandardSchema(schema: zod.ZodSchema, name: string): Draft07 {
const jsonSchema = zodToJsonSchema(schema, name) as Draft07
const jsonSchemaWithEditorMeta = zodToJsonSchema(
schema,
{
name,
override: (def) => {
if (def.editor) {
return {
$content: {
editor: def.editor,
},
// @deprecated Use $content.editor instead
editor: def.editor,
} as never
}

return ignoreOverride
},
}) as Draft07

return defu(jsonSchema, jsonSchemaWithEditorMeta)
}
31 changes: 15 additions & 16 deletions test/unit/defineCollection.test.ts
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import { defineCollection } from '../../src/utils/collection'
const metaFields = ['id', 'stem', 'meta', 'extension']
const pageFields = ['path', 'title', 'description', 'seo', 'body', 'navigation']

function expectProperties(shape: z.ZodRawShape, fields: string[]) {
function expectProperties(shape: Record<string, unknown>, fields: string[]) {
fields.forEach(field => expect(shape).toHaveProperty(field))
}

@@ -23,11 +23,10 @@ describe('defineCollection', () => {
cwd: '',
}],
})
expect(collection.schema.definitions.__SCHEMA__.properties).not.toHaveProperty('title')

expect(collection.schema.shape).not.ownProperty('title')

expectProperties(collection.extendedSchema.shape, metaFields)
expectProperties(collection.extendedSchema.shape, pageFields)
expectProperties(collection.extendedSchema.definitions.__SCHEMA__.properties, metaFields)
expectProperties(collection.extendedSchema.definitions.__SCHEMA__.properties, pageFields)
})

test('Page with custom schema', () => {
@@ -39,11 +38,11 @@ describe('defineCollection', () => {
}),
})

expect(collection.schema.shape).ownProperty('customField')
expect(collection.extendedSchema.shape).toHaveProperty('customField')
expect(collection.schema.definitions.__SCHEMA__.properties).ownProperty('customField')
expect(collection.extendedSchema.definitions.__SCHEMA__.properties).toHaveProperty('customField')

expectProperties(collection.extendedSchema.shape, metaFields)
expectProperties(collection.extendedSchema.shape, pageFields)
expectProperties(collection.extendedSchema.definitions.__SCHEMA__.properties, metaFields)
expectProperties(collection.extendedSchema.definitions.__SCHEMA__.properties, pageFields)
})

test('Page with object source', () => {
@@ -69,10 +68,10 @@ describe('defineCollection', () => {
}],
})

expect(collection.schema.shape).ownProperty('customField')
expect(collection.extendedSchema.shape).toHaveProperty('customField')
expect(collection.schema.definitions.__SCHEMA__.properties).ownProperty('customField')
expect(collection.extendedSchema.definitions.__SCHEMA__.properties).toHaveProperty('customField')

expectProperties(collection.extendedSchema.shape, pageFields)
expectProperties(collection.extendedSchema.definitions.__SCHEMA__.properties, pageFields)
})

test('Data with schema', () => {
@@ -93,11 +92,11 @@ describe('defineCollection', () => {
}],
})

expect(collection.schema.shape).toHaveProperty('customField')
expect(collection.extendedSchema.shape).toHaveProperty('customField')
expect(collection.schema.shape).not.toHaveProperty('title')
expect(collection.schema.definitions.__SCHEMA__.properties).toHaveProperty('customField')
expect(collection.extendedSchema.definitions.__SCHEMA__.properties).toHaveProperty('customField')
expect(collection.schema.definitions.__SCHEMA__.properties).not.toHaveProperty('title')

expectProperties(collection.extendedSchema.shape, metaFields)
expectProperties(collection.extendedSchema.definitions.__SCHEMA__.properties, metaFields)
})

test('Data with object source', () => {