
Product
Socket for Jira Is Now Available
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.
ajv-formats
Advanced tools
JSON Schema formats for Ajv
// ESM/TypeScript import
import Ajv from "ajv"
import addFormats from "ajv-formats"
// Node.js require:
const Ajv = require("ajv")
const addFormats = require("ajv-formats")
const ajv = new Ajv()
addFormats(ajv)
The package defines these formats:
See regular expressions used for format validation and the sources that were used in formats.ts.
Please note: JSON Schema draft-07 also defines formats iri, iri-reference, idn-hostname and idn-email for URLs, hostnames and emails with international characters. These formats are available in ajv-formats-draft2019 plugin.
formatMaximum / formatMinimum and formatExclusiveMaximum / formatExclusiveMinimumThese keywords allow to define minimum/maximum constraints when the format keyword defines ordering (compare function in format definition).
These keywords are added to ajv instance when ajv-formats is used without options or with option keywords: true.
These keywords apply only to strings. If the data is not a string, the validation succeeds.
The value of keywords formatMaximum/formatMinimum and formatExclusiveMaximum/formatExclusiveMinimum should be a string or $data reference. This value is the maximum (minimum) allowed value for the data to be valid as determined by format keyword. If format keyword is not present schema compilation will throw exception.
When these keyword are added, they also add comparison functions to formats "date", "time" and "date-time". User-defined formats also can have comparison functions. See addFormat method.
require("ajv-formats")(ajv)
const schema = {
type: "string",
format: "date",
formatMinimum: "2016-02-06",
formatExclusiveMaximum: "2016-12-27",
}
const validDataList = ["2016-02-06", "2016-12-26"]
const invalidDataList = ["2016-02-05", "2016-12-27", "abc"]
Options can be passed via the second parameter. Options value can be
addFormats(ajv, ["date", "time"])
Please note: when ajv encounters an undefined format it throws exception (unless ajv instance was configured with strict: false option). To allow specific undefined formats they have to be passed to ajv instance via formats option with true value:
const ajv = new Ajv((formats: {date: true, time: true})) // to ignore "date" and "time" formats in schemas.
"full") with optional list of format names and keywords option to add additional format comparison keywords:addFormats(ajv, {mode: "fast"})
or
addFormats(ajv, {mode: "fast", formats: ["date", "time"], keywords: true})
In "fast" mode the following formats are simplified: "date", "time", "date-time", "iso-time", "iso-date-time", "uri", "uri-reference", "email". For example, "date", "time" and "date-time" do not validate ranges in "fast" mode, only string structure, and other formats have simplified regular expressions.
npm install
git submodule update --init
npm test
The jsonschema package is a JSON Schema validator that also provides format validation. It supports similar functionalities to ajv-formats but is implemented in a different way and may have different performance characteristics.
tv4 is a tiny validator for JSON Schema v4. Like ajv-formats, it can validate formats specified in a JSON Schema, but it is less feature-rich and not as actively maintained as AJV.
Joi is a powerful schema description language and data validator for JavaScript. Unlike ajv-formats, Joi does not use JSON Schema but instead provides its own schema definition language, which can be more expressive and easier to use for some developers.
FAQs
Format validation for Ajv v7+
The npm package ajv-formats receives a total of 67,274,754 weekly downloads. As such, ajv-formats popularity was classified as popular.
We found that ajv-formats demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.