HTTP Header Validation Utility
http-headers-validation is a lightweight JavaScript utility designed to validate HTTP header names and values against standard specifications. Currently at version 0.0.2, this package appears to have an infrequent release cadence given its early version and specific utility focus. Its primary differentiator is providing a simple, focused API for ensuring header compliance, which is particularly useful in backend modules or proxies that process user-provided HTTP headers. It offers three core functions: `validateHeaderName`, `validateHeaderValue`, and `validateHeader` (which combines both). The library ships with TypeScript types, supporting modern development workflows. It differentiates itself from more comprehensive HTTP parsing libraries by focusing solely on header syntax validation, rather than full request/response object manipulation.
Common errors
-
TypeError: headerUtils.validateHeaderName is not a function
cause Attempting to use `headerUtils` global or CJS `require` object with ESM named imports, or incorrect destructuring.fixIf using ESM, ensure you are using named imports: `import { validateHeaderName } from 'http-headers-validation';`. If in a browser with a script tag, ensure the `index.js` file is loaded and the `headerUtils` global is available before calling its methods. -
Error: Cannot find module 'http-headers-validation'
cause The package has not been installed or is not correctly resolved in the module path.fixRun `npm install http-headers-validation` or `yarn add http-headers-validation` in your project directory. Check your `package.json` dependencies. -
Argument of type 'null' is not assignable to parameter of type 'string'.
cause Passing `null`, `undefined`, or a non-string type to a validation function in a TypeScript project.fixEnsure all arguments passed to `validateHeaderName`, `validateHeaderValue`, or `validateHeader` are non-empty strings, as required by the function signatures and documented API.
Warnings
- gotcha The package is currently at an early version (0.0.2). While functional, API stability may not be guaranteed, and future updates could introduce breaking changes without explicit major version bumps typical of more mature libraries.
- gotcha The README explicitly shows CommonJS `require` examples and mentions a global `headerUtils` for client-side script tags, but doesn't detail official ESM module entry points beyond what's implied by shipping TypeScript types. While modern bundlers handle CJS in ESM, direct ESM imports are preferred.
- gotcha The utility validates header names and values based on 'the standart' as stated in the README, implying RFCs. However, the specific RFC (e.g., RFC 7230, RFC 9110) or any edge cases regarding internationalization (e.g., non-ASCII characters in values for specific headers) are not explicitly detailed.
Install
-
npm install http-headers-validation -
yarn add http-headers-validation -
pnpm add http-headers-validation
Imports
- validateHeaderName
import validateHeaderName from 'http-headers-validation';
import { validateHeaderName } from 'http-headers-validation'; - validateHeaderValue
const { validateHeaderValue } = require('http-headers-validation');import { validateHeaderValue } from 'http-headers-validation'; - validateHeader
const headerUtils = require('http-headers-validation'); headerUtils.validateHeaderName('...');import { validateHeader } from 'http-headers-validation';
Quickstart
import { validateHeaderName, validateHeaderValue, validateHeader } from 'http-headers-validation';
// Validate a header name
const isValidName1 = validateHeaderName('If-Unmodified-Since'); // true
const isValidName2 = validateHeaderName('Front-End-[]'); // false
console.log(`'If-Unmodified-Since' is valid: ${isValidName1}`);
console.log(`'Front-End-[]' is valid: ${isValidName2}`);
// Validate a header value
const isValidValue1 = validateHeaderValue('VQcFUFFRCBABUFhaAwQOVw=='); // true
const isValidValue2 = validateHeaderValue('\n\b'); // false
console.log(`'VQcFUFFRC...' is valid: ${isValidValue1}`);
console.log(`'\n\b' is valid: ${isValidValue2}`);
// Validate both name and value combined
const isValidHeader1 = validateHeader('Cache-Control', 'public, max-age=2000'); // true
const isValidHeader2 = validateHeader('Front-End-[]', 'backspace\bValue'); // false
console.log(`'Cache-Control: public, max-age=2000' is valid: ${isValidHeader1}`);
console.log(`'Front-End-[]: backspace\bValue' is valid: ${isValidHeader2}`);
// Example of how it might be used in a server middleware
function processHeaders(headers: Record<string, string>) {
for (const name in headers) {
const value = headers[name];
if (!validateHeader(name, value)) {
console.warn(`Invalid header detected: ${name}: ${value}`);
// Potentially throw an error or sanitize
}
}
}
processHeaders({
'Accept': 'application/json',
'Custom-Bad-Header[]': 'oops\tbad value'
});