Skip to content

Commit

Permalink
fix(core): allow _dataset for cross-dataset references in templates (
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored Mar 4, 2024
1 parent c9a1dd6 commit 9fc34a2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
57 changes: 57 additions & 0 deletions packages/sanity/src/core/templates/__tests__/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,63 @@ describe('resolveInitialValue', () => {
})
})

test('throws on unknown prop in reference', () => {
expect(
resolveInitialValue(
schema,
{...example, value: {bestFriend: {_ref: 'grrm', name: 'GRRM'}}},
{},
mockConfigContext,
),
).rejects.toMatchObject({
message:
'Template "author" initial value: Disallowed property found in reference: "name" at path "bestFriend"',
})
})

test('throws on unknown props in reference', () => {
expect(
resolveInitialValue(
schema,
{...example, value: {bestFriend: {_ref: 'grrm', name: 'GRRM', age: 72}}},
{},
mockConfigContext,
),
).rejects.toMatchObject({
message:
'Template "author" initial value: Disallowed properties found in reference: "name", "age" at path "bestFriend"',
})
})

test('allows setting known reference properties', () => {
expect(
resolveInitialValue(
schema,
{...example, value: {bestFriend: {_ref: 'grrm', _type: 'reference', _weak: true}}},
{},
mockConfigContext,
),
).resolves.toMatchObject({
bestFriend: {_ref: 'grrm', _type: 'reference', _weak: true},
})
})

test('allows setting _dataset on cross-dataset references', () => {
expect(
resolveInitialValue(
schema,
{
...example,
value: {bestFriend: {_ref: 'grrm', _type: 'crossDatasetReference', _dataset: 'bffs'}},
},
{},
mockConfigContext,
),
).resolves.toMatchObject({
bestFriend: {_ref: 'grrm', _type: 'crossDatasetReference', _dataset: 'bffs'},
})
})

test('should call sync value resolvers', () => {
expect(
resolveInitialValue(schema, {...example, value: () => example.value}, {}, mockConfigContext),
Expand Down
5 changes: 5 additions & 0 deletions packages/sanity/src/core/templates/__tests__/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export const schema = SchemaBuilder.compile({
name: 'role',
type: 'string',
},
{
name: 'bestFriend',
type: 'reference',
to: [{type: 'author'}],
},
],
initialValue: () => ({
role: 'Developer',
Expand Down
2 changes: 1 addition & 1 deletion packages/sanity/src/core/templates/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {toString as pathToString} from '@sanity/util/paths'
import {type Template, type TemplateParameter} from './types'
import {isRecord} from './util/isRecord'

const ALLOWED_REF_PROPS = ['_key', '_ref', '_weak', '_type']
const ALLOWED_REF_PROPS = ['_dataset', '_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 9fc34a2

Please sign in to comment.