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:

Rxjs Adoption Guide: Overview, Examples, And Alternatives

RxJS adoption guide: Overview, examples, and alternatives

Get to know RxJS features, benefits, and more to help you understand what it is, how it works, and why you should use it.

Emmanuel Odioko
Jul 26, 2024 โ‹… 13 min read
Decoupling Monoliths Into Microservices With Feature Flags

Decoupling monoliths into microservices with feature flags

Explore how to effectively break down a monolithic application into microservices using feature flags and Flagsmith.

Kayode Adeniyi
Jul 25, 2024 โ‹… 10 min read
Lots of multi-colored blue and purplish rectangles.

Animating dialog and popover elements with CSS @starting-style

Native dialog and popover elements have their own well-defined roles in modern-day frontend web development. Dialog elements are known to […]

Rahul Chhodde
Jul 24, 2024 โ‹… 10 min read
Using Llama Index To Add Personal Data To Large Language Models

Using LlamaIndex to add personal data to LLMs

LlamaIndex provides tools for ingesting, processing, and implementing complex query workflows that combine data access with LLM prompting.

Ukeje Goodness
Jul 23, 2024 โ‹… 5 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