AJV Command Line Interface

0.12.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install `ajv-cmd` and use its `validate` and `transpile` commands within a bash script to process multiple JSON Schema files. It shows common `ajv` options for strict validation and transpilation.

#!/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/

view raw JSON →