What Tool Does Archify Use for Schema Validation?
Archify uses Ajv (Another JSON Validator) version 8.x with JSON Schema draft 2020-12 support to validate its JSON-IR and associated schemas, generating strict standalone validators that execute at runtime with a fallback degraded mode when the dependency is unavailable.
Archify enforces data integrity through rigorous schema validation across its JSON-IR architecture. The project implements Ajv, a high-performance JSON Schema validator for JavaScript, to compile strict validation rules into standalone runtime functions. According to the tt-a1i/archify source code, this integration spans dependency management, automated validator generation, and comprehensive test coverage.
Ajv: The Core Schema Validation Engine
Archify selected Ajv (Another JSON Validator) as its schema validation tool to handle JSON-IR validation with maximum performance and standards compliance. The implementation leverages Ajv version 8.x with the draft 2020-12 specification, utilizing strict mode to catch schema errors early during development.
Dependency Configuration
The validation pipeline begins with the dependency declaration in archify/package.json, which pins the ajv package to ensure consistent validation behavior across environments. This foundation enables the project to import specific Ajv distributions tailored to the JSON Schema draft 2020-12 standard.
Validator Generation Pipeline
The core validation logic resides in archify/scripts/generate-validators.mjs, which imports Ajv2020 from ajv/dist/2020.js to instantiate a strict-mode validator. This script processes Archify's schema definitions and compiles them into standalone validation functions using ajv/dist/standalone/index.js, eliminating runtime compilation overhead.
As documented in archify/schemas/README.md, the compilation process enforces Ajv's strict mode to ensure schemas contain no ambiguities or structural errors before deployment.
Implementation Details and Code Examples
The following patterns demonstrate how Archify implements Ajv validation based on the source code in tt-a1i/archify.
Generating Standalone Validators
The generate-validators.mjs script creates self-contained validation functions:
// Example: creating a validator for a custom schema
import Ajv2020 from 'ajv/dist/2020.js';
import standaloneCode from 'ajv/dist/standalone/index.js';
import fs from 'fs';
import path from 'path';
// Load a schema (e.g., archify/schemas/common.schema.json)
const schema = JSON.parse(
fs.readFileSync(path.join('archify/schemas', 'common.schema.json'), 'utf8')
);
// Configure Ajv in strict mode
const ajv = new Ajv2020({ strict: true });
ajv.addSchema(schema);
// Generate a self‑contained validator function
const validatorCode = standaloneCode(ajv, [schema.$id]);
// Export the validator for runtime use
export const validate = new Function('data', validatorCode);
Runtime Validation
Once generated, validators execute at runtime without requiring the full Ajv library:
# Run a validation at the command line (using the generated validator)
node -e "import {validate} from './generated/validator.js';
const data = JSON.parse(process.argv[1]);
console.log(validate(data) ? 'Valid' : 'Invalid');" '{"type":"example"}'
Runtime Behavior and Fallback Mode
When the ajv dependency is unavailable, Archify enters a degraded mode that skips schema validation but preserves layout checks. This ensures the application remains functional in constrained environments while maintaining structural integrity through alternative validation paths. The test suite in archify/test/layout-rules.test.mjs verifies both strict Ajv validation behavior and degraded-mode operation.
Summary
- Ajv serves as Archify's primary schema validation tool, specifically version 8.x with draft 2020-12 support.
- The
archify/scripts/generate-validators.mjsscript compiles schemas into standalone validators using Ajv's strict mode and standalone code generation. archify/package.jsondeclares theajvdependency, whilearchify/schemas/README.mddocuments the compilation process.- Runtime validation falls back to a degraded mode when Ajv is missing, maintaining layout checks without JSON Schema validation.
- Tests in
archify/test/layout-rules.test.mjsconfirm integration behavior across both standard and fallback scenarios.
Frequently Asked Questions
What specific version of Ajv does Archify use?
Archify utilizes Ajv version 8.x with support for JSON Schema draft 2020-12. This version provides the strict mode capabilities and standalone code generation features required for Archify's validation pipeline.
How does Archify generate standalone validators?
The project uses archify/scripts/generate-validators.mjs to import Ajv2020 from ajv/dist/2020.js, configure strict mode, and compile schemas into standalone functions via ajv/dist/standalone/index.js. These generated validators run at runtime without requiring the full Ajv library.
What happens when Ajv is not installed?
Archify implements a degraded mode that bypasses schema validation while preserving essential layout checks. This ensures continuity when the ajv dependency is missing, though JSON-IR strict validation is skipped.
Where are the schema validation tests located?
Integration tests validating Ajv behavior reside in archify/test/layout-rules.test.mjs. This file confirms that generated validators correctly enforce schema constraints and that the system handles both validation success and failure cases appropriately.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →