Commit 2452aefff945904ae90434ee819d0866d43d9581

Authored by nickAS21
1 parent d0928e4d

Lwm2m: front add validator json for security config value

@@ -88,6 +88,9 @@ @@ -88,6 +88,9 @@
88 <mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsValue').hasError('required')"> 88 <mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsValue').hasError('required')">
89 {{ 'device.lwm2m-value-required' | translate }} 89 {{ 'device.lwm2m-value-required' | translate }}
90 </mat-error> 90 </mat-error>
  91 + <mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsValue').hasError('jsonError')">
  92 + {{ 'device.lwm2m-value-json-error' | translate }}
  93 + </mat-error>
91 <div mat-dialog-actions fxLayoutAlign="center center"> 94 <div mat-dialog-actions fxLayoutAlign="center center">
92 <button mat-raised-button color="primary" 95 <button mat-raised-button color="primary"
93 matTooltip="{{'device.lwm2m-value-edit-tip' | translate }}" 96 matTooltip="{{'device.lwm2m-value-edit-tip' | translate }}"
@@ -196,13 +196,19 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit, @@ -196,13 +196,19 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit,
196 this.deviceCredentialsFormGroup.get('credentialsBasic').disable({emitEvent: false}); 196 this.deviceCredentialsFormGroup.get('credentialsBasic').disable({emitEvent: false});
197 break; 197 break;
198 case DeviceCredentialsType.X509_CERTIFICATE: 198 case DeviceCredentialsType.X509_CERTIFICATE:
199 - case DeviceCredentialsType.LWM2M_CREDENTIALS:  
200 this.deviceCredentialsFormGroup.get('credentialsValue').setValidators([Validators.required]); 199 this.deviceCredentialsFormGroup.get('credentialsValue').setValidators([Validators.required]);
201 this.deviceCredentialsFormGroup.get('credentialsValue').updateValueAndValidity({emitEvent: false}); 200 this.deviceCredentialsFormGroup.get('credentialsValue').updateValueAndValidity({emitEvent: false});
202 this.deviceCredentialsFormGroup.get('credentialsId').setValidators([]); 201 this.deviceCredentialsFormGroup.get('credentialsId').setValidators([]);
203 this.deviceCredentialsFormGroup.get('credentialsId').updateValueAndValidity({emitEvent: false}); 202 this.deviceCredentialsFormGroup.get('credentialsId').updateValueAndValidity({emitEvent: false});
204 this.deviceCredentialsFormGroup.get('credentialsBasic').disable({emitEvent: false}); 203 this.deviceCredentialsFormGroup.get('credentialsBasic').disable({emitEvent: false});
205 break; 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 case DeviceCredentialsType.MQTT_BASIC: 212 case DeviceCredentialsType.MQTT_BASIC:
207 this.deviceCredentialsFormGroup.get('credentialsBasic').enable({emitEvent: false}); 213 this.deviceCredentialsFormGroup.get('credentialsBasic').enable({emitEvent: false});
208 this.deviceCredentialsFormGroup.get('credentialsBasic').updateValueAndValidity({emitEvent: false}); 214 this.deviceCredentialsFormGroup.get('credentialsBasic').updateValueAndValidity({emitEvent: false});
@@ -247,7 +253,11 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit, @@ -247,7 +253,11 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit,
247 if (credentialsValue === null || credentialsValue.length === 0) { 253 if (credentialsValue === null || credentialsValue.length === 0) {
248 credentialsValue = getDefaultSecurityConfig(); 254 credentialsValue = getDefaultSecurityConfig();
249 } else { 255 } else {
250 - credentialsValue = JSON.parse(credentialsValue); 256 + try {
  257 + credentialsValue = JSON.parse(credentialsValue);
  258 + } catch (e) {
  259 + credentialsValue = getDefaultSecurityConfig();
  260 + }
251 } 261 }
252 const credentialsId = this.deviceCredentialsFormGroup.get('credentialsId').value || DEFAULT_END_POINT; 262 const credentialsId = this.deviceCredentialsFormGroup.get('credentialsId').value || DEFAULT_END_POINT;
253 this.dialog.open<SecurityConfigComponent, DeviceCredentialsDialogLwm2mData, object>(SecurityConfigComponent, { 263 this.dialog.open<SecurityConfigComponent, DeviceCredentialsDialogLwm2mData, object>(SecurityConfigComponent, {
@@ -273,4 +283,18 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit, @@ -273,4 +283,18 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit,
273 private isDefautLw2mResponse(response: object): boolean { 283 private isDefautLw2mResponse(response: object): boolean {
274 return Object.keys(response).length === 0 || JSON.stringify(response) === '[{}]'; 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,6 +881,7 @@
881 "lwm2m-key-required": "LwM2M Security config key is required.", 881 "lwm2m-key-required": "LwM2M Security config key is required.",
882 "lwm2m-value": "LwM2M Security config", 882 "lwm2m-value": "LwM2M Security config",
883 "lwm2m-value-required": "LwM2M Security config value is required.", 883 "lwm2m-value-required": "LwM2M Security config value is required.",
  884 + "lwm2m-value-json-error": "LwM2M Security config value is not json format.",
884 "lwm2m-endpoint": "Client endpoint/identity", 885 "lwm2m-endpoint": "Client endpoint/identity",
885 "lwm2m-security-info": "Security Config Info", 886 "lwm2m-security-info": "Security Config Info",
886 "lwm2m-value-edit": "Edit Security config", 887 "lwm2m-value-edit": "Edit Security config",