Added space trim and changed space validation

This commit is contained in:
mpetrov 2025-03-05 11:43:17 +02:00
parent 40133b94d2
commit d187c76599
3 changed files with 24 additions and 11 deletions

View File

@ -32,7 +32,7 @@ import {
OutputType, OutputType,
OutputTypeTranslations OutputTypeTranslations
} from '@shared/models/calculated-field.models'; } from '@shared/models/calculated-field.models';
import { noLeadTrailSpacesRegex } from '@shared/models/regex.constants'; import { oneSpaceInsideRegex } from '@shared/models/regex.constants';
import { AttributeScope } from '@shared/models/telemetry/telemetry.models'; import { AttributeScope } from '@shared/models/telemetry/telemetry.models';
import { EntityType } from '@shared/models/entity-type.models'; import { EntityType } from '@shared/models/entity-type.models';
import { map, startWith, switchMap } from 'rxjs/operators'; import { map, startWith, switchMap } from 'rxjs/operators';
@ -50,15 +50,15 @@ import { Observable } from 'rxjs';
export class CalculatedFieldDialogComponent extends DialogComponent<CalculatedFieldDialogComponent, CalculatedField> { export class CalculatedFieldDialogComponent extends DialogComponent<CalculatedFieldDialogComponent, CalculatedField> {
fieldFormGroup = this.fb.group({ fieldFormGroup = this.fb.group({
name: ['', [Validators.required, Validators.pattern(noLeadTrailSpacesRegex), Validators.maxLength(255)]], name: ['', [Validators.required, Validators.pattern(oneSpaceInsideRegex), Validators.maxLength(255)]],
type: [CalculatedFieldType.SIMPLE], type: [CalculatedFieldType.SIMPLE],
debugSettings: [], debugSettings: [],
configuration: this.fb.group({ configuration: this.fb.group({
arguments: this.fb.control({}), arguments: this.fb.control({}),
expressionSIMPLE: ['', [Validators.required, Validators.pattern(noLeadTrailSpacesRegex), Validators.maxLength(255)]], expressionSIMPLE: ['', [Validators.required, Validators.pattern(oneSpaceInsideRegex), Validators.maxLength(255)]],
expressionSCRIPT: [], expressionSCRIPT: [],
output: this.fb.group({ output: this.fb.group({
name: ['', [Validators.required, Validators.pattern(noLeadTrailSpacesRegex), Validators.maxLength(255)]], name: ['', [Validators.required, Validators.pattern(oneSpaceInsideRegex), Validators.maxLength(255)]],
scope: [{ value: AttributeScope.SERVER_SCOPE, disabled: true }], scope: [{ value: AttributeScope.SERVER_SCOPE, disabled: true }],
type: [OutputType.Timeseries] type: [OutputType.Timeseries]
}), }),
@ -120,9 +120,18 @@ export class CalculatedFieldDialogComponent extends DialogComponent<CalculatedFi
} }
get fromGroupValue(): CalculatedField { get fromGroupValue(): CalculatedField {
const { configuration, type, ...rest } = this.fieldFormGroup.value; const { configuration, type, name, ...rest } = this.fieldFormGroup.value;
const { expressionSIMPLE, expressionSCRIPT, ...restConfig } = configuration; const { expressionSIMPLE, expressionSCRIPT, output, ...restConfig } = configuration;
return { configuration: { ...restConfig, type, expression: configuration['expression'+type] }, ...rest, type } as CalculatedField; return {
configuration: {
...restConfig,
type, expression: configuration['expression'+type].trim(),
output: { ...output, name: output.name.trim() }
},
name: name.trim(),
type,
...rest,
} as CalculatedField;
} }
cancel(): void { cancel(): void {

View File

@ -17,7 +17,7 @@
import { ChangeDetectorRef, Component, Input, OnInit, output } from '@angular/core'; import { ChangeDetectorRef, Component, Input, OnInit, output } from '@angular/core';
import { TbPopoverComponent } from '@shared/components/popover.component'; import { TbPopoverComponent } from '@shared/components/popover.component';
import { FormBuilder, FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms'; import { FormBuilder, FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms';
import { charsWithNumRegex, noLeadTrailSpacesRegex } from '@shared/models/regex.constants'; import { charsWithNumRegex, oneSpaceInsideRegex } from '@shared/models/regex.constants';
import { import {
ArgumentEntityType, ArgumentEntityType,
ArgumentEntityTypeParamsMap, ArgumentEntityTypeParamsMap,
@ -71,10 +71,10 @@ export class CalculatedFieldArgumentPanelComponent implements OnInit {
}), }),
refEntityKey: this.fb.group({ refEntityKey: this.fb.group({
type: [ArgumentType.LatestTelemetry, [Validators.required]], type: [ArgumentType.LatestTelemetry, [Validators.required]],
key: [''], key: ['', [Validators.pattern(oneSpaceInsideRegex)]],
scope: [{ value: AttributeScope.SERVER_SCOPE, disabled: true }, [Validators.required]], scope: [{ value: AttributeScope.SERVER_SCOPE, disabled: true }, [Validators.required]],
}), }),
defaultValue: ['', [Validators.pattern(noLeadTrailSpacesRegex)]], defaultValue: ['', [Validators.pattern(oneSpaceInsideRegex)]],
limit: [{ value: this.defaultLimit, disabled: !this.maxDataPointsPerRollingArg }], limit: [{ value: this.defaultLimit, disabled: !this.maxDataPointsPerRollingArg }],
timeWindow: [MINUTE * 15], timeWindow: [MINUTE * 15],
}); });
@ -142,6 +142,10 @@ export class CalculatedFieldArgumentPanelComponent implements OnInit {
if (refEntityId.entityType === ArgumentEntityType.Tenant) { if (refEntityId.entityType === ArgumentEntityType.Tenant) {
refEntityId.id = this.tenantId; refEntityId.id = this.tenantId;
} }
if (value.defaultValue) {
value.defaultValue = value.defaultValue.trim();
}
value.refEntityKey.key = value.refEntityKey.key.trim();
this.argumentsDataApplied.emit({ value, index: this.index }); this.argumentsDataApplied.emit({ value, index: this.index });
} }

View File

@ -14,6 +14,6 @@
/// limitations under the License. /// limitations under the License.
/// ///
export const noLeadTrailSpacesRegex = /^\S+(?: \S+)*$/; export const oneSpaceInsideRegex = /^\s*\S+(?:\s\S+)*\s*$/;
export const charsWithNumRegex = /^[a-zA-Z]+[a-zA-Z0-9]*$/; export const charsWithNumRegex = /^[a-zA-Z]+[a-zA-Z0-9]*$/;