Skip to content

Commit

Permalink
fix(Admin UI): only save active validator configs (#2436)
Browse files Browse the repository at this point in the history
fixes #2424
  • Loading branch information
sleidig authored Jun 27, 2024
1 parent 35e8d55 commit c41c16e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -184,7 +184,7 @@ export class AdminEntityFieldComponent implements OnChanges {
}
}

entityFieldValidatorChanges(validatorData: DynamicValidator) {
entityFieldValidatorChanges(validatorData: FormValidatorConfig) {
this.schemaFieldsForm.get("validators").setValue(validatorData);
}
private autoGenerateId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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<DynamicValidator>();

/**
* Emit the latest state of the validators config whenever the user changed it in the displayed form.
*/
@Output() entityValidatorChanges = new EventEmitter<FormValidatorConfig>();

constructor(private fb: FormBuilder) {}

ngOnInit() {
Expand Down Expand Up @@ -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;
}
}

0 comments on commit c41c16e

Please sign in to comment.