diff --git a/examples/studio-i18n/page-tree-config.ts b/examples/studio-i18n/page-tree-config.ts index 7050a27..f6a6803 100644 --- a/examples/studio-i18n/page-tree-config.ts +++ b/examples/studio-i18n/page-tree-config.ts @@ -7,5 +7,6 @@ export const pageTreeConfig: PageTreeConfig = { titleFieldName: 'title', documentInternationalization: { supportedLanguages: ['nl', 'en', 'fr'], + documentLanguageShouldMatchParent: true, } }; diff --git a/src/schema/definePageType.ts b/src/schema/definePageType.ts index a0e2b9d..fa4ba28 100644 --- a/src/schema/definePageType.ts +++ b/src/schema/definePageType.ts @@ -4,7 +4,7 @@ import { defineField, defineType, DocumentDefinition, SlugOptions } from 'sanity import { PageTreeField } from '../components/PageTreeField'; import { SlugField } from '../components/SlugField'; import { PageTreeConfig } from '../types'; -import { allowedParentValidator } from '../validators/parent-validator'; +import { parentValidator } from '../validators/parent-validator'; import { slugValidator } from '../validators/slug-validator'; type Options = { @@ -67,7 +67,7 @@ const basePageFields = (config: PageTreeConfig, options: Options, ownType: Docum title: 'Parent page', type: 'reference', to: getPossibleParentsFromConfig(config, ownType).map(type => ({ type })), - validation: Rule => Rule.required().custom(allowedParentValidator(config, ownType.name)), + validation: Rule => Rule.required().custom(parentValidator(config, ownType.name)), group: options.fieldsGroupName, components: { field: props => PageTreeField({ ...props, config, mode: 'select-parent' }), diff --git a/src/validators/parent-validator.ts b/src/validators/parent-validator.ts index b273dd2..3df8174 100644 --- a/src/validators/parent-validator.ts +++ b/src/validators/parent-validator.ts @@ -2,47 +2,59 @@ import { Reference, ValidationContext } from 'sanity'; import { getLanguageFieldName } from '../helpers/config'; import { getRawPageMetadataQuery } from '../queries'; -import { PageTreeConfig, RawPageMetadata, SanityRef } from '../types'; +import { PageTreeConfig, RawPageMetadata } from '../types'; /** * Validates that the slug is unique within the parent page and therefore that entire the path is unique. */ -export const allowedParentValidator = +export const parentValidator = (config: PageTreeConfig, ownType: string) => async (selectedParentRef: Reference | undefined, context: ValidationContext) => { - const allowedParents = config.allowedParents?.[ownType]; - - const parentRef = context.document?.parent as SanityRef | undefined; - if (!parentRef) { - return true; - } - - const parentId = parentRef._ref; + const client = context.getClient({ apiVersion: config.apiVersion }); - if (parentId === undefined) { + if (!selectedParentRef) { return true; } - const client = context.getClient({ apiVersion: config.apiVersion }); - const selectedParent = (await client.fetch(getRawPageMetadataQuery(parentId, config)))?.[0]; + const parentId = selectedParentRef._ref; + const selectedParent = (await client.fetch(getRawPageMetadataQuery(parentId, config)))[0]; - if (!selectedParent._type) { - return 'Unable to check the type of the selected parent.'; + const allowedParentValidation = allowedParentValidator(selectedParent, config, ownType); + if (allowedParentValidation !== true) { + return allowedParentValidation; } - if (allowedParents && !allowedParents.includes(selectedParent._type)) { - return `The parent of type "${selectedParent._type}" is not allowed for this type of document.`; - } + return parentLanguageValidator(selectedParent, config, context); + }; - if (config.documentInternationalization?.documentLanguageShouldMatchParent) { - const languageFieldName = getLanguageFieldName(config); - const language = context.document?.[languageFieldName]; - const parentLanguage = selectedParent?.[languageFieldName]; +const allowedParentValidator = (selectedParent: RawPageMetadata, config: PageTreeConfig, ownType: string) => { + const allowedParents = config.allowedParents?.[ownType]; - if (language !== parentLanguage) { - return 'The language of the parent must match the language of the document.'; - } + if (allowedParents === undefined) { + return true; + } + + if (!allowedParents.includes(selectedParent._type)) { + return `The parent of type "${selectedParent._type}" is not allowed for this type of document.`; + } + + return true; +}; + +const parentLanguageValidator = ( + selectedParent: RawPageMetadata, + config: PageTreeConfig, + context: ValidationContext, +) => { + if (config.documentInternationalization?.documentLanguageShouldMatchParent) { + const languageFieldName = getLanguageFieldName(config); + const language = context.document?.[languageFieldName]; + const parentLanguage = selectedParent?.[languageFieldName]; + + if (language !== parentLanguage) { + return 'The language of the parent must match the language of the document.'; } + } - return true; - }; + return true; +};