Skip to content

Commit 9fc34a2

Browse files
authoredMar 4, 2024
fix(core): allow _dataset for cross-dataset references in templates (#5889)
1 parent c9a1dd6 commit 9fc34a2

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed
 

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

+57
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,63 @@ describe('resolveInitialValue', () => {
5858
})
5959
})
6060

61+
test('throws on unknown prop in reference', () => {
62+
expect(
63+
resolveInitialValue(
64+
schema,
65+
{...example, value: {bestFriend: {_ref: 'grrm', name: 'GRRM'}}},
66+
{},
67+
mockConfigContext,
68+
),
69+
).rejects.toMatchObject({
70+
message:
71+
'Template "author" initial value: Disallowed property found in reference: "name" at path "bestFriend"',
72+
})
73+
})
74+
75+
test('throws on unknown props in reference', () => {
76+
expect(
77+
resolveInitialValue(
78+
schema,
79+
{...example, value: {bestFriend: {_ref: 'grrm', name: 'GRRM', age: 72}}},
80+
{},
81+
mockConfigContext,
82+
),
83+
).rejects.toMatchObject({
84+
message:
85+
'Template "author" initial value: Disallowed properties found in reference: "name", "age" at path "bestFriend"',
86+
})
87+
})
88+
89+
test('allows setting known reference properties', () => {
90+
expect(
91+
resolveInitialValue(
92+
schema,
93+
{...example, value: {bestFriend: {_ref: 'grrm', _type: 'reference', _weak: true}}},
94+
{},
95+
mockConfigContext,
96+
),
97+
).resolves.toMatchObject({
98+
bestFriend: {_ref: 'grrm', _type: 'reference', _weak: true},
99+
})
100+
})
101+
102+
test('allows setting _dataset on cross-dataset references', () => {
103+
expect(
104+
resolveInitialValue(
105+
schema,
106+
{
107+
...example,
108+
value: {bestFriend: {_ref: 'grrm', _type: 'crossDatasetReference', _dataset: 'bffs'}},
109+
},
110+
{},
111+
mockConfigContext,
112+
),
113+
).resolves.toMatchObject({
114+
bestFriend: {_ref: 'grrm', _type: 'crossDatasetReference', _dataset: 'bffs'},
115+
})
116+
})
117+
61118
test('should call sync value resolvers', () => {
62119
expect(
63120
resolveInitialValue(schema, {...example, value: () => example.value}, {}, mockConfigContext),

‎packages/sanity/src/core/templates/__tests__/schema.ts

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export const schema = SchemaBuilder.compile({
2020
name: 'role',
2121
type: 'string',
2222
},
23+
{
24+
name: 'bestFriend',
25+
type: 'reference',
26+
to: [{type: 'author'}],
27+
},
2328
],
2429
initialValue: () => ({
2530
role: 'Developer',

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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 = ['_key', '_ref', '_weak', '_type']
8+
const ALLOWED_REF_PROPS = ['_dataset', '_key', '_ref', '_type', '_weak']
99
const REQUIRED_TEMPLATE_PROPS: (keyof Template)[] = ['id', 'title', 'schemaType', 'value']
1010

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

0 commit comments

Comments
 (0)
Please sign in to comment.