Skip to content

Commit

Permalink
fix(server): refine type handling in transformerDate and `transform…
Browse files Browse the repository at this point in the history
…erJson`

Refined the type checks and return values in both `transformerDate` and `transformerJson` to handle edge cases more effectively. This includes ensuring `null` and `undefined` are returned appropriately and that the types of inputs are strictly checked to prevent runtime errors. Added and adjusted tests to confirm the new behavior, ensuring consistency and reliability in data transformations within the database interaction layer.
  • Loading branch information
shorwood committed Jul 29, 2024
1 parent 5aba92a commit 899fc78
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
22 changes: 19 additions & 3 deletions packages/server/transformerDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ import { ValueTransformer } from 'typeorm'
* }
*/
export const transformerDate: ValueTransformer = {
to(value?: Date) { return value ? value.toISOString() : null },
from(value?: string) { return value ? new Date(value) : undefined },
to(value?: Date | null): string | null {
if (value instanceof Date === false) return null
return value.toISOString()
},
from(value?: string | null): Date | undefined {
if (typeof value !== 'string') return undefined
return new Date(value)
},
}

/* v8 ignore start */
Expand All @@ -26,13 +32,18 @@ if (import.meta.vitest) {
const date = new Date(0).toISOString()
const result = transformerDate.from(date) as Date
const expected = new Date(0)
expect(result).toMatchObject(expected)
expect(result).toStrictEqual(expected)
})

it('should return undefined when the value is null', () => {
const result = transformerDate.from(null)
expect(result).toBeUndefined()
})

it('should return undefined when the value is not a string', () => {
const result = transformerDate.from(0 as unknown as string)
expect(result).toBeUndefined()
})
})

describe('to', () => {
Expand All @@ -47,5 +58,10 @@ if (import.meta.vitest) {
const result = transformerDate.to(undefined)
expect(result).toBeNull()
})

it('should return null when the value is not a date', () => {
const result = transformerDate.to('not a date' as unknown as Date)
expect(result).toBeNull()
})
})
}
17 changes: 8 additions & 9 deletions packages/server/transformerJson.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable unicorn/no-null */
import { ValueTransformer } from 'typeorm'

Expand All @@ -13,25 +12,25 @@ import { ValueTransformer } from 'typeorm'
* passwordOptions: PasswordOptions
* }
*/
export const transformerJson: ValueTransformer = {
to(value?: Record<string, unknown>) {
if (!value) return null
export const transformerJson = {
to(value?: Record<string, unknown> | null): string | null {
if (typeof value !== 'object' || value === null) return null
return JSON.stringify(value)
},
from(value?: string) {
if (!value) return
from(value?: string | null): unknown {
if (typeof value !== 'string') return
try { return JSON.parse(value) as unknown }
catch { return }
},
}
} satisfies ValueTransformer

/* v8 ignore start */
if (import.meta.vitest) {
describe('from', () => {
it('should transform a JSON string to a JSON object', () => {
const json = JSON.stringify({ key: 'value' })
const result = transformerJson.from(json)
expect(result).toMatchObject({ key: 'value' })
expect(result).toStrictEqual({ key: 'value' })
})

it('should return undefined when the value is null', () => {
Expand All @@ -44,7 +43,7 @@ if (import.meta.vitest) {
it('should transform a JSON object to a JSON string', () => {
const json = { key: 'value' }
const result = transformerJson.to(json)
expect(result).toMatchObject('{"key":"value"}')
expect(result).toBe('{"key":"value"}')
})

it('should return null when the value is undefined', () => {
Expand Down

0 comments on commit 899fc78

Please sign in to comment.