Skip to content

Commit

Permalink
Merge pull request #263 from ditrit/bugfix/manage_errors
Browse files Browse the repository at this point in the history
Bugfix: manage errors
  • Loading branch information
Zorin95670 authored Jul 31, 2024
2 parents ef40ee6 + af30529 commit a87ed79
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 6 additions & 8 deletions src/models/ComponentAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/models/ComponentAttribute.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit a87ed79

Please sign in to comment.