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
  • Loading branch information
snorrees committed Nov 27, 2024
1 parent cd28b53 commit 5a7d8db
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
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 5a7d8db

Please sign in to comment.