Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: key validation #7

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/spacecat-shared-dynamo/src/utils/guards.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ const guardTableName = (tableName) => {
};

/**
* Validates that the provided key is an object and contains a partitionKey.
* Validates that the provided key is an object and contains at least one property.
*
* @param {object} key - The key object to validate.
* @throws {Error} If the key is not an object or does not contain a partitionKey.
* @throws {Error} If the key is not an object or does not contain at least one property.
*/
const guardKey = (key) => {
if (!isObject(key) || !key.partitionKey) {
throw new Error('Key must be an object with a partitionKey.');
if (!isObject(key) || Object.keys(key).length === 0) {
throw new Error('Key must be a non-empty object.');
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('getItem', () => {
await dynamoDbClient.getItem('TestTable', null);
expect.fail('getItem did not throw with invalid key');
} catch (error) {
expect(error.message).to.equal('Key must be an object with a partitionKey.');
expect(error.message).to.equal('Key must be a non-empty object.');
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('removeItem', () => {
await dynamoDbClient.removeItem('TestTable', null);
expect.fail('removeItem did not throw with invalid key');
} catch (error) {
expect(error.message).to.equal('Key must be an object with a partitionKey.');
expect(error.message).to.equal('Key must be a non-empty object.');
}
});

Expand Down
28 changes: 15 additions & 13 deletions packages/spacecat-shared-dynamo/test/utils/guards.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,44 @@ import { expect } from 'chai';
import { guardKey, guardQueryParameters, guardTableName } from '../../src/utils/guards.js';

describe('Query Parameter Guards', () => {
// Test guardTableName
describe('guardTableName', () => {
it('should throw an error if tableName is empty', () => {
it('throws an error if tableName is empty', () => {
expect(() => guardTableName('')).to.throw('Table name is required.');
});

it('should not throw an error for valid tableName', () => {
it('does not throw an error for valid tableName', () => {
expect(() => guardTableName('validTableName')).to.not.throw();
});
});

// Test guardKey
describe('guardKey', () => {
it('should throw an error if key is not an object', () => {
expect(() => guardKey('notAnObject')).to.throw('Key must be an object with a partitionKey.');
it('throws an error if key is not an object', () => {
expect(() => guardKey('notAnObject')).to.throw('Key must be a non-empty object.');
});

it('should throw an error if key does not have partitionKey', () => {
expect(() => guardKey({})).to.throw('Key must be an object with a partitionKey.');
it('throws an error if key is an empty object', () => {
expect(() => guardKey({})).to.throw('Key must be a non-empty object.');
});

it('should not throw an error for a valid key', () => {
expect(() => guardKey({ partitionKey: 'value' })).to.not.throw();
it('does not throw an error for a valid key with one property', () => {
expect(() => guardKey({ somePartitionKeyField: 'value' })).to.not.throw();
});

it('does not throw an error for a valid key with two properties', () => {
expect(() => guardKey({ somePartitionKeyField: 'value', someOptionalRangeKey: 'anotherValue' })).to.not.throw();
});
});

describe('guardQueryParameters', () => {
it('should throw an error if params is not an object', () => {
it('throws an error if params is not an object', () => {
expect(() => guardQueryParameters('notAnObject')).to.throw('Query parameters must be an object.');
});

it('should throw an error if any required parameter is missing', () => {
it('throws an error if any required parameter is missing', () => {
expect(() => guardQueryParameters({ TableName: 'table' })).to.throw('Query parameters is missing required parameter: KeyConditionExpression');
});

it('should not throw an error for valid params', () => {
it('does not throw an error for valid params', () => {
const validParams = {
TableName: 'table',
KeyConditionExpression: 'expression',
Expand Down
Loading