Skip to content

Commit

Permalink
move check to validation and add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-casey committed Jun 2, 2023
1 parent 5ec493e commit 2cb6ca1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
3 changes: 0 additions & 3 deletions packages/concerto-core/lib/serializer/jsongenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 4 additions & 0 deletions packages/concerto-core/lib/serializer/resourcevalidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
12 changes: 12 additions & 0 deletions packages/concerto-core/test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', '[email protected]');
Expand Down

0 comments on commit 2cb6ca1

Please sign in to comment.