diff --git a/packages/sanity/src/core/create/__tests__/createUtils.test.ts b/packages/sanity/src/core/create/__tests__/createUtils.test.ts new file mode 100644 index 00000000000..4a333c826d2 --- /dev/null +++ b/packages/sanity/src/core/create/__tests__/createUtils.test.ts @@ -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[], docName: string) { + return createSchema({name: 'test', types: docDefs}).get(docName) as ObjectSchemaType +} diff --git a/packages/sanity/src/core/create/createUtils.ts b/packages/sanity/src/core/create/createUtils.ts index b58f5dde28b..aa488eb7d0e 100644 --- a/packages/sanity/src/core/create/createUtils.ts +++ b/packages/sanity/src/core/create/createUtils.ts @@ -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 }