Skip to content

Commit

Permalink
fix validation of times, also allow iso string
Browse files Browse the repository at this point in the history
  • Loading branch information
dotFionn committed May 4, 2024
1 parent 4d53b21 commit f4e6392
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/backend/message/message.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PilotCallsignValidator } from '../pilot/pilot.dto';
const TimeStringValidator = Joi.string().regex(/^(0[0-9]|1[0-9]|2[0-3])[0-5][0-9]$/);

class BaseClientMessage {
@JoiSchema(PilotCallsignValidator.required())
@JoiSchema(PilotCallsignValidator('callsign').required())
callsign: string;

@JoiSchema(Joi.string().allow(MessageClass.Inbound, MessageClass.Internal))
Expand Down
2 changes: 1 addition & 1 deletion src/backend/pilot/pilot.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import logger from '../logger';
import { PilotDto, PilotCallsignValidator } from './pilot.dto';
import { PilotService } from './pilot.service';

const joiPipeCallSign = new JoiPipe(PilotCallsignValidator.required());
const joiPipeCallSign = new JoiPipe(PilotCallsignValidator('callsign').required());

@ApiTags('pilots')
@Controller('api/v1/pilots')
Expand Down
15 changes: 9 additions & 6 deletions src/backend/pilot/pilot.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { JoiSchema, UPDATE, getTypeSchema } from 'nestjs-joi';
import Pilot from '@/shared/interfaces/pilot.interface';
import { ConvertForApis, NestedPartial } from '@/shared/utils/type.utils';

export const PilotCallsignValidator = Joi.string().regex(/^[A-Z0-9]{1,12}$/).message('"callsign" must be between 1 and 12 letters between A and Z or digits between 0 and 9');
const dateRegex = /(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/;

export const PilotCallsignValidator = (field: string) => Joi.string().regex(/^[A-Z0-9]{1,12}$/).message(`"${field}" must be between 1 and 12 letters between A and Z or digits between 0 and 9`);
export const TimeValidator = (field: string) => Joi.alternatives(Joi.number().strict(), Joi.string().regex(dateRegex).message(`"${field}" must either be a numeric unix timestamp in milliseconds or a valid iso string`));

class PilotDtoPosition {
@JoiSchema(Joi.number().required())
Expand All @@ -27,12 +30,12 @@ class PilotDtoFlightplan {
}

class PilotDtoVacdm {
@JoiSchema(Joi.number().required())
@JoiSchema([UPDATE], Joi.number().optional())
@JoiSchema(TimeValidator('eobt').required())
@JoiSchema([UPDATE], TimeValidator('eobt').optional())
eobt: number;

@JoiSchema(Joi.number().required())
@JoiSchema([UPDATE], Joi.number().optional())
@JoiSchema(TimeValidator('tobt').required())
@JoiSchema([UPDATE], TimeValidator('tobt').optional())
tobt: number;
}

Expand All @@ -50,7 +53,7 @@ export class PilotDto implements NestedPartial<ConvertForApis<Pilot>> {
@JoiSchema(Joi.string().optional())
_id: string;

@JoiSchema(PilotCallsignValidator.required())
@JoiSchema(PilotCallsignValidator('callsign').required())
@JoiSchema([UPDATE], Joi.forbidden())
callsign: string;

Expand Down

0 comments on commit f4e6392

Please sign in to comment.