Skip to content

Commit

Permalink
Test Case for Nested Transform
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed May 3, 2024
1 parent 1f2d88c commit e41f076
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions test/runtime/value/transform/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,26 @@ describe('value/transform/Object', () => {
it('Should throw on map decode', () => {
Assert.Throws(() => Encoder.Decode(T5, {}))
})
// ----------------------------------------------------------------
// https://github.com/sinclairzx81/typebox/issues/859
// ----------------------------------------------------------------
// prettier-ignore
it('Should decode for nested transform with renamed property', () => {
class User { constructor(public name: string, public createdAt: Date) {} }
const TDate = Type.Transform(Type.Number())
.Decode(v => new Date(v))
.Encode(v => v.getTime())
const TUser = Type.Transform(Type.Object({
name: Type.String(),
created_at: TDate
}))
.Decode(v => new User(v.name, v.created_at))
.Encode(v => ({ name: v.name, created_at: v.createdAt }))

const D = Value.Decode(TUser, { name: 'name', created_at: 0 })
const E = Value.Encode(TUser, D)

Assert.IsEqual(E.name, 'name')
Assert.IsEqual(E.created_at, 0)
})
})

0 comments on commit e41f076

Please sign in to comment.