Skip to content

Commit a5026dd

Browse files
authored
Merge pull request dynamodb-toolbox#156 from JakeMaldonado/fix-required-check
Fix required check not accepting 0 and false
2 parents 09ddf4f + e6ca2e6 commit a5026dd

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/__tests__/entity.put.unit.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ const TestEntity4 = new Entity({
8888
table: TestTable3
8989
});
9090

91+
const TestEntity5 = new Entity({
92+
name: 'TestEntity5',
93+
autoExecute: false,
94+
attributes: {
95+
pk: { partitionKey: true },
96+
test_required_boolean: { type: 'boolean', required: true },
97+
test_required_number: { type: 'number', required: true },
98+
},
99+
table: TestTable2
100+
})
101+
91102
describe('put',()=>{
92103

93104
it('creates basic item',() => {
@@ -291,6 +302,17 @@ describe('put',()=>{
291302
})).toThrow(`'test' is a required field`)
292303
})
293304

305+
it('puts 0 and false to required fields', () => {
306+
let { Item } = TestEntity5.putParams({
307+
pk: 'test-pk',
308+
test_required_boolean: false,
309+
test_required_number: 0
310+
})
311+
312+
expect(Item.test_required_boolean).toBe(false)
313+
expect(Item.test_required_number).toBe(0)
314+
})
315+
294316
it('formats a batch put response', async () => {
295317
let result = TestEntity.putBatch({ pk: 'x', sk: 'y' })
296318

src/classes/Entity.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,8 +1109,7 @@ class Entity<
11091109

11101110

11111111
// Check for required fields
1112-
Object.keys(required).forEach(field =>
1113-
required[field] !== undefined && !data[field]
1112+
Object.keys(required).forEach(field => required[field] !== undefined && (data[field] === undefined || data[field] === null)
11141113
&& error(`'${field}${this.schema.attributes[field].alias ? `/${this.schema.attributes[field].alias}` : ''}' is a required field`)
11151114
) // end required field check
11161115

0 commit comments

Comments
 (0)