diff --git a/projects/ng-dynamic-forms/core/src/lib/service/dynamic-form.service.spec.ts b/projects/ng-dynamic-forms/core/src/lib/service/dynamic-form.service.spec.ts index 664f5dd7a..99dae0ec7 100644 --- a/projects/ng-dynamic-forms/core/src/lib/service/dynamic-form.service.spec.ts +++ b/projects/ng-dynamic-forms/core/src/lib/service/dynamic-form.service.spec.ts @@ -7,7 +7,11 @@ import { NG_VALIDATORS, NG_ASYNC_VALIDATORS } from "@angular/forms"; -import { DynamicFormService } from "./dynamic-form.service"; +import { + DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY, + DynamicFormService, + ModelJSONTransformFn +} from './dynamic-form.service'; import { DynamicFormValidationService } from "./dynamic-form-validation.service"; import { DynamicFormModel } from "../model/dynamic-form.model"; import { DynamicCheckboxModel } from "../model/checkbox/dynamic-checkbox.model"; @@ -31,6 +35,10 @@ import { DynamicTimePickerModel } from "../model/timepicker/dynamic-timepicker.m import { DynamicFormValueControlModel } from "../model/dynamic-form-value-control.model"; describe("DynamicFormService test suite", () => { + const labelSuffixTranformer: (suffix: string) => ModelJSONTransformFn = (suffix: string) => (jsonModel) => { + jsonModel.label = `${jsonModel.label}${suffix}`; + return jsonModel; + }; let testModel: DynamicFormModel, service: DynamicFormService; @@ -50,6 +58,7 @@ describe("DynamicFormService test suite", () => { providers: [ DynamicFormService, DynamicFormValidationService, + {provide: DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY, useValue: [labelSuffixTranformer('*')]}, {provide: NG_VALIDATORS, useValue: testValidator, multi: true}, {provide: NG_ASYNC_VALIDATORS, useValue: testAsyncValidator, multi: true} ] @@ -192,7 +201,6 @@ describe("DynamicFormService test suite", () => { expect(formGroup.get("testColorPicker") instanceof FormControl).toBe(true); }); - it("should parse dynamic form JSON", () => { let json = JSON.stringify(testModel), @@ -220,6 +228,15 @@ describe("DynamicFormService test suite", () => { }); + it("should transform dynamic form control JSON", () => { + const model = new DynamicInputModel({ id: 'name', label: 'Name' }), + json = JSON.stringify([model]), + formModel = service.fromJSON(json); + expect(formModel[0]).toEqual(jasmine.objectContaining({ label: 'Name*' })); + expect(service.getCustomJSONTransform(model)).toEqual(jasmine.objectContaining({ label: 'Name*' })); + }); + + it("should throw when unknown DynamicFormControlModel id is specified in JSON", () => { expect(() => service.fromJSON([{id: "test"}])) diff --git a/projects/ng-dynamic-forms/core/src/lib/service/dynamic-form.service.ts b/projects/ng-dynamic-forms/core/src/lib/service/dynamic-form.service.ts index 1aee7bce2..d6a31249d 100644 --- a/projects/ng-dynamic-forms/core/src/lib/service/dynamic-form.service.ts +++ b/projects/ng-dynamic-forms/core/src/lib/service/dynamic-form.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from "@angular/core"; +import { Inject, Injectable, InjectionToken, Optional } from '@angular/core'; import { AbstractControl, FormArray, FormControl, FormGroup } from "@angular/forms"; import { AbstractControlOptions } from "@angular/forms"; import { DynamicFormControlModel, FormHooks } from "../model/dynamic-form-control.model"; @@ -45,15 +45,26 @@ import { DynamicFormValidationService } from "./dynamic-form-validation.service" import { DynamicFormModel, DynamicUnionFormModel } from "../model/dynamic-form.model"; import { DynamicPathable } from "../model/misc/dynamic-form-control-path.model"; import { DynamicValidatorsConfig } from "../model/misc/dynamic-form-control-validation.model"; -import { maskFromString, parseReviver } from "../utils/json.utils"; -import { isString } from "../utils/core.utils"; +import { maskFromString, parseReviver, pipe } from '../utils/json.utils'; +import { isString } from '../utils/core.utils'; + +export type ModelJSONTransformFn = (modelJSON: T) => T; + +export type DynamicFormControlJSONTransformFnArray = ModelJSONTransformFn[]; +export const DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY = new InjectionToken( + 'DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY' +); @Injectable({ providedIn: "root" }) export class DynamicFormService { - constructor(private validationService: DynamicFormValidationService) {} + constructor(private validationService: DynamicFormValidationService, + @Inject(DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY) @Optional() + private readonly DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY: any) { + this.DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY = DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY as DynamicFormControlJSONTransformFnArray; + } private createAbstractControlOptions(validatorsConfig: DynamicValidatorsConfig | null = null, @@ -321,7 +332,7 @@ export class DynamicFormService { formModel: DynamicFormModel = []; formModelJSON.forEach((model: any) => { - + model = this.getCustomJSONTransform(model); let layout = model.layout || null; switch (model.type) { @@ -421,4 +432,10 @@ export class DynamicFormService { return formModel; } + + getCustomJSONTransform(model: object): object { + return Array.isArray(this.DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY) + ? pipe(...this.DYNAMIC_FORM_CONTROL_JSON_TRANSFORM_FN_ARRAY)(model) + : model; + } } diff --git a/projects/ng-dynamic-forms/core/src/lib/utils/json.utils.spec.ts b/projects/ng-dynamic-forms/core/src/lib/utils/json.utils.spec.ts index 7f2399c7f..9968aaf57 100644 --- a/projects/ng-dynamic-forms/core/src/lib/utils/json.utils.spec.ts +++ b/projects/ng-dynamic-forms/core/src/lib/utils/json.utils.spec.ts @@ -1,4 +1,4 @@ -import { maskFromString, maskToString, parseReviver } from "./json.utils"; +import { maskFromString, maskToString, parseReviver, pipe } from './json.utils'; describe("JSON utils test suite", () => { @@ -40,4 +40,14 @@ describe("JSON utils test suite", () => { expect(parseReviver("test", testValue1)).toEqual(new Date(testValue1)); }); + + it("should pass object in multiple functions", () => { + const object = { id: 'test' }, + modifyId = (modifier: string) => (x: any) => { + x.id = `${x.id}${modifier}`; + return x; + }; + + expect(pipe(modifyId('1'), modifyId('2'))(object)).toEqual(jasmine.objectContaining({ id: 'test12' })); + }); }); diff --git a/projects/ng-dynamic-forms/core/src/lib/utils/json.utils.ts b/projects/ng-dynamic-forms/core/src/lib/utils/json.utils.ts index 85cc708d6..32dfa70d6 100644 --- a/projects/ng-dynamic-forms/core/src/lib/utils/json.utils.ts +++ b/projects/ng-dynamic-forms/core/src/lib/utils/json.utils.ts @@ -40,3 +40,7 @@ export function parseReviver(_key: string, value: any): any { return isString(value) && regexDateISO.test(value) ? new Date(value) : value; } + +export function pipe(...fns: ((x: T) => T)[]): (x: T) => T { + return x => fns.reduce((v, f) => f(v), x); +}