npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

openapi-runtime-validator

v1.2.1

Published

Runtime validation API responses using OpenAPI schema

Downloads

17

Readme

openapi-runtime-validator

Runtime validation API responses using OpenAPI schema

Impressed by Openapi-validator (uses to validate Responses in NodeJS or in the unit tests)

Problem

When we render Typescript type from OpenAPI schema we've got perfect static types validation. But it does not help us handle inappropriate responses in runtime.

Openapi-validator does what we need. But it works only on NodeJS

Solution

We took Openapi-validator and excluded all NodeJS dependencies methods. So now it can work in the browser and use fetch

How to use

To validate responses we have to initialize responseValidator first:

import {createResponseValidator, ValidationResult} from "openapi-runtime-validator";
import openApiSchema from "./schema.json";

const responseValidator = createResponseValidator({
  /** object with Open API Schema */
  openApiSchema
});

Then we can use it directly with the fetch:

fetch("https://swapi.dev/api/people/").then(async response => {
  const method = "GET";

  const {validationError} = await responseValidator(response.clone(), method);
  if (validationError?.message) {
    const errorMessage = validationError?.message || "";
    console.error(errorMessage);
  }

  return response;
});

responseValidator params

import {createResponseValidator, ValidationResult} from "openapi-runtime-validator";
import openApiSchema from "./schema.json";

const responseValidator = createResponseValidator({
  /** object with Open API Schema */
  openApiSchema,
  /**
   * (optional) function to process request url
   * e.g. we can exclude paths to proxy
   * */
  preparePathname,
  /**
   * (optional) skip the validation if the function returns true
   * ATTENTION: @path calculated as a result of `preparePathname`
   * */
  skipValidation,
  /** (optional) callback */
  onValidate,
  /** (optional), by default we use `.json()` to get data from response */
  getResponseData
});

skipValidation

skipValidation(): boolean

We can skip validation for some reasons, e.g. load images or some API without OpenAPI Schema:

// we will skip validation if path started from `/image-url/`
const skipValidation = (path: string): boolean => {
    return return path.match(new RegExp("^/image-url/")) !== null;
}

Default value: false

getResponseData

getResponseData(response: Response): any

Should return object what will validate by Schema.

It can be response.text() or direct read the body.

By default, we use response.json().

Examples

Convert YAML to JSON

We can use next command to convert yaml to json. We need package https://www.npmjs.com/package/js-yaml

npx js-yaml ./examples/openapi.yaml > ./examples/openapi.json

Or we can run inside examples folder next script node convertYamlToJSON.js

Direct validate fetch response

Just keep in mind fetch Response does not have information about request method GET, POST, etc. but we need it to get expected result schema, so we must include request method to the validation.

examples/direct-fetch.ts

Try it on CodeSandbox

Use interceptor

If we use some library to manage our API requests we should follow the instruction how to create interceptor.

In case we use direct window.fetch - we can replace it wit our implementation e.g.:

const fetchWithInterceptor = (input: RequestInfo, init?: RequestInit): Promise<Response> => {
  let method = "GET";

  if (typeof input === "string" && init?.method) {
    method = init.method;
  } else if (typeof input !== "string" && input?.method) {
    method = input.method;
  }

  return fetch(input, init).then(async response => {
    if (method) {
      await responseValidator(response.clone(), method);
    }
    return response;
  });
};

responseValidator example implementation:

const preparePathname = (path: string): string => {
  // exclude internal Proxy paths
  return path
    .replace("/api/", "/");
};

const onValidate = ({response, validationError, method, path}: ValidationResult) => {
  if (validationError?.message) {
    const errorMessage = validationError?.message || "";
    const fullErrorMessage = `[BE] response error in schema
      CODE: ${validationError.code},
      METHOD: ${method},
      URL: ${response.url}
      STATUS: ${response.status}
      operationId: ${validationError?.operationId}
      
      VALIDATION ERROR: ${errorMessage}
    `;

    const msg = `[BE] schema validation error: <${validationError.code}> ${method}: ${
      validationError?.operationId || path
    }`;

    // captureSentryException(new Error(msg), fullErrorMessage);
    
    // throw new Error(msg);
    
    // console.error(fullErrorMessage);
  }

  switch (path) {
    case "/some/path": {
      if (response.status === 204) {
        // redirect somewere, etc.;
      }
      break;
    }
    default:
      break;
  }
};

const responseValidator = createResponseValidator({
  openApiSchema,
  preparePathname,
  onValidate
});

examples/fetch-with-interceptor.ts

Try it on CodeSandbox

TODO:

  • Add unit tests
  • Cleanup code (simplify and rid of inheritance)
  • try to use information from the Schema to run appropriate function to get data from the Response: .json(), .text(), etc.

Changelog:

  • 1.1.0 getResponseData delegate function
  • 1.2.0 skipValidation functionality