Commit 2452aefff945904ae90434ee819d0866d43d9581
1 parent
d0928e4d
Lwm2m: front add validator json for security config value
Showing
3 changed files
with
30 additions
and
2 deletions
... | ... | @@ -88,6 +88,9 @@ |
88 | 88 | <mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsValue').hasError('required')"> |
89 | 89 | {{ 'device.lwm2m-value-required' | translate }} |
90 | 90 | </mat-error> |
91 | + <mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsValue').hasError('jsonError')"> | |
92 | + {{ 'device.lwm2m-value-json-error' | translate }} | |
93 | + </mat-error> | |
91 | 94 | <div mat-dialog-actions fxLayoutAlign="center center"> |
92 | 95 | <button mat-raised-button color="primary" |
93 | 96 | matTooltip="{{'device.lwm2m-value-edit-tip' | translate }}" | ... | ... |
... | ... | @@ -196,13 +196,19 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit, |
196 | 196 | this.deviceCredentialsFormGroup.get('credentialsBasic').disable({emitEvent: false}); |
197 | 197 | break; |
198 | 198 | case DeviceCredentialsType.X509_CERTIFICATE: |
199 | - case DeviceCredentialsType.LWM2M_CREDENTIALS: | |
200 | 199 | this.deviceCredentialsFormGroup.get('credentialsValue').setValidators([Validators.required]); |
201 | 200 | this.deviceCredentialsFormGroup.get('credentialsValue').updateValueAndValidity({emitEvent: false}); |
202 | 201 | this.deviceCredentialsFormGroup.get('credentialsId').setValidators([]); |
203 | 202 | this.deviceCredentialsFormGroup.get('credentialsId').updateValueAndValidity({emitEvent: false}); |
204 | 203 | this.deviceCredentialsFormGroup.get('credentialsBasic').disable({emitEvent: false}); |
205 | 204 | break; |
205 | + case DeviceCredentialsType.LWM2M_CREDENTIALS: | |
206 | + this.deviceCredentialsFormGroup.get('credentialsValue').setValidators([Validators.required, this.jsonValidator]); | |
207 | + this.deviceCredentialsFormGroup.get('credentialsValue').updateValueAndValidity({emitEvent: false}); | |
208 | + this.deviceCredentialsFormGroup.get('credentialsId').setValidators([]); | |
209 | + this.deviceCredentialsFormGroup.get('credentialsId').updateValueAndValidity({emitEvent: false}); | |
210 | + this.deviceCredentialsFormGroup.get('credentialsBasic').disable({emitEvent: false}); | |
211 | + break; | |
206 | 212 | case DeviceCredentialsType.MQTT_BASIC: |
207 | 213 | this.deviceCredentialsFormGroup.get('credentialsBasic').enable({emitEvent: false}); |
208 | 214 | this.deviceCredentialsFormGroup.get('credentialsBasic').updateValueAndValidity({emitEvent: false}); |
... | ... | @@ -247,7 +253,11 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit, |
247 | 253 | if (credentialsValue === null || credentialsValue.length === 0) { |
248 | 254 | credentialsValue = getDefaultSecurityConfig(); |
249 | 255 | } else { |
250 | - credentialsValue = JSON.parse(credentialsValue); | |
256 | + try { | |
257 | + credentialsValue = JSON.parse(credentialsValue); | |
258 | + } catch (e) { | |
259 | + credentialsValue = getDefaultSecurityConfig(); | |
260 | + } | |
251 | 261 | } |
252 | 262 | const credentialsId = this.deviceCredentialsFormGroup.get('credentialsId').value || DEFAULT_END_POINT; |
253 | 263 | this.dialog.open<SecurityConfigComponent, DeviceCredentialsDialogLwm2mData, object>(SecurityConfigComponent, { |
... | ... | @@ -273,4 +283,18 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit, |
273 | 283 | private isDefautLw2mResponse(response: object): boolean { |
274 | 284 | return Object.keys(response).length === 0 || JSON.stringify(response) === '[{}]'; |
275 | 285 | } |
286 | + | |
287 | + private jsonValidator(control: FormControl) { | |
288 | + try { | |
289 | + debugger; | |
290 | + JSON.parse(control.value); | |
291 | + return null | |
292 | + } catch (e) { | |
293 | + return { | |
294 | + jsonError: { | |
295 | + parsedJson: "error" | |
296 | + } | |
297 | + } | |
298 | + } | |
299 | + } | |
276 | 300 | } | ... | ... |
... | ... | @@ -881,6 +881,7 @@ |
881 | 881 | "lwm2m-key-required": "LwM2M Security config key is required.", |
882 | 882 | "lwm2m-value": "LwM2M Security config", |
883 | 883 | "lwm2m-value-required": "LwM2M Security config value is required.", |
884 | + "lwm2m-value-json-error": "LwM2M Security config value is not json format.", | |
884 | 885 | "lwm2m-endpoint": "Client endpoint/identity", |
885 | 886 | "lwm2m-security-info": "Security Config Info", |
886 | 887 | "lwm2m-value-edit": "Edit Security config", | ... | ... |