DEV Community

Cover image for Validate javascript data type in runtime? Yes, Hello Type.
frustigor
frustigor

Posted on • Updated on

Validate javascript data type in runtime? Yes, Hello Type.

update: I have published tyshemo which contains the data type system inside, and more rationality.

Days earlier, I began to learn about Typescript. When I meet interface and generics, my mind is suddenly confused, "what's this? what's that?". As a frontend conservative weak type follower, I do not want to write such a lot of code which is not related to my bussiness logic. Later, I looked into Flow which is advocated by facebook, I feel so frustrated because it will be the trend.

However, they make our project code more complex but not resolve the data type checking in runtime. As a little jser, I am always broken by data from APIs. "TypeError: xxx property of undefined", "TypeError: xxx property of null", "xxx is not a function", "xxx has no 'slice' method"...

Is there a way to validate data type and object structure easily? Finally, I wrote one library: HelloType.

Survive from API data structure

Before you use the data from API, validate its structure by HelloType. Firstly, wrap a structure with HelloType's api method, then use the instance to assert:

If the parameter of put does not match BooksType, an Error will be thrown. In the function, after assert, you do not need to worry about any data type and structure, just use the data as you wanted.

Trace error without break program

If you just want to collect the error information and do not want to break the program process, use trace method:

It will not pay any effect on your render performance, because it is run asynchronously.

Strict mode

As your data which is posted to your server should must and should only has certain properties, use strict mode to check whether the data structure is right:

Becuase the first item of books has a additional property size, and strict mode used, assert will break the program.

There are more features, look into my github to find out your interests.

Top comments (9)

Collapse
 
stereobooster profile image
stereobooster

Checkout my post about IO validation, it shows how to make a bridge between static types (Flow or TypeScript) and runtime type checking.

Collapse
 
frustigor profile image
frustigor

That's great idea. However, it seems to be one thing no matter in Flow or in Typescript. For example, you should write a function assertPerson in which you must check its input and output with Flow syntax:

const assertPerson = (val: mixed): Person => is(val, PersonInterface, "Person")

It seems to make it metaphysics if we follow Flow.

And your example for io-ts, it use t.type to generate a PersonInterface and use PersonInterface.decode to check data type, which is like what HelloType did.

So I think, if I want to, I can create a bridge too😊

Collapse
 
stereobooster profile image
stereobooster

For example, you should write a function assertPerson in which you must check its input and output with Flow syntax

Not sure what your concern here, but if you worry that you will need to use flow syntax and it will be incompatible with good old JS, this is not the case. See source code of sarcastic it is absolutely valid JS, but at the same time, it is type-checked by Flow.

So I think, if I want to, I can create a bridge too

For sure. I think it will be a cool feature. Even cooler if you can get support for both TypeScript and Flow. πŸ‘

Thread Thread
 
frustigor profile image
frustigor

for the first point. I do not think flow will help you to check data type during run time, in fact, the checking is done by your own code logic, not by flow static system.

And in my opinion, Flow or Typescript make a way to access static data type, but weaken js flexibility, and make project code library bigger and bigger. It is for a big project with different level of developers, not for small projects or which developed by experienced developers.

Thread Thread
 
stereobooster profile image
stereobooster

Checkout my post about IO validation, it shows how to make a bridge between static types (Flow or TypeScript) and runtime type checking.

If you wonder why one need types, read my post on how types help to prevent bugs here

Collapse
 
entrptaher profile image
Md Abu Taher

...Are/Were you a python developer? :D Or did you perhaps get some inspiration from python syntax?

Interesting package. Any chance it will have similar api to mocha/chai or such unit testing frameworks? Or something that will be used to check type but not throw error.

Collapse
 
frustigor profile image
frustigor

Thanks for your attention.

Sorry, I am a pure js developer even though I like python syntax too.
The api is similar to chai which is my favorite assert library.
I think a developer who want to use HelloType may just need to know where error happens without

irrupting any business code logic. So finally, I provide four way to check data.

If you have any question or idea, please let me know on github.

Collapse
 
revskill10 profile image
Truong Hoang Dung

I would say no to static typing. Because i just want my freedom to hack on my things or someone else' things without any restriction.

Sorry, but a great static typing system couldn't save you if your logic is wrong.

Collapse
 
likern profile image
Victor Malov

It looks very promising and interesting. That's exactly how I fought runtime Json validation problem should be solved.

And it's very similar to Python Pydantic validation library. For my terms, the best lib for Python validation.

But, you need aggressively to promote your library, otherwise no one will care. Unfortunately that's true and it's not enough to just write good library.

Please, go to conferences, go as author to show your approach to solve problems. Use English as much as possible (excluding when present in China ;))

I hope this library will not die and will get attention.