Improved error handling for negative lap times

ref #71
This commit is contained in:
2021-01-03 17:21:53 +01:00
parent dcb791c9a2
commit 28c1b6d31d
5 changed files with 24 additions and 5 deletions

View File

@@ -1,4 +1,5 @@
import { IsInt, IsNotEmpty, IsOptional, IsPositive, IsString } from 'class-validator';
import { TrackLapTimeCantBeNegativeError } from '../../errors/TrackErrors';
import { Track } from '../entities/Track';
/**
@@ -25,8 +26,7 @@ export class CreateTrack {
*/
@IsInt()
@IsOptional()
@IsPositive()
minimumLapTime?: number;
minimumLapTime: number;
/**
* Creates a new Track entity from this.
@@ -37,6 +37,9 @@ export class CreateTrack {
newTrack.name = this.name;
newTrack.distance = this.distance;
newTrack.minimumLapTime = this.minimumLapTime;
if (this.minimumLapTime < 0) {
throw new TrackLapTimeCantBeNegativeError();
}
return newTrack;
}

View File

@@ -43,10 +43,9 @@ export class Track {
* The minimum time a runner should take to run a lap on this track.
* Will be used for fraud detection.
*/
@Column()
@Column({ nullable: true })
@IsInt()
@IsOptional()
@IsPositive()
minimumLapTime?: number;
/**

View File

@@ -1,4 +1,5 @@
import { IsInt, IsOptional, IsString } from "class-validator";
import { TrackLapTimeCantBeNegativeError } from '../../errors/TrackErrors';
import { Track } from '../entities/Track';
/**
@@ -40,5 +41,8 @@ export class ResponseTrack {
this.name = track.name;
this.distance = track.distance;
this.minimumLapTime = track.minimumLapTime;
if (this.minimumLapTime < 0) {
throw new TrackLapTimeCantBeNegativeError();
}
}
}