2020-07-07
2712
#typescript
Emanuel Suriano
21392
Jul 7, 2020 โ‹… 9 min read

Dynamic type validation in TypeScript

Emanuel Suriano Hi ๐Ÿ‘‹ I build stuff with JavaScript ๐Ÿ’ป Once a month I write an article โœ๏ธ and sometimes I give talks ๐Ÿ’ฌ

Recent posts:

A Guide To The React UseReducer Hook

A guide to the React useReducer Hook

The useReducer React Hook is a good alternative to tools like Redux, Recoil, or MobX.

Ejiro Asiuwhu
Oct 10, 2024 โ‹… 13 min read
Using The Built-In SQLite Module In Node.js

Using the built-in SQLite module in Node js

Node.js v22.5.0 introduced a native SQLite module, which is is similar to what other JavaScript runtimes like Deno and Bun already have.

Amazing Enyichi Agu
Oct 10, 2024 โ‹… 12 min read
Understanding and supporting zoom behaviors on the web

Understanding and supporting zoom behaviors on the web

Understanding and supporting pinch, text, and browser zoom significantly enhances the user experience. Let’s explore a few ways to do so.

Fimber Elemuwa
Oct 9, 2024 โ‹… 7 min read
Comedy and tragedy masks symbolizing Playwright Extra's ability to extend Playwright with customizable plugins for both stealth and interactive browser automation tasks.

Playwright Extra: extending Playwright with plugins

Playwright is a popular framework for automating and testing web applications across multiple browsers in JavaScript, Python, Java, and C#. […]

Antonello Zanini
Oct 8, 2024 โ‹… 9 min read
View all posts

3 Replies to "Dynamic type validation in TypeScript"

  1. In frontend we are very good in making stuff ultra complex ๐Ÿ™

    Your “typescript isse” is not a typescript issue, because you simply dont type your service. For that you can use autogenerating their DTO into types, libs like ‘dtsgenerator’ just need the open api spec of the resource api. Problem solved. Just automate more, write less code and dont inject another dep which is only overhead.

  2. Nice post,

    I’m the author of this project [typescript-field-validation](https://www.npmjs.com/package/typescript-field-validation) which aims to solve (some of) these issues. I’ve taken a different approach, rather than validate the incoming data against an openAPI schema, simply constrain the fields you need to use as non nullable and assume the value must be the resulting type as defined in the specification. Yes it defines new syntax, but it’s just dot notation and the same array syntax we already use in Typescript, no big deal. Compared to some of the above mentioned packages, this simple tool might be all you need to wrangle those enormous auto-generated schema types into constrained types with non optional and non nullable fields. At a minimum performing these checks upfront could help clean up hundreds of null checks peppered throughout many code bases.

  3. I like the approach,

    My case is little bit different I generate all of the types from the open api.This is huge help and covers generates all of the necessary types. How ever, we have a form of which data needs to be eventually typed to RestCreateType. With dynamic validation I can take RestCreateType generate validation schema and avoid additional work and lower code dependency on generated types.

Leave a Reply