diff --git a/src/bone.js b/src/bone.js index a6f481f7..7786c291 100644 --- a/src/bone.js +++ b/src/bone.js @@ -183,7 +183,6 @@ class Bone { } if (args.length > 1) { - // execute validators this.#raw[name] = value instanceof Raw ? value : attribute.cast(value); this.#rawUnset.delete(name); return this; diff --git a/src/drivers/abstract/attribute.js b/src/drivers/abstract/attribute.js index 3a3c33a0..18fde7fe 100644 --- a/src/drivers/abstract/attribute.js +++ b/src/drivers/abstract/attribute.js @@ -123,7 +123,8 @@ class Attribute { } cast(value) { - return this.type.cast(value); + const castedValue = this.type.cast(value); + return castedValue == null? null : castedValue; } uncast(value, strict = true) { diff --git a/test/integration/suite/basics.test.js b/test/integration/suite/basics.test.js index 9a5e234f..1e037a4f 100644 --- a/test/integration/suite/basics.test.js +++ b/test/integration/suite/basics.test.js @@ -195,9 +195,12 @@ describe('=> Basic', () => { assert.deepEqual(post.previousChanges('title'), {}); await post.save(); assert.deepEqual(post.previousChanges('title'), {}); + assert.deepEqual(post.previousChanges('authorId'), { }); post.title = 'MHW'; + post.authorId = 100; await post.save(); assert.deepEqual(post.previousChanges('title'), { title: [ 'Untitled', 'MHW' ] }); + assert.deepEqual(post.previousChanges('authorId'), { authorId: [ null, 100 ] }); // should return {} if key does not exist assert.deepEqual(post.previousChanges('notExisted'), {}); }); @@ -208,10 +211,11 @@ describe('=> Basic', () => { await post.save(); assert.deepEqual(post.previousChanges(), {}); post.title = 'MHW'; + post.authorId = 100; const prevUpdatedAt = post.updatedAt; await sleep(10); await post.save(); - assert.deepEqual(post.previousChanges(), { title: [ 'Untitled', 'MHW' ], updatedAt: [ prevUpdatedAt, post.updatedAt ] }); + assert.deepEqual(post.previousChanges(), { authorId: [ null, 100 ], title: [ 'Untitled', 'MHW' ], updatedAt: [ prevUpdatedAt, post.updatedAt ] }); }); it('Bone.changes(key): raw VS rawSaved', async function () { diff --git a/test/integration/suite/data_types.test.js b/test/integration/suite/data_types.test.js index aa7b6531..faa8d15e 100644 --- a/test/integration/suite/data_types.test.js +++ b/test/integration/suite/data_types.test.js @@ -118,6 +118,9 @@ describe('=> Data types - JSON', () => { assert.deepEqual(note3.meta, [ 1, 2, 3 ]); await note3.reload(); assert.deepEqual(note3.meta, [ 1, 2, 3 ]); + assert.deepEqual(note3.body, null); + assert.deepEqual(note3.isPrivate, null); + assert.notEqual(note3.id, null); const note4 = await Note.findOne({ meta: { $like: '%1,2,3%' }}); assert.deepEqual(note3.toJSON(), note4.toJSON()); diff --git a/test/unit/adapters/sequelize.test.js b/test/unit/adapters/sequelize.test.js index 52dd036b..1bab018c 100644 --- a/test/unit/adapters/sequelize.test.js +++ b/test/unit/adapters/sequelize.test.js @@ -1043,6 +1043,13 @@ describe('=> Sequelize adapter', () => { updatedAt: post.updatedAt, createdAt: post.createdAt, wordCount: 0, + authorId: null, + content: null, + deletedAt: null, + extra: null, + settings: null, + summary: null, + thumb: null, }); post.content = 'a'; assert.deepEqual(post.previous(), { @@ -1052,6 +1059,13 @@ describe('=> Sequelize adapter', () => { updatedAt: post.updatedAt, createdAt: post.createdAt, wordCount: 0, + authorId: null, + content: null, + deletedAt: null, + extra: null, + settings: null, + summary: null, + thumb: null, }); const prevUpdatedAt = post.updatedAt; await post.update(); @@ -1062,6 +1076,13 @@ describe('=> Sequelize adapter', () => { updatedAt: prevUpdatedAt, createdAt: post.createdAt, wordCount: 0, + authorId: null, + content: null, + deletedAt: null, + extra: null, + settings: null, + summary: null, + thumb: null, }); });