diff --git a/src-ui/src/app/components/admin/config/config.component.html b/src-ui/src/app/components/admin/config/config.component.html index 630b2eb41..e453a3fae 100644 --- a/src-ui/src/app/components/admin/config/config.component.html +++ b/src-ui/src/app/components/admin/config/config.component.html @@ -25,10 +25,10 @@
@switch (option.type) { - @case (ConfigOptionType.Select) { } - @case (ConfigOptionType.Number) { } - @case (ConfigOptionType.Boolean) { } - @case (ConfigOptionType.String) { } + @case (ConfigOptionType.Select) { } + @case (ConfigOptionType.Number) { } + @case (ConfigOptionType.Boolean) { } + @case (ConfigOptionType.String) { } }
@@ -47,7 +47,7 @@
- +
diff --git a/src-ui/src/app/components/admin/config/config.component.spec.ts b/src-ui/src/app/components/admin/config/config.component.spec.ts index 746f0e041..2e15c4399 100644 --- a/src-ui/src/app/components/admin/config/config.component.spec.ts +++ b/src-ui/src/app/components/admin/config/config.component.spec.ts @@ -93,4 +93,11 @@ describe('ConfigComponent', () => { OutputTypeConfig.PDF_A2 ) }) + + it('should support JSON validation for e.g. user_args', () => { + component.configForm.get('user_args').patchValue('{ foo bar }') + expect(component.errors).toEqual({ user_args: 'Invalid JSON' }) + component.configForm.get('user_args').patchValue('{ "foo": "bar" }') + expect(component.errors).toEqual({ user_args: null }) + }) }) diff --git a/src-ui/src/app/components/admin/config/config.component.ts b/src-ui/src/app/components/admin/config/config.component.ts index f9bc823c2..56cd027cc 100644 --- a/src-ui/src/app/components/admin/config/config.component.ts +++ b/src-ui/src/app/components/admin/config/config.component.ts @@ -1,5 +1,5 @@ import { Component, OnDestroy, OnInit } from '@angular/core' -import { FormControl, FormGroup } from '@angular/forms' +import { AbstractControl, FormControl, FormGroup } from '@angular/forms' import { BehaviorSubject, Observable, @@ -48,6 +48,8 @@ export class ConfigComponent user_args: new FormControl(), }) + public errors = {} + get optionCategories(): string[] { return Object.values(ConfigCategory) } @@ -87,6 +89,28 @@ export class ConfigComponent this.toastService.showError($localize`Error retrieving config`, e) }, }) + + // validate JSON input for user_args + this.configForm + .get('user_args') + .addValidators((control: AbstractControl) => { + if (!control.value || control.value.toString().length === 0) return null + try { + JSON.parse(control.value) + } catch (e) { + return [ + { + user_args: e, + }, + ] + } + return null + }) + this.configForm.get('user_args').statusChanges.subscribe((status) => { + this.errors['user_args'] = + status === 'INVALID' ? $localize`Invalid JSON` : null + }) + this.configForm.get('user_args').updateValueAndValidity() } ngOnDestroy(): void {