Skip to content

Commit

Permalink
fix: added handling for date format
Browse files Browse the repository at this point in the history
  • Loading branch information
wzarek committed May 10, 2024
1 parent c785603 commit b8be3e8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,62 @@ describe('CommentDto', () => {
expect(() => CommentDtoMapper.fromObject(objectWithoutCreatedAt)).toThrow(ModelMappingError)
expect(() => CommentDtoMapper.fromObject(objectWithoutUserId)).toThrow(ModelMappingError)
})

it('should throw an error if date is in incorrect format', () => {
const object = {
EventId: eventId,
Content: content,
CreatedAt: 'invalid date',
UserId: userId
}

const objectMissingZ = {
EventId: eventId,
Content: content,
CreatedAt: '2009-06-15T13:45:30.0000000',
UserId: userId
}

expect(() => CommentDtoMapper.fromObject(object)).toThrow(ModelMappingError)
expect(() => CommentDtoMapper.fromObject(objectMissingZ)).toThrow(ModelMappingError)
})

it('should properly parse date string', () => {
const object = {
EventId: eventId,
Content: content,
CreatedAt: '2009-06-15T13:45:30.0000000-07:00',
UserId: userId
}

const objectWithoutOffset = {
EventId: eventId,
Content: content,
CreatedAt: '2009-06-15T13:45:30.0000000Z',
UserId: userId
}

const objectWithoutMilliseconds = {
EventId: eventId,
Content: content,
CreatedAt: '2009-06-15T13:45:30Z',
UserId: userId
}

let comment = CommentDtoMapper.fromObject(object)
comment = comment as CommentDto

expect(comment.CreatedAt).toEqual(new Date('2009-06-15T13:45:30.0000000-07:00'))

comment = CommentDtoMapper.fromObject(objectWithoutOffset)
comment = comment as CommentDto

expect(comment.CreatedAt).toEqual(new Date('2009-06-15T13:45:30.0000000Z'))

comment = CommentDtoMapper.fromObject(objectWithoutMilliseconds)
comment = comment as CommentDto

expect(comment.CreatedAt).toEqual(new Date('2009-06-15T13:45:30Z'))
})
})
})
2 changes: 1 addition & 1 deletion Client/reasn-client/packages/common/models/CommentDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { z } from "zod"
export const CommentDtoSchema = z.object({
EventId: z.number(),
Content: z.string(),
CreatedAt: z.string().or(z.date()).transform(arg => new Date(arg)),
CreatedAt: z.string().datetime({ offset: true }).or(z.date()).transform(arg => new Date(arg)),
UserId: z.number()
})

Expand Down
8 changes: 4 additions & 4 deletions Client/reasn-client/packages/common/models/EventDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export const EventDtoSchema = z.object({
AddressId: z.number(),
Description: z.string(),
OrganizerId: z.number(),
StartAt: z.string().or(z.date()).transform(arg => new Date(arg)),
EndAt: z.string().or(z.date()).transform(arg => new Date(arg)),
CreatedAt: z.string().or(z.date()).transform(arg => new Date(arg)),
UpdatedAt: z.string().or(z.date()).transform(arg => new Date(arg)),
StartAt: z.string().datetime({ offset: true }).or(z.date()).transform(arg => new Date(arg)),
EndAt: z.string().datetime({ offset: true }).or(z.date()).transform(arg => new Date(arg)),
CreatedAt: z.string().datetime({ offset: true }).or(z.date()).transform(arg => new Date(arg)),
UpdatedAt: z.string().datetime({ offset: true }).or(z.date()).transform(arg => new Date(arg)),
Slug: z.string().nullable(),
StatusId: z.number(),
Tags: z.array(TagDtoSchema)
Expand Down

0 comments on commit b8be3e8

Please sign in to comment.