Skip to content

Commit

Permalink
feat(validation): validate reserved key names (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
olamothe authored May 10, 2021
1 parent c6014af commit ac678ad
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/documentBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
19 changes: 18 additions & 1 deletion src/documentBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit ac678ad

Please sign in to comment.