Is there a way to get rid of additional properties without throwing? #588
-
Consider this example const data = {
id: 123,
name: 'Test',
email: 'test@test.com',
timezone: 'Test/Test',
};
const schema = Type.Object(
{
id: Type.Number(),
name: Type.String(),
email: Type.String(),
imageUrl: Type.Optional(Type.String()),
},
{ additionalProperties: false },
);
console.log(Value.Decode(schema, data)); This will throw because |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@errmayank Hi, TypeBox requires that the data being decoded (or validated) be correct and it does not automatically remove additional properties during validation or decode phases. This is largely by design as the need to remove additional properties is usually the result of a sender sending invalid data (and should usually be fixed at the sender, not the receiver). It is possible to write logic to remove any additional properties prior to calling decode, but the extra processing of a value would need to be handled in user code (outside of TypeBox) Hope this helps |
Beta Was this translation helpful? Give feedback.
@errmayank Hi,
TypeBox requires that the data being decoded (or validated) be correct and it does not automatically remove additional properties during validation or decode phases. This is largely by design as the need to remove additional properties is usually the result of a sender sending invalid data (and should usually be fixed at the sender, not the receiver).
It is possible to write logic to remove any additional properties prior to calling decode, but the extra processing of a value would need to be handled in user code (outside of TypeBox)
Hope this helps
S