Skip to content

Commit aa5ad47

Browse files
authored
fix: named tab being required in generated types without any required fields (#6324)
1 parent 890c21d commit aa5ad47

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

packages/payload/src/utilities/configToJSONSchema.spec.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { configToJSONSchema } from './configToJSONSchema.js'
55

66
describe('configToJSONSchema', () => {
77
it('should handle optional arrays with required fields', async () => {
8+
// @ts-expect-error
89
const config: Config = {
910
collections: [
1011
{
@@ -58,4 +59,91 @@ describe('configToJSONSchema', () => {
5859
type: 'object',
5960
})
6061
})
62+
63+
it('should handle tabs and named tabs with required fields', async () => {
64+
// @ts-expect-error
65+
const config: Config = {
66+
collections: [
67+
{
68+
fields: [
69+
{
70+
type: 'tabs',
71+
tabs: [
72+
{
73+
label: 'unnamedTab',
74+
fields: [
75+
{
76+
type: 'text',
77+
name: 'fieldInUnnamedTab',
78+
},
79+
],
80+
},
81+
{
82+
label: 'namedTab',
83+
name: 'namedTab',
84+
fields: [
85+
{
86+
type: 'text',
87+
name: 'fieldInNamedTab',
88+
},
89+
],
90+
},
91+
{
92+
label: 'namedTabWithRequired',
93+
name: 'namedTabWithRequired',
94+
fields: [
95+
{
96+
type: 'text',
97+
name: 'fieldInNamedTab',
98+
required: true,
99+
},
100+
],
101+
},
102+
],
103+
},
104+
],
105+
slug: 'test',
106+
timestamps: false,
107+
},
108+
],
109+
}
110+
111+
const sanitizedConfig = await sanitizeConfig(config)
112+
const schema = configToJSONSchema(sanitizedConfig, 'text')
113+
114+
expect(schema?.definitions?.test).toStrictEqual({
115+
additionalProperties: false,
116+
properties: {
117+
id: {
118+
type: 'string',
119+
},
120+
fieldInUnnamedTab: {
121+
type: ['string', 'null'],
122+
},
123+
namedTab: {
124+
additionalProperties: false,
125+
type: 'object',
126+
properties: {
127+
fieldInNamedTab: {
128+
type: ['string', 'null'],
129+
},
130+
},
131+
required: [],
132+
},
133+
namedTabWithRequired: {
134+
additionalProperties: false,
135+
type: 'object',
136+
properties: {
137+
fieldInNamedTab: {
138+
type: 'string',
139+
},
140+
},
141+
required: ['fieldInNamedTab'],
142+
},
143+
},
144+
required: ['id', 'namedTabWithRequired'],
145+
title: 'Test',
146+
type: 'object',
147+
})
148+
})
61149
})

packages/payload/src/utilities/configToJSONSchema.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,13 @@ export function fieldsToJSONSchema(
466466
additionalProperties: false,
467467
...childSchema,
468468
})
469-
requiredFieldNames.add(tab.name)
469+
470+
// If the named tab has any required fields then we mark this as required otherwise it should be optional
471+
const hasRequiredFields = tab.fields.some((subField) => fieldIsRequired(subField))
472+
473+
if (hasRequiredFields) {
474+
requiredFieldNames.add(tab.name)
475+
}
470476
} else {
471477
Object.entries(childSchema.properties).forEach(([propName, propSchema]) => {
472478
fieldSchemas.set(propName, propSchema)

0 commit comments

Comments
 (0)