Registry / devops / ajv-pack

ajv-pack

JSON →
library0.3.1jsnpmunverified

ajv-pack produces compact standalone validation modules from JSON schemas compiled with Ajv (v4.7.4+). Version 0.3.1 is the latest stable release. It reduces bundle size by excluding Ajv at runtime and avoids dynamic code evaluation (useful for CSP). Compared to alternatives like @exodus/schemasafe or is-my-json-valid, ajv-pack specifically works with Ajv-generated code and is designed for build-time pre-compilation. Key limitations: no support for custom compiled/validated keywords, custom formats, recursive references (except self-ref), or async schemas.

npm install ajv-pack
INSTALL
IMPORT
SIG · AJV-PACK
A
ajv-pack
devopsjavascriptv0.3.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (pack function)
import pack from 'ajv-pack'
const pack = require('ajv-pack')
ajv-pack is an ESM package; use default import in TypeScript/ESM, or require in CJS with .default.
require
const pack = require('ajv-pack')
CommonJS require works but may need .default in some bundler configs if using ESM internals.
type import
import type { AjvPack } from 'ajv-pack'
import { AjvPack } from 'ajv-pack'
ajv-pack does not export named TypeScript types; use 'any' or rely on inference.

Compile a JSON schema with Ajv (sourceCode: true), generate standalone validation module, and save or require it.

const Ajv = require('ajv'); const ajv = new Ajv({ sourceCode: true }); const pack = require('ajv-pack'); const schema = { type: 'object', properties: { name: { type: 'string' }, age: { type: 'number', minimum: 0 } }, required: ['name', 'age'] }; const validate = ajv.compile(schema); const moduleCode = pack(ajv, validate); // Write to file const fs = require('fs'); const path = require('path'); fs.writeFileSync(path.join(__dirname, 'validate.js'), moduleCode); // Or require from string const requireFromString = require('require-from-string'); const packedValidate = requireFromString(moduleCode); console.log(packedValidate({ name: 'Alice', age: 30 })); // true
Debug
Known issues
breakingRequires Ajv version >=4.7.4 with sourceCode:true option.
fix
Update Ajv to >=4.7.4 and ensure { sourceCode: true } is set.
affects: <4.7.4
gotchaCustom formats are ignored during validation; they will not be checked.
fix
Avoid using custom formats with ajv-pack, or validate manually.
affects: >=0.0.1
gotchaRecursive references other than { $ref: '#' } are not supported.
fix
Simplify schemas to avoid circular references; use self-reference only.
affects: >=0.0.1
gotchaCustom compiled and validated keywords are not supported; only inline and macro keywords work.
fix
Refactor custom keywords to inline or macro types.
affects: >=0.0.1
gotchaAsync schemas (e.g., with custom async keywords) are not supported.
fix
Use sync-only schemas with ajv-pack.
affects: >=0.0.1
Errors
Common errors & fixes
TypeError: Cannot read property 'sourceCode' of undefined
Ajv instance not created with { sourceCode: true }
fix
const ajv = new Ajv({ sourceCode: true });
ReferenceError: require is not defined
Using require in ESM context without bundler
fix
Use import pack from 'ajv-pack' or set type: 'module' in package.json and use createRequire.
Module not found: Can't resolve 'ajv-pack'
Missing npm install ajv-pack
fix
Run npm install ajv-pack
Upgrade
Version history
0.3.1latest on npm
Audit
Dependencies
ajvrequiredajv-pack compiles schemas using Ajv; require('ajv') with {sourceCode: true} option is required
Agent activity
35 hits · last 30 days
node
32
OpenAI (training)
1
Resources
ajv-pack — npm install ajv-pack · libregistry