Skip to content

Commit

Permalink
fix: fixed DateBlockTiming class-validator validation/parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
dereekb committed Dec 12, 2022
1 parent 54a6763 commit 23596cb
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/date/src/lib/date/date.block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class DateBlockTiming extends DateDurationSpan {
@Type(() => Date)
end!: Date;

constructor(template: DateBlockTiming) {
constructor(template?: DateBlockTiming) {
super(template);

if (template) {
Expand Down
9 changes: 7 additions & 2 deletions packages/date/src/lib/date/date.calendar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ISO8601DayString } from '@dereekb/util';
import { Expose } from 'class-transformer';
import { IsEnum } from 'class-validator';
import { daysToMinutes } from './date';
import { DateDurationSpan } from './date.duration';
import { parseISO8601DayStringToUTCDate } from './date.format';
Expand Down Expand Up @@ -31,11 +32,15 @@ export class CalendarDate extends DateDurationSpan {
* The type of event date.
*/
@Expose()
@IsEnum(CalendarDateType)
type: CalendarDateType = CalendarDateType.TIME;

constructor(template: CalendarDate) {
constructor(template?: CalendarDate) {
super(template);
this.type = template.type;

if (template != null) {
this.type = template.type;
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions packages/date/src/lib/date/date.duration.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { plainToClass } from 'class-transformer';
import { DateBlockTiming, dateBlockTiming } from './date.block';

describe('DateBlockTiming', () => {
it('should be parsed by class validator.', () => {
const data = dateBlockTiming({ startsAt: new Date(), duration: 60 }, 10);
const json: object = JSON.parse(JSON.stringify(data));

const result = plainToClass(DateBlockTiming, json, {
excludeExtraneousValues: true
});

expect(result.start).toBeDefined();
expect(result.end).toBeDefined();
expect(result.duration).toBe(data.duration);
expect(result.startsAt).toBeSameSecondAs(data.startsAt);
});
});
16 changes: 10 additions & 6 deletions packages/date/src/lib/date/date.duration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Minutes as UtilMinutes } from '@dereekb/util';
import { Expose, Type } from 'class-transformer';
import { IsNumber } from 'class-validator';
import { addMinutes } from 'date-fns';
import { DateRange, dateRangeState, DateRangeState } from './date.range';

Expand All @@ -13,14 +14,17 @@ export interface DateDurationSpan {
export class DateDurationSpan {
@Expose()
@Type(() => Date)
startsAt: Date;
startsAt!: Date;

@Expose()
duration: Minutes;

constructor(template: DateDurationSpan) {
this.startsAt = template.startsAt;
this.duration = template.duration;
@IsNumber()
duration!: Minutes;

constructor(template?: DateDurationSpan) {
if (template != null) {
this.startsAt = template.startsAt;
this.duration = template.duration;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/date/src/lib/date/date.range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class DateRange {
end!: Date;

constructor(template: DateRange) {
if (template) {
if (template != null) {
this.start = template.start;
this.end = template.end;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/date/src/lib/date/date.schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ export class DateSchedule implements DateSchedule {
@Min(0, { each: true })
@IsArray()
ex?: DateBlockIndex[];

constructor(template?: DateSchedule) {
if (template) {
this.w = template.w;
this.d = template.d;
this.ex = template.ex;
}
}
}

/**
Expand Down
30 changes: 20 additions & 10 deletions packages/date/src/lib/rrule/date.recurrence.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Exclude, Expose } from 'class-transformer';
import { Exclude, Expose, Type } from 'class-transformer';
import { CalendarDate, DateRange } from '../date';
import { DateRRuleInstance, DateRRuleUtility } from './date.rrule';
import { DateRRuleParseUtility, RRuleLines, RRuleStringLineSet } from './date.rrule.parse';
import { TimezoneString as UtilTimezoneString } from '@dereekb/util';
import { IsBoolean, IsOptional, IsString } from 'class-validator';

export type TimezoneString = UtilTimezoneString; // TEMPORARY: weird issue with importing primative types with jest.

Expand All @@ -22,38 +23,47 @@ export class ModelRecurrenceInfo implements DateRange {
* Required for RRules that have timezone-sensitive implementations.
*/
@Expose()
@IsOptional()
@IsString()
timezone?: TimezoneString;

/**
* RRules for this recurrence.
*/
@Expose()
rrule: RRuleLines;
@IsString()
rrule!: RRuleLines;

/**
* First instance of the recurrence.
*/
@Expose()
start: Date;
@Type(() => Date)
start!: Date;

/**
* Final instance of the recurrence.
*/
@Expose()
end: Date;
@Type(() => Date)
end!: Date;

/**
* True if the recurrence has no end.
*/
@Expose()
@IsOptional()
@IsBoolean()
forever?: boolean;

constructor(template: ModelRecurrenceInfo) {
this.timezone = template.timezone;
this.rrule = template.rrule;
this.start = template.start;
this.end = template.end;
this.forever = template.forever;
constructor(template?: ModelRecurrenceInfo) {
if (template != null) {
this.timezone = template.timezone;
this.rrule = template.rrule;
this.start = template.start;
this.end = template.end;
this.forever = template.forever;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/model/src/lib/data/website/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export class WebsiteLink {
@MaxLength(WEBSITE_LINK_ENCODED_DATA_MAX_LENGTH)
d!: WebsiteLinkEncodedData;

constructor(template: WebsiteLink) {
if (template) {
constructor(template?: WebsiteLink) {
if (template != null) {
this.t = template.t;
this.d = template.d;
}
Expand Down

0 comments on commit 23596cb

Please sign in to comment.