Validation
Explore how to apply functional programming principles to validate incoming events in TypeScript applications. Understand the use of entry points for limiting module exposure, the composition of pure functions to validate and transform data, and handling errors with sum types and pattern matching for robust error management.
We'll cover the following...
Let’s start with the entrypoint.ts files. The name tells us that these are the entry points to all the code in this specific folder. Note that naming is extremely difficult and the names we picked represent our best effort.
Why do we need entry points
Now, take a look at the example of the pet. We can see functions for GET and POST. If we wanted a DELETE, we’d add a delete here that would be called by the outside code (that is, anything outside the pets folder). The first reason for entry points is that they limit the surface area of our pets module. Sadly, other parts of the application can still import from all the other files, as there’s currently no way to force a more local export in JavaScript or TypeScript. To enforce this, rely on agreement within the team or write some code to do it automatically.
In any case, this surface area limitation is especially useful for functional applications since a large number of functions combined with a large number of files and importing from anywhere can quickly create confusion. There are similar, well-known rules for structuring layered (controller-service-database) applications.
The second reason for entry points is that we need ...