cover-image

I Built a Rule Engine

#rules
#engine
#typescript
#zod
#open-source

Andrew Vo-Nguyen

March 18, 2023

2 min read

I was working on a React Native project that required my app to give the user a convenient way to filter payloads based on certain criteria of each field. I started building a data structure, then the methods to consume the data structures. Before I knew it, I had taken on a side quest inside my app. Then I thought, I am basically building a rules engine inside my app. Why not extract it out to something a bit more abstract an potentially re-usable for others in the community. This is when I decided to build rules-engine-ts.

So in order to build an open source rules engine that others would want to use, there were a few criteria in my mind that had to be met. Roughly speaking these were it:

  1. The data structure had to be as simple as possible, yet still being able to scale.
  2. Safe guard the data structure using type safety and validations.
  3. Prioritize developer experience over optimization or code correctness.
  4. Use plain objects as opposed to classes so that the data structure could easily be serialized and deserialized and can be persisted in any type of data store.
  5. Provide as many helper functions as possible to assist the consumer with operation.
  6. Be specific with types, which meant creating lots and lots of types.
  7. Document the hell out of the work.

Overall the package took me just over 1 week to code. I was able to import it into my original React Native project and use it to build the rules engine. It was also imported into a backend Lambda to read the rules from a database and evaluate it.

The top takeaways from this experience are:

  • There is a level of satisfaction in building something that fixes a real world problem for yourself and potentially others.
  • If you're going to release something to the public, make sure you build it for the public (stringently unit test, be clear with your code, and even clearer with your documentation).
  • It's hard to know exactly what your audience wants without proper research, so be prepared to take criticism for your implementation and be willing to change it if someone makes a good case.
  • Not everything has to be a grandioso project that changes the world. Even small tools that help other developers are worth pursuing.

As of today, after being released for 5 days, there have been 536 downloads. Lets hope it can gain some traction over the next few weeks! 👊

The project is Open Source and can be viewed on GitHub & NPM.