Skip to content

Commit 05524ff

Browse files
authoredMar 17, 2025··
fix(core): slugs validation (#8951)
1 parent 389bc50 commit 05524ff

File tree

2 files changed

+16
-26
lines changed

2 files changed

+16
-26
lines changed
 

‎packages/sanity/src/core/validation/validators/slugValidator.ts

+14-22
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,11 @@ import {
1010
} from '@sanity/types'
1111
import {memoize} from 'lodash'
1212

13-
import {VERSION_FOLDER} from '../../util/draftUtils'
13+
import {DEFAULT_STUDIO_CLIENT_OPTIONS} from '../../studioClient'
14+
import {getPublishedId} from '../../util/draftUtils'
1415

1516
const memoizedWarnOnArraySlug = memoize(warnOnArraySlug)
1617

17-
function getDocumentIds(id: string) {
18-
const isDraft = id.indexOf('drafts.') === 0
19-
return {
20-
published: isDraft ? id.slice('drafts.'.length) : id,
21-
draft: isDraft ? id : `drafts.${id}`,
22-
}
23-
}
24-
2518
function serializePath(path: Path): string {
2619
return path.reduce<string>((target, part, i) => {
2720
const isIndex = typeof part === 'number'
@@ -44,7 +37,6 @@ const defaultIsUnique: SlugIsUniqueValidator = (slug, context) => {
4437
}
4538

4639
const disableArrayWarning = schemaOptions?.disableArrayWarning || false
47-
const {published, draft} = getDocumentIds(document._id)
4840
const docType = document._type
4941
const atPath = serializePath(path.concat('current'))
5042

@@ -54,21 +46,21 @@ const defaultIsUnique: SlugIsUniqueValidator = (slug, context) => {
5446

5547
const constraints = [
5648
'_type == $docType',
57-
`!(_id in [$draft, $published])`,
58-
`!(_id in path("${VERSION_FOLDER}.**.${published}"))`,
49+
`!sanity::versionOf($published)`,
5950
`${atPath} == $slug`,
6051
].join(' && ')
6152

62-
return getClient({apiVersion: '2023-11-13'}).fetch<boolean>(
63-
`!defined(*[${constraints}][0]._id)`,
64-
{
65-
docType,
66-
draft,
67-
published,
68-
slug,
69-
},
70-
{tag: 'validation.slug-is-unique'},
71-
)
53+
return getClient({apiVersion: DEFAULT_STUDIO_CLIENT_OPTIONS.apiVersion})
54+
.withConfig({perspective: 'raw'})
55+
.fetch<boolean>(
56+
`!defined(*[${constraints}][0]._id)`,
57+
{
58+
docType,
59+
published: getPublishedId(document._id),
60+
slug,
61+
},
62+
{tag: 'validation.slug-is-unique'},
63+
)
7264
}
7365

7466
function warnOnArraySlug(serializedPath: string) {

‎packages/sanity/test/validation/infer.test.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,9 @@ describe('schema validation inference', () => {
146146

147147
expect(client.fetch).toHaveBeenCalledTimes(1)
148148
expect(client.fetch.mock.calls[0]).toEqual([
149-
'!defined(*[_type == $docType && !(_id in [$draft, $published]) && !(_id in path("versions.**.mockDocument")) && slugField.current == $slug][0]._id)',
149+
'!defined(*[_type == $docType && !sanity::versionOf($published) && slugField.current == $slug][0]._id)',
150150
{
151151
docType: 'documentWithSlug',
152-
draft: 'drafts.mockDocument',
153152
published: 'mockDocument',
154153
slug: 'example-value',
155154
},
@@ -185,10 +184,9 @@ describe('schema validation inference', () => {
185184

186185
expect(client.fetch).toHaveBeenCalledTimes(1)
187186
expect(client.fetch.mock.calls[0]).toEqual([
188-
'!defined(*[_type == $docType && !(_id in [$draft, $published]) && !(_id in path("versions.**.mockDocument")) && slugField.current == $slug][0]._id)',
187+
'!defined(*[_type == $docType && !sanity::versionOf($published) && slugField.current == $slug][0]._id)',
189188
{
190189
docType: 'documentWithSlug',
191-
draft: 'drafts.mockDocument',
192190
published: 'mockDocument',
193191
slug: 'example-value',
194192
},

0 commit comments

Comments
 (0)
Please sign in to comment.