diff --git a/src/__tests__/entity.update.unit.test.ts b/src/__tests__/entity.update.unit.test.ts index 8d59ba676..0910e51f7 100644 --- a/src/__tests__/entity.update.unit.test.ts +++ b/src/__tests__/entity.update.unit.test.ts @@ -545,6 +545,22 @@ describe('update',()=>{ })).toThrow(`'test2' is a required field`) }) + it('fails when removing a required field', () => { + expect(() => TestEntity3.updateParams({ + 'pk': 'test-pk', + 'test2': 'test-value', + '$remove': 'test' + })).toThrow(`'test' is required and cannot be removed`) + }) + + it('fails when setting a required field to a value that coerces to null', () => { + expect(() => TestEntity3.updateParams({ + 'pk': 'test-pk', + 'test': '', + 'test2': 'test-value', + })).toThrow(`'test' is required and cannot be removed`) + }) + it('fails when using non-numeric fields for indexed list updates', () => { expect(() => TestEntity.updateParams({ 'pk': 'test-pk', diff --git a/src/classes/Entity.ts b/src/classes/Entity.ts index 18dc8c6a0..afdb9154d 100644 --- a/src/classes/Entity.ts +++ b/src/classes/Entity.ts @@ -784,12 +784,17 @@ class Entity< // Verify attribute is not a pk/sk if (schema.attributes[attrs[i]].partitionKey === true || schema.attributes[attrs[i]].sortKey === true) error(`'${attrs[i]}' is the ${schema.attributes[attrs[i]].partitionKey === true ? 'partitionKey' : 'sortKey' } and cannot be removed`) + // Verify attribute is not required + if (schema.attributes[attrs[i]].required) + error(`'${attrs[i]}' is required 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}`) names[`#${attr}`] = attr } // end for } else if (this._table!._removeNulls === true && (data[field] === null || String(data[field]).trim() === '') && (!mapping.link || mapping.save)) { + if (schema.attributes[field].required) + error(`'${field}' is required and cannot be removed`); REMOVE.push(`#${field}`) names[`#${field}`] = field } else if (