Format validation extensions for Ajv JSON schema validator v7+
npx @tessl/cli install tessl/npm-ajv-formats@3.0.0Ajv Formats provides comprehensive format validation extensions for Ajv JSON schema validator version 7 and above. It implements a complete collection of standard and OpenAPI format validators including date/time formats, network formats, data encoding formats, and numeric precision formats.
npm install ajv-formatsimport addFormats from "ajv-formats";
import Ajv from "ajv";CommonJS:
const addFormats = require("ajv-formats");
const Ajv = require("ajv");Named imports:
import addFormats, {
FormatName,
FormatMode,
LimitFormatError,
formatNames,
fullFormats,
fastFormats,
formatLimitDefinition,
type DefinedFormats
} from "ajv-formats";import Ajv from "ajv";
import addFormats from "ajv-formats";
// Add all formats with default options
const ajv = new Ajv();
addFormats(ajv);
// Validate data with format
const schema = { type: "string", format: "email" };
const validate = ajv.compile(schema);
console.log(validate("user@example.com")); // true
console.log(validate("invalid-email")); // false
// Use specific formats only
const ajv2 = new Ajv();
addFormats(ajv2, ["date", "time", "email"]);
// Use fast validation mode
const ajv3 = new Ajv();
addFormats(ajv3, { mode: "fast", formats: ["date", "email"] });Ajv Formats is built around three core modules:
Core plugin functionality for integrating format validators with Ajv instances. Supports selective format inclusion, validation modes, and optional comparison keywords.
function addFormats(ajv: Ajv, opts: FormatsPluginOptions = {keywords: true}): Ajv;
type FormatsPluginOptions = FormatName[] | FormatOptions;
interface FormatOptions {
mode?: FormatMode;
formats?: FormatName[];
keywords?: boolean;
}
type FormatMode = "fast" | "full";Comprehensive collection of 26 format validators covering date/time, network, data encoding, and OpenAPI specification formats. Each format supports both full validation and fast validation modes.
type FormatName =
| "date" | "time" | "date-time" | "iso-time" | "iso-date-time" | "duration"
| "uri" | "uri-reference" | "uri-template" | "url"
| "email" | "hostname" | "ipv4" | "ipv6"
| "regex" | "uuid" | "json-pointer" | "json-pointer-uri-fragment" | "relative-json-pointer"
| "byte" | "int32" | "int64" | "float" | "double" | "password" | "binary";
interface FormatsPlugin {
get(format: FormatName, mode: FormatMode = "full"): Format;
}Format-based range validation keywords that work with ordered formats like dates and times. Enables minimum/maximum constraints based on format-specific comparison logic.
type LimitFormatError = ErrorObject<
"formatMaximum" | "formatMinimum" | "formatExclusiveMaximum" | "formatExclusiveMinimum",
{ limit: string; comparison: "<=" | ">=" | "<" | ">" }
>;Utility exports for advanced usage scenarios and direct access to format definitions.
const formatNames: FormatName[];
const fullFormats: DefinedFormats;
const fastFormats: DefinedFormats;
const formatLimitDefinition: CodeKeywordDefinition;interface FormatOptions {
/** Validation mode: "fast" for simplified validation, "full" for complete validation */
mode?: FormatMode;
/** Specific formats to add (defaults to all formats) */
formats?: FormatName[];
/** Whether to add format comparison keywords (formatMaximum, formatMinimum, etc.) */
keywords?: boolean;
}
type FormatsPluginOptions = FormatName[] | FormatOptions;
interface FormatsPlugin extends Plugin<FormatsPluginOptions> {
/** Retrieve a specific format definition */
get(format: FormatName, mode: FormatMode = "full"): Format;
}
type FormatMode = "fast" | "full";
type DefinedFormats = {
[key in FormatName]: Format;
};