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 🙏

© 2024 – Pkg Stats / Ryan Hefner

ajv-openapi-compile

v0.0.14

Published

Generate a compiled AJV validation module from an OpenAPI definition.

Downloads

9

Readme

ajv-openapi-compile

Given an OpenAPI definition, compile into AJV validation modules to be used in environments (like Cloudflare Workers) where eval is not available, or a single JavaScript file is desired.

Install

The normal way:

npm install ajv-openapi-compile --save-dev

Build With It

You can use the CLI tool, as part of your build process:

ajv-openapi-compile --definition=/path/to/openapi.json \
  --output=/path/to/compiled.js \
  --tree=/path/to/tree.js

You can also use in your build scripts:

import { compile } from 'ajv-openapi-compile'
import { readFile, writeFile } from 'node:fs'
const definition = JSON.parse(await readFile('/path/to/openapi.json', 'utf8'))
const { code, tree } = await compile(definition)
await writeFile('/path/to/compiled.js', code, 'utf8')
await writeFile('/path/to/tree.js', tree, 'utf8')

The output from the compile function contains the code property, which has all imports/requires resolved and concatenated into the single string.

Use Built Code

The compiled code is an ES string, and exports schema as the default, which is a map of schema identifiers to validation functions.

If you know the fully-resolved schema id, you can access the validation function explicitly:

import schemas from '/path/to/compiled.js'
const validate = schemas['#/components/schema/error']
const valid = validate({ code: 404 })
if (!valid) console.log(validate.errors)

Note: The schema identifiers are escaped using the JSON Pointer (RFC6901) specs, which turns ~ into ~0 and / into ~1.

If you don't know the fully-resolved schema id, you can use something like pointer-props to navigate the structure and resolve to the correct id:

import { resolve, toPointer } from 'pointer-props'
import { readFile } from 'node:fs'
import schemas from '/path/to/compiled.js'
const definition = JSON.parse(await readFile('/path/to/openapi.json', 'utf8'))
// lookup the id
const id = resolve(definition, '#/path/to/schema') // => '/path/to/fully/resolved/schema'
// note the relative reference requires "#" as the prefix
const validate = schemas['#' + id]
const valid = validate({ code: 404 })
if (!valid) console.log(validate.errors)

CLI ajv-openapi-compile

The CLI takes the following parameters:

  • ---definition, -d (String) - The path to the definition JSON file.
  • ---output, -o (String) - The path to write the compiled AJV validation code.

For convenience and compatability with other tooling, the definition parameter also supports importing JavaScript files, and will follow this algorithm:

  1. If the file extension is .json read and parse as JSON
  2. If the file extension is .yaml or .yml read and parse as YAML (using js-yaml internally)
  3. Otherwise, attempt importing * as schema
  4. If schema.definition is set, use that
  5. Otherwise, try using schema
  6. Otherwise, try importing the default as schema and try that

To be considered valid, the imported schema definition must have a paths object, with at least one "Path Object" defined.

API: function(definition: Object) => { code: String }

The function simply takes a valid OpenAPI 3.x object.

It returns an object with the following properties:

  • code: String - The compiled code, with all import and require statements resolved and placed inline.

License

Published and released under the Very Open License.

If you need a commercial license, contact me here.