瀏覽代碼

validate the airport inputs

Sven Czarnian 2 年之前
父節點
當前提交
d890bc5eb1
共有 1 個文件被更改,包括 61 次插入0 次删除
  1. 61 0
      src/airport/airport.controller.ts

+ 61 - 0
src/airport/airport.controller.ts

@@ -174,6 +174,56 @@ export class AirportController {
     };
   }
 
+  private static validateAirportEnums(airport: AirportDto): void {
+    airport.planning.assignments.forEach((assignment) => {
+      if (
+        assignment.type !== 'SHALL' &&
+        assignment.type !== 'SHOULD' &&
+        assignment.type !== 'MAY'
+      ) {
+        throw new HttpException(
+          'Invalid assignment type',
+          HttpStatus.UNPROCESSABLE_ENTITY,
+        );
+      }
+    });
+  }
+
+  private static validateConstraint(constraint: ConstraintDto): void {
+    if (
+      constraint.altitude.mode !== 'BELOW' &&
+      constraint.altitude.mode !== 'EXACT' &&
+      constraint.altitude.mode !== 'ABOVE'
+    ) {
+      throw new HttpException(
+        'Invalid altitude constraint mode',
+        HttpStatus.UNPROCESSABLE_ENTITY,
+      );
+    }
+    if (
+      constraint.speed.mode !== 'BELOW' &&
+      constraint.speed.mode !== 'EXACT' &&
+      constraint.speed.mode !== 'ABOVE'
+    ) {
+      throw new HttpException(
+        'Invalid speed constraint mode',
+        HttpStatus.UNPROCESSABLE_ENTITY,
+      );
+    }
+  }
+
+  private static validateAirportConstraints(airport: AirportDto): void {
+    airport.upperConstraints.forEach((constraint) =>
+      AirportController.validateConstraint(constraint),
+    );
+
+    airport.arrivalRoutes.forEach((route) => {
+      route.constraints.forEach((constraint) =>
+        AirportController.validateConstraint(constraint),
+      );
+    });
+  }
+
   @Get('/allCodes')
   @ApiResponse({
     status: 200,
@@ -221,7 +271,18 @@ export class AirportController {
     status: 409,
     description: 'The airport is already registered',
   })
+  @ApiResponse({
+    status: 422,
+    description: 'The assignment type is invalid',
+  })
+  @ApiResponse({
+    status: 422,
+    description: 'The constraint type is invalid',
+  })
   async register(@Body('airport') airport: AirportDto): Promise<void> {
+    AirportController.validateAirportConstraints(airport);
+    AirportController.validateAirportEnums(airport);
+
     await this.airportService
       .registerAirport(AirportController.airportDtoToAirport(airport))
       .then((registered) => {