Skip to content

Commit

Permalink
fix: Start in Create type exclusion now recursively checks for sanity…
Browse files Browse the repository at this point in the history
…Create.exclude options (#7890)
  • Loading branch information
snorrees authored Nov 27, 2024
1 parent db5495b commit af03b86
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
14 changes: 14 additions & 0 deletions dev/test-create-integration-studio/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ export const seoGroup: FieldGroupDefinition = {
}

export const schemaTypes = [
defineType({
type: 'document',
name: 'sanity-create-excluded',
fields: [
defineField({
name: 'title',
title: 'New documents of this type should not have a Start in Create button',
type: 'string',
}),
],
options: {
sanityCreate: {exclude: true},
},
}),
defineType({
title: 'Documentation Article',
name: 'create-test-article',
Expand Down
76 changes: 76 additions & 0 deletions packages/sanity/src/core/create/__tests__/createUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {defineType, type ObjectSchemaType} from '@sanity/types'
import {describe, expect, it} from 'vitest'

import {createSchema} from '../../schema'
import {isSanityCreateExcludedType} from '../createUtils'

const basicDoc = defineType({
type: 'document',
name: 'test',
fields: [{type: 'string', name: 'title'}],
})

describe('createUtils', () => {
describe('isSanityCreateExcludedType', () => {
it(`should include type without options`, async () => {
const documentType = getDocumentType([basicDoc], basicDoc.name)
expect(isSanityCreateExcludedType(documentType)).toEqual(false)
})

it(`should exclude type via direct options`, async () => {
const documentType = getDocumentType(
[
defineType({
...basicDoc,
options: {sanityCreate: {exclude: true}},
}),
],
basicDoc.name,
)
expect(isSanityCreateExcludedType(documentType)).toEqual(true)
})

it(`should exclude type via parent options`, async () => {
const documentType = getDocumentType(
[
{
type: 'document',
name: 'parentDoc',
fields: [{type: 'string', name: 'title'}],
options: {sanityCreate: {exclude: true}},
},
{
type: 'parentDoc',
name: 'test',
},
],
basicDoc.name,
)
expect(isSanityCreateExcludedType(documentType)).toEqual(true)
})

it(`should include type when child type overrides parent options`, async () => {
const documentType = getDocumentType(
[
{
type: 'document',
name: 'parentDoc',
fields: [{type: 'string', name: 'title'}],
options: {sanityCreate: {exclude: true}},
},
{
type: 'parentDoc',
name: 'test',
options: {sanityCreate: {exclude: false}},
},
],
basicDoc.name,
)
expect(isSanityCreateExcludedType(documentType)).toEqual(false)
})
})
})

function getDocumentType(docDefs: ReturnType<typeof defineType>[], docName: string) {
return createSchema({name: 'test', types: docDefs}).get(docName) as ObjectSchemaType
}
9 changes: 8 additions & 1 deletion packages/sanity/src/core/create/createUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,12 @@ export function isSanityCreateLinkedDocument(doc: SanityDocumentLike | undefined
* @internal
*/
export function isSanityCreateExcludedType(schemaType: SchemaType): boolean {
return !!(schemaType?.type?.options as BaseSchemaTypeOptions | undefined)?.sanityCreate?.exclude
const options = schemaType.options as BaseSchemaTypeOptions | undefined
if (typeof options?.sanityCreate?.exclude === 'boolean') {
return options?.sanityCreate?.exclude
}
if (schemaType?.type) {
return isSanityCreateExcludedType(schemaType?.type)
}
return false
}

0 comments on commit af03b86

Please sign in to comment.