Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/server/spec/buildDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const buildDocument = async (input: BuildInput): Promise<Document> => {

for (const block of config.blocks ?? []) {
try {
schemas[blockSchemaName(block.slug)] = buildBlockSchema(block, config)
schemas[blockSchemaName(block.slug)] = buildBlockSchema(block, config, ctx.i18n)
} catch (error) {
logger.warn(`${PLUGIN_NAME}: skipped block "${block.slug}": ${(error as Error).message}`)
}
Expand Down
13 changes: 8 additions & 5 deletions src/server/spec/entitySchemas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getTranslation } from '@payloadcms/translations'
import type { I18n } from '@payloadcms/translations'
import { entityToJSONSchema } from 'payload'
import type { Block, Field, SanitizedConfig, SchemaVariant } from 'payload'

Expand All @@ -20,12 +21,14 @@ const toComponentSchema = ({
config,
source,
idType,
i18n,
defs = new Map(),
variant,
}: {
config: SanitizedConfig
source: Entity | Block
idType: IDType
i18n?: I18n
defs?: Map<string, unknown>
variant?: SchemaVariant
}): Schema => {
Expand All @@ -36,7 +39,7 @@ const toComponentSchema = ({
idType,
new Set(),
undefined,
undefined,
i18n,
undefined,
variant,
)
Expand Down Expand Up @@ -205,8 +208,8 @@ export const buildEntitySchemas = async ({
const source = stripInterfaceName(entity)
const defs = new Map<string, unknown>()
const idType = ctx.defaultIDType
const read = toComponentSchema({ config, source, idType, defs })
const write = toComponentSchema({ config, source, idType, defs, variant: 'input' })
const read = toComponentSchema({ config, source, idType, i18n: ctx.i18n, defs })
const write = toComponentSchema({ config, source, idType, i18n: ctx.i18n, defs, variant: 'input' })
const isAuth = 'auth' in entity && Boolean(entity.auth)

for (const schema of [read, write]) {
Expand Down Expand Up @@ -237,8 +240,8 @@ export const buildEntitySchemas = async ({
return { read, create, update }
}

export const buildBlockSchema = (block: Block, config: SanitizedConfig): Schema => {
const schema = toComponentSchema({ config, source: stripInterfaceName(block), idType: 'text' })
export const buildBlockSchema = (block: Block, config: SanitizedConfig, i18n?: I18n): Schema => {
const schema = toComponentSchema({ config, source: stripInterfaceName(block), idType: 'text', i18n })
const blockType: Schema = { type: 'string', enum: [block.slug] }
const properties = { ...schema.properties, blockType }
const required = [...new Set(['blockType', ...(schema.required ?? [])])]
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,20 @@ describe('spec/entitySchemas', () => {
expect(update.properties).not.toHaveProperty('lockedLater')
expect(update.properties).toHaveProperty('adminOnly')
})

it('resolves a locale-keyed field description without an `en` key', async () => {
const { buildEntitySchemas } = await import('@/server/spec/entitySchemas.js')
const fields = [{ name: 'title', type: 'text', admin: { description: { de: 'Titel' } } }] as Field[]
const entity = {
slug: 'posts',
fields,
flattenedFields: flattenAllFields({ fields }),
} as unknown as SanitizedCollectionConfig
const config = { collections: [entity], blocks: [] } as unknown as SanitizedConfig
const deCtx = { ...ctx, i18n: { ...ctx.i18n, language: 'de' } as I18n }
const { read } = await buildEntitySchemas({ entity, config, ctx: deCtx })
expect(read.properties?.title).toMatchObject({ description: 'Titel' })
})
})

describe('spec/params', () => {
Expand Down