Aspect patterns
Common patterns for defining aspects
In the Create an aspect guide you learned to define and deploy an aspect. This guide explores common patterns for defining aspects.
Prerequisites:
sanity
CLI v3.88.0 or newer.
Single string field
import { defineAssetAspect } from 'sanity'
export default defineAssetAspect({
name: 'copyright',
title: 'Copyright',
type: 'string',
description: 'Enter the copyright value for this asset.',
}),
})
Single boolean field
import {defineAssetAspect} from 'sanity'
export default defineAssetAspect({
name: 'placeholder',
title: 'Placeholder',
type: 'boolean',
initialValue: false,
description: 'Set to true for temporary placeholder assets.',
})
Multiple fields
import {defineAssetAspect, defineField} from 'sanity'
export default defineAssetAspect({
name: 'copyright',
title: 'copyright',
type: 'object',
fields: [
defineField({
name: 'copyrightHolder',
title: 'Copyright Holder',
type: 'string',
}),
defineField({
name: 'copyrightDate',
title: 'Date',
type: 'date',
}),
],
})
Global references
You can combine aspects with global document references. Use the globalDocumentReference
type to target documents in another project and dataset. This example targets a photographer
type in the example
dataset of the 3do82whm
project.
import { defineAssetAspect } from 'sanity'
export default defineAssetAspect({
name: 'photographer',
title: 'Photographer',
type: 'globalDocumentReference',
description: 'Select the photographer.',
resourceType: 'dataset',
resourceId: '3do82whm.example',
weak: true,
to: [
{
type: 'photographer',
preview: {
select: {
title: 'name'
}
}
}
]
})
The resourceId
value is the projectId.datasetName
. As with normal references in Studio, you can use the preview
property to select fields to display. In this case, it sets the preview title
to the photographer.name
field.
Learn more about Global Document References.
Was this page helpful?