[typebox/value] Property Transformation from One Property to Another #514
moontai0724
started this conversation in
Ideas
Replies: 1 comment 2 replies
-
@moontai0724 Hi, There is a partially implemented version of transforms available in the https://github.com/sinclairzx81/typebox/tree/master/examples/transform import { Transform, Encode, Decode } from './transform'
const User = Transform(Type.Object({
id: Type.String()
}), {
decode: (value) => {
return { userId: value.id }
},
encode: (value) => {
return { id: value.userId }
}
})
const decoded = Decode(User, { id: '100' }) // const decoded = { userId: '100' }
const encoded = Encode(User, decoded) // const encoded = { id: '100' } This implementation is pending inclusion in the library until it's had a good review and all the requirements ironed out, however you can use this module today by copying and pasting the |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Currently, the TypeBox provides useful functions like
Cast
inValue
to transform an object into a target type. However, the transformation is only possible when the property matches the key.I think to introducing a new feature that allows property transformation from one property to another would be highly beneficial and enhance the capabilities of TypeBox.
Beta Was this translation helpful? Give feedback.
All reactions