What is har-validator?
The har-validator npm package is a library designed to validate HTTP Archive (HAR) files. HAR files are JSON-formatted archives that track all the logging of a web browser's interaction with a site. This package ensures that HAR files adhere to the specification and structure expected, making it useful for developers working with web performance and debugging, API testing, and network monitoring.
What are har-validator's main functionalities?
HAR file validation
This feature allows developers to validate HAR files against the HAR format specification. The code sample demonstrates how to use the har-validator package to validate a HAR file. If the file is valid, it logs a success message; otherwise, it catches and logs the validation error.
const HARValidator = require('har-validator');
async function validateHAR(harData) {
try {
const valid = await HARValidator.har(harData);
console.log('HAR is valid:', valid);
} catch (err) {
console.error('HAR validation error:', err.message);
}
}
Other packages similar to har-validator
ajv
Ajv is a fast JSON schema validator. While not specifically designed for HAR files, it can be used to validate any JSON structure against a schema, making it a versatile alternative for general JSON validation tasks. Compared to har-validator, Ajv requires you to define or have access to the JSON schema, offering more flexibility at the cost of requiring more setup for HAR validation.
tv4
Tiny Validator (tv4) is another JSON schema validator that supports asynchronous validation and v4 of the JSON Schema standard. Similar to Ajv, it can be used for validating various JSON formats, including HAR files, if the appropriate schema is provided. It's a bit simpler and smaller than Ajv, making it a good choice for projects with less complex validation needs or tighter size constraints.
HAR Validator
Extremely fast HTTP Archive (HAR) validator using JSON Schema.
Installation
install from source or through npm:
npm install --global har-validator
Usage
CLI
Usage: har-validator <file ...>
Options:
-h, --help output usage information
-V, --version output the version number
-s, --schema [request | response] validate sub schema
Example
har-validator ./path/to/har.json
har-validator --schema ./path/to/request.json
Code
Include the module in your code and use directly or with any of the utility methods to validate sub-schemas:
method | description |
---|
cache | validates a cache object |
cacheEntry | validates a [beforeRequest |
content | validates a content object |
cookie | validates a cookie object |
creator | validates a creator object |
entry | validates a entry object |
log | validates a log object |
page | validates a page object |
pageTimings | validates a pageTimings object |
postData | validates a postData object |
record | validates a record object |
request | validates a request object |
response | validates a response object |
timings | validates a timings object |
Example
var HAR = require('har.json');
var validate = require('har-validator');
validate(HAR, function (err, valid) {
if (err) console.log(err)
if (valid) {
console.log('horray!');
}
});
var request = require('request.json');
var validate = require('har-validator');
validate(request, function (err, valid) {
if (err) console.log(err)
if (valid) {
console.log('horray!');
}
});
License
Licensed under The MIT License.