Schema Validator | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: Schema Validation
Instructor: Artem Bondar
Lesson Summary
This lesson focuses on implementing schema validation using the AGV library in a TypeScript project. Below are the key steps covered in the video: Steps to Implement Schema Validation Install AGV: Open the AGV schema validator documentation. Run the command in the terminal: npm install AGV --save-dev Verify installation in package.json . Import and Create AGV Instance: Import AGV in your TypeScript file: import AGV from 'AGV'; Create a new instance of AGV: const validator = new AGV(); Compile Schema: Use AGV.compile to compile the schema: const validate = validator.compile(schema, { allErrors: true }); This allows validation to continue after encountering the first error. Validate JSON Object: Pass the JSON object (response body) to the validation function: const valid = validate(responseBody); If validation fails, throw a new error with detailed messages. Error Handling Implement meaningful error messages by throwing an error that includes: The message: schema validation failed Details of validation errors using JSON.stringify(validate.errors) The actual response body for analysis. In summary, this lesson demonstrates how to effectively use AGV for schema validation, handle errors, and improve the reporting of validation results.
Video Transcript
So we continue working with a function validate schema and in this lesson we will make this function actually doing a schema validation. Alright, so let's jump into it. How we're gonna do a schema validation? For that we will need a package open in the browser and call AGV. So this is AGV schema validator. Open the first link. So this is the library or package that we gonna use. So click on the guide and here on getting started. This is how we will install it. So first step we need to npm install AGV and add this dependency to our project. Going back here, open the terminal and paste this command over here. And to add this dependency into the package.json I add the flag dash dash save dev. So this dependency will be part of our projects. Hit enter and that's it. We can open package.json. We can see AGV was installed successfully. This was the step one. Step two, going back to documentation, then we need to basically use it. Here is how to use it. We need to call new AGV instance inside of our TS file. First we need to import it. So this is the schema validator. Let's import AGV. Import AGV from AGV. Like this. And then we can call a constructor and create a new instance of AGV within the function. Now we'll do it somewhere here just above the function. And it should be imported as a capital case like this. Import AGV from AGV. So now we can use AGV within the function. So how are we going to use it? Scrolling down and this is the process. So we call AGV.compile and first step we need to compile the schema to have a method validate. So this is going to be a function that will perform the actual validation. So I copy this step going back over here and instead of console.log and just replace it with this. So now the schema will become arguments for the AGV. compile. And by the way one more thing I want to add this little option over here all errors to true. Like this. So what this flag does is when AGV will perform validation of the schema it will continue validation of the schema even after the first error. For example your schema may have several errors during the validation and if the first error occurs it will just accumulate all the errors within the schema and will print a single output. This is what this flag does. So we have a validate. Now the next step we need to validate the schema against the actual JSON object or in other words validate the object against the schema. So I copy these two lines and also copy it, paste it over here in the project like this. So data have to be the JSON object from our API, the response body. So I will add a new property response body right here which type of the object. And going back to the test and we'll add the response here as the argument into this function. Okay so we make API request to get the text and the response object we provide as the argument to validate the schema. Then inside of the schema we need to pass this response body over here to validate the response body against the schema which is get-text-schema.json. And after that we should validate. If the validation is invalid, if valid is false then print to the console with all validation errors. Well that's it so let's try to run it and the first test it should pass because it should match our schema if we did everything right. Yes the test passed. Now let's mess around a little bit with our schema and change something. For example let's assume that we expect that tags would be not array. Let's say we expect it to be an object like this. So according to our schema the response, the text property have to be an object. And let's run this test one more time to see what's gonna happen. So running the test and we see in the console log a validation failure that message must be an object, the parameter type of object and the instance path is text. So the message that brings the EJV to the console, well you need to get used to it because it's kind of unusual, different properties and so on. But you can start with reading okay must be an object so something is expected as an object. This is a type and what property is a text. Okay so what else? Let's change this back to the array and let's change something different. For example let's assume that our return tag property should be not tag, not text, it should be a tag. And let's run this test one more time. Running it and we have a new message must be required property tag. So schema validator looked in our JSON and JSON schema and compared that hey where is the property missing property tag that is required by our schema. So we got text but instead the tag was expected. So everything is working this is how schema validation works. And now one final tweak here so let me fix it back to tags. Final tweak over here instead of printing this to the console validate errors let's make it meaningful error messaging that will be attached to the report because right now it's just printing to the console. So let's make it nice. So instead of the console log we will throw a new error. So throw new error that's the error body and what message we would like to see. For example something like this schema validation failed and what schema I will take this file name so it will be meaningful for us which file actually we were looking for which schema we were comparing against. Okay then on the next line what we want to print next. So backticks and let's print the details of this validation. So I type JSON stringify and then the error messages so it was what validate.errors like this and then let's format it nicely with null and then for tabbing. Okay this is done then let's make dash n dash n and from the next line we would like to print probably the actual response just for analysis that we can see one by one what was the actual response and what the error message of the schema. So the message would be actual response body and then from the next line plus we would add the response body like this JSON stringify and we will print response body comma null null and comma for. Alright I think it looks good let's test it so running the get text so it passed now let's change something in our schema let's say from array to object like this run this test and the test fail let's open their report open in default browser looking in the report and here we go everything is working so error schema validation failed the details about what is failed that tags have to be an object and then we see that actual response tags is array and this is why it failed so error handling is implemented correctly now we see a schema validation in the report so that's it guys you see so simple so we just added a couple of lines of code that makes a validation of the schema so in this lesson we will make few more improvements in this process and instead of calling the validate schema directly from this function we will integrate it into a custom assertion to keep the same formatting in the same styling of our test scripting alright so see you in the next lesson