diff --git a/src/mongoose/buildQuery.ts b/src/mongoose/buildQuery.ts index e0bface9a..b1e90bed4 100644 --- a/src/mongoose/buildQuery.ts +++ b/src/mongoose/buildQuery.ts @@ -142,7 +142,7 @@ class ParamParser { }, ]; - pathSegments.forEach((segment, i) => { + pathSegments.every((segment, i) => { const lastIncompletePath = paths.find(({ complete }) => !complete); const { path } = lastIncompletePath; @@ -152,7 +152,7 @@ class ParamParser { if (currentSchemaPathType === 'nested') { lastIncompletePath.path = currentPath; - return; + return true; } const upcomingSegment = pathSegments[i + 1]; @@ -161,25 +161,25 @@ class ParamParser { const currentSchemaTypeOptions = getSchemaTypeOptions(currentSchemaType); if (currentSchemaTypeOptions.localized) { + const upcomingLocalizedPath = `${currentPath}.${upcomingSegment}`; + const upcomingSchemaTypeWithLocale = schema.path(upcomingLocalizedPath); + + if (upcomingSchemaTypeWithLocale) { + lastIncompletePath.path = currentPath; + return true; + } + const localePath = `${currentPath}.${this.locale}`; const localizedSchemaType = schema.path(localePath); if (localizedSchemaType || operator === 'near') { lastIncompletePath.path = localePath; - return; - } - - const upcomingPathWithLocale = `${currentPath}.${this.locale}.${upcomingSegment}`; - const upcomingSchemaTypeWithLocale = schema.path(upcomingPathWithLocale); - - if (upcomingSchemaTypeWithLocale) { - lastIncompletePath.path = upcomingPathWithLocale; - return; + return true; } } lastIncompletePath.path = currentPath; - return; + return true; } const priorSchemaType = schema.path(path); @@ -197,19 +197,16 @@ class ParamParser { ...paths, ...this.getLocalizedPaths(RefModel, remainingPath, operator), ]; - return; - } - if (priorSchemaType.instance === 'Mixed' || priorSchemaType.instance === 'Array') { - lastIncompletePath.path = currentPath; + return false; } - } else { + } + + if (operator === 'near' || currentSchemaPathType === 'adhocOrUndefined') { lastIncompletePath.path = currentPath; } - if (operator === 'near') { - lastIncompletePath.path = currentPath; - } + return true; }); return paths; diff --git a/src/mongoose/buildSchema.ts b/src/mongoose/buildSchema.ts index dd6652937..e0421fed4 100644 --- a/src/mongoose/buildSchema.ts +++ b/src/mongoose/buildSchema.ts @@ -334,19 +334,9 @@ const fieldToSchemaMap: Record = { }, blocks: (field: BlockField, schema: Schema, config: SanitizedConfig, buildSchemaOptions: BuildSchemaOptions): void => { const fieldSchema = [new Schema({}, { _id: false, discriminatorKey: 'blockType' })]; - let schemaToReturn; - - if (field.localized && config.localization) { - schemaToReturn = config.localization.locales.reduce((localeSchema, locale) => ({ - ...localeSchema, - [locale]: fieldSchema, - }), {}); - } else { - schemaToReturn = fieldSchema; - } schema.add({ - [field.name]: schemaToReturn, + [field.name]: localizeSchema(field, fieldSchema, config.localization), }); field.blocks.forEach((blockItem: Block) => { diff --git a/test/fields/collections/Blocks/index.ts b/test/fields/collections/Blocks/index.ts index f0196b241..77c0fd390 100644 --- a/test/fields/collections/Blocks/index.ts +++ b/test/fields/collections/Blocks/index.ts @@ -72,7 +72,14 @@ export const blocksField: Field = { const BlockFields: CollectionConfig = { slug: 'block-fields', - fields: [blocksField], + fields: [ + blocksField, + { + ...blocksField, + name: 'localizedBlocks', + localized: true, + }, + ], }; export const blocksFieldSeedData = [ @@ -107,6 +114,7 @@ export const blocksFieldSeedData = [ export const blocksDoc = { blocks: blocksFieldSeedData, + localizedBlocks: blocksFieldSeedData, }; export default BlockFields; diff --git a/test/fields/collections/Text/index.ts b/test/fields/collections/Text/index.ts index 9d932239e..58c662d28 100644 --- a/test/fields/collections/Text/index.ts +++ b/test/fields/collections/Text/index.ts @@ -13,6 +13,11 @@ const TextFields: CollectionConfig = { type: 'text', required: true, }, + { + name: 'localizedText', + type: 'text', + localized: true, + }, { name: 'defaultFunction', type: 'text', @@ -32,6 +37,7 @@ const TextFields: CollectionConfig = { export const textDoc = { text: 'Seeded text document', + localizedText: 'Localized text', }; export default TextFields; diff --git a/test/fields/config.ts b/test/fields/config.ts index 434beef4d..78bf1281a 100644 --- a/test/fields/config.ts +++ b/test/fields/config.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/ban-ts-comment */ import path from 'path'; import fs from 'fs'; import { buildConfig } from '../buildConfig'; @@ -91,9 +92,10 @@ export default buildConfig({ const blocksDocWithRichText = { ...blocksDoc }; - // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore blocksDocWithRichText.blocks[0].richText = richTextDocWithRelationship.richText; + // @ts-ignore + blocksDocWithRichText.localizedBlocks[0].richText = richTextDocWithRelationship.richText; await payload.create({ collection: 'block-fields', data: blocksDocWithRichText }); }, diff --git a/test/fields/int.spec.ts b/test/fields/int.spec.ts index a36d8b826..8f4e3bea5 100644 --- a/test/fields/int.spec.ts +++ b/test/fields/int.spec.ts @@ -362,6 +362,54 @@ describe('Fields', () => { expect(blockFieldsFail.docs).toHaveLength(0); }); + + it('should query based on richtext data within a localized block, specifying locale', async () => { + const blockFieldsSuccess = await payload.find({ + collection: 'block-fields', + where: { + 'localizedBlocks.en.richText.children.text': { + like: 'fun', + }, + }, + }); + + expect(blockFieldsSuccess.docs).toHaveLength(1); + + const blockFieldsFail = await payload.find({ + collection: 'block-fields', + where: { + 'localizedBlocks.en.richText.children.text': { + like: 'funny', + }, + }, + }); + + expect(blockFieldsFail.docs).toHaveLength(0); + }); + + it('should query based on richtext data within a localized block, without specifying locale', async () => { + const blockFieldsSuccess = await payload.find({ + collection: 'block-fields', + where: { + 'localizedBlocks.richText.children.text': { + like: 'fun', + }, + }, + }); + + expect(blockFieldsSuccess.docs).toHaveLength(1); + + const blockFieldsFail = await payload.find({ + collection: 'block-fields', + where: { + 'localizedBlocks.richText.children.text': { + like: 'funny', + }, + }, + }); + + expect(blockFieldsFail.docs).toHaveLength(0); + }); }); describe('richText', () => {