dynamodb icon indicating copy to clipboard operation
dynamodb copied to clipboard

Support Serializing Joi .array().items() Schemas

Open andrewmunsell opened this issue 7 years ago • 1 comments

This module is pinned to an old (5.x.x) version of Joi without support for validated arrays.

Because this module relies on the Joi schema in order to determine how to serialize a value, values in nested arrays are not serialized properly.

This fixes the issue by properly storing the DynamoDB data type for schema-validated Joi arrays, and also ensures that the array items are serialized recursively.

For example, with the current version of this module and the schema defined with the latest version of Joi (10.x.x), the nested date "itemCreatedAt" will not be serialized:

var Items = dynamo.define('Items', {
    schema: {
        items: Joi.array().items({ itemCreatedAt: Joi.date(), type: Joi.string() })
    }
});

new Items({
    items: [
        { itemCreatedAt: new Date(), type: 'red' }
    ]
})
.save();

// Results in an object in DynamoDB:
// { items: [ { "M": { "type": "red" } ] }

Notice in the example above, the date is not serialized and therefore is not included in the DynamoDB record.

I have a commit that fixes the issue (https://github.com/andrewmunsell/dynamodb/commit/d0543ae49eff4662c8bee4dc774d6cbc5a69437e), but the problem is that this module is on a version of Joi so old that it can't just be upgraded and I can't add the proper tests. It's worth discussing how to best address the issue and potentially just upgrade the version of Joi, but this has some ramifications to it for the clients of this module.

I can submit a pull request after we can figure out how best to actually implement support for the validated arrays in newer versions of Joi without introducing backwards compatibility problems.

andrewmunsell avatar Apr 22 '17 20:04 andrewmunsell

Joi is at version 10 now, can we make a pull request with your changes?

kicktheken avatar May 09 '18 05:05 kicktheken