Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(core) Stricter validation for atomic values #161

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 31 additions & 7 deletions packages/concerto-core/lib/serializer/jsonpopulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,41 @@ class JSONPopulator {
}
break;
case 'Integer':
case 'Long':
result = this.ergo ? parseInt(json.nat) : parseInt(json);
case 'Long': {
const num = this.ergo ? json.nat : json;
if (typeof num === 'number') {
if (Math.trunc(num) !== num) {
throw new ValidationException(`Expected value ${JSON.stringify(json)} to be of type ${field.getType()}`);
} else {
result = num;
}
} else {
throw new ValidationException(`Expected value ${JSON.stringify(json)} to be of type ${field.getType()}`);
}
}
break;
case 'Double':
result = parseFloat(json);
case 'Double': {
if (typeof json === 'number') {
result = parseFloat(json);
} else {
throw new ValidationException(`Expected value ${JSON.stringify(json)} to be of type ${field.getType()}`);
}
}
break;
case 'Boolean':
result = (json === true || json === 'true');
case 'Boolean': {
if (typeof json === 'boolean') {
result = json;
} else {
throw new ValidationException(`Expected value ${JSON.stringify(json)} to be of type ${field.getType()}`);
}
}
break;
case 'String':
result = json.toString();
if (typeof json === 'string') {
result = json;
} else {
throw new ValidationException(`Expected value ${JSON.stringify(json)} to be of type ${field.getType()}`);
}
break;
default: {
// everything else should be an enumerated value...
Expand Down
64 changes: 41 additions & 23 deletions packages/concerto-core/test/serializer/jsonpopulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const ModelManager = require('../../lib/modelmanager');
const Relationship = require('../../lib/model/relationship');
const Resource = require('../../lib/model/resource');
const TypedStack = require('../../lib/serializer/typedstack');
const ValidationException = require('../../lib/serializer/validationexception');
const TypeNotFoundException = require('../../lib/typenotfoundexception');
const Util = require('../composer/systemmodelutility');
const Moment = require('moment-mini');
Expand Down Expand Up @@ -114,47 +115,54 @@ describe('JSONPopulator', () => {
value.format('YYYY-MM-DDTHH:mm:ss.SSS[Z]').should.equal(Moment.parseZone('2016-10-20T05:34:03.000Z').format('YYYY-MM-DDTHH:mm:ss.SSS[Z]'));
});

it('should convert to integers from strings', () => {
it('should not convert to integers from strings', () => {
let field = sinon.createStubInstance(Field);
field.getType.returns('Integer');
let value = jsonPopulator.convertToObject(field, '32768');
value.should.equal(32768);
value = ergoJsonPopulator.convertToObject(field, {'nat':'32768'});
value.should.equal(32768);
(() => {
jsonPopulator.convertToObject(field, '32768');
}).should.throw(ValidationException, /Expected value "32768" to be of type Integer/);
});

it('should convert to integers from numbers', () => {
let field = sinon.createStubInstance(Field);
field.getType.returns('Integer');
let value = jsonPopulator.convertToObject(field, 32768);
value.should.equal(32768);
value = ergoJsonPopulator.convertToObject(field, {'nat':'32768'});
value = ergoJsonPopulator.convertToObject(field, {'nat':32768});
value.should.equal(32768);
});

it('should convert to longs from strings', () => {
it('should not convert to longs from strings', () => {
let field = sinon.createStubInstance(Field);
field.getType.returns('Long');
let value = jsonPopulator.convertToObject(field, '32768');
value.should.equal(32768);
value = ergoJsonPopulator.convertToObject(field, {'nat':'32768'});
value.should.equal(32768);
(() => {
jsonPopulator.convertToObject(field, '32768');
}).should.throw(ValidationException, /Expected value "32768" to be of type Long/);
});

it('should convert to longs from numbers', () => {
let field = sinon.createStubInstance(Field);
field.getType.returns('Long');
let value = jsonPopulator.convertToObject(field, 32768);
value.should.equal(32768);
value = ergoJsonPopulator.convertToObject(field, {'nat':'32768'});
value = ergoJsonPopulator.convertToObject(field, {'nat':32768});
value.should.equal(32768);
});

it('should convert to doubles from strings', () => {
it('should convert to longs from numbers that are not integers', () => {
let field = sinon.createStubInstance(Field);
field.getType.returns('Long');
(() => {
jsonPopulator.convertToObject(field, 32.768);
}).should.throw(ValidationException, /Expected value 32.768 to be of type Long/);
});

it('should not convert to doubles from strings', () => {
let field = sinon.createStubInstance(Field);
field.getType.returns('Double');
let value = jsonPopulator.convertToObject(field, '32.768');
value.should.equal(32.768);
(() => {
jsonPopulator.convertToObject(field, '32.768');
}).should.throw(ValidationException, /Expected value "32.768" to be of type Double/);
});

it('should convert to doubles from numbers', () => {
Expand All @@ -164,18 +172,27 @@ describe('JSONPopulator', () => {
value.should.equal(32.768);
});

it('should convert to booleans from strings', () => {
it('should convert to booleans from true', () => {
let field = sinon.createStubInstance(Field);
field.getType.returns('Boolean');
let value = jsonPopulator.convertToObject(field, 'true');
let value = jsonPopulator.convertToObject(field, true);
value.should.equal(true);
});

it('should convert to booleans from numbers', () => {
it('should not convert to booleans from strings', () => {
let field = sinon.createStubInstance(Field);
field.getType.returns('Boolean');
(() => {
jsonPopulator.convertToObject(field, 'true');
}).should.throw(ValidationException, /Expected value "true" to be of type Boolean/);
});

it('should not convert to booleans from numbers', () => {
let field = sinon.createStubInstance(Field);
field.getType.returns('Boolean');
let value = jsonPopulator.convertToObject(field, false);
value.should.equal(false);
(() => {
jsonPopulator.convertToObject(field, 32.768);
}).should.throw(ValidationException, /Expected value 32.768 to be of type Boolean/);
});

it('should convert to strings from strings', () => {
Expand All @@ -185,11 +202,12 @@ describe('JSONPopulator', () => {
value.should.equal('hello world');
});

it('should convert to strings from numbers', () => {
it('should not convert to strings from numbers', () => {
let field = sinon.createStubInstance(Field);
field.getType.returns('String');
let value = jsonPopulator.convertToObject(field, 32768);
value.should.equal('32768');
(() => {
jsonPopulator.convertToObject(field, 32.768);
}).should.throw(ValidationException, /Expected value 32.768 to be of type String/);
});

});
Expand Down