@@ -10,18 +10,11 @@ import {
10
10
} from '@sanity/types'
11
11
import { memoize } from 'lodash'
12
12
13
- import { VERSION_FOLDER } from '../../util/draftUtils'
13
+ import { DEFAULT_STUDIO_CLIENT_OPTIONS } from '../../studioClient'
14
+ import { getPublishedId } from '../../util/draftUtils'
14
15
15
16
const memoizedWarnOnArraySlug = memoize ( warnOnArraySlug )
16
17
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
-
25
18
function serializePath ( path : Path ) : string {
26
19
return path . reduce < string > ( ( target , part , i ) => {
27
20
const isIndex = typeof part === 'number'
@@ -44,7 +37,6 @@ const defaultIsUnique: SlugIsUniqueValidator = (slug, context) => {
44
37
}
45
38
46
39
const disableArrayWarning = schemaOptions ?. disableArrayWarning || false
47
- const { published, draft} = getDocumentIds ( document . _id )
48
40
const docType = document . _type
49
41
const atPath = serializePath ( path . concat ( 'current' ) )
50
42
@@ -54,21 +46,21 @@ const defaultIsUnique: SlugIsUniqueValidator = (slug, context) => {
54
46
55
47
const constraints = [
56
48
'_type == $docType' ,
57
- `!(_id in [$draft, $published])` ,
58
- `!(_id in path("${ VERSION_FOLDER } .**.${ published } "))` ,
49
+ `!sanity::versionOf($published)` ,
59
50
`${ atPath } == $slug` ,
60
51
] . join ( ' && ' )
61
52
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
+ )
72
64
}
73
65
74
66
function warnOnArraySlug ( serializedPath : string ) {
0 commit comments