diff --git a/changelog.md b/changelog.md index 4454f5e3..b38ba8e3 100644 --- a/changelog.md +++ b/changelog.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) +## [Unreleased] + +### Fixed + +- Fix some null on manage errors. + ## [0.26.0] - 2024/07/29 ### Added diff --git a/src/models/ComponentAttribute.js b/src/models/ComponentAttribute.js index 82a5dfab..82fa49f1 100644 --- a/src/models/ComponentAttribute.js +++ b/src/models/ComponentAttribute.js @@ -74,7 +74,7 @@ class ComponentAttribute { this.validateRuleValues(errors, id); this.validateRuleRegex(errors, id); - if (recurse && this.definition.type === 'Object' && Array.isArray(this.value)) { + if (recurse && this.definition?.type === 'Object' && Array.isArray(this.value)) { this.value.forEach((attribute) => attribute.getErrors(errors, true, id)); } @@ -116,7 +116,7 @@ class ComponentAttribute { if (this.definition.type !== 'Reference' && this.definition.type !== 'Link' - && this.definition?.type !== this.type) { + && this.definition.type !== this.type) { errors.push(new ParserLog({ componentId: id, severity: ParserLog.SEVERITY_ERROR, @@ -220,14 +220,13 @@ class ComponentAttribute { * @returns {ParserLog[]} All attributes error. */ validateRuleMinMax(errors = [], id = null) { - if (this.type === 'Boolean' || this.type === 'Object') { + if (this.type === 'Boolean' || this.type === 'Object' || !this.definition?.rules) { return errors; } - const value = typeof this.value === 'number' ? this.value : this.value.length; + const value = typeof this.value === 'number' ? this.value : this.value?.length || 0; - if (this.definition - && this.definition.rules.min !== null + if (this.definition.rules.min !== null && value < this.definition.rules.min) { errors.push(new ParserLog({ componentId: id, @@ -238,8 +237,7 @@ class ComponentAttribute { })); } - if (this.definition - && this.definition.rules.max !== null + if (this.definition.rules.max !== null && value > this.definition.rules.max) { errors.push(new ParserLog({ componentId: id, diff --git a/tests/unit/models/ComponentAttribute.spec.js b/tests/unit/models/ComponentAttribute.spec.js index 52280aba..8c95df8b 100644 --- a/tests/unit/models/ComponentAttribute.spec.js +++ b/tests/unit/models/ComponentAttribute.spec.js @@ -119,6 +119,18 @@ describe('Test class: ComponentAttribute', () => { attribute: 'child', })]); }); + + it('should not set error without definition', () => { + const attribute = new ComponentAttribute({ + name: 'parent', + type: 'String', + value: 'test', + }); + const errors = []; + + attribute.getErrors(errors, true); + expect(errors).toEqual([]); + }); }); describe('Test method: validateType', () => { @@ -345,6 +357,23 @@ describe('Test class: ComponentAttribute', () => { }); describe('Test method: validateRuleMinMax', () => { + it('should not set error without definition', () => { + const attribute = new ComponentAttribute({ + type: 'String', + }); + + expect(attribute.validateRuleMinMax()).toEqual([]); + }); + + it('should not set error without rules', () => { + const attribute = new ComponentAttribute({ + type: 'String', + definition: new ComponentAttributeDefinition(), + }); + + expect(attribute.validateRuleMinMax()).toEqual([]); + }); + it('should not set error with boolean type', () => { const attribute = new ComponentAttribute({ type: 'Boolean',