-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from Q42/feature/validate-parent-language
adds check for language
- Loading branch information
Showing
10 changed files
with
69 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,23 @@ | ||
import { getLanguageFieldName } from '../helpers/config'; | ||
import { PageTreeConfig } from '../types'; | ||
|
||
export const getRawPageMetadataQuery = (config: PageTreeConfig) => `*[_type in [${Object.values(config.pageSchemaTypes) | ||
export const getAllRawPageMetadataQuery = (config: PageTreeConfig) => `*[_type in [${Object.values( | ||
config.pageSchemaTypes, | ||
) | ||
.map(key => `"${key}"`) | ||
.join(', ')}]]{ | ||
${rawPageMetadataFragment(config)} | ||
}`; | ||
|
||
export const getRawPageMetadataQuery = (documentId: string, config: PageTreeConfig) => `*[_id == "${documentId}"]{ | ||
${rawPageMetadataFragment(config)} | ||
}`; | ||
|
||
export const rawPageMetadataFragment = (config: PageTreeConfig) => ` | ||
_id, | ||
_type, | ||
_updatedAt, | ||
parent, | ||
slug, | ||
title, | ||
${getLanguageFieldName(config) ?? ''} | ||
}`; | ||
|
||
export const getDocumentTypeQuery = (documentId: string) => `*[_id == "${documentId}"]{ | ||
_type | ||
}`; | ||
${getLanguageFieldName(config) ?? ''}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,60 @@ | ||
import { Reference, ValidationContext } from 'sanity'; | ||
|
||
import { getDocumentTypeQuery } from '../queries'; | ||
import { PageTreeConfig, RawPageMetadata, SanityRef } from '../types'; | ||
import { getLanguageFieldName } from '../helpers/config'; | ||
import { getRawPageMetadataQuery } from '../queries'; | ||
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 (selectedParent: Reference | undefined, context: ValidationContext) => { | ||
const allowedParents = config.allowedParents?.[ownType]; | ||
|
||
if (allowedParents === undefined) { | ||
return true; | ||
} | ||
async (selectedParentRef: Reference | undefined, context: ValidationContext) => { | ||
const client = context.getClient({ apiVersion: config.apiVersion }); | ||
|
||
const parentRef = context.document?.parent as SanityRef | undefined; | ||
if (!parentRef) { | ||
if (!selectedParentRef) { | ||
return true; | ||
} | ||
|
||
const parentId = parentRef._ref; | ||
const parentId = selectedParentRef._ref; | ||
const selectedParent = (await client.fetch<RawPageMetadata[]>(getRawPageMetadataQuery(parentId, config)))[0]; | ||
|
||
if (parentId === undefined) { | ||
return true; | ||
const allowedParentValidation = allowedParentValidator(selectedParent, config, ownType); | ||
if (allowedParentValidation !== true) { | ||
return allowedParentValidation; | ||
} | ||
|
||
const client = context.getClient({ apiVersion: config.apiVersion }); | ||
const selectedParentType = (await client.fetch<Pick<RawPageMetadata, '_type'>[]>(getDocumentTypeQuery(parentId)))[0] | ||
?._type; | ||
return parentLanguageValidator(selectedParent, config, context); | ||
}; | ||
|
||
if (!selectedParentType) { | ||
return 'Unable to check the type of the selected parent.'; | ||
} | ||
const allowedParentValidator = (selectedParent: RawPageMetadata, config: PageTreeConfig, ownType: string) => { | ||
const allowedParents = config.allowedParents?.[ownType]; | ||
|
||
if (!allowedParents.includes(selectedParentType)) { | ||
return `The parent of type "${selectedParentType}" is not allowed for this type of 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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters