HTTP Header Validation Utility

0.0.2 · active · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and use the `validateHeaderName`, `validateHeaderValue`, and `validateHeader` functions to check the conformance of HTTP header strings against the standard, including a practical usage example.

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'
});

view raw JSON →