handle case where selected union enum is invalid after migration (#1658)

* handle case where selected union enum is invalid after migration

* revert necessary ternary and fix types

Co-authored-by: Lucy Cifferello <12953208+elvece@users.noreply.github.com>
This commit is contained in:
Matt Hill
2022-07-17 13:42:36 -06:00
committed by GitHub
parent 778471d3cc
commit 6dfd46197d

View File

@@ -43,7 +43,7 @@ export class FormService {
getUnionObject( getUnionObject(
spec: ValueSpecUnion | ListValueSpecUnion, spec: ValueSpecUnion | ListValueSpecUnion,
selection: string, selection: string,
current?: { [key: string]: any }, current?: { [key: string]: any } | null,
): FormGroup { ): FormGroup {
const { variants, tag } = spec const { variants, tag } = spec
const { name, description, warning } = isFullUnion(spec) const { name, description, warning } = isFullUnion(spec)
@@ -84,7 +84,7 @@ export class FormService {
private getFormGroup( private getFormGroup(
config: ConfigSpec, config: ConfigSpec,
validators: ValidatorFn[] = [], validators: ValidatorFn[] = [],
current: { [key: string]: any } = {}, current?: { [key: string]: any } | null,
): FormGroup { ): FormGroup {
let group: Record<string, FormGroup | FormArray | FormControl> = {} let group: Record<string, FormGroup | FormArray | FormControl> = {}
Object.entries(config).map(([key, spec]) => { Object.entries(config).map(([key, spec]) => {
@@ -128,10 +128,13 @@ export class FormService {
}) })
return this.formBuilder.array(mapped, validators) return this.formBuilder.array(mapped, validators)
case 'union': case 'union':
const currentSelection = currentValue?.[spec.tag.id]
const isValid = !!spec.variants[currentSelection]
return this.getUnionObject( return this.getUnionObject(
spec, spec,
currentValue?.[spec.tag.id] || spec.default, isValid ? currentSelection : spec.default,
currentValue, isValid ? currentValue : undefined,
) )
case 'boolean': case 'boolean':
case 'enum': case 'enum':