Skip to content
Merged
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
89 changes: 88 additions & 1 deletion packages/payload/src/fields/config/sanitize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,95 @@ describe('sanitizeFields', () => {
expect(sanitizedField.validate).toBeDefined()
// Non-virtual text field should use the text validator which checks required/minLength/etc.
// Passing undefined with required should fail
const result = sanitizedField.validate!(undefined as any, { required: true, req: { payload: { config: {} }, t: ((v: string) => v) as any } } as any)
const result = sanitizedField.validate!(
undefined as any,
{ required: true, req: { payload: { config: {} }, t: ((v: string) => v) as any } } as any,
)
expect(result).not.toBe(true)
})

it('should default readOnly to true for virtual: true fields', async () => {
const fields: Field[] = [
{
name: 'virtualText',
type: 'text',
virtual: true,
},
]

const sanitizedField = (
await sanitizeFields({
config,
collectionConfig,
fields,
validRelationships: [],
})
)[0] as TextField

expect(sanitizedField.admin?.readOnly).toBe(true)
})

it('should default readOnly to true for virtual: "string" fields', async () => {
const fields: Field[] = [
{
name: 'virtualRef',
type: 'text',
virtual: 'post.title',
},
]

const sanitizedField = (
await sanitizeFields({
config,
collectionConfig,
fields,
validRelationships: [],
})
)[0] as TextField

expect(sanitizedField.admin?.readOnly).toBe(true)
})

it('should not override readOnly: false on virtual fields', async () => {
const fields: Field[] = [
{
name: 'virtualText',
type: 'text',
virtual: true,
admin: { readOnly: false },
},
]

const sanitizedField = (
await sanitizeFields({
config,
collectionConfig,
fields,
validRelationships: [],
})
)[0] as TextField

expect(sanitizedField.admin?.readOnly).toBe(false)
})

it('should not set readOnly on non-virtual fields', async () => {
const fields: Field[] = [
{
name: 'normalText',
type: 'text',
},
]

const sanitizedField = (
await sanitizeFields({
config,
collectionConfig,
fields,
validRelationships: [],
})
)[0] as TextField

expect(sanitizedField.admin?.readOnly).toBeUndefined()
})
})
})
4 changes: 4 additions & 0 deletions packages/payload/src/fields/config/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ export const sanitizeField = async ({
field.admin = {}
}

if ('virtual' in field && field.virtual && field.admin.readOnly !== false && fieldAffectsData) {
field.admin.readOnly = true
}

// Make sure that the richText field has an editor
if (field.type === 'richText') {
const sanitizeRichText = async (_config: SanitizedConfig) => {
Expand Down
Loading