3 Secrets to Get Your Followers to Engage With You Every time

So you are on your way to becoming a social media influencer, you are working hard to create amazing content your follower count is growing everyday, and you’re even having fun doing it! But there is…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Type Safe Models in Node

Type Safe Models in Node

This is the first of an ongoing series on how I build testable, input-validated systems in Node. This post focuses on the first building block, validated models.

As a result, from early days I developed a kind of paranoia for including runtime-checks and assertions in my code as a way of ensuring that everything if something unexpected happened, the code would explode in a useful way, rather than in a confusing way, or worse, just silently corrupt data.

You can add testing (or just raw self-confidence) to try to avoid these checks, but in my experience some level of runtime checking is more useful than it is expensive.

A simple check would look something like this:

Or you can make it a bit more concise with Node assert.

Of course this only really works for non-object parameters. Asserting all of the properties of an object parameter quickly becomes a mess.

So I came up with a solution that seemed to work pretty well without being overly verbose. I’d create a class that validates it’s members before construction, and then I could pass instances of that class around and just assert that the argument was an instance of that class.

Not perfect, technically you could still mutate the class outside of the constructor, but it was good enough for my purposes in a pre-Typescript world.

Some features of this approach:

For me, in my implementations, and this blog going forward, a model is a (mostly) immutable instance of a class that validates its member variables at construction, and can be assumed to only contain valid data from that point forward.

This allows you to pass model instances from service to service without re-checking all of the internal state, and serves as a centralised place to put all the validation logic associated with a given concept. In my designs, models are created anytime data crosses a system boundary (API to UI, or UI to API, or API to DB, etc), and this way you can be sure that everything is expecting the same data structure with the same constraints.

Creating new instances of classes at boundaries like this does have a computational cost, but that’s usually minimal, and I’ll talk later about what to do when it isn’t.

For those that haven’t made the transition, my biggest points would be:

So beyond just the benefits of Typescript as whole, Typescript also offered the opportunity to rethink and refine my validated model approach I had built in Javascript above.

Of course the gotcha with Typescript is that all of that fancy type-safety stuff completely evaporates at runtime, by design. That’s not to say it isn’t useful in finding and fixing bugs during development, but it’s not helping you in production. My non-typescript approach had been trying to address both, making development faster with better errors, and making production safer with validation. So switching entirely to Typescript types and abandoning runtime checks was not an option for me.

At the same time, I didn’t want to duplicate my work by implementing both runtime and compile time type-checks everywhere. This seems like a waste.

So, as with all good engineering solutions, I settled on a compromise. I’d validate at runtime within my models, and let Typescript do the the rest of the work everywhere else. Sure that’s not perfect, but I good enough was good enough.

There are a number of libraries and options for translating Typescript types to runtime checks, but I didn’t really like any of them. They seemed like a lot of verbosity and work, basically re-implementing a runtime version of Typescript for every model.

To make this easier, I created a base class that contains the validation logic that I extend for every instance of every model in my system. The core of the base class looks like this:

This does a few things:

An example implementation of this class would look like:

With this class defined, you can just create an instance of it, and the omit asserting the types of function parameters

And that’s it!

Add a comment

Related posts:

Pink Rose Picture Writing Prompt

Writing prompts are great ways to help your creative juices get flowing. It might be just a thought or a word that inspires your imagination. A writing prompt can be a sentence, a picture, a memory…

Manfaat Menggunakan Aplikasi Blockpost Bagi Para Trader Cryptocurrency

Para trader kripto tentunya membutuhkan banyak informasi untuk mendukung tradingnya agar sukses dan peluang mendapatkan penghasilan tinggi. Seiring perkembangan teknologi semakin banyak aplikasi…

How to Find the Right Point of View Character for Your Story

In the very few opening moments there’s a kid, maybe nine years old, wearing jeans and a beanie. The kid is AJ. The visual shifts to a drag queen in a huge red wig. Anyone who’s got any cultural…