diff --git a/src/documentBuilder.spec.ts b/src/documentBuilder.spec.ts index c797e4d9..3a654e9d 100644 --- a/src/documentBuilder.spec.ts +++ b/src/documentBuilder.spec.ts @@ -77,6 +77,23 @@ describe('DocumentBuilder', () => { expect(docBuilder.withPermanentId('id').marshal().permanentId).toBe('id'); }); + it('throws when adding a reserved key name metadata', () => { + const theseShouldThrow = [ + 'compressedBinaryData', + 'compressedBinaryDataFileId', + 'parentId', + 'fileExtension', + 'data', + 'permissions', + 'documentId', + 'orderingId', + ]; + + for (const shouldThrow of theseShouldThrow) { + expect(() => docBuilder.withMetadataValue(shouldThrow, 'foo')).toThrow(); + } + }); + it('should validate file extension', () => { expect(() => docBuilder.withFileExtension('nope')).toThrow(); }); diff --git a/src/documentBuilder.ts b/src/documentBuilder.ts index 15b2bc8f..a27fab2e 100644 --- a/src/documentBuilder.ts +++ b/src/documentBuilder.ts @@ -132,7 +132,24 @@ export class DocumentBuilder { * @returns */ public withMetadataValue(key: string, value: MetadataValue) { - // TODO: validate reserved names + const reservedKeyNames = [ + 'compressedBinaryData', + 'compressedBinaryDataFileId', + 'parentId', + 'fileExtension', + 'data', + 'permissions', + 'documentId', + 'orderingId', + ]; + if ( + reservedKeyNames.some( + (reservedKey) => reservedKey.toLowerCase() === key.toLowerCase() + ) + ) { + throw `Cannot use ${key} as a metadata key: It is a reserved key name. See https://docs.coveo.com/en/78/index-content/push-api-reference#json-document-reserved-key-names`; + } + this.doc.metadata![key] = value; return this; }