Skip to content

Commit

Permalink
[schema] Fix combining slug validation rules with custom ones
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Mar 7, 2018
1 parent 8923552 commit 2b13b12
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
46 changes: 30 additions & 16 deletions packages/@sanity/schema/src/legacy/types/slug.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {pick, get} from 'lodash'
import client from 'part:@sanity/base/client'
import {lazyGetter} from './utils'

const OVERRIDABLE_FIELDS = [
'jsonType',
Expand Down Expand Up @@ -44,27 +45,29 @@ const defaultIsUnique = (slug, options) => {
return client.fetch(`!defined(*[${constraints}][0]._id)`, {docType, draft, published, slug})
}

const slugValidator = Rule =>
Rule.custom((value, options) => {
if (!value) {
return true
}

if (!value.current) {
return 'Slug must have a value'
}

const errorMessage = 'Slug is already in use'
const isUnique = get(options, 'type.options.isUnique', defaultIsUnique)
return Promise.resolve(isUnique(value.current, {...options, defaultIsUnique})).then(
slugIsUnique => (slugIsUnique ? true : errorMessage)
)
})

const SLUG_CORE = {
name: 'slug',
title: 'Slug',
type: null,
jsonType: 'object',
validation: Rule =>
Rule.custom((value, options) => {
if (!value) {
return true
}

if (!value.current) {
return 'Slug must have a value'
}

const errorMessage = 'Slug is already in use'
const isUnique = get(options, 'type.options.isUnique', defaultIsUnique)
return Promise.resolve(isUnique(value.current, {...options, defaultIsUnique})).then(
slugIsUnique => (slugIsUnique ? true : errorMessage)
)
})
validation: slugValidator
}

export const SlugType = {
Expand All @@ -78,6 +81,17 @@ export const SlugType = {
select: {title: 'current'}
}
})

lazyGetter(
parsed,
'validation',
() =>
subTypeDef.validation
? Rule => [subTypeDef.validation(Rule), slugValidator(Rule)]
: slugValidator,
{writable: true}
)

return subtype(parsed)

function subtype(parent) {
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/schema/src/legacy/types/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function lazyGetter(target, key, getter, config = {}) {
const val = getter()
Object.defineProperty(target, key, {
value: val,
writable: false,
writable: Boolean(config.writable),
configurable: false
})
return val
Expand Down
11 changes: 11 additions & 0 deletions packages/test-studio/schemas/slugs.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ export default {
maxLength: 96
}
},
{
name: 'requiredSlug',
type: 'slug',
title: 'Required slug',
description: 'Slug field that is required',
validation: Rule => Rule.required(),
options: {
source: 'title',
maxLength: 96
}
},
{
name: 'slugWithFunction',
type: 'slug',
Expand Down

0 comments on commit 2b13b12

Please sign in to comment.