Nest.js Crash Course #6 – Pipes

In this series you’ll learn how to make a ninja-themed API with Nest.js, which is a node.js framework for making server side applications.

⭐ Thanks to Marius for making this course. Subscribe to his channel here –

?‍? Nest.js docs –
?‍? VS Code –

In this video we're going to talk about Pipes in sjs pipes really have two core Use cases as you can see here Transformation and validation so far in The series we've been building this Ninjas API a crud API that allows you to Manage an Army of Ninjas there's a Couple places here that could still Benefit from the addition of pipes for Example notice that we're constantly Transforming Our IDs to a number here What if we could just do that Automatically using a pipe so there's a Couple built-in pipes that you can use One of them is the parse in pipe you can See we have Parts in pipe here but There's a couple other ones like for Example parse array pipe parse bullpipe So remember that our URL parameters are Coming in as a string so there's a lot Of situations where you might want to Automatically transform those parameters Into an actual you know typescript or JavaScript type so a simple example in Our case is to Simply do a parseint pipe Then we should be able to change this This To a number and our code is back to Being happy again right so this is now Coming in as a number and as you can see If we request ninja slash one it's Correctly getting us back that ID that Ninja with the ID of one so that's a Simple example of one of the built-in

Pipes in sjs you can create custom pipes Again the two purposes for a pipes is to Either transform data or validate data So if you wanted to do any custom Transformations to the data that's Coming in even before it gets to your Controller and service methods that's What pipes are for now pipes come really In handy when it comes to validating a Request body if you remember in our Earlier videos we tried to create a Ninja with a the wrong weapon we use the Stick weapon which is not really part of What we expect it would be great if we Can validate that a ninja has to have You know one of the specific weapons or Maybe that their name has to be a Certain length so what we're going to do Is within our create ninja dto we're Going to install something new to our Application we're gonna install class Validator and class Transformer now if You're not familiar with class validator You can kind of think of it as just a Couple extra set of decorators that you Can add to your classes so for example In their example here they have a string That has to have a length between 10 and 20. they have a number here that has a Minute Max they want a text here that Has to have hello inside it it can do Patterns like emails dates phone numbers So there's a bunch of custom decorators Here that you can take advantage of for

Your own application in our case let's Try something simple perhaps a Min Length class validator so let's maybe Provide this with uh three so our the Name when you create a new Ninja has to Be at least three letters now right now All we're doing is providing additional Metadata to this class in order for an Sjs to take advantage of these Decorators we need to add a specific Pipe called the validation pipe so in Our our post requests here in our body Decorator we're going to add the Validation pipe this is another one of Those built-in pipes that necess has Which specifically looks at your dtos Your objects and checks to see what are These annotations that you have on there And then Compares it with the object That's coming in so if we go back to Testing our application post 3000 slash Ninjas and let's provide a request body That has you know let's test it out First with the long name this should Work because it's it's more than three Characters if we hit send we'll get back This should work right but if we were to Reduce this to just two characters And hit send we'll get back a 400 bad Request so it automatically assumes that If you throw if an exception is thrown During the validation phase in the Validation pipe respond with a 400 and You can see that it also tells you in

The response what happened it says that Name must be longer than or equal to Three characters and this is something That client applications can take Advantage of for example maybe you want To display that error message on a form Right as you're adding ninjas you say That your ninja has to have a longer Name alright so we can see that that's Working but we also said that it would Be great if we can validate the type of Weapon that they're using right it has To be either the stars or the nunchucks So let's extend this a little bit more And add a perhaps an is enum Decorator here and let's say that it Needs to be stars or nunchucks and then Know that you can even provide Uh custom Messages for when it fails so let's say Use correct Weapon and let's test this out so the Name here should pass the length Validation but now when we send this it Should say that again 400 bad requests Use correct weapon right so this is a Pretty useful pattern to know about and Know that you can use other types of Validators you don't have to use class Validator for example you know you can Use Joy if you ever use that before the Reason that I recommend class validator Is because it just feels cohesive to the Rest of the framework right everything In SGS is really classes with

Annotations with decorators so that's Pipes in a nutshell we're almost done With our crash course here there's one More topic that I'd like to cover before We finish which is guards that's how you Protect your routes so make sure to head On over to the next video

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *