diff --git a/src/app/core/admin/admin-entity-details/admin-entity-field/admin-entity-field.component.ts b/src/app/core/admin/admin-entity-details/admin-entity-field/admin-entity-field.component.ts index 2de05c091a..eb53dd6f46 100644 --- a/src/app/core/admin/admin-entity-details/admin-entity-field/admin-entity-field.component.ts +++ b/src/app/core/admin/admin-entity-details/admin-entity-field/admin-entity-field.component.ts @@ -44,7 +44,7 @@ import { merge } from "rxjs"; import { filter } from "rxjs/operators"; import { uniqueIdValidator } from "app/core/common-components/entity-form/unique-id-validator/unique-id-validator"; import { ConfigureEntityFieldValidatorComponent } from "./configure-entity-field-validator/configure-entity-field-validator.component"; -import { DynamicValidator } from "app/core/common-components/entity-form/dynamic-form-validators/form-validator-config"; +import { FormValidatorConfig } from "app/core/common-components/entity-form/dynamic-form-validators/form-validator-config"; import { AnonymizeOptionsComponent } from "app/core/common-components/anonymize-options/anonymize-options.component"; import { MatCheckbox } from "@angular/material/checkbox"; @@ -184,7 +184,7 @@ export class AdminEntityFieldComponent implements OnChanges { } } - entityFieldValidatorChanges(validatorData: DynamicValidator) { + entityFieldValidatorChanges(validatorData: FormValidatorConfig) { this.schemaFieldsForm.get("validators").setValue(validatorData); } private autoGenerateId() { diff --git a/src/app/core/admin/admin-entity-details/admin-entity-field/configure-entity-field-validator/configure-entity-field-validator.component.spec.ts b/src/app/core/admin/admin-entity-details/admin-entity-field/configure-entity-field-validator/configure-entity-field-validator.component.spec.ts index 57c6788bb1..6d639b4d23 100644 --- a/src/app/core/admin/admin-entity-details/admin-entity-field/configure-entity-field-validator/configure-entity-field-validator.component.spec.ts +++ b/src/app/core/admin/admin-entity-details/admin-entity-field/configure-entity-field-validator/configure-entity-field-validator.component.spec.ts @@ -49,4 +49,28 @@ describe("ConfigureEntityFieldValidatorComponent", () => { it("should create", () => { expect(component).toBeTruthy(); }); + + it("should remove unchanged default values before emitting output", () => { + // set up + const testFormValues = { + required: true, + min: null, + max: 100, + regex: "abc", + validEmail: false, + uniqueId: "", + }; + + // execute + const actualResult = + component.removeDefaultValuesFromValidatorConfig(testFormValues); + + // check result + const expectedResult = { + required: true, + max: 100, + regex: "abc", + }; + expect(actualResult).toEqual(expectedResult); + }); }); diff --git a/src/app/core/admin/admin-entity-details/admin-entity-field/configure-entity-field-validator/configure-entity-field-validator.component.ts b/src/app/core/admin/admin-entity-details/admin-entity-field/configure-entity-field-validator/configure-entity-field-validator.component.ts index e62cff5085..ed807ad311 100644 --- a/src/app/core/admin/admin-entity-details/admin-entity-field/configure-entity-field-validator/configure-entity-field-validator.component.ts +++ b/src/app/core/admin/admin-entity-details/admin-entity-field/configure-entity-field-validator/configure-entity-field-validator.component.ts @@ -8,7 +8,7 @@ import { } from "@angular/forms"; import { MatCheckboxModule } from "@angular/material/checkbox"; import { EntitySchemaField } from "app/core/entity/schema/entity-schema-field"; -import { DynamicValidator } from "app/core/common-components/entity-form/dynamic-form-validators/form-validator-config"; +import { FormValidatorConfig } from "app/core/common-components/entity-form/dynamic-form-validators/form-validator-config"; import { CommonModule } from "@angular/common"; import { HelpButtonComponent } from "../../../../common-components/help-button/help-button.component"; @@ -28,8 +28,17 @@ import { HelpButtonComponent } from "../../../../common-components/help-button/h }) export class ConfigureEntityFieldValidatorComponent implements OnInit { validatorForm: FormGroup; + + /** + * the field definition with the currently existing validator settings to be edited + */ @Input() entitySchemaField: EntitySchemaField; - @Output() entityValidatorChanges = new EventEmitter(); + + /** + * Emit the latest state of the validators config whenever the user changed it in the displayed form. + */ + @Output() entityValidatorChanges = new EventEmitter(); + constructor(private fb: FormBuilder) {} ngOnInit() { @@ -58,7 +67,31 @@ export class ConfigureEntityFieldValidatorComponent implements OnInit { } this.validatorForm.valueChanges.subscribe((value) => { - this.entityValidatorChanges.emit(this.validatorForm.getRawValue()); + const rawValues = this.validatorForm.getRawValue(); + const cleanedValues = + this.removeDefaultValuesFromValidatorConfig(rawValues); + + this.entityValidatorChanges.emit(cleanedValues); }); } + + /** + * Removes default fields and returns a validator config that only contains explicitly activated validators. + * @param validators form values including default values that are unchanged + */ + removeDefaultValuesFromValidatorConfig( + validators: FormValidatorConfig, + ): FormValidatorConfig { + for (let key of Object.keys(validators)) { + if (isDefaultValue(validators[key])) { + delete validators[key]; + } + } + + function isDefaultValue(value): boolean { + return value === false || value === "" || value === null; + } + + return validators; + } }