ForbesLindesay / funtypes

Runtime validation for static types
MIT License
29 stars 4 forks source link

Codec Type #16

Open ForbesLindesay opened 3 years ago

ForbesLindesay commented 3 years ago

The types in io-ts are actually "Codecs": https://github.com/gcanti/io-ts/blob/master/index.md#the-idea

What this means is that they support having different runtime types on the "serialized" end (where they are typically of type unknown in TypeScript) and the "parsed" end. This can be useful for:

We're actually in a pretty good place to support this. Our primative Runtype has a validate method with the perfect signature to act as parse.

Requested in https://github.com/pelotom/runtypes/issues/56

ForbesLindesay commented 3 years ago

Draft implementation: #17

ForbesLindesay commented 3 years ago

A side effect of this change is that we are also effectively deep cloning everything, which takes care of https://github.com/pelotom/runtypes/issues/132 and https://github.com/pelotom/runtypes/issues/24 (remove unknown keys from validated object). It may be worth looking into supporting a fast path that doesn't clone objects if none of the validations change anything.