or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

comparison-keywords.mdformat-validation.mdindex.mdplugin-integration.md
tile.json

tessl/npm-ajv-formats

Format validation extensions for Ajv JSON schema validator v7+

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/ajv-formats@3.0.x

To install, run

npx @tessl/cli install tessl/npm-ajv-formats@3.0.0

index.mddocs/

Ajv Formats

Ajv 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.

Package Information

  • Package Name: ajv-formats
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install ajv-formats

Core Imports

import 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";

Basic Usage

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"] });

Architecture

Ajv Formats is built around three core modules:

  • Plugin System: Main plugin function that integrates with Ajv instances
  • Format Definitions: Complete library of 26 format validators with full and fast modes
  • Comparison Keywords: Additional validation keywords for format-based range validation
  • Type Safety: Full TypeScript support with detailed type definitions

Capabilities

Plugin Integration

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";

Plugin Integration

Format Validation

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 Validation

Comparison Keywords

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: "<=" | ">=" | "<" | ">" }
>;

Comparison Keywords

Format Utilities

Utility exports for advanced usage scenarios and direct access to format definitions.

const formatNames: FormatName[];

const fullFormats: DefinedFormats;

const fastFormats: DefinedFormats;

const formatLimitDefinition: CodeKeywordDefinition;

Types

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;
};