Skip to content

Commit

Permalink
fix(core): allow _projectId and _strengthOnPublish in templates (#…
Browse files Browse the repository at this point in the history
…5942)

* fix(core): allow `_projectId` and `_strengthOnPublish` in templates

* test(core): add _projectId and _strengthOnPublish to templates test
  • Loading branch information
ricokahler authored Mar 11, 2024
1 parent 4e72b80 commit 5adca88
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
36 changes: 36 additions & 0 deletions dev/test-studio/schema/standard/crossDatasetReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,42 @@ export default defineType({
name: 'crossDatasetSubtype',
type: 'crossDatasetSubtype',
},
{
name: 'initialValueTest',
type: 'crossDatasetReference',
dataset: 'playground',
studioUrl: ({id, type}) => {
return type
? `${document.location.protocol}//${document.location.host}/playground/content/${type};${id}`
: null
},
to: [
{
type: 'book',
icon: BookIcon,
preview: {
select: {
title: 'title',
subtitle: 'descriptionMd',
media: 'coverImage',
},
prepare(val) {
return {
title: val.title,
subtitle: val.subtitle,
media: val.coverImage,
}
},
},
},
],
initialValue: () => ({
_type: 'crossDatasetReference',
_ref: '4203c6bd-98c2-418e-9558-3ed56ebaf1d8',
_dataset: 'playground',
_projectId: 'ppsg7ml5',
}),
},
],
preview: {
select: {
Expand Down
16 changes: 16 additions & 0 deletions dev/test-studio/schema/standard/references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,22 @@ export default defineType({
},
],
},
{
name: 'withInitialValue',
type: 'reference',
to: [{type: 'author'}],
initialValue: () => ({
_type: 'reference',
_ref: 'f9a5f215-da97-47fe-960f-3452c85ed205',
_weak: true,
_strengthenOnPublish: {
type: 'author',
template: {
id: 'author',
},
},
}),
},
],
preview: {
select: {
Expand Down
35 changes: 31 additions & 4 deletions packages/sanity/src/core/templates/__tests__/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,27 @@ describe('resolveInitialValue', () => {
expect(
resolveInitialValue(
schema,
{...example, value: {bestFriend: {_ref: 'grrm', _type: 'reference', _weak: true}}},
{
...example,
value: {
bestFriend: {
_ref: 'grrm',
_type: 'reference',
_weak: true,
_strengthenOnPublish: {type: 'author', template: {id: 'author'}},
},
},
},
{},
mockConfigContext,
),
).resolves.toMatchObject({
bestFriend: {_ref: 'grrm', _type: 'reference', _weak: true},
bestFriend: {
_ref: 'grrm',
_type: 'reference',
_weak: true,
_strengthenOnPublish: {type: 'author', template: {id: 'author'}},
},
})
})

Expand All @@ -105,13 +120,25 @@ describe('resolveInitialValue', () => {
schema,
{
...example,
value: {bestFriend: {_ref: 'grrm', _type: 'crossDatasetReference', _dataset: 'bffs'}},
value: {
bestFriend: {
_ref: 'grrm',
_type: 'crossDatasetReference',
_dataset: 'bffs',
_projectId: 'beep',
},
},
},
{},
mockConfigContext,
),
).resolves.toMatchObject({
bestFriend: {_ref: 'grrm', _type: 'crossDatasetReference', _dataset: 'bffs'},
bestFriend: {
_ref: 'grrm',
_type: 'crossDatasetReference',
_dataset: 'bffs',
_projectId: 'beep',
},
})
})

Expand Down
10 changes: 9 additions & 1 deletion packages/sanity/src/core/templates/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import {toString as pathToString} from '@sanity/util/paths'
import {type Template, type TemplateParameter} from './types'
import {isRecord} from './util/isRecord'

const ALLOWED_REF_PROPS = ['_dataset', '_key', '_ref', '_type', '_weak']
const ALLOWED_REF_PROPS = [
'_dataset',
'_projectId',
'_strengthenOnPublish',
'_key',
'_ref',
'_type',
'_weak',
]
const REQUIRED_TEMPLATE_PROPS: (keyof Template)[] = ['id', 'title', 'schemaType', 'value']

function templateId(template: Template, i: number) {
Expand Down

0 comments on commit 5adca88

Please sign in to comment.