Empty String Validation Exception - DynamoDB

2020 May 18 Update:

DynamoDB now supports empty string values for non-key attributes: https://aws.amazon.com/about-aws/whats-new/2020/05/amazon-dynamodb-now-supports-empty-values-for-non-key-string-and-binary-attributes-in-dynamodb-tables/

You should no longer see this Exception when writing to DynamoDB.


Until now, lack of support for empty strings in DynamoDB's DocumentClient is still a limitation. However, you should know that since July 2017, AWS SDK v2.7.16 added a constructor option (called convertEmptyValues) for document client to convert empty strings, sets, and binary strings to a DynamoDB NULL typed field.

If this client-side workaround works out for you, you'll need to set to true if you would like the document client to convert empty values (0-length strings, binary buffers, and sets) to be converted to NULL types when persisting to DynamoDB.

Something like this:

const AWS = require("aws-sdk");

const docClient = new AWS.DynamoDB.DocumentClient({
    convertEmptyValues: true
});

Nowhere in your code are you assigning the values of name, age, etc. into your params object. Are you still not showing all the code? Disregarding that for a minute and looking at your validation loop, you are just throwing away the "N/A" value. You need to modify the actual params object you are using to insert records. Change the validation loop to this:

    //Validation loops to make sure the items are defined
    for (var key in params.Item) {
        for (var items in params.Item[key]) {
            var value = params.Item[key][items];
            if (value === undefined || value === "") {
                params.Item[key][items] = "N/A";
        }
    }

There's probably an easier way to do this as you are taking the values from data and copying that to the params object, but you don't seem to be showing that code.