Validating Protocol Buffers in Golang gRPC Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is Mario welcome to n system designning Go video in today's episode I'm sharing with you how to add validations to protocol buffers for adding validations to protocol buffers we're going to be using a new plugin called Proto validate in this case we're going to be adding the validations directly to the protocol buffer itself we're not going to be adding validations to the generated code what we're trying to do is actually generate the validations through the plug-in itself this plugin is part of the buff ecosystem that I showed you in the beginning of the Series so you can easily generate code using this validator and this plugin this new generator uses Google's common expression language or cell to Define rules Associated to each field in the protocol buffer it could be overwhelming in the beginning but I assure you is really powerful it's similar to what we do with other packages in go when you use a stru tax and you add the validations directly I will show you how that works as usual the link to the code and example that I'm going to be showing you is in the description of this video so feel free to check that out so let's get started what I want to be doing first is update buff. jaml which is the configuration for buff itself so if I open buff. jaml and I add a new dependency called buff build both build Proto validate what this is going to be doing is adding the dependency that we need and therefore it could be used when we are generating code next we need to run buff Mod update this will create a new lock file which is right here we didn't have this one before what this includes is sort of like a gsum equivalent that includes the dependencies that we're using for generating code in this case the new generator next we need to go and modify the configuration that we use for buff gen in this case under buff. gen. Jamel what we have to do exactly is add this new plugin and specifically make a few changes that are related to the configuration for go for doing that we go under the manage section we add another section called go package prefix we add a default and accept buff build buff build Proto validate what this indicates is that now because we are going to be importing these files we need to explicitly exclude them in the generated code this is what this instruction is doing we're going to be making another changes in the previous configuration that we had in the protocol buffer so let me show you for this we need to go to our protocol buffers under user V1 user Proto we're going to be removing the option of go package we don't need that anymore because it's going to be generated automatically anyway but what we need to do is add this import buff validate validate dopro then after doing that we can actually use Google's cell to define the rules that we are going to be using specifically to three Fields full name birth year and salary let me show you this is where Google sales documentation comes into place because that is literally what we have to use to specify the rules in our field so for example for the full name what I'm going to be doing is I'm going to indicate that the minimum length is going to be one character with this instruction I indicate to Google cell that now you need to validate this field to be at least one character so when we run the validate method in this protocol buffer type it will fail if the string for the full name is empty let's take a look at Birth year for this one I want to be at least great greater than 1900 for that we do something similar we add both validate field and in this case we use the N64 greater than and we decate the 1900 next I want to add the validation to the salary field for this case I want to indicate that it has to be greater than zero or not empty similarly we go and add the validation we use a uint 32 and we indicate it has to be greater than zero so with this new configuration what the Proto validate generator is going to do is going to generate rules that are going to be enforcing the rules that we Define in here let me show you next what we have to do is to generate the code to do that we call Buff generate this is going to generate the new code that is coming from the protocol buffer with the new validation rules in order to use this as an example we need to implement and add new package because I'm going to be using go for this example the package is called both build pro validate go you go get it it will update your go mod and your go some and then we can create a new example to call out the validation itself next in order to use the package that we just got we can create a new folder cor examples validate under that examples validate we're going to be creating a new file to represent the validation and new main. go file this is going to be pretty straightforward we're going toine a package main we're going to be Define the function Main and more importantly we're going to be defining a user which is coming from a user PB generated protocol buffer the user PB will be the package that we are generating that we are defining in our protocol buffers then we can have a validation so for that we have a Proto validate which is the module that we just got a while ago so Proto validate will be used to validate the protocol buffer type in this case will be user so we do a v error for validate which in this case this initializes the validation the pro validate module itself if there is an error we're going to do a typical you know log fatal in use to exit error Pro validate and next we can actually start using the validation that we defined in the protocol buffer so we validate the user that we Define above and there is an error we are going to print out there was an error and the error itself otherwise we just going to say everything okay if we run this it will fail under this section because we're missing some required file values let me show you you will notice that now we have the two validations that we had before the one for the full name and the one for the birth year and you will maybe you you probably are thinking where is the one for the salary well that one is because it's an optional argument so that's kind of ignore because optional is not required therefore it doesn't matter but if we go back and change it and let me go and change it so you can see how it actually works I want to say that the minimum value will be one if I go on generate so what we do is salary and we put zero we add the full name we are the birth year we run this again you will notice that now is failing because it has to be greater than one which is the rule that we added before let's go back and change it so we will'll see that now and everything is okay and that's it this is how you add validation rules to protocol offers I hope you find it useful thank you for watching I will talk to you next time until then keep it up and stay safe see you
Info
Channel: Mario Carrion
Views: 2,124
Rating: undefined out of 5
Keywords: golang, go lang, golang tutorial, go lang tutorial, golang beginners, golang for beginners, learn golang, grpc deadline, grpc deadlines, grpc timeout, grpc timeouts, grpc tutorial, grpc, grpc golang, grpc go lang, golang protocol buffer, protocol buffer validation, protobuf validation
Id: j2ss8lTflQI
Channel Id: undefined
Length: 7min 5sec (425 seconds)
Published: Mon Nov 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.