@bernierllc/validators-mime-structure
v1.2.0
Published
Primitive validator for MIME type structure validation - MIME parts, content-type correctness
Readme
@bernierllc/validators-mime-structure
Primitive validator for MIME message structure validation - multipart boundaries, content-type correctness, transfer encoding, and MIME part structure.
Installation
npm install @bernierllc/validators-mime-structureUsage
Basic Validation
import { validateMimeStructure, isValidMimeStructure } from '@bernierllc/validators-mime-structure';
const mimeMessage = `Content-Type: multipart/mixed; boundary="boundary123"
--boundary123
Content-Type: text/plain
Hello World
--boundary123--`;
// Get detailed validation problems
const problems = await validateMimeStructure(mimeMessage);
if (problems.length === 0) {
console.log('Valid MIME structure');
} else {
problems.forEach(problem => {
console.log(`${problem.severity}: ${problem.message}`);
});
}
// Quick validation check
const isValid = await isValidMimeStructure(mimeMessage);
console.log(isValid ? 'Valid' : 'Invalid');Configuration Options
import { validateMimeStructure } from '@bernierllc/validators-mime-structure';
const problems = await validateMimeStructure(content, {
// Enable/disable specific checks
checkBoundaries: true, // Validate multipart boundaries
checkContentType: true, // Validate Content-Type headers
checkTransferEncoding: true, // Validate Content-Transfer-Encoding
checkPartStructure: true, // Validate MIME part structure
checkBoundaryMismatch: true, // Detect boundary mismatches
// Configuration
maxNestingDepth: 10 // Maximum multipart nesting depth
});Helper Functions
import {
extractBoundary,
isMultipart,
parseMimeParts
} from '@bernierllc/validators-mime-structure';
// Extract boundary from Content-Type header
const boundary = extractBoundary('multipart/mixed; boundary="test"');
console.log(boundary); // "test"
// Check if message is multipart
const isMultipartMsg = isMultipart(mimeMessage);
console.log(isMultipartMsg); // true
// Parse MIME message into parts
const parts = parseMimeParts(mimeMessage);
parts.forEach(part => {
console.log('Headers:', part.headers);
console.log('Body:', part.body);
});API Reference
validateMimeStructure(content, options?, utils?)
Validates MIME message structure and returns an array of validation problems.
Parameters:
content: string- MIME message content to validateoptions?: Partial<MimeStructureOptions>- Validation optionsutils?: SharedUtils- Shared utilities from validators-core
Returns: Promise<Problem[]>
Example:
const problems = await validateMimeStructure(mimeMessage, {
checkBoundaries: true,
maxNestingDepth: 5
});isValidMimeStructure(content, options?)
Checks if MIME message structure is valid (no errors).
Parameters:
content: string- MIME message content to validateoptions?: Partial<MimeStructureOptions>- Validation options
Returns: Promise<boolean>
Example:
if (await isValidMimeStructure(mimeMessage)) {
console.log('Valid MIME structure');
}extractBoundary(contentType)
Extracts boundary string from Content-Type header value.
Parameters:
contentType: string- Content-Type header value
Returns: string | null
Example:
const boundary = extractBoundary('multipart/mixed; boundary="abc123"');
// Returns: "abc123"isMultipart(content)
Checks if content is a multipart MIME message.
Parameters:
content: string- Content to check
Returns: boolean
Example:
if (isMultipart(content)) {
console.log('This is a multipart message');
}parseMimeParts(content)
Parses MIME message into individual parts with headers and body.
Parameters:
content: string- MIME message content
Returns: Array<{ headers: string; body: string }>
Example:
const parts = parseMimeParts(mimeMessage);
console.log(`Found ${parts.length} parts`);Validation Rules
Boundary Validation (boundary-validator)
Validates multipart boundaries according to RFC 2046:
- Boundary parameter presence in Content-Type
- Boundary format and characters
- Boundary length (max 70 characters)
- Boundary markers in message body
- Closing boundary marker presence
Content-Type Validation (content-type-validator)
Validates Content-Type headers according to RFC 2045:
- MIME type format (type/subtype)
- Token validity for type and subtype
- Parameter format and syntax
- Required boundary for multipart types
- Recommended charset for text types
Transfer Encoding Validation (transfer-encoding-validator)
Validates Content-Transfer-Encoding according to RFC 2045:
- Valid encoding values (7bit, 8bit, binary, quoted-printable, base64)
- Base64 format and padding
- Quoted-printable escape sequences
- Line length requirements
- Character restrictions for 7bit encoding
Part Structure Validation (part-structure-validator)
Validates MIME message part structure:
- Header-body separation (blank line)
- Header format and continuation
- Nested multipart messages
- Maximum nesting depth
- Empty body warnings
Boundary Mismatch Detection (boundary-mismatch-validator)
Detects boundary mismatches and inconsistencies:
- Declared boundaries used in body
- Undeclared boundary markers
- Multiple closing markers
- Incorrect boundary marker format
- Duplicate boundary declarations
Examples
Validating Email with Attachments
const email = `Content-Type: multipart/mixed; boundary="outer"
--outer
Content-Type: multipart/alternative; boundary="inner"
--inner
Content-Type: text/plain; charset=utf-8
Plain text version
--inner
Content-Type: text/html; charset=utf-8
<html><body>HTML version</body></html>
--inner--
--outer
Content-Type: application/pdf; name="document.pdf"
Content-Transfer-Encoding: base64
JVBERi0xLjQK
--outer--`;
const problems = await validateMimeStructure(email);
console.log(`Found ${problems.length} validation issues`);Selective Validation
// Only validate boundaries and content-type
const problems = await validateMimeStructure(content, {
checkBoundaries: true,
checkContentType: true,
checkTransferEncoding: false,
checkPartStructure: false,
checkBoundaryMismatch: false
});Custom Nesting Depth
// Allow deeper nesting for complex email structures
const problems = await validateMimeStructure(content, {
maxNestingDepth: 15
});Integration Status
- Logger: not-applicable - Primitive validator with no internal logging
- Docs-Suite: ready - Complete API documentation and examples
- NeverHub: not-applicable - Stateless validation utility
See Also
- @bernierllc/validators-core - Core validation framework
- @bernierllc/validators-runner - Validation orchestration
- @bernierllc/validators-html-syntax - HTML validation
- @bernierllc/validators-json-structure - JSON validation
License
Copyright (c) 2025 Bernier LLC. All rights reserved.
