From 70993d76c6f7caac93a05a719603b00cf3f43fe8 Mon Sep 17 00:00:00 2001
From: L4168 <L4168@student.jamk.fi>
Date: Tue, 2 Jul 2019 17:08:25 +0300
Subject: [PATCH] json validation for node_settings

---
 src/game/game.dto.ts             |  9 +++++----
 src/game/game.entity.ts          |  4 ++--
 src/game/game.json-nested.dto.ts | 16 ++++++++++++++++
 src/game/game.json.dto.ts        | 11 +++++++++--
 4 files changed, 32 insertions(+), 8 deletions(-)
 create mode 100644 src/game/game.json-nested.dto.ts

diff --git a/src/game/game.dto.ts b/src/game/game.dto.ts
index 4b1fa9b..e4f12e3 100644
--- a/src/game/game.dto.ts
+++ b/src/game/game.dto.ts
@@ -14,7 +14,7 @@ import {
 import { ObjectivePointEntity } from './game.entity';
 import { CenterJSON } from '../shared/custom-validation';
 import { FactionDTO } from '../faction/faction.dto';
-import { CenterDTO } from './game.json.dto';
+import { CenterDTO, NodeSettingsDTO } from './game.json.dto';
 import { Type } from 'class-transformer';
 
 export class GameDTO {
@@ -25,14 +25,15 @@ export class GameDTO {
   @IsNotEmpty()
   @Length(1, 255)
   desc: string;
-  @IsNotEmpty()
   @ValidateNested()
   @Type(() => CenterDTO)
   center: CenterDTO;
   @Allow()
-  map?: JSON;
+  @ValidateNested()
+  @Type(() => NodeSettingsDTO)
+  nodesettings?: NodeSettingsDTO;
   @Allow()
-  nodesettings?: JSON;
+  map?: JSON;
   @IsDateString()
   @IsNotEmpty()
   startdate: string;
diff --git a/src/game/game.entity.ts b/src/game/game.entity.ts
index 030e81d..5f019e8 100644
--- a/src/game/game.entity.ts
+++ b/src/game/game.entity.ts
@@ -14,7 +14,7 @@ import { PersonEntity } from '../user/user.entity';
 import { GameGroupEntity } from '../faction/faction.entity';
 import { FactionEntity } from '../faction/faction.entity';
 import { TaskEntity } from '../task/task.entity';
-import { CenterDTO } from './game.json.dto';
+import { CenterDTO, NodeSettingsDTO } from './game.json.dto';
 
 // table that stores all created games
 @Entity('Game')
@@ -24,7 +24,7 @@ export class GameEntity {
   @Column('text') desc: string;
   @Column('json') center: CenterDTO;
   @Column({ type: 'json', nullable: true }) map: JSON;
-  @Column({ type: 'json', nullable: true }) nodesettings?: JSON;
+  @Column({ type: 'json', nullable: true }) nodesettings?: NodeSettingsDTO;
   @Column('timestamp') startdate: Timestamp;
   @Column('timestamp') enddate: Timestamp;
 
diff --git a/src/game/game.json-nested.dto.ts b/src/game/game.json-nested.dto.ts
new file mode 100644
index 0000000..df67071
--- /dev/null
+++ b/src/game/game.json-nested.dto.ts
@@ -0,0 +1,16 @@
+import { IsNumber } from 'class-validator';
+
+export class NodeCoreSettingsDTO {
+  @IsNumber()
+  capture_time: number;
+  @IsNumber()
+  confirmation_time: number;
+  @IsNumber()
+  owner: number;
+  @IsNumber()
+  capture: number;
+  @IsNumber()
+  buttons_available: number;
+  @IsNumber()
+  heartbeat_interval: number;
+}
diff --git a/src/game/game.json.dto.ts b/src/game/game.json.dto.ts
index 31bb481..5b9484a 100644
--- a/src/game/game.json.dto.ts
+++ b/src/game/game.json.dto.ts
@@ -1,5 +1,6 @@
-import { GameDTO } from './game.dto';
-import { IsNumber } from 'class-validator';
+import { IsNumber, ValidateNested } from 'class-validator';
+import { NodeCoreSettingsDTO } from './game.json-nested.dto';
+import { Type } from 'class-transformer';
 
 export class CenterDTO {
   @IsNumber()
@@ -7,3 +8,9 @@ export class CenterDTO {
   @IsNumber()
   lng: number;
 }
+
+export class NodeSettingsDTO {
+  @ValidateNested()
+  @Type(() => NodeCoreSettingsDTO)
+  node_settings: NodeCoreSettingsDTO;
+}
-- 
GitLab