Tanmay
Tanmay

Reputation: 404

Invalid attribute value type: ValidationException on DynamoDB Put

I am trying to do a put item on a Dynamo Table using the nodejs sdk. I tried using the same document and a few other variations but nothing seems to work. Everytime I receive the same error:

"message":"Invalid attribute value type"
"code":"ValidationException"
"time":"2016-10-11T06:32:26.361Z"
"statusCode":400
"retryable":false

The following is the relevant code snippet:

var params = {
    TableName: "MY_Table_Name",
    Item: { 
        "stringAtt": "stringValue",
        "boolAtt": true,
        "numAtt": 123,
    },
};
docClient.put(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response
});

My table's indexes are as follows:

Primary: Partition Key: stringAtt, Sort Key: boolAtt
GSI: Partition Key: boolAtt, Sort Key: numAtt

I am not sure if it's my query or the index structure that is wrong.

Upvotes: 3

Views: 16360

Answers (2)

primegxy
primegxy

Reputation: 1858

You can simply use Binary:

You can set the boolean attribute type to binary B and define these values as constant.


export const BinaryBoolean = {
  True: new Uint8Array([1]),
  False: new Uint8Array([0]),
} as const;

export const boolToBinary = (value: boolean): Uint8Array => value
  ? BinaryBoolean.True
  : BinaryBoolean.False;

export const binaryToBool = (value: Uint8Array): boolean => value[0] === 1;

Now you can use this to convert boolean to binary and successfully put Items into DynamoDB. So far its not throwing errors while dbClientInstance.putItem().

Querying also works. It does return Uint8Array, that you can pass to binaryToBool.

Note: Tested using docker local : DockerHub/LocalDynamoDB

Upvotes: 0

notionquest
notionquest

Reputation: 39186

The BOOL data type can't be a key attribute (i.e. Partition or Sort key). The Partition or Sort key data type can be of three types (listed below). If you have created the table with Sort key of type 'B', it means that sort key is of type Binary (i.e. not Bool).

EDIT: You can't use a BOOL attribute as the partition or sort key in a GSI as well.

AttributeType: 'S | N | B'

S - the attribute is of type String

N - the attribute is of type Number

B - the attribute is of type Binary

When the table is created with key of type BOOL, the API would throw the below exception.

Unable to create table. Error JSON: {
  "message": "Member must satisfy enum value set: [B, N, S]",
  "code": "ValidationException",

Upvotes: 8

Related Questions