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-json-loader-extended

v0.3.0

Published

Webpack loader that compiles JSON strings into Ajv validation functions using ajv-pack

Downloads

37

Readme

ajv-json-loader

Build Status

Webpack loader that compiles JSON strings into Ajv validation functions using ajv-pack

Slightly increases application performance - all schemas are compiled at build step, not at runtime

It also decreases build size because of excluding Ajv library itself from bundle

However, it may depend on total size of schemas. For few schemas with small size it's a 100% profit. Not sure about the ratio between size of JSON-schema and size of compiled function, so for large schemas it may be not so useful

Installation

npm i -D ajv-json-loader

Usage

Use it as loader for JSON-schemas in your webpack config like this:

module.exports = {
  // ...
  module: {
    loaders: [
      {
        test: /\.schema\.json$/,
        use: [
          {
            loader: 'ajv-json-loader',
            options: {
              ajv: {
                // Pass any Ajv constructor options here
                allErrors: true,
              }
            }
          }
        ],
        // "type" option only for Webpack >= 4
        // (https://webpack.js.org/configuration/module/#ruletype)
        type: "javascript/auto"
      }
    ]
  }
};

Then you can use your schemas like this:

const validateMySchema = require('/path/to/schemas/my.schema.json');

if (validateMySchema(data)) {
  console.log('Valid!');
} else {
  console.error('Invalid: ' + validateMySchema.errors.join(','));
}

Referencing to external schemas (using $ref)

Loader uses Ajv's loadSchema option to load external schemas. Default implementation will try to just require file with name specified in $ref field to load referenced schema. You can override it by passing corresponding option to loader (see example above)

If you think that your custom loadSchema option is pretty general, feel free to create an issue or PR to add it as loader option to make webpack config more clean

For example, if you have following schemas in /path/to/schemas:

foo.json

{
  "id": "foo.json#",
  "properties": {
    "bar": { "$ref": "bar.json#/definitions/bar" }
  }
}

bar.json

{
  "id": "bar.json#",
  "properties": {
    "baz": {
      "type": "string"
    }
  }
}

Loader will call require(path.resolve('/path/to/schemas', 'bar.json')) to load bar.json# schema

TypeScript typings

If you are using TypeScript, you can add typings to your project like this:

declare module '*.schema.json' {
  import { ErrorObject } from 'ajv';

  interface ValidateFn {
      (data: object): boolean;
      errors: ErrorObject[] | null;
  }
  const validate: ValidateFn;
  export default validate;
}

Then when you import your schema After this TypeScript compiler will know that modules ending with .schema.json exports an ValidateFn object

Limitations

This loader uses ajv-pack package, so limitations are the same