implementation

Signed-off-by: Yaapa Hage <hage.yaapa@in.ibm.com>
This commit is contained in:
Yaapa Hage 2020-11-06 20:19:49 +05:30
parent d6a28f6aa1
commit 92286e14c3
2 changed files with 11 additions and 6 deletions

View File

@ -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'});
}

View File

@ -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();
});