Io-ts: Some kind of utility type for handling additional properties

Created on 30 Oct 2019  路  1Comment  路  Source: gcanti/io-ts

Sorry if this does not fit the exact issue type format.

I'm struggling to find a way to cleanly use a type that is built from an io-ts definition which respects the nature of the way io-ts works where it WILL allow additional properties, but will simply ignore them (or strip them, if exact is used). Especially when it comes to more deeply nested structures.

For example we have some database entities, which almost totally match what we define in our io-ts contracts, however they do have some additional properties. We want to be able to emit these entities as validated by io-ts, however we get pushback from the interfaces built with io-ts if those additional properties are present (despite that not being the actual behavior of io-ts itself.

Any suggestions or examples you all have that could get me a type which more closely matches how io-ts decode works?

Most helpful comment

For example we have some database entities, which almost totally match what we define in our io-ts contracts, however they do have some additional properties

To me this sounds like a good reason to have separate models (io-ts codecs and/or TS interfaces) for two separate domains: the DB one, and the "contracts" one (is it a client-facing API?)

You can always reuse "pieces" of any io-ts codec definition if you really wish to do so, e.g.

const User = t.type({ name: t.string })
const DBUser = t.type({ ...User.props, created_at: t.number })

>All comments

For example we have some database entities, which almost totally match what we define in our io-ts contracts, however they do have some additional properties

To me this sounds like a good reason to have separate models (io-ts codecs and/or TS interfaces) for two separate domains: the DB one, and the "contracts" one (is it a client-facing API?)

You can always reuse "pieces" of any io-ts codec definition if you really wish to do so, e.g.

const User = t.type({ name: t.string })
const DBUser = t.type({ ...User.props, created_at: t.number })
Was this page helpful?
0 / 5 - 0 ratings