Skip to content

Commit 5adca88

Browse files
authoredMar 11, 2024
fix(core): allow _projectId and _strengthOnPublish in templates (#5942)
* fix(core): allow `_projectId` and `_strengthOnPublish` in templates * test(core): add _projectId and _strengthOnPublish to templates test
1 parent 4e72b80 commit 5adca88

File tree

4 files changed

+92
-5
lines changed

4 files changed

+92
-5
lines changed
 

‎dev/test-studio/schema/standard/crossDatasetReference.ts

+36
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,42 @@ export default defineType({
188188
name: 'crossDatasetSubtype',
189189
type: 'crossDatasetSubtype',
190190
},
191+
{
192+
name: 'initialValueTest',
193+
type: 'crossDatasetReference',
194+
dataset: 'playground',
195+
studioUrl: ({id, type}) => {
196+
return type
197+
? `${document.location.protocol}//${document.location.host}/playground/content/${type};${id}`
198+
: null
199+
},
200+
to: [
201+
{
202+
type: 'book',
203+
icon: BookIcon,
204+
preview: {
205+
select: {
206+
title: 'title',
207+
subtitle: 'descriptionMd',
208+
media: 'coverImage',
209+
},
210+
prepare(val) {
211+
return {
212+
title: val.title,
213+
subtitle: val.subtitle,
214+
media: val.coverImage,
215+
}
216+
},
217+
},
218+
},
219+
],
220+
initialValue: () => ({
221+
_type: 'crossDatasetReference',
222+
_ref: '4203c6bd-98c2-418e-9558-3ed56ebaf1d8',
223+
_dataset: 'playground',
224+
_projectId: 'ppsg7ml5',
225+
}),
226+
},
191227
],
192228
preview: {
193229
select: {

‎dev/test-studio/schema/standard/references.ts

+16
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,22 @@ export default defineType({
235235
},
236236
],
237237
},
238+
{
239+
name: 'withInitialValue',
240+
type: 'reference',
241+
to: [{type: 'author'}],
242+
initialValue: () => ({
243+
_type: 'reference',
244+
_ref: 'f9a5f215-da97-47fe-960f-3452c85ed205',
245+
_weak: true,
246+
_strengthenOnPublish: {
247+
type: 'author',
248+
template: {
249+
id: 'author',
250+
},
251+
},
252+
}),
253+
},
238254
],
239255
preview: {
240256
select: {

‎packages/sanity/src/core/templates/__tests__/resolve.test.ts

+31-4
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,27 @@ describe('resolveInitialValue', () => {
9090
expect(
9191
resolveInitialValue(
9292
schema,
93-
{...example, value: {bestFriend: {_ref: 'grrm', _type: 'reference', _weak: true}}},
93+
{
94+
...example,
95+
value: {
96+
bestFriend: {
97+
_ref: 'grrm',
98+
_type: 'reference',
99+
_weak: true,
100+
_strengthenOnPublish: {type: 'author', template: {id: 'author'}},
101+
},
102+
},
103+
},
94104
{},
95105
mockConfigContext,
96106
),
97107
).resolves.toMatchObject({
98-
bestFriend: {_ref: 'grrm', _type: 'reference', _weak: true},
108+
bestFriend: {
109+
_ref: 'grrm',
110+
_type: 'reference',
111+
_weak: true,
112+
_strengthenOnPublish: {type: 'author', template: {id: 'author'}},
113+
},
99114
})
100115
})
101116

@@ -105,13 +120,25 @@ describe('resolveInitialValue', () => {
105120
schema,
106121
{
107122
...example,
108-
value: {bestFriend: {_ref: 'grrm', _type: 'crossDatasetReference', _dataset: 'bffs'}},
123+
value: {
124+
bestFriend: {
125+
_ref: 'grrm',
126+
_type: 'crossDatasetReference',
127+
_dataset: 'bffs',
128+
_projectId: 'beep',
129+
},
130+
},
109131
},
110132
{},
111133
mockConfigContext,
112134
),
113135
).resolves.toMatchObject({
114-
bestFriend: {_ref: 'grrm', _type: 'crossDatasetReference', _dataset: 'bffs'},
136+
bestFriend: {
137+
_ref: 'grrm',
138+
_type: 'crossDatasetReference',
139+
_dataset: 'bffs',
140+
_projectId: 'beep',
141+
},
115142
})
116143
})
117144

‎packages/sanity/src/core/templates/validate.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ import {toString as pathToString} from '@sanity/util/paths'
55
import {type Template, type TemplateParameter} from './types'
66
import {isRecord} from './util/isRecord'
77

8-
const ALLOWED_REF_PROPS = ['_dataset', '_key', '_ref', '_type', '_weak']
8+
const ALLOWED_REF_PROPS = [
9+
'_dataset',
10+
'_projectId',
11+
'_strengthenOnPublish',
12+
'_key',
13+
'_ref',
14+
'_type',
15+
'_weak',
16+
]
917
const REQUIRED_TEMPLATE_PROPS: (keyof Template)[] = ['id', 'title', 'schemaType', 'value']
1018

1119
function templateId(template: Template, i: number) {

0 commit comments

Comments
 (0)
Please sign in to comment.