Additional Data Formats | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: Schema Validation
Instructor: Artem Bondar
Lesson Summary
This lesson covers the schema validation feature in the AJV library, specifically focusing on adding custom formats for more granular validation. Key Steps to Add Custom Formats Install AJV Formats: Search for "AJV formats" and install the package using npm. npm install ajv-formats Import the Package: Add the import statement in your project: import addFormats from 'ajv-formats'; Integrate with AJV: Call the method and pass the AJV instance: addFormats(ajv); Using Custom Formats After adding formats, you can validate specific types such as: date-time email URI IP For example, to validate the createdAt and updatedAt properties as date-time , you would modify your schema: "createdAt": { "type": "string", "format": "date-time" }, "updatedAt": { "type": "string", "format": "date-time" } Trade-offs Be aware that if you regenerate your schema using json.js , any custom formats will be lost. To automate the addition of formats, implement a post-processing function that loops through properties and adds the necessary formats. In summary, the AJV library allows for enhanced schema validation by adding formats like date-time and email . For automation, custom code is required to maintain these formats after schema generation.
Video Transcript
All right, let me show you one more cool feature about the schema validation is adding additional formats. So far, we have just the general types such as string, number, array, and so on. But we can add additional custom formats to make the schema validation more granular. So let's jump into it. Go to the Google and search for AJV formats. We're looking for this one. So this is additional package to the AJV library that adds this granularity with the formats. So first of all, we need to install it. We open npm. We need the dependency, copy it here, and adding to the project. Go into terminal, paste right here, and save def, and install. It was added to the package.json. Sounds good. The next step. The next step, we need to add this import, import add formats from AJV formats. Going back and adding to schema validator right here. The last step, we need just to add this method and pass instance of AJV as an argument like this, and that's it. We don't need to set up anything else. Now, how to use it? So if I scroll a little bit down, look, there are additional formats that can be used. For example, date and time, URI, e-mail, IP, JSON, byte, password, binary, and so on. Specifically, useful type would be definitely date and time. Look at our response for the articles. So this is the response for the articles. We have property created at and updated at. This is a date and time format, and it would be nice if we would validate this, that this is date and time rather than just a string. Because right now inside of our schema, we validate that create at, at is right here, type string and updated it at string. How to add a new format? So additionally, next to the type of string, you just type this new property, format of, what is it? Go back, it called date time like this. And the same thing, we need to add to updated at like this. And I think that's it. Let's try to run this test for get the articles. Running the test, and assertion pass. So let me show you that it is actually work. For example, we change date time to, for example, email like this. And I run the test one more time. And now, assertion failed with this error message that must match format email. And right now, our schema expecting that this is not just any random string, it should be in the format of email. So that's very cool and powerful feature. But there is a little kind of trade-off. Because look, we right now generate schema using json.js package, right? And if we, for example, want to update this schema, and if we added those formats and so on, the schema will be just wiped off and all our formats will be deleted. Unfortunately, json.json not able to add those formats. If you need to automatically add the formats like that, and if for some of your responses is absolutely crucial, then you need to add some custom code over here. So where we generate a new schema after this step, you need to add some extra function that will do post-processing of your generated schema and will add additional formats. So for example, you can create some, I don't know, loop that will loop through the objects of your properties. And if the property will find, for example, created add, add a new property, format daytime, something like this. It should not be very difficult. Some of just basic JavaScript programming, that kind of stuff. You can create the additional function inside the schema validator method that will do just that. So you just need to be aware of this trade-off if you want to completely automate this process. But if you need it, that's how you would do it. All right, that's it guys. So quick summary. So the format of the AGV is a cool thing that you can add extra formats to your schema such as daytime, email, and so on. To do that, you need to add extra property of the format and provide the format that you expect. If you want to make it automatic, you need to add some kind of processing after the schema generated with your some custom code adding those formats. All right, that's it guys and see you in the next lesson.