Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
Improve Resource Validator (#4566)
Browse files Browse the repository at this point in the history
This will resolve the specific problem raised in

Signed-off-by: Dave Kelsey <[email protected]>
#4562
  • Loading branch information
Dave Kelsey authored Jan 7, 2019
1 parent f8e0095 commit 11c7783
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/composer-common/lib/serializer/resourcevalidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ class ResourceValidator {
case 'Double':
case 'Long':
case 'Integer':
if(dataType !== 'number') {
if(dataType !== 'number' || isNaN(obj)) {
invalid = true;
}
break;
Expand Down Expand Up @@ -425,6 +425,9 @@ class ResourceValidator {
catch(err) {
value = value.toString();
}
} else if (typeOfValue === 'number' && isNaN(value)) {
value = 'Non-numeric';
typeOfValue = 'unknown';
}
}

Expand Down
37 changes: 37 additions & 0 deletions packages/composer-common/test/serializer/resourcevalidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,43 @@ describe('ResourceValidator', function () {
}).should.throw(/Model violation in instance identifier field propName/);
});

it('should throw if type of data is not a number for a numeric field', () => {
let mockField = sinon.createStubInstance(Field);
mockField.getName.returns('propName');
mockField.getType.returns('Integer');
mockField.isPrimitive.returns(true);
(() => {
resourceValidator.checkItem('fred', mockField, {rootResourceIdentifier: 'identifier'});
}).should.throw(/Model violation in instance identifier field propName.*fred.*string.*Integer/);
});

it('should throw if number is actually NaN', () => {
let mockField = sinon.createStubInstance(Field);
mockField.getName.returns('propName');
mockField.getType.returns('Integer');
mockField.isPrimitive.returns(true);
(() => {
resourceValidator.checkItem(parseInt('fred') /* creates NaN object*/, mockField, {rootResourceIdentifier: 'identifier'});
}).should.throw(/Model violation in instance identifier field propName.*Non-numeric.*unknown.*Integer/);

mockField = sinon.createStubInstance(Field);
mockField.getName.returns('propName');
mockField.getType.returns('Double');
mockField.isPrimitive.returns(true);
(() => {
resourceValidator.checkItem(parseInt('fred') /* creates NaN object*/, mockField, {rootResourceIdentifier: 'identifier'});
}).should.throw(/Model violation in instance identifier field propName.*Non-numeric.*unknown.*Double/);

mockField = sinon.createStubInstance(Field);
mockField.getName.returns('propName');
mockField.getType.returns('Long');
mockField.isPrimitive.returns(true);
(() => {
resourceValidator.checkItem(parseInt('fred') /* creates NaN object*/, mockField, {rootResourceIdentifier: 'identifier'});
}).should.throw(/Model violation in instance identifier field propName.*Non-numeric.*unknown.*Long/);

});

it('should throw if class declaration is not found', () => {
let mockField = sinon.createStubInstance(Field);
mockField.isPrimitive.returns(false);
Expand Down

0 comments on commit 11c7783

Please sign in to comment.