5

I'm wondering if there are any tools or techniques that are being used to do low level validation on object data when using TypeScript. An example would be a JSON body of a POST request on a HTTP service. Typically I've created an interface for the expected data and then cast the data to that interface but I'm aware that this is superficial.

Example:

router.route('/supercres')
    .get((req, res, next) => {
        const typedBody = <SuperCresBody>req.body;
    })

interface SuperCresBody {
    name: string,
    yoyo: boolean,
}

The problem with enforcing the interface is that TypeScript is just a compile-time concept and does not enforce anything at runtime. Knowing this I'm curious if anybody has found a clever way without a lot of boilerplate or tooling in order to pull off type checking at runtime for these things without having to repeat the interface contract as a set of imperative checks as a validation step.

On a somewhat related note, in the most recent episode of Functional Geekery, Matthias Felleisen explains a similar need in Typed Racket near the tail end of the episode.

Related Reading:

jpierson
  • 16,435
  • 14
  • 105
  • 149
  • 2
    Kind of related... code generation can be useful for this. I have been working on [this](https://github.com/dsherret/ts-type-info) which allows you to get information from code, modify it, then write out new code (still needs a lot of work... it's a big task). Then there is [this](https://github.com/dsherret/server-bridge) which allows you to generate client-side code to access the server from the server-side code. It's not well tested at the moment though and kind of experimental... I wrote it really fast to demonstrate the concept. – David Sherret Apr 01 '16 at 15:42
  • The problem with runtime checking is that it will cost you CPU resource to perform. It is possible to do though. – BotMaster Apr 03 '16 at 00:40
  • @BotMaster, I'm not looking to runtime type check everything but mostly data that comes from the boundaries of the application (ex. HTTP requests, file I/O). I don't mind doing it manually which is what would have to be done anyway but it would be nice to leverage the type information afforded by type annotations in TypeScript. – jpierson Apr 04 '16 at 14:57
  • Please, see my answer to [this question](http://stackoverflow.com/questions/33800497/check-if-an-object-implements-an-interface-at-runtime-with-typescript/39146325#39146325). Now it's possible to do runtime checks against interfaces in TypeScript. – pcan Aug 25 '16 at 13:20

1 Answers1

3

Check these 2 equivalent projects:

I use the latest, it works flawlessly, but the former looks awesome as well.

In both cases, you build a validator that you use to:

  • Check object at runtime
  • Extract a static type that can be used at compile time

In your specific (api) use case, you can also use some tooling to extract TS types form the JSON or XML you produce

You can also generate typings easily from JSON data:

http://json2ts.com/

Benoit B.
  • 3,561
  • 24
  • 19