From 2dc63870f0b42962e1c48fa744bde3d2393fb483 Mon Sep 17 00:00:00 2001 From: Hundraw Date: Tue, 28 Feb 2023 17:09:40 +0100 Subject: [PATCH 1/2] Fix hasError when not required attribute has empty value --- src/models/Component.js | 4 +- src/models/ComponentAttribute.js | 8 ++-- tests/unit/models/Component.spec.js | 60 +++++++++++++++++++++++------ 3 files changed, 55 insertions(+), 17 deletions(-) diff --git a/src/models/Component.js b/src/models/Component.js index 90bb065f..172f9568 100644 --- a/src/models/Component.js +++ b/src/models/Component.js @@ -244,8 +244,8 @@ class Component extends FileInformation { return !attribute || attribute.value === null || attribute.value === undefined - || (attribute.type === 'Array' && attribute.value.length === 0) - || (attribute.type === 'Object' && Object.keys(attribute.value).length === 0) + || ((attribute.type === 'Array' || attribute.type === 'Object') + && attribute.value.length === 0) || (attribute.type === 'String' && attribute.value.trim() === ''); }); } diff --git a/src/models/ComponentAttribute.js b/src/models/ComponentAttribute.js index cb48ecab..de460da1 100644 --- a/src/models/ComponentAttribute.js +++ b/src/models/ComponentAttribute.js @@ -72,6 +72,10 @@ class ComponentAttribute { return typeof this.value !== this.type.toLowerCase(); } + if (this.value === null || this.value === undefined) { + return false; + } + return this.__typeOfValueValidation() || this.__ruleValueValidation() || this.__ruleMinAndMaxValidation() @@ -95,10 +99,6 @@ class ComponentAttribute { return !Array.isArray(this.value); } - if (type === 'object') { - return Object.prototype.toString.call(this.value) !== '[object Object]'; - } - // eslint-disable-next-line valid-typeof return typeof this.value !== type; } diff --git a/tests/unit/models/Component.spec.js b/tests/unit/models/Component.spec.js index d1b1a9f6..3c266a18 100644 --- a/tests/unit/models/Component.spec.js +++ b/tests/unit/models/Component.spec.js @@ -591,6 +591,29 @@ describe('Test class: Component', () => { }); describe('Test method: hasError', () => { + describe('Test not required attribute with no value', () => { + const attributeDefinition = new ComponentAttributeDefinition({ + name: 'attribute-string', + type: 'String', + }); + + it('Should return false if the attribute is not required', () => { + const attribute = new ComponentAttribute({ + name: 'attribute-string', + type: 'String', + definition: attributeDefinition, + }); + + expect(new Component({ + attributes: [attribute], + definition: { + definedAttributes: [attributeDefinition], + }, + }).hasError()) + .toEqual(false); + }); + }); + describe('Test required attribute', () => { const attributeDefinition = new ComponentAttributeDefinition({ name: 'attribute-string', @@ -629,11 +652,26 @@ describe('Test class: Component', () => { }).hasError()).toEqual(true); }); + it('Should return true if the value is empty', () => { + const attribute = new ComponentAttribute({ + name: 'attribute-string', + type: 'String', + definition: attributeDefinition, + }); + + expect(new Component({ + attributes: [attribute], + definition: { + definedAttributes: [attributeDefinition], + }, + }).hasError()).toEqual(true); + }); + it('Should not fail on Object type attribute', () => { const attribute = new ComponentAttribute({ name: 'attribute-object', value: {}, - type: 'Object', + type: 'Array', }); let error = null; @@ -1051,8 +1089,8 @@ describe('Test class: Component', () => { it('Should return false if value type is an object', () => { const attribute = new ComponentAttribute({ name: 'attribute-object', - value: {}, - type: 'Object', + value: [{}], + type: 'Array', definition: attributeDefinition, }); @@ -1068,7 +1106,7 @@ describe('Test class: Component', () => { const attribute = new ComponentAttribute({ name: 'attribute-object', value: 'object', - type: 'Object', + type: 'Array', definition: attributeDefinition, }); @@ -1081,8 +1119,8 @@ describe('Test class: Component', () => { }); it('Should return false if the "values" rule includes the attribute\'s value', () => { - const value1 = { test: 'test1' }; - const value2 = { test: 'test2' }; + const value1 = [{ test: 'test1' }]; + const value2 = [{ test: 'test2' }]; const attributeDefinitionValue = new ComponentAttributeDefinition({ name: 'attribute-object', type: 'Object', @@ -1092,7 +1130,7 @@ describe('Test class: Component', () => { const attribute = new ComponentAttribute({ name: 'attribute-object', value: value2, - type: 'Object', + type: 'Array', definition: attributeDefinitionValue, }); @@ -1107,8 +1145,8 @@ describe('Test class: Component', () => { it( 'Should return true if the "values" rule doesn\'t include the attribute\'s value', () => { - const value1 = { test: 'test1' }; - const value2 = { test: 'test2' }; + const value1 = [{ test: 'test1' }]; + const value2 = [{ test: 'test2' }]; const attributeDefinitionValue = new ComponentAttributeDefinition({ name: 'attribute-object', type: 'Object', @@ -1117,8 +1155,8 @@ describe('Test class: Component', () => { const attribute = new ComponentAttribute({ name: 'attribute-object', - value: { test: 'test3' }, - type: 'Object', + value: [{}, {}], + type: 'Array', definition: attributeDefinitionValue, }); From 36ffe83aaa9a33a142f4bab207f2cf9c605cbd70 Mon Sep 17 00:00:00 2001 From: Hundraw Date: Tue, 28 Feb 2023 17:18:24 +0100 Subject: [PATCH 2/2] Update changelog.md --- changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.md b/changelog.md index e5983d79..751cb1b0 100644 --- a/changelog.md +++ b/changelog.md @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fix the links width by multiplying it by the zoom scale. +- Fix `hasError` in `ComponentAttribute.js` to return false when the value of attribute is empty. ## [0.13.0] - 2023/02/09