Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mister-what/esbuild-plugin-ajv
AJV plugin for esbuild
https://github.com/mister-what/esbuild-plugin-ajv
Last synced: about 1 month ago
JSON representation
AJV plugin for esbuild
- Host: GitHub
- URL: https://github.com/mister-what/esbuild-plugin-ajv
- Owner: mister-what
- License: mit
- Created: 2022-01-19T16:36:20.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-12-14T11:33:28.000Z (almost 2 years ago)
- Last Synced: 2024-03-24T20:21:50.750Z (6 months ago)
- Language: TypeScript
- Size: 492 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
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[1](#ref-1):**
- 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
- [1]: _Standalone Validation Code_ in _Ajv documentation_
https://ajv.js.org/guide/managing-schemas.html#standalone-validation-code