nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: jeremydaly/dynamodb-toolbox
Pull request #156 (2021-04-17 17:20:40)

Fix required check not accepting 0 and false
Fix for https://github.com/jeremydaly/dynamodb-toolbox/issues/154

`required: true` is not accepting `false` and `0` values being passed into an Entity put.


Patch:
diff --git a/src/classes/Entity.ts b/src/classes/Entity.ts
index a51df7cd..ff547cff 100644
--- a/src/classes/Entity.ts
+++ b/src/classes/Entity.ts
@@ -1109,8 +1109,7 @@ class Entity<
 
 
     // Check for required fields
-    Object.keys(required).forEach(field => 
-      required[field] !== undefined && !data[field] 
+    Object.keys(required).forEach(field => required[field] !== undefined && (data[field] === undefined || data[field] === null)
         && error(`'${field}${this.schema.attributes[field].alias ? `/${this.schema.attributes[field].alias}` : ''}' is a required field`)
     ) // end required field check
 


Test patch:
diff --git a/src/__tests__/entity.put.unit.test.ts b/src/__tests__/entity.put.unit.test.ts
index c4c3934c..aec04ff6 100644
--- a/src/__tests__/entity.put.unit.test.ts
+++ b/src/__tests__/entity.put.unit.test.ts
@@ -88,6 +88,17 @@ const TestEntity4 = new Entity({
   table: TestTable3
 });
 
+const TestEntity5 = new Entity({
+  name: 'TestEntity5',
+  autoExecute: false,
+  attributes: {
+    pk: { partitionKey: true },
+    test_required_boolean: { type: 'boolean', required: true },
+    test_required_number: { type: 'number', required: true },
+  },
+  table: TestTable2
+})
+
 describe('put',()=>{
 
   it('creates basic item',() => {
@@ -291,6 +302,17 @@ describe('put',()=>{
     })).toThrow(`'test' is a required field`)
   })
 
+  it('puts 0 and false to required fields', () => {
+    let { Item } = TestEntity5.putParams({
+      pk: 'test-pk',
+      test_required_boolean: false,
+      test_required_number: 0
+    })
+
+    expect(Item.test_required_boolean).toBe(false)
+    expect(Item.test_required_number).toBe(0)
+  })
+
   it('formats a batch put response', async () => {
     let result = TestEntity.putBatch({ pk: 'x', sk: 'y' })
Repository: jeremydaly/dynamodb-toolbox
Pull request #279 (2022-07-07 20:26:48)

feat(entity/update): throw on removal of attribute with default value


Patch:
diff --git a/src/classes/Entity/Entity.ts b/src/classes/Entity/Entity.ts
index 12fd365c..3d3b891a 100644
--- a/src/classes/Entity/Entity.ts
+++ b/src/classes/Entity/Entity.ts
@@ -1056,6 +1056,12 @@ class Entity<
           if (schema.attributes[attrs[i]].required) {
             error(`'${attrs[i]}' is required and cannot be removed`)
           }
+
+          const attributeHasDefaultValue = schema.attributes[attrs[i]].default !== undefined
+          if(attributeHasDefaultValue) {
+error(`'${attrs[i]}' has a default value and cannot be removed`)
+          }
+
           // Grab the attribute name and add to REMOVE and names
           const attr = schema.attributes[attrs[i]].map || attrs[i]
           REMOVE.push(`#${attr}`)


Test patch:
diff --git a/src/__tests__/entity.update.unit.test.ts b/src/__tests__/entity.update.unit.test.ts
index 09a7b445..d50656b2 100644
--- a/src/__tests__/entity.update.unit.test.ts
+++ b/src/__tests__/entity.update.unit.test.ts
@@ -164,29 +164,8 @@ describe('update', () => {
     expect(TableName).toBe('test-table')
   })
 
-  // TODO: Fix removes with default values
-  it.skip('creates update that removes fields', () => {
-    let { TableName, Key, UpdateExpression, ExpressionAttributeNames, ExpressionAttributeValues } =
-      TestEntity.updateParams({ email: 'test-pk', sort: 'test-sk', $remove: 'test_string' })
-
-    expect(UpdateExpression).toBe(
-      'SET #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et) REMOVE #test_string'
-    )
-    expect(ExpressionAttributeNames).toEqual({
-      '#_md': '_md',
-      '#_ct': '_ct',
-      '#test_string': 'test_string',
-      '#_et': '_et'
-    })
-    expect(ExpressionAttributeValues).toHaveProperty(':_md')
-    expect(ExpressionAttributeValues).toHaveProperty(':_ct')
-    expect(ExpressionAttributeValues).not.toHaveProperty(':pk')
-    expect(ExpressionAttributeValues).not.toHaveProperty(':sk')
-    expect(ExpressionAttributeValues).not.toHaveProperty(':test_string')
-    expect(ExpressionAttributeValues).toHaveProperty(':_et')
-    expect(ExpressionAttributeValues![':_et']).toBe('TestEntity')
-    expect(Key).toEqual({ pk: 'test-pk', sk: 'test-sk' })
-    expect(TableName).toBe('test-table')
+  it('fails when removing fields with default values', () => {
+    expect(()=>TestEntity.updateParams({ email: 'test-pk', sort: 'test-sk', $remove: 'test_string' })).toThrow(`'test_string' has a default value and cannot be removed`);
   })
 
   it('creates update that just removes a field', () => {