From 11c77833037f28a55dee9f0ae6abff76e53a9564 Mon Sep 17 00:00:00 2001 From: Dave Kelsey Date: Mon, 7 Jan 2019 15:49:21 +0000 Subject: [PATCH] Improve Resource Validator (#4566) This will resolve the specific problem raised in Signed-off-by: Dave Kelsey #4562 --- .../lib/serializer/resourcevalidator.js | 5 ++- .../test/serializer/resourcevalidator.js | 37 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/packages/composer-common/lib/serializer/resourcevalidator.js b/packages/composer-common/lib/serializer/resourcevalidator.js index 3933a9a8e4..896dbaa44a 100644 --- a/packages/composer-common/lib/serializer/resourcevalidator.js +++ b/packages/composer-common/lib/serializer/resourcevalidator.js @@ -301,7 +301,7 @@ class ResourceValidator { case 'Double': case 'Long': case 'Integer': - if(dataType !== 'number') { + if(dataType !== 'number' || isNaN(obj)) { invalid = true; } break; @@ -425,6 +425,9 @@ class ResourceValidator { catch(err) { value = value.toString(); } + } else if (typeOfValue === 'number' && isNaN(value)) { + value = 'Non-numeric'; + typeOfValue = 'unknown'; } } diff --git a/packages/composer-common/test/serializer/resourcevalidator.js b/packages/composer-common/test/serializer/resourcevalidator.js index ba55d124e7..7998fb7017 100644 --- a/packages/composer-common/test/serializer/resourcevalidator.js +++ b/packages/composer-common/test/serializer/resourcevalidator.js @@ -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);