Tooling for converting, validating, and parsing OpenAPI, Swagger, and Postman API definitions
npx @tessl/cli install tessl/npm-oas-normalize@15.0.0OAS Normalize is a comprehensive TypeScript library that provides tooling for converting, validating, and parsing OpenAPI, Swagger, and Postman API definitions. It offers a unified interface for handling diverse API specification formats with robust error handling, automatic format conversion, and built-in validation capabilities.
npm install oas-normalizeimport OASNormalize from "oas-normalize";For CommonJS:
const OASNormalize = require("oas-normalize");import OASNormalize from "oas-normalize";
// Initialize with API definition (URL, file path, JSON object, etc.)
const oas = new OASNormalize(
'https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore-expanded.yaml'
);
// Validate the API definition
try {
await oas.validate();
console.log('API definition is valid!');
} catch (error) {
console.error('Validation failed:', error.message);
}
// Convert to OpenAPI format
const openapi = await oas.convert();
// Bundle external references
const bundled = await oas.bundle();
// Dereference all $ref pointers
const dereferenced = await oas.dereference();OAS Normalize is built around several key components:
Main class for loading, processing, and manipulating API definitions with comprehensive format support and validation.
class OASNormalize {
constructor(file: any, opts?: Options);
// Load the original API definition
async load(): Promise<Record<string, unknown>>;
// Get specification type and version information
async version(): Promise<{
specification: 'openapi' | 'postman' | 'swagger';
version: string | 'unknown';
}>;
}
interface Options {
colorizeErrors?: boolean;
enablePaths?: boolean;
parser?: ParserOptions;
}Automatic conversion capabilities for transforming Swagger and Postman collections to OpenAPI format.
class OASNormalize {
// Convert API definition to OpenAPI format
async convert(): Promise<OpenAPI.Document>;
}Advanced reference resolution for bundling external $ref pointers and dereferencing all references inline.
class OASNormalize {
// Bundle external $ref pointers
async bundle(): Promise<OpenAPI.Document>;
// Dereference all $ref pointers inline
async dereference(): Promise<OpenAPI.Document>;
// Deprecated alias for dereference
async deref(): Promise<OpenAPI.Document>;
}Comprehensive validation system with detailed error reporting, contextual messages, and customizable validation rules.
class OASNormalize {
// Validate API definition with detailed error reporting
async validate(opts?: {
parser?: ParserOptions;
}): Promise<ValidationResult>;
}
interface ValidationResult {
valid: boolean;
warnings: WarningDetails[];
errors: ErrorDetails[];
}Custom error classes and utility functions for managing validation errors and compilation.
class ValidationError extends Error {
constructor(message: string);
name: 'ValidationError';
}
// Utility functions
function compileErrors(result: ValidationResult): string;Helper functions for type detection, schema validation, and input processing.
// Type detection and validation
function getType(obj: any): 'buffer' | 'json' | 'path' | 'string-json' | 'string-yaml' | 'url' | false;
function isOpenAPI(schema: Record<string, unknown>): boolean;
function isSwagger(schema: Record<string, unknown>): boolean;
function isPostman(schema: Record<string, unknown>): boolean;
function isAPIDefinition(schema: Record<string, unknown>): boolean;
function getAPIDefinitionType(schema: Record<string, unknown>): 'openapi' | 'postman' | 'swagger' | 'unknown';
// Data processing utilities
function stringToJSON(string: Record<string, unknown> | string): Record<string, unknown>;
function prepareURL(url: string): { options: RequestInit; url: string };
function isBuffer(obj: any): boolean;