Are nested transforms executed when encoding? #672
-
Hello, Encode does not appear to work on deep object -- is this supported by TypeBox? Sample code below illustrates:
Any advice much appreciated. Thanks!
|
Beta Was this translation helpful? Give feedback.
Answered by
sinclairzx81
Nov 20, 2023
Replies: 1 comment 1 reply
-
@rolanday Hi, This was actually a bug with Intersect transforms (these have been difficult to get right). I have published a fix on 0.31.28 which should resolve the issue. The following works on this revision. npm install @sinclair/typebox@latest import { Value } from '@sinclair/typebox/value'
import { Type } from '@sinclair/typebox'
const T = Type.Transform(Type.Object({
isHybrid: Type.Boolean()
}))
.Decode((value) => ({ isHybrid: value.isHybrid ? 1 : 0 }))
.Encode((value) => ({ isHybrid: value.isHybrid === 1 ? true : false }))
const I = Type.Intersect([
Type.Object({ model: Type.String() }),
Type.Object({ features: Type.Array(T) }),
])
const D = Value.Decode(I, { // const D = {
model: 'Prius', // model: 'Prius',
features: [ // features: [
{ isHybrid: true }, // { isHybrid: 1 },
{ isHybrid: false }, // { isHybrid: 0 },
] // ]
}) // }
const E = Value.Encode(I, { // const E = {
model: 'Prius', // model: 'Prius',
features: [ // features: [
{ isHybrid: 1 }, // { isHybrid: true },
{ isHybrid: 0 }, // { isHybrid: false },
] // ]
}) // } Let me know if this helps |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
rolanday
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@rolanday Hi,
This was actually a bug with Intersect transforms (these have been difficult to get right). I have published a fix on 0.31.28 which should resolve the issue. The following works on this revision.