HAR JSON Schemas
The `har-schema` package provides the official JSON Schema definitions for the HTTP Archive (HAR) format, enabling robust validation of HAR files. It encompasses schemas for all HAR entities, including `log`, `pages`, `entries`, `requests`, `responses`, and more. The current stable version, 2.0.0 (released in 2017), migrated its schemas to conform with JSON-Schema Draft-06, a significant breaking change from prior versions. This package is foundational for tools that process or validate HAR data, acting as a data source for schema definitions rather than an executable library. Its release cadence is effectively dormant, with no new feature development since 2017, reflecting the stability of the HAR 1.2 specification it implements. It differentiates itself by being the direct, standard representation of the HAR spec in JSON Schema, compatible with any generic JSON Schema validation library.
Common errors
-
Cannot find module 'har-schema'
cause The package was not installed or is not correctly resolved by your module system.fixRun `npm install har-schema` or `yarn add har-schema` in your project directory. -
Schema is invalid: data.schema should be object
cause This error typically indicates that your JSON Schema validator is configured for an incompatible JSON Schema draft or that the schema object itself is malformed. `har-schema` v2.x uses Draft-06.fixEnsure your validator is set up to correctly parse Draft-06 schemas. For AJV, initialize with `new Ajv()` which defaults to Draft-07 (compatible), or explicitly `new Ajv({ allErrors: true, messages: true, schemaId: 'id' })` for Draft-06 specific handling if needed. -
Property 'har' does not exist on type 'typeof import("har-schema")'.cause This TypeScript error occurs when trying to access `harSchema.har` (or similar) if TypeScript cannot correctly infer the structure of the default export.fixEnsure your `tsconfig.json` has `"esModuleInterop": true` and `"allowSyntheticDefaultImports": true`. If using `require`, ensure you're accessing `require('har-schema').har`.
Warnings
- breaking Version 2.0.0 migrated all schemas to JSON-Schema Draft-06. If you were using `har-schema` with a validator configured for an older draft (e.g., Draft-04 or Draft-05), you must update your validator's configuration to support Draft-06 or newer, or stick to `har-schema` v1.x.
- gotcha The `har-schema` package is effectively abandoned since its last update in 2017. While the HAR 1.2 specification is stable, there will be no updates for potential future HAR spec versions, new JSON Schema drafts, or bug fixes. Relying on this package for critical new development should be done with caution.
- gotcha This package *only* provides the JSON Schema definitions. It does not include a HAR parser, validator, or any other utility functions. You must use a separate JSON Schema validation library (like `ajv` or `har-validator`) to utilize these schemas.
Install
-
npm install har-schema -
yarn add har-schema -
pnpm add har-schema
Imports
- harSchema
import { har } from 'har-schema';import harSchemas from 'har-schema'; const harSchema = harSchemas.har;
- allSchemas
const allSchemas = require('har-schema').default;import allSchemas from 'har-schema';
- RequestSchema
import { RequestSchema } from 'har-schema';import allSchemas from 'har-schema'; const requestSchema = allSchemas.request;
Quickstart
import allSchemas from 'har-schema';
import Ajv from 'ajv';
const ajv = new Ajv({ allErrors: true, messages: true }); // Initialize AJV validator
const validate = ajv.compile(allSchemas.har); // Compile the main HAR schema
const minimalHar = {
log: {
version: '1.2',
creator: {
name: 'HAR Schema Example',
version: '1.0'
},
entries: []
}
};
const invalidHar = {
log: {
version: '1.2',
creator: {
name: 'HAR Schema Example'
} // Missing 'version' in creator
}
};
// Validate a minimal, valid HAR object
const isValidMinimal = validate(minimalHar);
console.log('Minimal HAR is valid:', isValidMinimal); // Expected: true
// Validate an invalid HAR object
const isValidInvalid = validate(invalidHar);
console.log('Invalid HAR is valid:', isValidInvalid); // Expected: false
if (!isValidInvalid) {
console.log('Validation errors:', ajv.errors);
// Expected output will show 'creator' missing 'version' and 'entries' missing
}
/* Example of accessing an individual schema */
const validateRequest = ajv.compile(allSchemas.request);
const request = {
method: 'GET',
url: 'http://example.com',
httpVersion: 'HTTP/1.1',
headers: [],
queryString: [],
cookies: [],
headersSize: -1,
bodySize: -1
};
console.log('Request schema validation:', validateRequest(request)); // Expected: true