From 92286e14c33f219379163b89c0ed71f1d0145235 Mon Sep 17 00:00:00 2001 From: Yaapa Hage Date: Fri, 6 Nov 2020 20:19:49 +0530 Subject: [PATCH] implementation Signed-off-by: Yaapa Hage --- lib/model-builder.js | 5 +++++ test/id.test.js | 12 ++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/model-builder.js b/lib/model-builder.js index 5f053577..ef3372e4 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -355,6 +355,11 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett forceId = false; } + // Make id value mandatory if it is not autogenerated + if (!idProp.generated) { + ModelClass.validatesPresenceOf(idName); + } + if (forceId) { ModelClass.validatesAbsenceOf(idName, {if: 'isNewRecord'}); } diff --git a/test/id.test.js b/test/id.test.js index a3f96b52..52e47c74 100644 --- a/test/id.test.js +++ b/test/id.test.js @@ -15,7 +15,7 @@ const uid = require('./helpers/uid-generator'); let ds; const ValidationError = require('..').ValidationError; -describe.only('id property', function() { +describe('id property', function() { before(function() { ds = getSchema(); }); @@ -74,7 +74,7 @@ describe.only('id property', function() { // If id is not autogenerated, forceId is not applicable and a value must be specified it('should require id value', async () => { - await IdCheckC.create({name: 'Pablo'}).should.be.rejected(); + await IdCheckC.create({name: 'Pablo'}).should.be.rejectedWith({statusCode: 422}); }); // If id is not autogenerated, forceId is not applicable and a value can be specified @@ -96,7 +96,7 @@ describe.only('id property', function() { // If id is not autogenerated, forceId is not applicable and a value must be specified it('should require id value', async () => { - await IdCheckD.create({name: 'Pablo'}).should.be.rejected(); + await IdCheckD.create({name: 'Pablo'}).should.be.rejectedWith({statusCode: 422}); }); // If id is not autogenerated, forceId is not applicable and a value can be specified @@ -118,7 +118,7 @@ describe.only('id property', function() { await IdCheckE.destroyAll(); }); - // If id is autogenerated and forceId is true, id value must not be specified + // If id is autogenerated and forceId is true, id value must not be specified it('should not require id value', async () => { await IdCheckE.create({name: 'Pablo'}).should.be.fulfilled(); }); @@ -140,12 +140,12 @@ describe.only('id property', function() { await IdCheckF.destroyAll(); }); - // If id is autogenerated and forceId is false, a value need not be specified + // If id is autogenerated and forceId is false, a value need not be specified it('should not require id value', async () => { await IdCheckF.create({name: 'Pablo'}).should.be.fulfilled(); }); - // If id is autogenerated and forceId is false, a value can be specified + // If id is autogenerated and forceId is false, a value can be specified it('should allow id value to be specified', async () => { await IdCheckF.create({id: 10, name: 'Pablo'}).should.be.fulfilled(); });