Skip to content

Commit

Permalink
fix(schema): use base type for title if no subtype title or name is g…
Browse files Browse the repository at this point in the history
…iven (#6947)
  • Loading branch information
rexxars authored Jun 14, 2024
1 parent fbf90ec commit bbe7ac0
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/@sanity/schema/src/legacy/types/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const FileType = {

const parsed = Object.assign(pick(FILE_CORE, OVERRIDABLE_FIELDS), subTypeDef, {
type: FILE_CORE,
title: subTypeDef.title || (subTypeDef.name ? startCase(subTypeDef.name) : ''),
title: subTypeDef.title || (subTypeDef.name ? startCase(subTypeDef.name) : FILE_CORE.title),
options: options,
fields: subTypeDef.fields.map((fieldDef: any) => {
const {name, fieldset, ...rest} = fieldDef
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/schema/src/legacy/types/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const ImageType = {

const parsed = Object.assign(pick(this.get(), OVERRIDABLE_FIELDS), subTypeDef, {
type: IMAGE_CORE,
title: subTypeDef.title || (subTypeDef.name ? startCase(subTypeDef.name) : ''),
title: subTypeDef.title || (subTypeDef.name ? startCase(subTypeDef.name) : IMAGE_CORE.title),
options: options,
fields: subTypeDef.fields.map((fieldDef: any) => {
const {name, fieldset, ...rest} = fieldDef
Expand Down
4 changes: 2 additions & 2 deletions packages/@sanity/schema/src/legacy/types/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const ObjectType = {
const options = {...(subTypeDef.options || {})}
const parsed = Object.assign(pick(this.get(), OVERRIDABLE_FIELDS), subTypeDef, {
type: this.get(),
title: subTypeDef.title || (subTypeDef.name ? startCase(subTypeDef.name) : ''),
title: subTypeDef.title || (subTypeDef.name ? startCase(subTypeDef.name) : 'Object'),
options: options,
orderings: subTypeDef.orderings || guessOrderingConfig(subTypeDef),
fields: subTypeDef.fields.map((fieldDef: any) => {
Expand Down Expand Up @@ -104,7 +104,7 @@ export const ObjectType = {
title:
extensionDef.title ||
subTypeDef.title ||
(subTypeDef.name ? startCase(subTypeDef.name) : ''),
(subTypeDef.name ? startCase(subTypeDef.name) : 'Object'),
type: parent,
})
lazyGetter(current, '__experimental_search', () => parent.__experimental_search)
Expand Down
42 changes: 42 additions & 0 deletions packages/@sanity/schema/test/legacy/fixtures/schemas/arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,47 @@ export default {
},
],
},
{
name: 'withAnonymousMemberSubtypes',
type: 'object',
fields: [
{
name: 'assets',
type: 'array',
title: 'Array of assets',
of: [{type: 'image'}, {type: 'file'}],
},
],
},
{
name: 'withNamedMemberSubtypes',
type: 'object',
fields: [
{
name: 'assets',
type: 'array',
title: 'Array of assets',
of: [
{type: 'image', name: 'myImage'},
{type: 'file', name: 'myFile'},
],
},
],
},
{
name: 'withTitledMemberSubtypes',
type: 'object',
fields: [
{
name: 'assets',
type: 'array',
title: 'Array of assets',
of: [
{type: 'image', name: 'myImage', title: 'My beautiful image'},
{type: 'file', name: 'myFile', title: 'My wonderful file'},
],
},
],
},
],
}
24 changes: 24 additions & 0 deletions packages/@sanity/schema/test/legacy/schemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,27 @@ Object.keys(rawSchemas).forEach((name) => {
expect(() => parseSchema((rawSchemas as any)[name])).not.toThrow()
})
})

test('anonymous image and file subtypes inherit title', () => {
const withSubtypes = new Schema(rawSchemas.arrays).get('withAnonymousMemberSubtypes')
const assetsField = withSubtypes.fields.find((field: any) => field.name === 'assets')
const [imageSubtype, fileSubtype] = assetsField.type.of
expect(imageSubtype).toHaveProperty('title', 'Image')
expect(fileSubtype).toHaveProperty('title', 'File')
})

test('named image and file subtypes infer title', () => {
const withSubtypes = new Schema(rawSchemas.arrays).get('withNamedMemberSubtypes')
const assetsField = withSubtypes.fields.find((field: any) => field.name === 'assets')
const [imageSubtype, fileSubtype] = assetsField.type.of
expect(imageSubtype).toHaveProperty('title', 'My Image')
expect(fileSubtype).toHaveProperty('title', 'My File')
})

test('titled image and file subtypes use defined title', () => {
const withSubtypes = new Schema(rawSchemas.arrays).get('withTitledMemberSubtypes')
const assetsField = withSubtypes.fields.find((field: any) => field.name === 'assets')
const [imageSubtype, fileSubtype] = assetsField.type.of
expect(imageSubtype).toHaveProperty('title', 'My beautiful image')
expect(fileSubtype).toHaveProperty('title', 'My wonderful file')
})

0 comments on commit bbe7ac0

Please sign in to comment.