# esbuild-plugin-ajv

> [![npm](https://img.shields.io/npm/v/esbuild-plugin-ajv.svg)](https://www.npmjs.com/package/esbuild-plugin-ajv)

Latest version **1.1.0** (published 2022-01-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install esbuild-plugin-ajv
pnpm add esbuild-plugin-ajv
yarn add esbuild-plugin-ajv
bun add esbuild-plugin-ajv
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2022-01-28 |
| First published | 2022-01-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Jonas Winzen |
| Maintainers | mister_what |

## Links

- npm: https://www.npmjs.com/package/esbuild-plugin-ajv
- Repository: https://github.com/mister-what/esbuild-plugin-ajv
- Homepage: https://github.com/mister-what/esbuild-plugin-ajv#readme
- Issues: https://github.com/mister-what/esbuild-plugin-ajv/issues
- npm.io page: https://npm.io/package/esbuild-plugin-ajv

## Recent versions

- 1.1.0 (latest) — 2022-01-28
- 1.0.0 — 2022-01-19

## README

[![npm](https://img.shields.io/npm/v/esbuild-plugin-ajv.svg)](https://www.npmjs.com/package/esbuild-plugin-ajv)

# esbuild-plugin-ajv

## Installation

With npm:

```sh
npm i -D esbuild-plugin-ajv ajv
```

Or yarn:

```sh
yarn add -D esbuild-plugin-ajv ajv
```

## Use-cases

**The motivation to pre-compile schemas<sup>[1](#ref-1)</sup>:**

- avoids dynamic code evaluation with Function constructor (used for schema
  compilation) - _useful in browser environments where `'unsafe-eval'` is not
  allowed by CSP (Content Security Policy)_
- faster startup times
- lower memory footprint/bundle size
- compatible with strict content security policies
- almost no risk to compile schema more than once
- better for short-lived environments

## Usage

### Build config

```ts
import esbuild from "esbuild";
import AjvPlugin from "esbuild-plugin-ajv";

esbuild.build({
  /* ... */
  plugins: [
    AjvPlugin({
      extraKeywords: [
        /* Ajv.CodeKeywordDefinition */
      ],
      ajvOptions: {
        coerceTypes: true,
      },
    }),
  ],
  /* ... */
});
```

### Precompile imported schema

```js
import validator from "./someJsonSchema.json?ajv";

/**
 * @description use compiled schema in your code
 */
const validate = (x) => {
  if (!validator(x)) throw validator.errors;
  return x;
};
```

## References

- <b name="ref-1">[1]</b>: _Standalone Validation Code_ in _Ajv documentation_
  https://ajv.js.org/guide/managing-schemas.html#standalone-validation-code

---
_Source: https://npm.io/package/esbuild-plugin-ajv · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
