AJV Command Line Interface
ajv-cmd provides a robust command-line interface (CLI) for performing essential operations on JSON Schema files, leveraging the powerful AJV (Another JSON Schema Validator) library. It enables users to dereference, validate, transpile, and test JSON Schema definitions directly from the terminal, streamlining schema management and integration into build processes. The current stable version is 0.12.0. While no explicit release cadence is stated, its `0.x.x` versioning coupled with recent GitHub activity suggests active development and potentially rapid iterations. A key differentiator is its modern approach, requiring Node.js 24+, and its commitment to supply chain security standards like SLSA 3 and OpenSSF Scorecard, building upon the foundation of `ajv-cli` with a focus on contemporary JavaScript environments and best practices. It's primarily designed for developers who need to automate schema validation and compilation within CI/CD pipelines or local development workflows, providing a convenient wrapper around AJV's extensive capabilities.
Common errors
-
ajv: command not found
cause The `ajv-cmd` package's executable (`ajv`) is not in your system's PATH. This usually happens when installed locally without using `npx` or without adding `node_modules/.bin` to PATH.fixWhen installed locally, use `npx ajv ...` or add `./node_modules/.bin` to your system's PATH. If installed globally (`npm install -g ajv-cmd`), ensure your global `npm` bin directory is in your PATH. -
Error: This package requires Node.js version >=24.
cause You are running `ajv-cmd` with an incompatible Node.js version.fixUpdate your Node.js environment to version 24 or higher. Use a Node Version Manager (e.g., `nvm install 24`, `nvm use 24`) to easily switch versions. -
Schema failed validation: keyword 'type' expected string, got object at #/properties/name
cause Your JSON Schema contains an error preventing successful validation or compilation by AJV.fixCarefully review your JSON Schema for syntax errors, incorrect keywords, or violations of the JSON Schema specification. Use a schema linter or online validator to pinpoint issues. -
Error: Cannot find module 'ajv-cmd/src/commands/validate.js'
cause You are attempting to import an internal module path of `ajv-cmd` that either does not exist, has changed, or is not intended for public consumption.fixAvoid importing directly from `ajv-cmd/src/...` paths. This package is designed for command-line use. If programmatic validation or transpilation is required, use the core `ajv` library directly or rely on `ajv-cmd` via `child_process` (e.g., `execa`).
Warnings
- breaking ajv-cmd requires Node.js version >=24. Running it with older Node.js versions will result in an error.
- gotcha The package is in `0.x.x` versioning, indicating that its API and CLI options may undergo breaking changes in minor releases. Always review release notes when upgrading.
- gotcha When using `ajv-cmd` programmatically via internal paths (e.g., `ajv-cmd/src/commands/validate.js`), these paths are implementation details and not part of the public API. They are highly susceptible to change without notice, leading to import errors.
- gotcha Using strict validation modes (e.g., `--strict true`) and type coercion (`--coerce-types`) can lead to unexpected validation failures or data transformations if not fully understood, especially with complex schemas.
Install
-
npm install ajv-cmd -
yarn add ajv-cmd -
pnpm add ajv-cmd
Imports
- validate
import { validate } from 'ajv-cmd/src/commands/validate.js' - transpile
import { transpile } from 'ajv-cmd/src/commands/transpile.js' - test
import { test } from 'ajv-cmd/src/commands/test.js'
Quickstart
#!/usr/bin/env bash
# Install ajv-cmd as a development dependency
npm install -D ajv-cmd
# Define a function to validate and transpile a schema
function bundle_schema {
# Validate the schema with strict mode, type coercion, all errors, and default values
ajv validate "${1}" --valid \
--strict true --coerce-types array --all-errors true --use-defaults empty
# Transpile the schema to a JavaScript file
ajv transpile "${1}" \
--strict true --coerce-types array --all-errors true --use-defaults empty \
-o "${1%.json}.js"
}
# Create a dummy schema file for demonstration
mkdir -p handlers/user
echo '{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer", "minimum": 0, "default": 18 }
},
"required": ["name"]
}' > handlers/user/schema.create.json
# Loop through all schema files in handlers/ and process them
for file in handlers/*/schema.*.json; do
echo "Processing: $file"
if ! bundle_schema "$file"; then
echo "$file failed processing." >&2
exit 1
fi
echo "Successfully processed $file"
done
echo "\nCheck the 'handlers/' directory for generated JS files."
# Clean up dummy schema file
# rm -rf handlers/