Giter VIP home page Giter VIP logo

esbuild-plugin-ajv's Introduction

npm

esbuild-plugin-ajv

Installation

With npm:

npm i -D esbuild-plugin-ajv ajv

Or yarn:

yarn add -D esbuild-plugin-ajv ajv

Use-cases

The motivation to pre-compile schemas1:

  • 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

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

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

Precompile imported schema

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

esbuild-plugin-ajv's People

Contributors

mister-what avatar semantic-release-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

goto-bus-stop

esbuild-plugin-ajv's Issues

Typescript file support

Hi! Awesome plugin :)

I'm wondering if it would be possible to add support for typescript file imports. This would bring a bit of dx in the form of using types when creating the json schemas. I'm thinking of a default export which has the type JSONSchemaType<T> which contains the schema.

const foo: JSONSchemaType<{foo: 'bar'}> = {
  type: 'object',
  required: ['foo'],
  properties: {
    foo: {
      type: 'string',
      const: 'bar'
    }
  }
}

export default foo

This is just a feature request nothing I require to make things work.

Thanks for your awesome package!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.