Repository: jeremydaly/dynamodb-toolbox (ts)

Issue:
Bug on Item.get, where unneeded properties are required
Today I upgraded from v0.9.2 to 0.9.3 and I noticed my test suite failed. After some debugging I found out that on every Item.get command all items that have the `required` and `dependsOn` attribute AND are missing on my Item.get command, trigger an error.

Example:
I have an Item called `Meeting` with a PK and SK, as well as some other attributes. Two of them are:
```
    status: { type: "string", map: "F12", required: true },
    statusNumber: { type: "number", map: "F16", required: true, onUpdate: true, dependsOn: "status", default: ({status}) => processStatus(status) },
```

To retrieve a record of this type all I need is a PK and SK, nothing more. However, all my `Meeting.get` calls fail because I have not provided a status (which is not needed at all in this case). I understand that this check is good for updates or creates, but is not needed for Scan, Query or Get commands.

This issue is introduced in v0.9.3.


Pull request:
fix(Entity/get): don't fail when missing non partition required properties
closes #734 

Patch:
diff --git a/src/classes/Entity/Entity.ts b/src/classes/Entity/Entity.ts
index ef7c7d00..3c08a0b6 100644
--- a/src/classes/Entity/Entity.ts
+++ b/src/classes/Entity/Entity.ts
@@ -415,6 +415,7 @@ class Entity<Name extends string = string,
       linked,
       Object.assign({}, defaults, item),
       true,
+      false
     )
 
     const {
diff --git a/src/lib/normalizeData.ts b/src/lib/normalizeData.ts
index 6ad36b97..7f50f46a 100644
--- a/src/lib/normalizeData.ts
+++ b/src/lib/normalizeData.ts
@@ -13,7 +13,8 @@ export default () => (
   schema: any,
   linked: any,
   data: any,
-  filter = false
+  filter = false,
+  throwOnMissingNonPartitionRequiredProperties = true
 ) => {
   // Intialize validate type
   const validateType = validateTypes()
@@ -84,7 +85,7 @@ export default () => (
         const map = dependsOn(defaultMap, attr)
         defaultMap = map
       } catch (e) {
-        if (e instanceof DependsOnUndefined && (schema[attr].partitionKey || schema[attr].sortKey || schema[attr].required)) {
+        if (throwOnMissingNonPartitionRequiredProperties && e instanceof DependsOnUndefined && (schema[attr].partitionKey || schema[attr].sortKey || schema[attr].required)) {
           throw new Error(`Required field '${attr}' depends on attribute(s), one or more of which can't be resolved (${schema[attr].dependsOn})`)
         }
         delete defaultMap[attr]


Test patch:
diff --git a/src/__tests__/entity.get.unit.test.ts b/src/__tests__/entity.get.unit.test.ts
index 49906921..aa2e7843 100644
--- a/src/__tests__/entity.get.unit.test.ts
+++ b/src/__tests__/entity.get.unit.test.ts
@@ -37,6 +37,19 @@ const TestEntity2 = new Entity({
   table: TestTable2
 } as const)
 
+// https://github.com/jeremydaly/dynamodb-toolbox/issues/734
+const TestEntityWithRequiredAndMappedProperties = new Entity({
+  name: 'TestEntityWithRequiredAndMappedProperties',
+  autoExecute: false,
+  attributes: {
+    pk: { type: 'string', partitionKey: true },
+    sk: { type: 'string', sortKey: true },
+    status: { type: 'string', map: 'F12', required: true },
+    statusNumber: { type: 'number', map: 'F16', required: true, onUpdate: true, dependsOn: 'status', default: ({ status }: any) => status.length},
+  },
+  table: TestTable
+} as const)
+
 describe('get', () => {
   it('gets the key from inputs (sync)', async () => {
     const { TableName, Key } = TestEntity.getParams({ email: 'test-pk', sort: 'test-sk' })
@@ -86,6 +99,12 @@ describe('get', () => {
     expect(Key).toEqual({ pk: '1', sk: 'true' })
   })
 
+  it('should successfully run when missing non partition key properties', async () => {
+    const { TableName, Key } = TestEntityWithRequiredAndMappedProperties.getParams({ pk: 'test-pk', sk: 'test-sk' })
+    expect(TableName).toBe('test-table')
+    expect(Key).toEqual({ pk: 'test-pk', sk: 'test-sk' })
+  })
+
   it('fails with undefined input (sync)', () => {
     // @ts-expect-error
     expect(() => TestEntity.getParams()).toThrow(`'pk' or 'email' is required`)