Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions packages/concerto-core/src/serializer/resourcevalidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class ResourceValidator {
* @param {Object} value - the object being validated
* @param {Object} parameters - the parameter
* @param {Map} mapDeclaration - the object being visited
* @throws {ValidationException}
* @private
*/
checkMapType(type, value, parameters, mapDeclaration, ) {
Expand Down Expand Up @@ -150,28 +151,28 @@ class ResourceValidator {
switch(typeName) {
case 'String':
if (typeof value !== 'string') {
throw new Error(`Model violation in ${mapDeclaration.getFullyQualifiedName()}. Expected Type of String but found '${value}' instead.`);
throw new ValidationException(`Model violation in ${mapDeclaration.getFullyQualifiedName()}. Expected Type of String but found '${value}' instead.`);
}
break;
case 'DateTime':
if (typeof value !== 'string' || !dayjs.utc(value).isValid()) {
throw new Error(`Model violation in ${mapDeclaration.getFullyQualifiedName()}. Expected Type of DateTime but found '${value}' instead.`);
throw new ValidationException(`Model violation in ${mapDeclaration.getFullyQualifiedName()}. Expected Type of DateTime but found '${value}' instead.`);
}
break;
case 'Boolean':
if (typeof value !== 'boolean') {
throw new Error(`Model violation in ${mapDeclaration.getFullyQualifiedName()}. Expected Type of Boolean but found ${typeof value} instead, for value '${value}'.`);
throw new ValidationException(`Model violation in ${mapDeclaration.getFullyQualifiedName()}. Expected Type of Boolean but found ${typeof value} instead, for value '${value}'.`);
}
break;
case 'Integer':
case 'Long':
if (typeof value !== 'number' || !isFinite(value) || !Number.isInteger(value)) {
throw new Error(`Model violation in ${mapDeclaration.getFullyQualifiedName()}. Expected Type of ${typeName} but found '${value}' instead.`);
if (typeof value !== 'number' || !Number.isInteger(value)) {
throw new ValidationException(`Model violation in ${mapDeclaration.getFullyQualifiedName()}. Expected Type of ${typeName} but found '${value}' instead.`);
}
break;
case 'Double':
if (typeof value !== 'number' || !isFinite(value)) {
throw new Error(`Model violation in ${mapDeclaration.getFullyQualifiedName()}. Expected Type of Double but found '${value}' instead.`);
throw new ValidationException(`Model violation in ${mapDeclaration.getFullyQualifiedName()}. Expected Type of Double but found '${value}' instead.`);
}
break;
}
Expand All @@ -192,15 +193,21 @@ class ResourceValidator {
* @param {MapDeclaration} mapDeclaration - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
*
* @throws {ValidationException}
* @private
*/
visitMapDeclaration(mapDeclaration, parameters) {

const obj = parameters.stack.pop();

if (!((obj instanceof Map))) {
throw new Error('Expected a Map, but found ' + JSON.stringify(obj));
let objStr;
try {
objStr = JSON.stringify(obj);
} catch {
objStr = String(obj);
}
throw new ValidationException('Expected a Map, but found ' + objStr);
}

obj.forEach((value, key) => {
Expand Down
31 changes: 16 additions & 15 deletions packages/concerto-core/test/serializer/maptype/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const { Factory } = require('../../../src/factory');
const { ModelManager } = require('../../../src/modelmanager');
const { Resource } = require('../../../src/model/resource');
const { Serializer } = require('../../../src/serializer');
const { ValidationException } = require('../../../src/serializer/validationexception');
const Util = require('../../composer/composermodelutility');
const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc');
Expand Down Expand Up @@ -937,7 +938,7 @@ describe('Serializer', () => {

(() => {
serializer.toJSON(concept);
}).should.throw('Model violation in org.acme.sample@1.0.0.Appointment. Expected Type of DateTime but found \'BAD-DATE-28T01:02:03Z\' instead.');
}).should.throw(ValidationException, 'Model violation in org.acme.sample@1.0.0.Appointment. Expected Type of DateTime but found \'BAD-DATE-28T01:02:03Z\' instead.');
});

it('should throw if bad Key value is provided for Map, where Key Type String is expected', () => {
Expand All @@ -948,7 +949,7 @@ describe('Serializer', () => {

(() => {
serializer.toJSON(concept);
}).should.throw('Model violation in org.acme.sample@1.0.0.Dictionary. Expected Type of String but found \'1234\' instead.');
}).should.throw(ValidationException, 'Model violation in org.acme.sample@1.0.0.Dictionary. Expected Type of String but found \'1234\' instead.');
});

it('should throw if a bad Value is Supplied for Map, where Value type Boolean is expected', () => {
Expand All @@ -960,7 +961,7 @@ describe('Serializer', () => {

(() => {
serializer.toJSON(concept);
}).should.throw('Model violation in org.acme.sample@1.0.0.RSVP. Expected Type of Boolean but found string instead, for value \'false\'.');
}).should.throw(ValidationException, 'Model violation in org.acme.sample@1.0.0.RSVP. Expected Type of Boolean but found string instead, for value \'false\'.');
});

it('should throw if a bad Value is Supplied for Map, where Value type String is expected', () => {
Expand All @@ -971,7 +972,7 @@ describe('Serializer', () => {

(() => {
serializer.toJSON(concept);
}).should.throw('Model violation in org.acme.sample@1.0.0.Dictionary. Expected Type of String but found \'1234\' instead.');
}).should.throw(ValidationException, 'Model violation in org.acme.sample@1.0.0.Dictionary. Expected Type of String but found \'1234\' instead.');
});

it('should throw if a bad value is Supplied for Map - where Value type Boolean is expected', () => {
Expand All @@ -983,7 +984,7 @@ describe('Serializer', () => {

(() => {
serializer.toJSON(concept);
}).should.throw('Model violation in org.acme.sample@1.0.0.Timer. Expected Type of DateTime but found \'BAD-DATE-VALUE\' instead.');
}).should.throw(ValidationException, 'Model violation in org.acme.sample@1.0.0.Timer. Expected Type of DateTime but found \'BAD-DATE-VALUE\' instead.');
});

it('should throw if the value of a Map is not a Map instance', () => {
Expand All @@ -993,7 +994,7 @@ describe('Serializer', () => {

(() => {
serializer.toJSON(concept);
}).should.throw(`Expected a Map, but found ${JSON.stringify(concept.dict)}`);
}).should.throw(ValidationException, `Expected a Map, but found ${JSON.stringify(concept.dict)}`);
});

it('should ignore system properties', () => {
Expand Down Expand Up @@ -1435,14 +1436,14 @@ describe('Serializer', () => {
$class: 'org.acme.types@1.0.0.Container',
intMap: { k: 'abc' }
});
}).should.throw(/Expected Type of Integer but found 'abc'/);
}).should.throw(ValidationException, /Expected Type of Integer but found 'abc'/);

(() => {
typeSerializer.fromJSON({
$class: 'org.acme.types@1.0.0.Container',
intMap: { k: 1.5 }
});
}).should.throw(/Expected Type of Integer but found '1.5'/);
}).should.throw(ValidationException, /Expected Type of Integer but found '1.5'/);
});

it('should accept valid Long and reject non-long values', () => {
Expand All @@ -1457,14 +1458,14 @@ describe('Serializer', () => {
$class: 'org.acme.types@1.0.0.Container',
longMap: { k: 'abc' }
});
}).should.throw(/Expected Type of Long but found 'abc'/);
}).should.throw(ValidationException, /Expected Type of Long but found 'abc'/);

(() => {
typeSerializer.fromJSON({
$class: 'org.acme.types@1.0.0.Container',
longMap: { k: 2.5 }
});
}).should.throw(/Expected Type of Long but found '2.5'/);
}).should.throw(ValidationException, /Expected Type of Long but found '2.5'/);
});

it('should accept valid Double and reject non-number values', () => {
Expand All @@ -1479,14 +1480,14 @@ describe('Serializer', () => {
$class: 'org.acme.types@1.0.0.Container',
doubleMap: { k: 'abc' }
});
}).should.throw(/Expected Type of Double but found 'abc'/);
}).should.throw(ValidationException, /Expected Type of Double but found 'abc'/);

(() => {
typeSerializer.fromJSON({
$class: 'org.acme.types@1.0.0.Container',
doubleMap: { k: Infinity }
});
}).should.throw(/Expected Type of Double/);
}).should.throw(ValidationException, /Expected Type of Double/);
});

it('should accept valid Boolean and reject non-boolean values', () => {
Expand All @@ -1501,7 +1502,7 @@ describe('Serializer', () => {
$class: 'org.acme.types@1.0.0.Container',
boolMap: { k: 'true' }
});
}).should.throw(/Expected Type of Boolean/);
}).should.throw(ValidationException, /Expected Type of Boolean/);
});

it('should accept valid DateTime and reject numbers, booleans, and non-date strings', () => {
Expand All @@ -1516,14 +1517,14 @@ describe('Serializer', () => {
$class: 'org.acme.types@1.0.0.Container',
dateMap: { k: true }
});
}).should.throw(/Expected Type of DateTime but found 'true'/);
}).should.throw(ValidationException, /Expected Type of DateTime but found 'true'/);

(() => {
typeSerializer.fromJSON({
$class: 'org.acme.types@1.0.0.Container',
dateMap: { k: 1 }
});
}).should.throw(/Expected Type of DateTime but found '1'/);
}).should.throw(ValidationException, /Expected Type of DateTime but found '1'/);

(() => {
typeSerializer.fromJSON({
Expand Down
32 changes: 30 additions & 2 deletions packages/concerto-core/test/serializer/resourcevalidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ describe('ResourceValidator', function () {

(() => {
mapDeclaration.accept(resourceValidator,parameters );
}).should.throw('Model violation in org.acme.map@1.0.0.PhoneBook. Expected Type of String but found \'3\' instead.');
}).should.throw(ValidationException, 'Model violation in org.acme.map@1.0.0.PhoneBook. Expected Type of String but found \'3\' instead.');
});

it('should not validate map with bad key', function () {
Expand All @@ -376,7 +376,35 @@ describe('ResourceValidator', function () {

(() => {
mapDeclaration.accept(resourceValidator,parameters );
}).should.throw('Model violation in org.acme.map@1.0.0.PhoneBook. Expected Type of String but found \'1\' instead');
}).should.throw(ValidationException, 'Model violation in org.acme.map@1.0.0.PhoneBook. Expected Type of String but found \'1\' instead');
});

it('should throw ValidationException when visited object is not a Map', function () {
const mapDeclaration = modelManager.getType('org.acme.map@1.0.0.PhoneBook');

['not-a-map', null, undefined, {}, { a: 1 }].forEach((invalidObj) => {
const typedStack = new TypedStack(invalidObj);
const parameters = { stack : typedStack, 'modelManager' : modelManager, rootResourceIdentifier : 'TEST' };

(() => {
mapDeclaration.accept(resourceValidator, parameters);
}).should.throw(ValidationException, /Expected a Map, but found/);
});
});

it('should throw ValidationException as an instance of Error', function () {
const map = new Map([['$class', 'org.acme.map@1.0.0.PhoneBook'], ['Lorem', 3]]);
const typedStack = new TypedStack(map);
const mapDeclaration = modelManager.getType('org.acme.map@1.0.0.PhoneBook');
const parameters = { stack : typedStack, 'modelManager' : modelManager, rootResourceIdentifier : 'TEST' };

try {
mapDeclaration.accept(resourceValidator, parameters);
throw new Error('should have thrown');
} catch (err) {
err.should.be.an.instanceof(ValidationException);
err.should.be.an.instanceof(Error);
}
});
});

Expand Down
Loading