Skip to content

Commit

Permalink
[schema] Change slug uniqueness function name to isUnique
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Mar 1, 2018
1 parent 50bc679 commit 130c978
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
10 changes: 5 additions & 5 deletions packages/@sanity/schema/src/legacy/types/slug.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function getDocumentIds(id) {
}
}

const defaultChecker = (slug, options) => {
const defaultIsUnique = (slug, options) => {
const {document, path} = options
const {published, draft} = getDocumentIds(document._id)
const docType = document._type
Expand All @@ -31,7 +31,7 @@ const defaultChecker = (slug, options) => {
`${atPath} == $slug`
].join(' && ')

return client.fetch(`*[${constraints}][0]._id`, {docType, draft, published, slug})
return client.fetch(`!defined(*[${constraints}][0]._id)`, {docType, draft, published, slug})
}

const SLUG_CORE = {
Expand All @@ -46,9 +46,9 @@ const SLUG_CORE = {
}

const errorMessage = 'Slug is already in use'
const checkUnique = get(options, 'type.options.checkUnique', defaultChecker)
return Promise.resolve(checkUnique(value.current, options)).then(
hasDupe => (hasDupe ? errorMessage : true)
const isUnique = get(options, 'type.options.isUnique', defaultIsUnique)
return Promise.resolve(isUnique(value.current, {...options, defaultIsUnique})).then(
slugIsUnique => (slugIsUnique ? true : errorMessage)
)
})
}
Expand Down
3 changes: 2 additions & 1 deletion packages/test-studio/schemas/slugs.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export default {
options: {
source: 'title',
maxLength: 100,
checkUnique: value => /^hei/i.test(value)
isUnique: (value, options) =>
!/^hei/i.test(value) && options.defaultIsUnique(value, options)
}
},
{
Expand Down

0 comments on commit 130c978

Please sign in to comment.