Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/auth/profile/dto/update-job-preferences.input.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { ValidationPipe } from "@nestjs/common";
import { UpdateJobPreferencesInput } from "./update-job-preferences.input";

describe("Job preference UTC offset validation", () => {
const pipe = new ValidationPipe({ enableDebugMessages: false });
const validate = (utcOffset: unknown): Promise<UpdateJobPreferencesInput> =>
pipe.transform(
{ workModes: ["remote"], utcOffset },
{ type: "body", metatype: UpdateJobPreferencesInput },
);

it.each(Array.from({ length: 105 }, (_, index) => -12 + index / 4))(
"accepts UTC offset %s",
async utcOffset => {
await expect(validate(utcOffset)).resolves.toMatchObject({ utcOffset });
},
);

it.each(["3", "5.5", "5.75", "-3.5"])(
"accepts existing numeric string inputs for %s",
async utcOffset => {
await expect(validate(utcOffset)).resolves.toMatchObject({
utcOffset: Number(utcOffset),
});
},
);

it.each([null, undefined])("allows an unset offset (%s)", async utcOffset => {
await expect(validate(utcOffset)).resolves.toMatchObject({ utcOffset });
});

it.each([-12.25, 14.25, 3.1, 5.6, -3.3, 3.001, "invalid", NaN, Infinity])(
"rejects invalid UTC offset %s",
async utcOffset => {
await expect(validate(utcOffset)).rejects.toThrow();
},
);

it("explains the allowed increments for an invalid offset", async () => {
await expect(validate(3.1)).rejects.toMatchObject({
response: {
message: [
"UTC offset must use quarter-hour increments, such as 3, 5.5, or 5.75",
],
},
});
});
});
4 changes: 2 additions & 2 deletions src/auth/profile/dto/update-job-preferences.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ArrayUnique,
IsArray,
IsBoolean,
IsDivisibleBy,
IsIn,
IsNumber,
IsOptional,
Expand All @@ -26,6 +25,7 @@ import {
WORK_MODES,
WorkMode,
} from "src/shared/interfaces";
import { IsQuarterHourOffset } from "src/shared/validators/quarter-hour-offset.validator";

const IsPreferenceList = (): ReturnType<typeof applyDecorators> =>
applyDecorators(
Expand Down Expand Up @@ -58,7 +58,7 @@ export class UpdateJobPreferencesInput {
@IsOptional()
@Type(() => Number)
@IsNumber({ maxDecimalPlaces: 2 })
@IsDivisibleBy(0.25)
@IsQuarterHourOffset()
@Min(-12)
@Max(14)
utcOffset: number | null;
Expand Down
20 changes: 20 additions & 0 deletions src/shared/validators/quarter-hour-offset.validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ValidateBy, ValidationOptions } from "class-validator";

export function IsQuarterHourOffset(
validationOptions?: ValidationOptions,
): PropertyDecorator {
return ValidateBy(
{
name: "isQuarterHourOffset",
validator: {
// IsDivisibleBy truncates fractional divisors to integers. Scaling
// hours to quarter-hours avoids that and is exact for multiples of .25.
validate: (value: unknown): boolean =>
typeof value === "number" && Number.isInteger(value * 4),
defaultMessage: () =>
"UTC offset must use quarter-hour increments, such as 3, 5.5, or 5.75",
},
},
validationOptions,
);
}
Loading