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:

Exploring Nushell, A Rust Powered, Cross Platform Shell

Exploring Nushell, a Rust-powered, cross-platform shell

Nushell is a modern, performant, extensible shell built with Rust. Explore its pros, cons, and how to install and get started with it.

Oduah Chigozie
Apr 23, 2024 โ‹… 6 min read
Exploring Zed, A Newly Open Source Code Editor Written In Rust

Exploring Zed, an open source code editor written in Rust

The Zed code editor sets itself apart with its lightning-fast performance and cutting-edge collaborative features.

Nefe Emadamerho-Atori
Apr 22, 2024 โ‹… 7 min read
Implementing Infinite Scroll In Next Js With Server Actions

Implementing infinite scroll in Next.js with Server Actions

Infinite scrolling in Next.js no longer requires external libraries โ€” Server Actions let us fetch initial data directly on the server.

Rahul Chhodde
Apr 19, 2024 โ‹… 10 min read
Integrating Django Templates With React For Dynamic Webpages

Integrating Django templates with React for dynamic webpages

Create a dynamic demo blog site using Django and React to demonstrate Djangoโ€™s server-side functionalities and Reactโ€™s interactive UI.

Kayode Adeniyi
Apr 18, 2024 โ‹… 7 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