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-pack

v0.3.1

Published

Produces a compact module exporting JSON-schema validation functions compiled by Ajv

Downloads

72,829

Readme

ajv-pack

Produces a compact module exporting JSON-schema validation functions compiled by Ajv

Build Status npm version Coverage Status

Purpose

This package allows to create standalone modules for validation functions that are pre-compiled and can be used without Ajv. It can be necessary for several reasons:

  • to reduce the browser bundle size - Ajv is not included in the bundle (although if you have a large number of schemas the bundle can become bigger - when the total size of generated validation code is bigger than Ajv code).
  • to reduce the startup time - the validation and compilation of schemas will happen during build time.
  • to avoid dynamic code evaluation with Function constructor (used for schema compilation) - it can be prohibited in case Content Security Policy is used.

Please note: there are many cases when Ajv works as expected and ajv-pack does not. Some of these cases are listed in Limitations. It is recommended to test schemas compiled with this package more thoroughly than you would when using Ajv (which is very stable and well tested). Please submit issues with cases that fail.

Usage with CLI

In most cases you would use this package via ajv-cli (>= 1.0.0) to generate module that exports validation function.

npm install -g ajv-cli
ajv compile -s schema.json -o validate_schema.js

validate_schema.js will contain the module exporting validation function that can be bundled into your application.

Usage from code

npm install ajv-pack
var Ajv = require('ajv'); // version >= 4.7.4
var ajv = new Ajv({sourceCode: true}); // this option is required
var pack = require('ajv-pack');

var schema = {
  type: 'object',
  properties: {
    foo: {
      type: 'string',
      pattern: '^[a-z]+$'
    }
  }
};

var validate = ajv.compile(schema);
var moduleCode = pack(ajv, validate);

// now you can
// 1. write module code to file
var fs = require('fs');
var path = require('path');
fs.writeFileSync(path.join(__dirname, '/validate.js'), moduleCode);

// 2. require module from string
var requireFromString = require('require-from-string');
var packedValidate = requireFromString(moduleCode);

Ajv should still be a run-time dependency, but generated modules will only depend on some parts of it, the whole Ajv will not be included in the bundle if you require these modules from your code.

Limitations

At the moment ajv-pack does not support schemas with:

  • custom 'compiled' and 'validated' keywords; custom inline and macro keywords are supported.
  • custom formats (they will be ignored during validation).
  • recursive references (reference to the current schema { "$ref": "#" } is supported).
  • asynchronous schemas (they require custom keywords/formats).

License

MIT