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

View File

@ -17,7 +17,7 @@
import { ChangeDetectorRef, Component, Input, OnInit, output } from '@angular/core';
import { TbPopoverComponent } from '@shared/components/popover.component';
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 {
ArgumentEntityType,
ArgumentEntityTypeParamsMap,
@ -71,10 +71,10 @@ export class CalculatedFieldArgumentPanelComponent implements OnInit {
}),
refEntityKey: this.fb.group({
type: [ArgumentType.LatestTelemetry, [Validators.required]],
key: [''],
key: ['', [Validators.pattern(oneSpaceInsideRegex)]],
scope: [{ value: AttributeScope.SERVER_SCOPE, disabled: true }, [Validators.required]],
}),
defaultValue: ['', [Validators.pattern(noLeadTrailSpacesRegex)]],
defaultValue: ['', [Validators.pattern(oneSpaceInsideRegex)]],
limit: [{ value: this.defaultLimit, disabled: !this.maxDataPointsPerRollingArg }],
timeWindow: [MINUTE * 15],
});
@ -142,6 +142,10 @@ export class CalculatedFieldArgumentPanelComponent implements OnInit {
if (refEntityId.entityType === ArgumentEntityType.Tenant) {
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 });
}

View File

@ -14,6 +14,6 @@
/// 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]*$/;