From 2cb6ca196d5434b29106ea3f5c232fceaf10f0b7 Mon Sep 17 00:00:00 2001 From: "jonathan.casey" Date: Fri, 2 Jun 2023 17:56:16 +0100 Subject: [PATCH] move check to validation and add test coverage --- .../concerto-core/lib/serializer/jsongenerator.js | 3 --- .../lib/serializer/resourcevalidator.js | 4 ++++ packages/concerto-core/test/serializer.js | 12 ++++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/concerto-core/lib/serializer/jsongenerator.js b/packages/concerto-core/lib/serializer/jsongenerator.js index a81e25fa1..e351fb623 100644 --- a/packages/concerto-core/lib/serializer/jsongenerator.js +++ b/packages/concerto-core/lib/serializer/jsongenerator.js @@ -85,9 +85,6 @@ class JSONGenerator { */ visitMapDeclaration(mapDeclaration, parameters) { const obj = parameters.stack.pop(); - if (!((obj instanceof Map))) { - throw new Error('Expected a Map, but found ' + obj); - } return Object.fromEntries(obj); } diff --git a/packages/concerto-core/lib/serializer/resourcevalidator.js b/packages/concerto-core/lib/serializer/resourcevalidator.js index cb1415859..b0474c702 100644 --- a/packages/concerto-core/lib/serializer/resourcevalidator.js +++ b/packages/concerto-core/lib/serializer/resourcevalidator.js @@ -117,6 +117,10 @@ class ResourceValidator { const obj = parameters.stack.pop(); + if (!((obj instanceof Map))) { + throw new Error('Expected a Map, but found ' + obj); + } + obj.forEach((key,value) => { if(!ModelUtil.isSystemProperty(key)) { if (typeof key !== 'string') { diff --git a/packages/concerto-core/test/serializer.js b/packages/concerto-core/test/serializer.js index 4efec13f0..bfdd536a9 100644 --- a/packages/concerto-core/test/serializer.js +++ b/packages/concerto-core/test/serializer.js @@ -261,6 +261,18 @@ describe('Serializer', () => { }); }); + it('should throw if the value for a map is not a Map instance', () => { + let address = factory.newConcept('org.acme.sample', 'Address'); + address.city = 'Winchester'; + address.country = 'UK'; + address.elevation = 3.14; + address.postcode = 'SO21 2JN'; + address.testMap = 'xyz'; // bad value + (() => { + serializer.toJSON(address); + }).should.throw(Error, /Expected a Map, but found xyz/); + }); + it('should generate a field if an empty string is specififed', () => { let resource = factory.newResource('org.acme.sample', 'SampleAsset', '1'); resource.owner = factory.newRelationship('org.acme.sample', 'SampleParticipant', 'alice@email.com');